From: Dan Sugalski <dan@sidhe.org>
Date: Thu, 10 Oct 2002 04:11:06 +0000 (-0800)
Subject: import Devel-Size 0.52 from CPAN
X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5c2e1b1290f2cc1677c307216577384513f45430;p=p5sagit%2FDevel-Size.git

import Devel-Size 0.52 from CPAN

git-cpan-module:   Devel-Size
git-cpan-version:  0.52
git-cpan-authorid: DSUGAL
git-cpan-file:     authors/id/D/DS/DSUGAL/Devel-Size-0.52.tar.gz
---

diff --git a/MANIFEST b/MANIFEST
index 3bca5f4..e3c1321 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -3,4 +3,4 @@ MANIFEST
 Makefile.PL
 Size.pm
 Size.xs
-test.pl
+t/basic.t
diff --git a/Size.pm b/Size.pm
index 2c46e3f..71fb850 100644
--- a/Size.pm
+++ b/Size.pm
@@ -24,7 +24,7 @@ require DynaLoader;
 @EXPORT = qw(
 	
 );
-$VERSION = '0.51';
+$VERSION = '0.52';
 
 bootstrap Devel::Size $VERSION;
 
@@ -44,6 +44,11 @@ Devel::Size - Perl extension for finding the memory usage of perl variables
   $size = size("abcde");
   $other_size = size(\@foo);
 
+  $foo = {a => [1, 2, 3],
+	  b => {a => [1, 3, 4]}
+         };
+  $total_size = total_size($foo);
+
 =head1 DESCRIPTION
 
 This module figures out the real sizes of perl variables. Call it with
@@ -52,6 +57,17 @@ 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)
 
+The C<size> 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<not> the contents.
+
+The C<total_size> 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)
+
 =head2 EXPORT
 
 None by default.
@@ -61,10 +77,6 @@ None by default.
 Doesn't currently walk all the bits for code refs, globs, formats, and
 IO. Those throw a warning, but a minimum size for them is returned.
 
-Also, this module currently only returns the size used by the variable
-itself, I<not> the contents of arrays or hashes, nor does it follow
-references past one level. That's for later.
-
 =head1 AUTHOR
 
 Dan Sugalski dan@sidhe.org
diff --git a/Size.xs b/Size.xs
index e92dbe6..248637c 100644
--- a/Size.xs
+++ b/Size.xs
@@ -165,10 +165,19 @@ UV thing_size(SV *orig_thing, HV *tracking_hash) {
     break;
   case SVt_PVCV:
     total_size += sizeof(XPVCV);
+    total_size += magic_size(thing, tracking_hash);
     carp("CV isn't complete");
     break;
   case SVt_PVGV:
+    total_size += magic_size(thing, tracking_hash);
     total_size += sizeof(XPVGV);
+    total_size += GvNAMELEN(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:
@@ -273,14 +282,14 @@ CODE:
 	case SVt_PVHV:
 	  /* Is there anything in here? */
 	  if (hv_iterinit((HV *)thing)) {
-	    SV *temp_thing;
-	    while (&PL_sv_undef != 
-		   (temp_thing = hv_iternextsv((HV *)thing, NULL, NULL))) {
-	      av_push(pending_array, temp_thing);
+	    HE *temp_he;
+	    while (temp_he = hv_iternext((HV *)thing)) {
+	      av_push(pending_array, hv_iterval((HV *)thing, temp_he));
 	    }
 	  }
 	  break;
 	 
+	case SVt_PVGV:
 	default:
 	  break;
 	}
diff --git a/test.pl b/t/basic.t
similarity index 100%
rename from test.pl
rename to t/basic.t