home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / SNIP9404.ZIP / MEM.H < prev    next >
C/C++ Source or Header  |  1994-04-03  |  6KB  |  213 lines

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