9 UV thing_size(SV *orig_thing) {
10 SV *thing = orig_thing;
11 UV total_size = sizeof(SV);
13 /* If they passed us a reference then dereference it. This is the
14 only way we can check the sizes of arrays and hashes */
15 if (SvOK(thing) && SvROK(thing)) {
19 switch (SvTYPE(thing)) {
23 /* Just a plain integer. This will be differently sized depending
24 on whether purify's been compiled in */
27 total_size += sizeof(sizeof(XPVIV));
29 total_size += sizeof(IV);
32 /* Is it a float? Like the int, it depends on purify */
35 total_size += sizeof(sizeof(XPVNV));
37 total_size += sizeof(NV);
40 /* Is it a reference? */
42 total_size += sizeof(XRV);
44 /* How about a plain string? In which case we need to add in how
45 much has been allocated */
47 total_size += sizeof(XPV);
48 total_size += SvLEN(thing);
50 /* A string with an integer part? */
52 total_size += sizeof(XPVIV);
53 total_size += SvLEN(thing);
55 /* A string with a float part? */
57 total_size += sizeof(XPVNV);
58 total_size += SvLEN(thing);
69 /* How much space is dedicated to the array? Not counting the
70 elements in the array, mind, just the array itself */
72 total_size += sizeof(XPVAV);
73 /* Is there anything in the array? */
74 if (AvMAX(thing) != -1) {
75 total_size += sizeof(SV *) * AvMAX(thing);
77 /* Add in the bits on the other side of the beginning */
78 total_size += (sizeof(SV *) * (AvARRAY(thing) - AvALLOC(thing)));
79 /* Is there something hanging off the arylen element? */
80 if (AvARYLEN(thing)) {
81 total_size += thing_size(AvARYLEN(thing));
85 /* First the base struct */
86 total_size += sizeof(XPVHV);
87 /* Now the array of buckets */
88 total_size += (sizeof(HE *) * (HvMAX(thing) + 1));
89 /* Now walk the bucket chain */
94 for (cur_bucket = 0; cur_bucket <= HvMAX(thing); cur_bucket++) {
95 cur_entry = *(HvARRAY(thing) + cur_bucket);
97 total_size += sizeof(HE);
98 if (cur_entry->hent_hek) {
99 total_size += sizeof(HEK);
100 total_size += cur_entry->hent_hek->hek_len - 1;
102 cur_entry = cur_entry->hent_next;
120 croak("Unknown variable type");
126 MODULE = Devel::Size PACKAGE = Devel::Size
133 RETVAL = thing_size(orig_thing);