I'm using a bit vector to detect circular references. Currently this is coded as:

## Scalar will always be quad aligned and have ## a mimimum size of 12 bytes. return 'SELFREF' if vec( $circ, $addr / 12, 1 ); vec( $circ, $addr / 12, 1 ) = 1;

Three questions arise:

  1. Can anyone see a time when this would fail?
  2. Can I safely increase the constant 12?

    According to PerlGuts Illustrated SvNULL is the simplest (smallest) SV that can be allocated--which is where the / 12 comes from above.

    Most SV structures are greater than 12 bytes, and I could reduce the overhead commensurately if (for example) there was only ever one SvNULL allocated in the system and all other SV's were multiples of 16 or 20 bytes?

    Also, many alloc() functions actually allocate an extra word or two at the front or end of a memory allocation for integrity checking. If I could include those words in the count I could further reduce the length of the bit vector?

  3. Is there any way to reliably determine the lowest and (current) highest address that can be allocated from the heap (without Perl requesting more memory from the OS)?

    As it currently stands, the maximum overhead for the bit vector is 43 MB for a 32-bit processor that can address 4 GB of ram. This is okay, and in most instances will be much less, but if I could also figure out (at runtime) the fixed amount of memory allocated by the Perl process before the user program is interpreted, I could subtract that amount from the addresses and factor out the start of the bit vector.

    Likewise, as the code will be called as a function, if I could determine the current end-of-heap value, I could pre-allocate the bit-vector and prevent it from causing allocations on the fly as it discovers new, higher addresses.


Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.

In reply to Circular reference testing. by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.