ADMB Documentation  Fournier-pthread.1088
 All Classes Namespaces Files Functions Variables Typedefs Friends Defines
safe_mem.h
Go to the documentation of this file.
00001 /*
00002  * $Id: safe_mem.h 418 2012-04-10 23:36:33Z arnima $
00003  *
00004  * Author: Walter Bright
00005  *
00006  * This in an early version of Walter Bright's MEM package originally
00007  * published in the March-April 1990 edition of Micro Cornucopia magazine.
00008  * The MEM package was freely distributed with the Zortech compiler.
00009  * Bright contributed an updated version of the MEM package to the 
00010  * Public Domain. It can be downladed from http://snippets.snippets.org/
00011  */
00012 
00013 
00014 // safe_mem.h
00015 //  based on:
00016 /*_ mem.h   Fri May 26 1989   Modified by: Walter Bright */
00017 /* Copyright 1986-1991 by Walter Bright   */
00018 /* All Rights Reserved        */
00019 /* Written by Walter Bright     */
00020 
00021 #ifndef SAFE_MEM_H
00022 #define SAFE_MEM_H  1
00023 
00024 /*
00025  * Memory management routines.
00026  *
00027  * Compiling:
00028  *
00029  *  #define MEM_DEBUG 1 when compiling to enable extended debugging
00030  *  features.
00031  *
00032  * Features always enabled:
00033  *
00034  *  o mem_init() is called at startup, and mem_term() at
00035  *    close, which checks to see that the number of alloc's is
00036  *    the same as the number of free's.
00037  *  o Behavior on out-of-memory conditions can be controlled
00038  *    via mem_setexception().
00039  *
00040  * Extended debugging features:
00041  *
00042  *  o Enabled by #define MEM_DEBUG 1 when compiling.
00043  *  o Check values are inserted before and after the alloc'ed data
00044  *    to detect pointer underruns and overruns.
00045  *  o Free'd pointers are checked against alloc'ed pointers.
00046  *  o Free'd storage is cleared to smoke out references to free'd data.
00047  *  o Realloc'd pointers are always changed, and the previous storage
00048  *    is cleared, to detect erroneous dependencies on the previous
00049  *    pointer.
00050  *  o The routine mem_checkptr() is provided to check an alloc'ed
00051  *    pointer.
00052  */
00053 
00054 /********************* GLOBAL VARIABLES *************************/
00055 
00056 extern int mem_inited;    /* != 0 if mem package is initialized.  */
00057         /* Test this if you have other packages */
00058         /* that depend on mem being initialized */
00059 
00060 /********************* PUBLIC FUNCTIONS *************************/
00061 
00062 /***********************************
00063  * Set behavior when mem runs out of memory.
00064  * Input:
00065  *  flag =  MEM_ABORTMSG: Abort the program with the message
00066  *        'Fatal error: out of memory' sent
00067  *        to stdout. This is the default behavior.
00068  *    MEM_ABORT:  Abort the program with no message.
00069  *    MEM_RETNULL:  Return NULL back to caller.
00070  *    MEM_CALLFP: Call application-specified function.
00071  *        fp must be supplied.
00072  *  fp      Optional function pointer. Supplied if
00073  *        (flag == MEM_CALLFP). This function returns
00074  *        MEM_XXXXX, indicating what mem should do next.
00075  *        The function could do things like swap
00076  *        data out to disk to free up more memory.
00077  *  fp could also return:
00078  *    MEM_RETRY:  Try again to allocate the space. Be
00079  *        careful not to go into an infinite loop.
00080  */
00081 
00082 #if __ZTC__
00083   enum MEM_E { MEM_ABORTMSG, MEM_ABORT, MEM_RETNULL, MEM_CALLFP, MEM_RETRY };
00084   void mem_setexception (enum MEM_E,...);
00085 #else
00086   #define MEM_ABORTMSG  0
00087   #define MEM_ABORT 1
00088   #define MEM_RETNULL 2
00089   #define MEM_CALLFP  3
00090   #define MEM_RETRY 4
00091   void mem_setexception(int,...);
00092 #endif
00093 
00094 
00095 /****************************
00096  * Allocate space for adstring, copy adstring into it, and
00097  * return pointer to the new adstring.
00098  * This routine doesn't really belong here, but it is used so often
00099  * that I gave up and put it here.
00100  * Use:
00101  *  char *mem_strdup(const char *s);
00102  * Returns:
00103  *  pointer to copied adstring if succussful.
00104  *  else returns NULL (if MEM_RETNULL)
00105  */
00106 
00107 char *mem_strdup(const char *);
00108 
00109 /**************************
00110  * Function so we can have a pointer to function mem_free().
00111  * This is needed since mem_free is sometimes defined as a macro,
00112  * and then the preprocessor screws up.
00113  * The pointer to mem_free() is used frequently with the list package.
00114  * Use:
00115  *  void mem_freefp(void *p);
00116  */
00117 
00118 /***************************
00119  * Check for errors. This routine does a consistency check on the
00120  * storage allocator, looking for corrupted data. It should be called
00121  * when the application has CPU cycles to burn.
00122  * Use:
00123  *  void mem_check(void);
00124  */
00125 
00126 void mem_check(void);
00127 
00128 /***************************
00129  * Check ptr to see if it is in the range of allocated data.
00130  * Cause assertion failure if it isn't.
00131  */
00132 
00133 void mem_checkptr(void *ptr);
00134 
00135 /***************************
00136  * Allocate and return a pointer to numbytes of storage.
00137  * Use:
00138  *  void *mem_malloc(unsigned numbytes);
00139  *  void *mem_calloc(unsigned numbytes); allocated memory is cleared
00140  * Input:
00141  *  numbytes  Number of bytes to allocate
00142  * Returns:
00143  *  if (numbytes > 0)
00144  *    pointer to allocated data, NULL if out of memory
00145  *  else
00146  *    return NULL
00147  */
00148 
00149 void *mem_calloc(unsigned);
00150 void *mem_malloc(unsigned);
00151 /*****************************
00152  * Reallocate memory.
00153  * Use:
00154  *  void *mem_realloc(void *ptr,unsigned numbytes);
00155  */
00156 
00157 void *mem_realloc(void *,unsigned);
00158 
00159 /*****************************
00160  * Free memory allocated by mem_malloc(), mem_calloc() or mem_realloc().
00161  * Use:
00162  *  void mem_free(void *ptr);
00163  */
00164 
00165 void mem_free(void *);
00166 
00167 
00168 /***************************
00169  * Initialize memory handler.
00170  * Use:
00171  *  void mem_init(void);
00172  * Output:
00173  *  mem_inited = 1
00174  */
00175 
00176 void mem_init(void);
00177 
00178 /***************************
00179  * Terminate memory handler. Useful for checking for errors.
00180  * Use:
00181  *  void mem_term(void);
00182  * Output:
00183  *  mem_inited = 0
00184  */
00185 
00186 void mem_term(void);
00187 
00188 /*******************************
00189  * Allocate memory and clear it.
00190  * This routine is special in that no overhead is kept for what
00191  * the size of the allocated block is. Thus, the application needs
00192  * too keep track of that. Free only with mem_sfree().
00193  */
00194 
00195 #if MEM_DEBUG || !__ZTC__
00196 #define mem_scalloc mem_calloc
00197 #else
00198 void *mem_scalloc(size_t numbytes);
00199 #endif
00200 
00201 /*******************************
00202  * Free memory allocated with mem_scalloc().
00203  * Input:
00204  *  ptr value returned by mem_scalloc().
00205  *  numbytes  size (must be same value as passed to mem_scalloc())
00206  */
00207 
00208 #if MEM_DEBUG || !__ZTC__
00209 #define mem_sfree(ptr,size) mem_free(ptr)
00210 #else
00211 void mem_sfree(void *ptr, size_t numbytes);
00212 #endif
00213 
00214 /* The following stuff forms the implementation rather than the
00215  * definition, so ignore it.
00216  */
00217 
00218 #if MEM_DEBUG   /* if creating debug version  */
00219 #define mem_strdup(p) mem_strdup_debug((p),__FILE__,__LINE__)
00220 #define mem_malloc(u) mem_malloc_debug((u),__FILE__,__LINE__)
00221 #define mem_calloc(u) mem_calloc_debug((u),__FILE__,__LINE__)
00222 #define mem_realloc(p,u)  mem_realloc_debug((p),(u),__FILE__,__LINE__)
00223 #define mem_free(p) mem_free_debug((p),__FILE__,__LINE__)
00224 
00225 char *mem_strdup_debug(const char *,char *,int);
00226 void *mem_calloc_debug(unsigned,char *,int);
00227 void *mem_malloc_debug(unsigned,char *,int);
00228 void *mem_realloc_debug(void *,unsigned,char *,int);
00229 void  mem_free_debug(void *,char *,int);
00230 void  mem_freefp(void *);
00231 
00232 #else
00233 
00234 #define mem_freefp  mem_free
00235 #define mem_check()
00236 #define mem_checkptr(p)
00237 
00238 #endif /* MEM_DEBUG */
00239 
00240 #endif /* SAFE_MEM_H */