home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / biology / gsrc208a.zip / MEM.H < prev    next >
C/C++ Source or Header  |  1991-03-29  |  7KB  |  232 lines

  1. /*_ mem.h   Fri May 26 1989   Modified by: Walter Bright */
  2. /* Copyright 1986-1991 by Walter Bright        */
  3. /* All Rights Reserved                */
  4. /* Written by Walter Bright            */
  5.  
  6. #ifndef MEM_H
  7. #define MEM_H    1
  8.  
  9. #ifndef P
  10. #include    "toolkit.h"
  11. #endif
  12.  
  13. /*
  14.  * Memory management routines.
  15.  *
  16.  * Compiling:
  17.  *
  18.  *    #define MEM_DEBUG 1 when compiling to enable extended debugging
  19.  *    features.
  20.  *
  21.  * Features always enabled:
  22.  *
  23.  *    o mem_init() is called at startup, and mem_term() at
  24.  *      close, which checks to see that the number of alloc's is
  25.  *      the same as the number of free's.
  26.  *    o Behavior on out-of-memory conditions can be controlled
  27.  *      via mem_setexception().
  28.  *
  29.  * Extended debugging features:
  30.  *
  31.  *    o Enabled by #define MEM_DEBUG 1 when compiling.
  32.  *    o Check values are inserted before and after the alloc'ed data
  33.  *      to detect pointer underruns and overruns.
  34.  *    o Free'd pointers are checked against alloc'ed pointers.
  35.  *    o Free'd storage is cleared to smoke out references to free'd data.
  36.  *    o Realloc'd pointers are always changed, and the previous storage
  37.  *      is cleared, to detect erroneous dependencies on the previous
  38.  *      pointer.
  39.  *    o The routine mem_checkptr() is provided to check an alloc'ed
  40.  *      pointer.
  41.  */
  42.  
  43. /********************* GLOBAL VARIABLES *************************/
  44.  
  45. extern int mem_inited;        /* != 0 if mem package is initialized.    */
  46.                 /* Test this if you have other packages    */
  47.                 /* that depend on mem being initialized    */
  48.  
  49. /********************* PUBLIC FUNCTIONS *************************/
  50.  
  51. /***********************************
  52.  * Set behavior when mem runs out of memory.
  53.  * Input:
  54.  *    flag =    MEM_ABORTMSG:    Abort the program with the message
  55.  *                'Fatal error: out of memory' sent
  56.  *                to stdout. This is the default behavior.
  57.  *        MEM_ABORT:    Abort the program with no message.
  58.  *        MEM_RETNULL:    Return NULL back to caller.
  59.  *        MEM_CALLFP:    Call application-specified function.
  60.  *                fp must be supplied.
  61.  *    fp            Optional function pointer. Supplied if
  62.  *                (flag == MEM_CALLFP). This function returns
  63.  *                MEM_XXXXX, indicating what mem should do next.
  64.  *                The function could do things like swap
  65.  *                data out to disk to free up more memory.
  66.  *    fp could also return:
  67.  *        MEM_RETRY:    Try again to allocate the space. Be
  68.  *                careful not to go into an infinite loop.
  69.  */
  70.  
  71. #if __ZTC__
  72. enum MEM_E { MEM_ABORTMSG, MEM_ABORT, MEM_RETNULL, MEM_CALLFP, MEM_RETRY };
  73. void mem_setexception P((enum MEM_E,...));
  74. #else
  75. #define MEM_ABORTMSG    0
  76. #define MEM_ABORT    1
  77. #define MEM_RETNULL    2
  78. #define MEM_CALLFP    3
  79. #define MEM_RETRY    4
  80. void mem_setexception P((int,...));
  81. #endif
  82.  
  83.  
  84. /****************************
  85.  * Allocate space for string, copy string into it, and
  86.  * return pointer to the new string.
  87.  * This routine doesn't really belong here, but it is used so often
  88.  * that I gave up and put it here.
  89.  * Use:
  90.  *    char *mem_strdup(const char *s);
  91.  * Returns:
  92.  *    pointer to copied string if succussful.
  93.  *    else returns NULL (if MEM_RETNULL)
  94.  */
  95.  
  96. char *mem_strdup P((const char *));
  97.  
  98. /**************************
  99.  * Function so we can have a pointer to function mem_free().
  100.  * This is needed since mem_free is sometimes defined as a macro,
  101.  * and then the preprocessor screws up.
  102.  * The pointer to mem_free() is used frequently with the list package.
  103.  * Use:
  104.  *    void mem_freefp(void *p);
  105.  */
  106.  
  107. /***************************
  108.  * Check for errors. This routine does a consistency check on the
  109.  * storage allocator, looking for corrupted data. It should be called
  110.  * when the application has CPU cycles to burn.
  111.  * Use:
  112.  *    void mem_check(void);
  113.  */
  114.  
  115. void mem_check P((void ));
  116.  
  117. /***************************
  118.  * Check ptr to see if it is in the range of allocated data.
  119.  * Cause assertion failure if it isn't.
  120.  */
  121.  
  122. void mem_checkptr P((void *ptr));
  123.  
  124. /***************************
  125.  * Allocate and return a pointer to numbytes of storage.
  126.  * Use:
  127.  *    void *mem_malloc(unsigned numbytes);
  128.  *    void *mem_calloc(unsigned numbytes); allocated memory is cleared
  129.  * Input:
  130.  *    numbytes    Number of bytes to allocate
  131.  * Returns:
  132.  *    if (numbytes > 0)
  133.  *        pointer to allocated data, NULL if out of memory
  134.  *    else
  135.  *        return NULL
  136.  */
  137.  
  138. void *mem_malloc P((unsigned));
  139. void *mem_calloc P((unsigned));
  140.  
  141. /*****************************
  142.  * Reallocate memory.
  143.  * Use:
  144.  *    void *mem_realloc(void *ptr,unsigned numbytes);
  145.  */
  146.  
  147. void *mem_realloc P((void *,unsigned));
  148.  
  149. /*****************************
  150.  * Free memory allocated by mem_malloc(), mem_calloc() or mem_realloc().
  151.  * Use:
  152.  *    void mem_free(void *ptr);
  153.  */
  154.  
  155. void mem_free P((void *));
  156.  
  157. /***************************
  158.  * Initialize memory handler.
  159.  * Use:
  160.  *    void mem_init(void);
  161.  * Output:
  162.  *    mem_inited = 1
  163.  */
  164.  
  165. void mem_init P((void ));
  166.  
  167. /***************************
  168.  * Terminate memory handler. Useful for checking for errors.
  169.  * Use:
  170.  *    void mem_term(void);
  171.  * Output:
  172.  *    mem_inited = 0
  173.  */
  174.  
  175. void mem_term P((void ));
  176.  
  177. /*******************************
  178.  * Allocate memory and clear it.
  179.  * This routine is special in that no overhead is kept for what
  180.  * the size of the allocated block is. Thus, the application needs
  181.  * too keep track of that. Free only with mem_sfree().
  182.  */
  183.  
  184. #if MEM_DEBUG || !__ZTC__
  185. #define mem_scalloc mem_calloc
  186. #else
  187. void *mem_scalloc P((size_t numbytes));
  188. #endif
  189.  
  190. /*******************************
  191.  * Free memory allocated with mem_scalloc().
  192.  * Input:
  193.  *    ptr    value returned by mem_scalloc().
  194.  *    numbytes    size (must be same value as passed to mem_scalloc())
  195.  */
  196.  
  197. #if MEM_DEBUG || !__ZTC__
  198. #define mem_sfree(ptr,size) mem_free(ptr)
  199. #else
  200. void mem_sfree P((void *ptr, size_t numbytes));
  201. #endif
  202.  
  203. /* The following stuff forms the implementation rather than the
  204.  * definition, so ignore it.
  205.  */
  206.  
  207. #if MEM_DEBUG        /* if creating debug version    */
  208. #define mem_strdup(p)    mem_strdup_debug((p),__FILE__,__LINE__)
  209. #define mem_malloc(u)    mem_malloc_debug((u),__FILE__,__LINE__)
  210. #define mem_calloc(u)    mem_calloc_debug((u),__FILE__,__LINE__)
  211. #define mem_realloc(p,u)    mem_realloc_debug((p),(u),__FILE__,__LINE__)
  212. #define mem_free(p)    mem_free_debug((p),__FILE__,__LINE__)
  213.  
  214. char *mem_strdup_debug    P((const char *,char *,int));
  215. void *mem_calloc_debug    P((unsigned,char *,int));
  216. void *mem_malloc_debug    P((unsigned,char *,int));
  217. void *mem_realloc_debug P((void *,unsigned,char *,int));
  218. void  mem_free_debug    P((void *,char *,int));
  219. void  mem_freefp    P((void *));
  220.  
  221. void mem_setnewfileline P((void *,char *,int));
  222.  
  223. #else
  224.  
  225. #define mem_freefp    mem_free
  226. #define mem_check()
  227. #define mem_checkptr(p)
  228.  
  229. #endif /* MEM_DEBUG */
  230.  
  231. #endif /* MEM_H */
  232.