From: Dan Sugalski Date: Thu, 10 Oct 2002 19:34:33 +0000 (-0800) Subject: import Devel-Size 0.53 from CPAN X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0bff12d84a7bba010da7e689e45b0311f274c276;p=p5sagit%2FDevel-Size.git import Devel-Size 0.53 from CPAN git-cpan-module: Devel-Size git-cpan-version: 0.53 git-cpan-authorid: DSUGAL git-cpan-file: authors/id/D/DS/DSUGAL/Devel-Size-0.53.tar.gz --- diff --git a/Changes b/Changes index ff4837b..3091c2b 100644 --- a/Changes +++ b/Changes @@ -4,3 +4,7 @@ Revision history for Perl extension Devel::Size. - original version; created by h2xs 1.2 with options -A -n Devel::Size +0.53 Thu Oct 10 12:30:00 2002 + - Finally started updating Changes file + - Applied doc patch from Ann Barcomb + - Got globs sizing right \ No newline at end of file diff --git a/Size.pm b/Size.pm index 71fb850..f568bc2 100644 --- a/Size.pm +++ b/Size.pm @@ -16,7 +16,7 @@ require DynaLoader; # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK # will save memory. %EXPORT_TAGS = ( 'all' => [ qw( - size, total_size + size total_size ) ] ); @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); @@ -24,7 +24,7 @@ require DynaLoader; @EXPORT = qw( ); -$VERSION = '0.52'; +$VERSION = '0.53'; bootstrap Devel::Size $VERSION; @@ -32,45 +32,53 @@ bootstrap Devel::Size $VERSION; 1; __END__ -# Below is stub documentation for your module. You better edit it! =head1 NAME -Devel::Size - Perl extension for finding the memory usage of perl variables +Devel::Size - Perl extension for finding the memory usage of Perl variables =head1 SYNOPSIS - use Devel::Size qw(size); - $size = size("abcde"); - $other_size = size(\@foo); + use Devel::Size qw(size total_size); - $foo = {a => [1, 2, 3], + my $size = size("A string"); + + my @foo = (1, 2, 3, 4, 5); + my $other_size = size(\@foo); + + my $foo = {a => [1, 2, 3], b => {a => [1, 3, 4]} }; - $total_size = total_size($foo); + my $total_size = total_size($foo); =head1 DESCRIPTION -This module figures out the real sizes of perl variables. Call it with -a reference to the variable you want the size of. If you pass in a -plain scalar it returns the size of that scalar. (Just be careful if -you're asking for the size of a reference, as it'll follow the -reference if you don't reference it first) +This module figures out the real sizes of Perl variables in bytes. +Call functions with a reference to the variable you want the size +of. If the variable is a plain scalar it returns the size of +the scalar. If the variable is a hash or an array, use a reference +when calling. + +=head1 FUNCTIONS + +=head2 size($ref) The C function returns the amount of memory the variable -uses. If the variable is a hash or array, it only reports the amount -used by the variable structure, I the contents. +returns. If the variable is a hash or an array, it only reports +the amount used by the structure, I the contents. + +=head2 total_size($ref) -The C function will walk the variable and look at the -sizes of the contents. If the variable contains references those -references will be walked, so if you have a multidimensional data -structure you'll get the total structure size. (There isn't, at the -moment, a way to get the size of an array or hash and its elements -without a full walk) +The C function will traverse the variable and look +at the sizes of contents. Any references contained in the variable +will also be followed, so this function can be used to get the +total size of a multidimensional data structure. At the moment +there is no way to get the size of an array or a hash and its +elements without using this function. =head2 EXPORT -None by default. +None but default, but optionally C and C. =head1 BUGS diff --git a/Size.xs b/Size.xs index 248637c..33699d1 100644 --- a/Size.xs +++ b/Size.xs @@ -172,13 +172,18 @@ UV thing_size(SV *orig_thing, HV *tracking_hash) { total_size += magic_size(thing, tracking_hash); total_size += sizeof(XPVGV); total_size += GvNAMELEN(thing); + /* Is there a file? */ + if (GvFILE(thing)) { + if (check_new(tracking_hash, GvFILE(thing))) { + total_size += strlen(GvFILE(thing)); + } + } /* Is there something hanging off the glob? */ if (GvGP(thing)) { if (check_new(tracking_hash, GvGP(thing))) { total_size += sizeof(GP); } } - carp("GC isn't complete"); break; case SVt_PVFM: total_size += sizeof(XPVFM); @@ -290,6 +295,23 @@ CODE: break; case SVt_PVGV: + /* Run through all the pieces and push the ones with bits */ + if (GvSV(thing)) { + av_push(pending_array, (SV *)GvSV(thing)); + } + if (GvFORM(thing)) { + av_push(pending_array, (SV *)GvFORM(thing)); + } + if (GvAV(thing)) { + av_push(pending_array, (SV *)GvAV(thing)); + } + if (GvHV(thing)) { + av_push(pending_array, (SV *)GvHV(thing)); + } + if (GvCV(thing)) { + av_push(pending_array, (SV *)GvCV(thing)); + } + break; default: break; }