Keeping C/C++ Code Scalable
by Kirk J. Krauss

Listing One

/* The Track Heap Commitment algorithm                        */
/* Determines whether the specified memory range,             */
/* corresponding to a recently allocated heap memory block,   */
/* resides completely within the tracked committed regions of */
/* the heap.  Tracks new heap ranges in a list.               */
/* The caller should ascertain that the specified address     */
/* actually belongs to a heap: in other words, that it is     */
/* from a heap operation on a specific heap, or it is from a  */
/* local or C runtime allocation and is therefore on the      */
/* default local or C runtime heap.                           */

/* Input parameters */
ADDRESS        triggerAddr
SIZE           triggerSize
LIST           a list of tracked heap ranges

IF (the virtual memory at triggerAddr is tracked on the list
    as part of a heap range)
    DO
    IF (triggerAddr + triggerSize >
           (the tracked upper boundary of that heap range))
        DO
        /* An existing heap range has spread.                 */

        make system call(s) to determine the base and extent of
        the newly committed range that contains the addresses
        from triggerAddr to (triggerAddr + triggerSize)

        update the size of the tracked heap range to indicate
        its new upper limit
        END
    END
ELSE DO
    /* There is a new heap range at triggerAddr.              */

    make system call(s) to determine the base and extent of the
    newly committed range that contains the addresses from
    triggerAddr to (triggerAddr + triggerSize)

    track the new committed range in the list of heap ranges
    END


Listing Two

    /* The Track Heap Decommitment algorithm                      */
    /*                                                            */
    /* Determines whether the specified memory range,             */
    /* corresponding to a recently deallocated heap memory block, */
    /* has evidently become decommitted.  Cleans up tracking of   */
    /* decommitted heap ranges.                                   */

    /* Input parameters */
    ADDRESS        triggerAddr
    SIZE           triggerSize
    LIST           a list of tracked heap ranges

    /* Local variables */
    ADDRESS        origRangeBase
    SIZE           origRangeSize
    BOOL           bFoundChange

    bFoundChange = FALSE

    IF (the virtual memory at triggerAddr is not tracked on the
        heap range list as part of a heap range)
        DO
        /* Looks like we already know about this decommitment. */
        END
    ELSE IF (an access exception occurs when memory at triggerAddr
             is read)
        DO
        bFoundChange = TRUE
        END

    IF (bFoundChange) DO
        /* The set of virtual memory ranges occupied by the heap  */
        /* has changed to decommit space formerly occupied by     */
        /* memory blocks.                                         */
        /*                                                        */
        make system calls to determine the bases and extents of the
        tracked committed heap ranges in the immediate vicinity of
        the decommitted range that includes the addresses from
        triggerAddr to (triggerAddr + triggerSize)

        /* Update heap range tracking to reflect only the         */
        /* remaining committed ranges.                            */
        /*                                                        */
        IF (any portion of the tracked heap range that contained the
            block at TriggerAddr is still committed)
            DO
            update the heap range list to track just the portion(s)
            of that range that remain committed
            END
        ELSE
            DO
            delete the list element that tracks the range
            END
        END



