⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 heap.man

📁 一个堆栈管理器的源码
💻 MAN
📖 第 1 页 / 共 5 页
字号:
        char far *ptr = _fmalloc( 100 );

        for ( index=0 ; index<100 ; index++ )

            ptr[index] = (char) index;

        _fheapwatch( ptr, 1 );              // establishes the enty as read-only

        report( "read-only enabled" );      // should not detect an error

        ptr[0] = -1;                        // change part of the data

        report( "data modified" );          // should detect an error

        _fheapwatch( ptr, 0 );              // stop watching this region

        report( "read-only disabled" );     // should not detect an error

        return( 0 );
        }

        void report( char *position )
        {
        int status = _fheapchk();

        printf( "Heap status after %s : %s\n", position, _heapstat(status) );
        }

        This program allocates a region of 100 bytes and fills it with data.
        It then declares the region to be read-only, and checks that _fheapchk
        reports the heap as intact.  Then the program changes the data, and
        checks the heap status again.  At this point the error should be
        detected.  Finally, the program unprotects the region and verifies
        that _fheapchk stops complaining.


_FMALLOC

    Summary

        #include <heap.h>

        void far *_fmalloc( size );
        size_t size;                    // Desired number of bytes

        #if !defined( STDMSC ) && !defined( NDEBUG )

        void far *_fmalloc_d( size, file, line, stream );   // debug
        void far *_fmalloc_t( size, file, line, stream );   // trace
        size_t     size;                // Desired number of bytes
        char near *file;                // Name of source file (__FILE__)
        uint       line;                // Source file line number (__LINE__)
        FILE      *stream;              // Error output file

        #endif


    Differences

        The replacement version of _fmalloc differs from the original
        in the following ways:

            1. SIZE_MAX is 65535 rather than 65516.

            2. The _HEAPDEBUG symbol enables the debugging alternate
               version of _fmalloc.  Prior to every heap access it checks
               the heap status via _fheapchk, and reports errors with
               on the indicated stream file with _heapdump.
               
            3. The _HEAPTRACE symbol enables the tracing alternate
               version of _fmalloc.  In addition to the debugging described
               under _HEAPDEBUG, after ever heap transaction it reports the
               nature of the transaction, its source-code location, and the
               values of the arguments and returned result. 
 

    Further info

        See page 409 of the MSC 5.1 Optimizing Compiler Run-Time Reference


_FMSIZE

    Summary

        #include <heap.h>

        size_t _fmsize( buffer );
        void far *buffer;               // Pointer to memory block

        #if !defined( STDMSC ) && !defined( NDEBUG )

        size_t _fmsize_c( buffer, file, line, stream );  // check pointers
        void far  *buffer;              // Pointer to memory block
        char near *file;                // Name of source file (__FILE__)
        uint       line;                // Source file line number (__LINE__)
        FILE      *stream;              // Error output file

        #endif


    Differences

        The replacement version of _fmsize differs from the original
        in the following ways:

            1. Since the "huge" heap is part of the "far" heap,
               _fmsize can handle any "huge" heap entry.  However,
               it cannot return the actual size because size_t is
               too thin, so it returns SIZE_MAX (65535).  The
               solution is to use _hmsize for all "far" and "huge"
               heap entries.

            2. NULL pointers have size zero rather than garbage.

            3. The _HEAPCHECK symbol enables the pointer checking
               alternate version of _fmsize, which reports invalid
               pointers, detailing the location of the offending
               function in the source code.


    Further info

        See page 440 of the MSC 5.1 Optimizing Compiler Run-Time Reference


FREE

    Summary

        #include <heap.h>
    
        void free( buffer );
        void far *buffer;               // Pointer to memory block

        #if !defined( STDMSC ) && !defined( NDEBUG ) && defined( _DATA_FAR )

        void free_c( buffer, file, line, stream );  // check pointers
        void free_d( buffer, file, line, stream );  // debug
        void free_t( buffer, file, line, stream );  // trace
        void far  *buffer;              // Pointer to memory block
        char near *file;                // Name of source file (__FILE__)
        uint       line;                // Source file line number (__LINE__)
        FILE      *stream;              // Error output file

        #endif


    Differences

        The replacement version of free differs from the original
        in the following ways:

            1. Since the "huge" heap is part of the "far" heap, 
               free can handle any "huge" heap entry.

            2. Invalid pointers are ignored by free.

            3. The _HEAPCHECK symbol enables the pointer checking
               alternate version of free, which reports invalid
               pointers, detailing the location of the offending
               function in the source code.

            4. The _HEAPDEBUG symbol enables the debugging alternate
               version of free.  In addition to the pointer checking
               described under _HEAPCHECK, prior to every heap access it
               checks the heap status via _fheapchk, and reports errors
               on the indicated stream file with _heapdump.
               
            5. The _HEAPTRACE symbol enables the tracing alternate
               version of free.  In addition to the debugging described
               under _HEAPDEBUG, after every heap transaction it reports the
               nature of the transaction, its source-code location, and the
               values of the arguments and the returned result. 
 


    Further info

        See page 289 of the MSC 5.1 Optimizing Compiler Run-Time Reference


_FREECT

    Summary

        uint _freect( size );
        size_t size;                    // Desired size of allocation


    Differences

        In "small" and "medium" model programs _freect comes from the
        original library.  In "compact", "large", and "huge" model
        programs _freect comes from the replacement library, and,
        because there is no "near" heap, always returns zero.


    Further info

        See page 291 of the MSC 5.1 Optimizing Compiler Run-Time Reference


_FRELOCATE

    Summary

        #include <heap.h

        #if !defined( STDMSC )

        void far *_frelocate( buffer );
        void far *buffer;               // Pointer to memory block

        #endif

        #if !defined( STDMSC ) && !defined( NDEBUG ) && defined( _DATA_FAR )

        void far *_frelocate_c( buffer, file, line, stream );  // check pointers
        void far *_frelocate_d( buffer, file, line, stream );  // debug
        void far *_frelocate_t( buffer, file, line, stream );  // trace
        void far  *buffer;              // Pointer to memory block
        char near *file;                // Name of source file (__FILE__)
        uint       line;                // Source file line number (__LINE__)
        FILE      *stream;              // Error output file

        #endif


    Description

        The _frelocate function moves a heap entry as low as possible
        in memory.  It scans the free list looking for the block that
        has the lowest address and is large enough to contain the data.
        If it finds one it copies the old entry to the new region and
        frees the old entry.

        If no free entry is both large enough to contain the data
        and lower in the heap _frelocate checks the preceeding entry.
        If the preceeding entry is free, no matter what the size,
        _frelocate slides the data down into the free region and
        frees the high end of the old region.  Essentially this swaps
        the free and used entries.

        Since the "huge" heap is part of the "far" heap, _frelocate can
        handle any "huge" heap entry.

        Invaid pointers are ignored by _frelocate, which returns NULL
        when one is detected.

        The alternate forms of _frelocate are controlled as follows:

            1. The _HEAPCHECK symbol enables the pointer checking
               alternate version of _frelocate, which reports invalid
               pointers, detailing the location of the offending
               function in the source code.

            2. The _HEAPDEBUG symbol enables the debugging alternate
               version of _frelocate.  In addition to the pointer checking
               described under _HEAPCHECK, prior to every heap access it
               checks the heap status via _fheapchk, and reports errors
               on the indicated stream file with _heapdump.
               
            3. The _HEAPTRACE symbol enables the tracing alternate
               version of _frelocate.  In addition to the debugging described
               under _HEAPDEBUG, after every heap transaction it reports the
               nature of the transaction, its source-code location, and the
               values of the arguments and the returned result. 
 

    Return Value

        If the entry is moved _frelocate returns a pointer to the new
        entry.  If the pointer argument is invalid, or the entry cannot
        be relocated _frelocate returns NULL.


    Differences

        The _frelocate function is unique to the replacement heap manager.


    See Also

        calloc, _expand, _fheappack, _fmalloc, halloc, _hexpand, hrealloc,
        malloc, realloc


    Example

        #include <stdio.h>

        #include <heap.h>

        int main()
        {
        void far *p1 = _fmalloc( 1000 );

        void far *p2 = _fmalloc( 2000 );

        void far *p3 = _fmalloc( 3000 );

        void far *p4 = _fmalloc( 4000 );

        void far *new;

        _ffree( p1 );

        _ffree( p3 );

        printf("Before relocation\n\n");

        _fheapdump( stdout, 1 );

        new = _frelocate( p2 );   if (new != NULL)   p2 = new;

        new = _frelocate( p4 );   if (new != NULL)   p4 = new;

        printf("After relocation\n\n");

        _fheapdump( stdout, 1 );

        return( 0 );
        }

        This program allocates four heap entries and then frees the
        first and third, leaving the second in the middle of the heap
        and the fourth at the end of the heap.  Then it relocates the
        two remaining entries to maximize the size of the free area.
        The before and after heap reports illustrate the compaction.


HALLOC

    Summary

        #include <heap.h>

        void huge *halloc( nitems, size );
        long   nitems;                  // Number of array elements
        size_t size;                    // Bytes in each element

        #if !defined( STDMSC ) && !defined( NDEBUG )

        void huge *halloc_d( nitems, size, file, line, stream );  // debug
        void huge *halloc_d( nitems, size, file, line, stream );  // trace
        long       nitems;              // Number of array elements
        size_t     size;                // Bytes in each element
        char near *file;                // Name of source file (__FILE__)
        uint       line;                // Source file line number (__LINE__)
        FILE      *stream;              // Error output file

        #endif


    Differences

        The replacement version of hmalloc differs from the original
        in the following ways:

            1. The halloc function allocates memory from the "far" heap
               rather than from DOS.  It is a proper superset of calloc.
               The results of halloc can be handled by all of the "far"
               heap management functions.

            2. The _HEAPDEBUG symbol enables the debugging alternate
               version of halloc.  Prior to every heap access it checks
               the heap status via _fheapchk, and reports errors with
               on the indicated stream file with _heapdump.
               
            3. The _HEAPTRACE symbol enables the tracing alternate
               version of halloc.  In addition to the debugging described

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -