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

📄 heap.h

📁 一个堆栈管理器的源码
💻 H
字号:
//
//     Copyright (c) 1990 by Optimal Software, All Rights Reserved
//

#if !defined( HEAP_H )

#define HEAP_H

#if   defined( M_I86SM )

   #define _HEAPLIB "sheap" 

   #define _DATA_NEAR
   #define _CODE_NEAR

#elif defined( M_I86MM )

   #define _HEAPLIB "mheap" 

   #define _DATA_NEAR
   #define _CODE_FAR

#elif defined( M_I86CM )

   #define _HEAPLIB "cheap" 

   #define _DATA_FAR
   #define _CODE_NEAR

#elif defined( M_I86LM ) || defined( M_I86HM )

   #define _HEAPLIB "lheap" 

   #define _DATA_FAR
   #define _CODE_FAR

#else

   #error Undefined address model

#endif

//
//    Tell linker to look for heap.lib
//
//    N.b., this does not work, but MS might fix it some day.
//
#pragma comment ( lib, _HEAPLIB )

/*/
   The following pre-processor symbols control the objective of the
   heap manager.  The current setting is recorded in the _heapmode
   variable.  Programs may change this value on the fly, but it is
   usually best to set the desired mode at the beginning of the program
   and leave it at that.

   Default: TIME   (because this approximates MSC usage)
/*/

#if !defined( STDMSC )
   #define _HEAPTIME  0
   #define _HEAPSIZE  1
   #define _HEAPSPACE 2

   #if !defined(_HEAPMODE)
      #define _HEAPMODE _HEAPTIME
   #endif
#endif

/*/
   Result code for _heapchk()

   c.f. MSC _heapchk(), _heapset(), _heapwalk()
/*/
#if !defined(_HEAPEMPTY)
   #define _HEAPEMPTY      -1
   #define _HEAPOK         -2
   #define _HEAPBADBEGIN   -3
   #define _HEAPBADNODE    -4    /* invalid control block */
   #define _HEAPEND        -5
   #define _HEAPBADPTR     -6    /* bad _heapwalk ptr or data corrupted */

   #define _FREEENTRY       0
   #define _USEDENTRY       1
#endif

#if !defined( STDMSC )
   #define SIZE_MAX 0xFFFF /* full segment addressing */
#endif

/********** type definitions **********/

typedef unsigned int uint;

#if !defined(_SIZE_T_DEFINED)
   typedef uint size_t;
   #define _SIZE_T_DEFINED
#endif

typedef struct    /* linkage in a doubly-linked list */
   {
   uint  last;    /* segment pointer to preceeding list entry */
   uint  next;    /* segment pointer to following list entry */
   } _LINK;

typedef struct    /* every arena obtained from DOS has one of these */    
   {
   _LINK link;    /* links to other arenas */
   uint  size;    /* size of this arena including header and trailer */
   uint  isdos;   /* memory from DOS?  [freeable by _heappack()] */
   } _ARENA;

typedef struct    /* every allocated block has a header of this form */
   {
   _LINK clist;   /* links to other chunks in same arena */
   _LINK flist;   /* links to free/join list -- 0 for an in-use entry */
                  /* ... used by debugger to record info on used entries */
   } _CHUNK;

#if !defined(_HEAPINFO_DEFINED)     /* c.f. MSC malloc.h */
   typedef struct _heapinfo
       {
       int far *_pentry;
       size_t   _size;
       int      _useflag;
       } _HEAPINFO;
   #define _HEAPINFO_DEFINED
#endif

/********** global variables; init in heaputil.c **********/

extern size_t _amblksiz;   /* DOS allocation quantum */

#if !defined( STDMSC )

extern int    _heapmode;   /* mode of operation; see TIME/SIZE/SPACE above */

extern size_t _heappad ;   /* padding at ends of every allocation */

#endif

/********** Function prototypes and aliases **********/

#define DBG_ARGS char near *file, uint line, void *stream

extern void      *alloca( size_t nbytes );

extern void      *calloc(   size_t nitems, size_t nbytes );
#if !defined( STDMSC ) && !defined( NDEBUG ) && defined( _DATA_FAR )
extern void far  *calloc_d( size_t nitems, size_t nbytes, DBG_ARGS );
extern void far  *calloc_t( size_t nitems, size_t nbytes, DBG_ARGS );
#endif

extern void      *_expand(   void     *ptr, size_t nbytes );
#if !defined( STDMSC ) && !defined( NDEBUG ) && defined( _DATA_FAR )
extern void far  *_expand_c( void far *ptr, size_t nbytes, DBG_ARGS );
extern void far  *_expand_d( void far *ptr, size_t nbytes, DBG_ARGS );
extern void far  *_expand_t( void far *ptr, size_t nbytes, DBG_ARGS );
#endif

extern void       _ffree(   void far *ptr );
#if !defined( STDMSC ) && !defined( NDEBUG )
extern void       _ffree_c( void far *ptr, DBG_ARGS );
extern void       _ffree_d( void far *ptr, DBG_ARGS );
extern void       _ffree_t( void far *ptr, DBG_ARGS );
#endif

extern int        _fheapchk( void );

#if !defined( STDMSC ) && defined( FILE )
extern void       _fheapdump( FILE *output, int verbose );
#endif

#if !defined( STDMSC )
extern int        _fheappack( void );
#endif
#if !defined( SRDMSC ) && !defined( NDEBUG )
extern int        _fheappack_d( DBG_ARGS );
extern int        _fheappack_t( DBG_ARGS );
#endif

extern int        _fheapset( uint fill );

extern int        _fheapwalk( struct _heapinfo *hp );

#if !defined( STDMSC ) && !defined( NDEBUG )
extern int        _fheapwatch( void far *ptr, int enable );
#endif

extern void far  *_fmalloc(   size_t nbytes );
#if !defined( STDMSC ) && !defined( NDEBUG )
extern void far  *_fmalloc_d( size_t nbytes, DBG_ARGS );
extern void far  *_fmalloc_t( size_t nbytes, DBG_ARGS );
#endif

extern size_t     _fmsize(   void far *ptr );
#if !defined( STDMSC ) && !defined( NDEBUG )
extern size_t     _fmsize_c( void far *ptr, DBG_ARGS );
#endif

extern void       free(   void     *ptr );
#if !defined( STDMSC ) && !defined( NDEBUG ) && defined( _DATA_FAR )
extern void       free_c( void far *ptr, DBG_ARGS );
extern void       free_d( void far *ptr, DBG_ARGS );
extern void       free_t( void far *ptr, DBG_ARGS );
#endif

extern uint       _freect( size_t nbytes );

#if !defined( STDMSC )
extern void far  *_frelocate(   void far *ptr );
#endif
#if !defined( STDMSC ) && !defined( NDEBUG )
extern void far  *_frelocate_c( void far *ptr, DBG_ARGS );
extern void far  *_frelocate_d( void far *ptr, DBG_ARGS );
extern void far  *_frelocate_t( void far *ptr, DBG_ARGS );
#endif

extern void huge *halloc(   long nitems, size_t nbytes );
#if !defined( STDMSC ) && !defined( NDEBUG )
extern void huge *halloc_d( long nitems, size_t nbytes, DBG_ARGS );
extern void huge *halloc_t( long nitems, size_t nbytes, DBG_ARGS );
#endif

#if !defined( STDMSC )
extern char      *_heapstat( int status );
#endif

#if !defined( STDMSC )
extern void huge *_hexpand(   void huge *ptr, long nbytes );
#endif
#if !defined( STDMSC ) && !defined( NDEBUG )
extern void huge *_hexpand_c( void huge *ptr, long nbytes, DBG_ARGS );
extern void huge *_hexpand_d( void huge *ptr, long nbytes, DBG_ARGS );
extern void huge *_hexpand_t( void huge *ptr, long nbytes, DBG_ARGS );
#endif

extern void       hfree(   void huge *ptr );
#if !defined( STDMSC ) && !defined( NDEBUG )
extern void       hfree_c( void huge *ptr, DBG_ARGS );
extern void       hfree_d( void huge *ptr, DBG_ARGS );
extern void       hfree_t( void huge *ptr, DBG_ARGS );
#endif

#if !defined( STDMSC )
extern long       _hmsize(   void huge *ptr );
#endif
#if !defined( STDMSC ) && !defined( NDEBUG )
extern long       _hmsize_c( void huge *ptr, DBG_ARGS );
#endif

#if !defined( STDMSC )
extern void huge *hrealloc(   void huge *ptr, long nbytes );
#endif
#if !defined( STDMSC ) && !defined( NDEBUG )
extern void huge *hrealloc_c( void huge *ptr, long nbytes, DBG_ARGS );
extern void huge *hrealloc_d( void huge *ptr, long nbytes, DBG_ARGS );
extern void huge *hrealloc_t( void huge *ptr, long nbytes, DBG_ARGS );
#endif

extern void      *malloc(   size_t nbytes );
#if !defined( STDMSC ) && !defined( NDEBUG ) && defined( _DATA_FAR )
extern void far  *malloc_d( size_t nbytes, DBG_ARGS );
extern void far  *malloc_t( size_t nbytes, DBG_ARGS );
#endif

extern size_t     _memavl( void );

extern size_t     _memmax( void );

extern size_t     _msize(   void     *ptr );
#if !defined( STDMSC ) && !defined( NDEBUG ) && defined( _DATA_FAR )
extern size_t     _msize_c( void far *ptr, DBG_ARGS );
#endif

extern void       _nfree( void near *ptr );

extern int        _nheapchk( void );

extern int        _nheapset( uint fill );

extern int        _nheapwalk( struct _heapinfo *hp );

extern void near *_nmalloc( size_t nbytes );

extern size_t     _nmsize( void near *ptr );

extern void      *realloc(   void     *ptr, size_t nbytes );
#if !defined( STDMSC ) && !defined( NDEBUG ) && defined( _DATA_FAR )
extern void far  *realloc_c( void far *ptr, size_t nbytes, DBG_ARGS );
extern void far  *realloc_d( void far *ptr, size_t nbytes, DBG_ARGS );
extern void far  *realloc_t( void far *ptr, size_t nbytes, DBG_ARGS );
#endif

extern void      *sbrk( int );

extern size_t     stackavail( void );

//
//    Redefine functions to activate pointer checks, debugging, and tracing
//

#if !defined( STDMSC )

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

      #if !defined( FILE )
         #include <stdio.h>
      #endif

      #pragma comment ( user, "Compiled with -D_HEAPTRACE -- heap tracing enabled" )

      #define DBG_VALUES __FILE__, __LINE__, _HEAPTRACE

      //
      //    Tracing aliases: xxxxx() becomes xxxxx_t()
      //
      //    Any function that changes the heap must first check for errors,
      //    and any function that allocates a heap entry must record the salient
      //    info for heap traceback.
      //
      //    These functions also announce their parameters and results
      //

      #if defined( _DATA_FAR )
      #define calloc( nitems, nbytes ) calloc_t( (nitems), (nbytes), DBG_VALUES )
      #endif

      #if defined( _DATA_FAR )
      #define _expand( ptr, nbytes )   _expand_t( (ptr), (nbytes), DBG_VALUES )
      #endif

      #define _ffree( ptr )            _ffree_t( (ptr), DBG_VALUES )

      #define _fheappack()             _fheappack_t( DBG_VALUES )

      #define _fmalloc( nbytes )       _fmalloc_t( (nbytes), DBG_VALUES )

      #define _fmsize(ptr)             _fmsize_c( (ptr), DBG_VALUES )

      #if defined( _DATA_FAR )
      #define free( ptr )              free_t( (ptr), DBG_VALUES )
      #endif

      #define _frelocate( ptr )        _frelocate_t( (ptr), DBG_VALUES )

      #define halloc( nitems, nbytes ) halloc_t( (nitems), (nbytes), DBG_VALUES )

      #define _hexpand( ptr, nbytes )  _hexpand_t( (ptr), (nbytes), DBG_VALUES )

      #define hfree( ptr )             hfree_t( (ptr), DBG_VALUES )

      #define _hmsize( ptr )           _hmsize_c( (ptr), DBG_VALUES )

      #define hrealloc( ptr, nbytes )  hrealloc_t( (ptr), (nbytes), DBG_VALUES )

      #if defined( _DATA_FAR )
      #define malloc( nbytes )         malloc_t( (nbytes), DBG_VALUES )
      #endif

      #if defined( _DATA_FAR )
      #define _msize( ptr )            _msize_c( (ptr), DBG_VALUES )
      #endif

      #if defined( _DATA_FAR )
      #define realloc( ptr, nbytes )   realloc_t( (ptr), (nbytes), DBG_VALUES )
      #endif

   #elif !defined( NDEBUG ) && defined( _HEAPDEBUG )

      #if !defined( FILE )
         #include <stdio.h>
      #endif

      #pragma comment ( user, "Compiled with -D_HEAPDEBUG -- heap debugging enabled" )

      #define DBG_VALUES __FILE__, __LINE__, _HEAPDEBUG

      //
      //    Debugging aliases: xxxxx() becomes xxxxx_d()
      //
      //    Any function that changes the heap must first check for errors,
      //    and any function that allocates a heap entry must record the salient
      //    info for heap traceback.
      //

      #if defined( _DATA_FAR )
      #define calloc( nitems, nbytes ) calloc_d( (nitems), (nbytes), DBG_VALUES )
      #endif

      #if defined( _DATA_FAR )
      #define _expand( ptr, nbytes )   _expand_d( (ptr), (nbytes), DBG_VALUES )
      #endif

      #define _ffree( ptr )            _ffree_d( (ptr), DBG_VALUES )

      #define _fheappack()             _fheappack_d( DBG_VALUES )

      #define _fmalloc( nbytes )       _fmalloc_d( (nbytes), DBG_VALUES )

      #define _fmsize(ptr)             _fmsize_c( (ptr), DBG_VALUES )

      #if defined( _DATA_FAR )
      #define free( ptr )              free_d( (ptr), DBG_VALUES )
      #endif

      #define _frelocate( ptr )        _frelocate_d( (ptr), DBG_VALUES )

      #define halloc( nitems, nbytes ) halloc_d( (nitems), (nbytes), DBG_VALUES )

      #define _hexpand( ptr, nbytes )  _hexpand_d( (ptr), (nbytes), DBG_VALUES )

      #define hfree( ptr )             hfree_d( (ptr), DBG_VALUES )

      #define _hmsize(ptr)             _hmsize_c( (ptr), DBG_VALUES )

      #define hrealloc( ptr, nbytes )  hrealloc_d( (ptr), (nbytes), DBG_VALUES )

      #if defined( _DATA_FAR )
      #define malloc( nbytes )         malloc_t( (nbytes), DBG_VALUES )
      #endif

      #if defined( _DATA_FAR )
      #define _msize( ptr )            _msize_c( (ptr), DBG_VALUES )
      #endif

      #if defined( _DATA_FAR )
      #define realloc( ptr, nbytes )   realloc_d( (ptr), (nbytes), DBG_VALUES )
      #endif

   #elif !defined( NDEBUG ) && defined( _HEAPCHECK )

      #if !defined( FILE )
         #include <stdio.h>
      #endif

      #pragma comment ( user, "Compiled with -D_HEAPCHECK -- heap pointer checking enabled" )

      #define DBG_VALUES __FILE__, __LINE__, _HEAPCHECK

      //
      //    Pointer checking aliases: xxxxx() becomes xxxxx_c()
      //
      //    Any function that accepts a pointer must check for validity
      //

      #if defined( _DATA_FAR )
      #define _expand( ptr, nbytes )   _expand_c( (ptr), (nbytes), DBG_VALUES )
      #endif

      #define _ffree( ptr )            _ffree_c( (ptr), DBG_VALUES )

      #define _fmsize(ptr)             _fmsize_c( (ptr), DBG_VALUES )

      #if defined( _DATA_FAR )
      #define free( ptr )              free_c( (ptr), DBG_VALUES )
      #endif

      #define _frelocate( ptr )        _frelocate_c( (ptr), DBG_VALUES )

      #define _hexpand( ptr, nbytes )  _hexpand_c( (ptr), (nbytes), DBG_VALUES )

      #define hfree( ptr )             hfree_c( (ptr), DBG_VALUES )

      #define _hmsize( ptr )           _hmsize_c( (ptr), DBG_VALUES )

      #define hrealloc( ptr, nbytes )  hrealloc_c( (ptr), (nbytes), DBG_VALUES )

      #if defined( _DATA_FAR )
      #define _msize( ptr )            _msize_c( (ptr), DBG_VALUES )
      #endif

      #if defined( _DATA_FAR )
      #define realloc( ptr, nbytes )   realloc_c( (ptr), (nbytes), DBG_VALUES )
      #endif

   #endif

#endif   /*  !defined( STDMSC ) */

//
//    Standard model-specific aliases
//
#if defined( _DATA_FAR )

   #define _heapchk   _fheapchk
   #define _heapset   _fheapset
   #define _heapwalk  _fheapwalk

#else

   #define _heapchk   _nheapchk
   #define _heapset   _nheapset
   #define _heapwalk  _nheapwalk

#endif

#endif   /* defined( HEAP_H ) */

⌨️ 快捷键说明

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