Changeset 1407 for trunk/psLib/src/sysUtils/psMemory.h
- Timestamp:
- Aug 6, 2004, 2:06:06 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/sysUtils/psMemory.h (modified) (20 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sysUtils/psMemory.h
r1406 r1407 1 1 #if !defined(PS_MEMORY_H) 2 #define PS_MEMORY_H 2 # define PS_MEMORY_H 3 3 4 /** @file psMemory.h 4 5 * … … 14 15 * @ingroup MemoryManagement 15 16 * 16 * @version $Revision: 1.2 3$ $Name: not supported by cvs2svn $17 * @date $Date: 2004-08-0 6 22:34:05$17 * @version $Revision: 1.24 $ $Name: not supported by cvs2svn $ 18 * @date $Date: 2004-08-07 00:06:06 $ 18 19 * 19 20 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 20 21 */ 21 22 22 # include <stdio.h>// needed for FILE23 # include <stdbool.h>24 # include <pthread.h>// we need a mutex to make this stuff thread safe.23 # include <stdio.h> // needed for FILE 24 # include <stdbool.h> 25 # include <pthread.h> // we need a mutex to make this stuff thread safe. 25 26 26 27 /** @addtogroup MemoryManagement … … 53 54 54 55 /// typedef for deallocator. 55 typedef void (*psFreeFcn) (void*ptr);56 typedef void (*psFreeFcn) (void *ptr); 56 57 57 58 /** Book-keeping data for storage allocator. … … 62 63 typedef struct psMemBlock 63 64 { 64 const void * startblock; ///< initialised to p_psMEMMAGIC65 struct psMemBlock * previousBlock; ///< previous block in allocation list66 struct psMemBlock * nextBlock; ///< next block allocation list67 psFreeFcn freeFcn; ///< deallocator. If NULL, use generic deallocation.68 size_t userMemorySize; ///< the size of the user-portion of the memory block69 const psMemoryId id; ///< a unique ID for this allocation70 const char * file; ///< set from __FILE__ in e.g. p_psAlloc71 const int lineno; ///< set from __LINE__ in e.g. p_psAlloc72 pthread_mutex_t refCounterMutex; ///< mutex to ensure exclusive access to reference counter73 psReferenceCount refCounter; ///< how many times pointer is referenced74 const void * endblock; ///< initialised to p_psMEMMAGIC65 const void *startblock; // /< initialised to p_psMEMMAGIC 66 struct psMemBlock *previousBlock; // /< previous block in allocation list 67 struct psMemBlock *nextBlock; // /< next block allocation list 68 psFreeFcn freeFcn; // /< deallocator. If NULL, use generic deallocation. 69 size_t userMemorySize; // /< the size of the user-portion of the memory block 70 const psMemoryId id; // /< a unique ID for this allocation 71 const char *file; // /< set from __FILE__ in e.g. p_psAlloc 72 const int lineno; // /< set from __LINE__ in e.g. p_psAlloc 73 pthread_mutex_t refCounterMutex; // /< mutex to ensure exclusive access to reference counter 74 psReferenceCount refCounter; // /< how many times pointer is referenced 75 const void *endblock; // /< initialised to p_psMEMMAGIC 75 76 } 76 77 psMemBlock; … … 81 82 * @ingroup memCallback 82 83 */ 83 typedef psMemoryId (*psMemAllocateCallback)( 84 const psMemBlock *ptr ///< the psMemBlock just allocated 85 ); 84 typedef psMemoryId(*psMemAllocateCallback) (const psMemBlock * ptr // /< the psMemBlock just allocated 85 ); 86 86 87 87 /** prototype of memory free callback used by memory functions … … 90 90 * @ingroup memCallback 91 91 */ 92 typedef psMemoryId (*psMemFreeCallback)( 93 const psMemBlock *ptr ///< the psMemBlock being freed 94 ); 92 typedef psMemoryId(*psMemFreeCallback) (const psMemBlock * ptr // /< the psMemBlock being freed 93 ); 95 94 96 95 /** prototype of a callback used in error conditions … … 101 100 * @ingroup memCallback 102 101 */ 103 typedef void (*psMemProblemCallback) (104 const psMemBlock *ptr, ///< the pointer to the problematic memoryblock.105 const char *file, ///< the file in which the problem originated106 int lineno ///< the line number in which the problem originated107 );102 typedef void (*psMemProblemCallback) (const psMemBlock * ptr, // /< the pointer to the problematic memory 103 // block. 104 const char *file, // /< the file in which the problem originated 105 int lineno // /< the line number in which the problem originated 106 ); 108 107 109 108 /** prototype of a callback function used when memory runs out … … 115 114 * @ingroup memCallback 116 115 */ 117 typedef void *(*psMemExhaustedCallback)( 118 size_t size //< the size of buffer required 119 ); 116 typedef void *(*psMemExhaustedCallback) (size_t size // < the size of buffer required 117 ); 120 118 121 119 /** Memory allocation. This operates much like malloc(), but is guaranteed to return a non-NULL value. … … 124 122 * @see psFree 125 123 */ 126 #ifdef DOXYGEN 127 void* psAlloc( 128 size_t size ///< Size required 129 ); 130 #else 131 void* p_psAlloc( 132 size_t size, ///< Size required 133 const char *file, ///< File of call 134 int lineno ///< Line number of call 135 ); 136 137 void p_psMemSetDeallocator(void* ptr, psFreeFcn freeFcn); 138 psFreeFcn p_psMemGetDeallocator(void* ptr); 124 # ifdef DOXYGEN 125 void *psAlloc(size_t size // /< Size required 126 ); 127 # else 128 void *p_psAlloc(size_t size, // /< Size required 129 const char *file, // /< File of call 130 int lineno // /< Line number of call 131 ); 132 133 void p_psMemSetDeallocator(void *ptr, psFreeFcn freeFcn); 134 psFreeFcn p_psMemGetDeallocator(void *ptr); 139 135 140 136 /// Memory allocation. psAlloc sends file and line number to p_psAlloc. 141 #define psAlloc(size) p_psAlloc(size, __FILE__, __LINE__) 142 #endif 143 137 # define psAlloc(size) p_psAlloc(size, __FILE__, __LINE__) 138 # endif 144 139 145 140 /** Memory re-allocation. This operates much like realloc(), but is guaranteed to return a non-NULL value. … … 148 143 * @see psAlloc, psFree 149 144 */ 150 # ifdef DOXYGEN151 void * psRealloc(void *ptr ///< Pointer to re-allocate152 size_t size, ///< Size required145 # ifdef DOXYGEN 146 void *psRealloc(void *ptr // /< Pointer to re-allocate 147 size_t size, // /< Size required 153 148 ); 154 #else 155 void* p_psRealloc(void *ptr, ///< Pointer to re-allocate 156 size_t size, ///< Size required 157 const char *file, ///< File of call 158 int lineno ///< Line number of call 149 # else 150 void *p_psRealloc(void *ptr, // /< Pointer to re-allocate 151 size_t size, // /< Size required 152 const char *file, // /< File of call 153 int lineno // /< Line number of call 154 ); 155 156 /// Memory re-allocation. psRealloc sends file and line number to p_psRealloc. 157 # define psRealloc(ptr, size) p_psRealloc(ptr, size, __FILE__, __LINE__) 158 159 # endif 160 161 /** Free memory. This operates much like free(). 162 * 163 * @see psAlloc, psRealloc 164 */ 165 # ifdef DOXYGEN 166 void psFree(void *ptr, // /< Pointer to free, if NULL, function returns immediately. 167 ); 168 # else 169 void p_psFree(void *ptr, // /< Pointer to free 170 const char *file, // /< File of call 171 int lineno // /< Line number of call 159 172 ); 160 173 161 /// Memory re-allocation. psRealloc sends file and line number to p_psRealloc.162 #define psRealloc(ptr, size) p_psRealloc(ptr, size, __FILE__, __LINE__)163 164 #endif165 166 167 /** Free memory. This operates much like free().168 *169 * @see psAlloc, psRealloc170 */171 #ifdef DOXYGEN172 void psFree(void *ptr, ///< Pointer to free, if NULL, function returns immediately.173 );174 #else175 void p_psFree(void *ptr, ///< Pointer to free176 const char *file, ///< File of call177 int lineno ///< Line number of call178 );179 180 174 /// Free memory. psFree sends file and line number to p_psFree. 181 # define psFree(size) p_psFree(size, __FILE__, __LINE__)182 183 # endif175 # define psFree(size) p_psFree(size, __FILE__, __LINE__) 176 177 # endif 184 178 185 179 /** Check for memory leaks. This scans for allocated memory buffers not freed with an ID not less than id0. … … 197 191 * @ingroup memTracing 198 192 */ 199 int psMemCheckLeaks( 200 psMemoryId id0, ///< don't list blocks with id < id0 201 psMemBlock ***arr, ///< pointer to array of pointers to leaked blocks, or NULL 202 FILE *fd ///< print list of leaks to fd (or NULL) 203 ); 193 int psMemCheckLeaks(psMemoryId id0, // /< don't list blocks with id < id0 194 psMemBlock *** arr, // /< pointer to array of pointers to leaked blocks, or NULL 195 FILE * fd // /< print list of leaks to fd (or NULL) 196 ); 204 197 205 198 /** Check for memory corruption. Scans all currently allocated memory buffers and checks for corruptions, … … 208 201 * @ingroup memTracing 209 202 */ 210 int psMemCheckCorruption( 211 bool abort_on_error ///< Abort on detecting corruption? 212 ); 203 int psMemCheckCorruption(bool abort_on_error // /< Abort on detecting corruption? 204 ); 213 205 214 206 /** Return reference counter … … 216 208 * @ingroup memRefCount 217 209 */ 218 psReferenceCount psMemGetRefCounter( 219 void *vptr ///< Pointer to get refCounter for 220 ); 210 psReferenceCount psMemGetRefCounter(void *vptr // /< Pointer to get refCounter for 211 ); 221 212 222 213 /** Increment reference counter and return the pointer … … 224 215 * @ingroup memRefCount 225 216 */ 226 #ifdef DOXYGEN 227 void* psMemIncrRefCounter( 228 void *vptr ///< Pointer to increment refCounter, and return 229 ); 230 #else 231 void* p_psMemIncrRefCounter( 232 void *vptr, ///< Pointer to increment refCounter, and return 233 const char *file, ///< File of call 234 int lineno ///< Line number of call 235 ); 236 #define psMemIncrRefCounter(vptr) p_psMemIncrRefCounter(vptr, __FILE__, __LINE__) 237 #endif 217 # ifdef DOXYGEN 218 void *psMemIncrRefCounter(void *vptr // /< Pointer to increment refCounter, and return 219 ); 220 # else 221 void *p_psMemIncrRefCounter(void *vptr, // /< Pointer to increment refCounter, and return 222 const char *file, // /< File of call 223 int lineno // /< Line number of call 224 ); 225 226 # define psMemIncrRefCounter(vptr) p_psMemIncrRefCounter(vptr, __FILE__, __LINE__) 227 # endif 238 228 239 229 /** Decrement reference counter and return the pointer … … 241 231 * @ingroup memRefCount 242 232 */ 243 #ifdef DOXYGEN 244 void* psMemDecrRefCounter( 245 void *vptr ///< Pointer to decrement refCounter, and return 246 ); 247 #else 248 void* p_psMemDecrRefCounter( 249 void *vptr, ///< Pointer to decrement refCounter, and return 250 const char *file, ///< File of call 251 int lineno ///< Line number of call 252 ); 253 #define psMemDecrRefCounter(vptr) p_psMemDecrRefCounter(vptr, __FILE__, __LINE__) 254 #endif 233 # ifdef DOXYGEN 234 void *psMemDecrRefCounter(void *vptr // /< Pointer to decrement refCounter, and return 235 ); 236 # else 237 void *p_psMemDecrRefCounter(void *vptr, // /< Pointer to decrement refCounter, and return 238 const char *file, // /< File of call 239 int lineno // /< Line number of call 240 ); 241 242 # define psMemDecrRefCounter(vptr) p_psMemDecrRefCounter(vptr, __FILE__, __LINE__) 243 # endif 255 244 256 245 /** Set callback for problems 257 246 * @ingroup memCallback 258 247 */ 259 psMemProblemCallback psMemProblemCallbackSet( 260 psMemProblemCallback func ///< Function to run 261 ); 248 psMemProblemCallback psMemProblemCallbackSet(psMemProblemCallback func // /< Function to run 249 ); 262 250 263 251 /** Set callback for out-of-memory … … 265 253 * @ingroup memCallback 266 254 */ 267 psMemExhaustedCallback psMemExhaustedCallbackSet( 268 psMemExhaustedCallback func ///< Function to run 269 ); 255 psMemExhaustedCallback psMemExhaustedCallbackSet(psMemExhaustedCallback func // /< Function to run 256 ); 270 257 271 258 /** Set call back for when a particular memory block is allocated … … 273 260 * @ingroup memCallback 274 261 */ 275 psMemAllocateCallback psMemAllocateCallbackSet( 276 psMemAllocateCallback func ///< Function to run 277 ); 262 psMemAllocateCallback psMemAllocateCallbackSet(psMemAllocateCallback func // /< Function to run 263 ); 278 264 279 265 /** Set call back for when a particular memory block is freed … … 281 267 * @ingroup memCallback 282 268 */ 283 psMemFreeCallback psMemFreeCallbackSet( 284 psMemFreeCallback func ///< Function to run 285 ); 269 psMemFreeCallback psMemFreeCallbackSet(psMemFreeCallback func // /< Function to run 270 ); 286 271 287 272 /** get next memory ID … … 295 280 * @ingroup memCallback 296 281 */ 297 psMemoryId psMemAllocateCallbackSetID( 298 psMemoryId id ///< ID to set 299 ); 282 psMemoryId psMemAllocateCallbackSetID(psMemoryId id // /< ID to set 283 ); 300 284 301 285 /** set p_psMemFreeID to id … … 303 287 * @ingroup memCallback 304 288 */ 305 psMemoryId psMemFreeCallbackSetID( 306 psMemoryId id ///< ID to set 307 ); 289 psMemoryId psMemFreeCallbackSetID(psMemoryId id // /< ID to set 290 ); 308 291 309 292 //@} End of Memory Management Functions 310 293 311 # ifndef DOXYGEN294 # ifndef DOXYGEN 312 295 313 296 /* 314 297 * Ensure that any program using malloc/realloc/free will fail to compile 315 298 */ 316 #ifndef PS_ALLOW_MALLOC 317 #ifdef __GNUC__ 318 #pragma GCC poison malloc realloc calloc free 319 #else 320 #define malloc(S) _Pragma("error Use of malloc is not allowed. Use psAlloc instead.") 321 #define realloc(P,S) _Pragma("error Use of realloc is not allowed. Use psRealloc instead.") 322 #define calloc(S) _Pragma("error Use of calloc is not allowed. Use psAlloc instead.") 323 #define free(P) _Pragma("error Use of free is not allowed. Use psFree instead.") 324 #endif 325 #endif 326 327 #endif // doxygen skip 328 329 #endif // end of header file 299 # ifndef PS_ALLOW_MALLOC 300 # ifdef __GNUC__ 301 # pragma GCC poison malloc realloc calloc free 302 # else 303 # define malloc(S) _Pragma("error Use of malloc is not allowed. Use psAlloc instead.") 304 # define realloc(P,S) _Pragma("error Use of realloc is not allowed. Use psRealloc instead.") 305 # define calloc(S) _Pragma("error Use of calloc is not allowed. Use psAlloc instead.") 306 # define free(P) _Pragma("error Use of free is not allowed. Use psFree instead.") 307 # endif 308 # endif 309 310 # endif 311 // doxygen skip 312 313 #endif // end of header file
Note:
See TracChangeset
for help on using the changeset viewer.
