home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / MEM.H < prev    next >
C/C++ Source or Header  |  1997-07-05  |  7KB  |  215 lines

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