Changeset 1448 for trunk/psLib/src/sys/psMemory.h
- Timestamp:
- Aug 9, 2004, 3:55:34 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/sys/psMemory.h (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sys/psMemory.h
r1441 r1448 1 #if !defined(PS_MEMORY_H)2 #define PS_MEMORY_H3 4 1 /** @file psMemory.h 5 2 * … … 15 12 * @ingroup MemoryManagement 16 13 * 17 * @version $Revision: 1.2 7$ $Name: not supported by cvs2svn $18 * @date $Date: 2004-08- 09 23:40:55$14 * @version $Revision: 1.28 $ $Name: not supported by cvs2svn $ 15 * @date $Date: 2004-08-10 01:55:34 $ 19 16 * 20 17 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 21 18 */ 19 20 #if !defined(PS_MEMORY_H) 21 #define PS_MEMORY_H 22 22 23 23 #include <stdio.h> // needed for FILE … … 63 63 typedef struct psMemBlock 64 64 { 65 const void *startblock; ///< initialised to p_psMEMMAGIC66 struct psMemBlock* previousBlock; ///< previous block in allocation list67 struct psMemBlock* nextBlock; ///< next block allocation list68 psFreeFcn freeFcn; ///< deallocator. If NULL, use generic deallocation.69 size_t userMemorySize; ///< the size of the user-portion of the memory block70 const psMemoryId id; ///< a unique ID for this allocation71 const char *file; ///< set from __FILE__ in e.g. p_psAlloc72 const int lineno; ///< set from __LINE__ in e.g. p_psAlloc73 pthread_mutex_t refCounterMutex; ///< mutex to ensure exclusive access to reference counter74 psReferenceCount refCounter; ///< how many times pointer is referenced75 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 76 76 } 77 77 psMemBlock; … … 82 82 * @ingroup memCallback 83 83 */ 84 typedef psMemoryId(*psMemAllocateCallback) (const psMemBlock* ptr ///< the psMemBlock just allocated 85 ); 84 typedef psMemoryId(*psMemAllocateCallback) ( 85 const psMemBlock* ptr ///< the psMemBlock just allocated 86 ); 86 87 87 88 /** prototype of memory free callback used by memory functions … … 90 91 * @ingroup memCallback 91 92 */ 92 typedef psMemoryId(*psMemFreeCallback) (const psMemBlock* ptr ///< the psMemBlock being freed 93 ); 93 typedef psMemoryId(*psMemFreeCallback) ( 94 const psMemBlock* ptr ///< the psMemBlock being freed 95 ); 94 96 95 97 /** prototype of a callback used in error conditions … … 100 102 * @ingroup memCallback 101 103 */ 102 typedef void (*psMemProblemCallback) ( const psMemBlock* ptr, ///< the pointer to the problematic memory103 //block.104 const char *file,///< the file in which the problem originated105 int lineno///< the line number in which the problem originated106 );104 typedef void (*psMemProblemCallback) ( 105 const psMemBlock* ptr, ///< the pointer to the problematic memory block. 106 const char *file, ///< the file in which the problem originated 107 int lineno ///< the line number in which the problem originated 108 ); 107 109 108 110 /** prototype of a callback function used when memory runs out … … 114 116 * @ingroup memCallback 115 117 */ 116 typedef void *(*psMemExhaustedCallback) (size_t size // < the size of buffer required 117 ); 118 typedef void *(*psMemExhaustedCallback) ( 119 size_t size ///< the size of buffer required 120 ); 118 121 119 122 /** Memory allocation. This operates much like malloc(), but is guaranteed to return a non-NULL value. … … 145 148 */ 146 149 #ifdef DOXYGEN 147 void *psRealloc(void *ptr ///< Pointer to re-allocate 148 size_t size, ///< Size required 149 ); 150 #else 151 152 void *p_psRealloc(void *ptr, ///< Pointer to re-allocate 153 size_t size, ///< Size required 154 const char *file, ///< File of call 155 int lineno ///< Line number of call 156 ); 150 void *psRealloc( 151 void *ptr ///< Pointer to re-allocate 152 size_t size, ///< Size required 153 ); 154 #else 155 156 void *p_psRealloc( 157 void *ptr, ///< Pointer to re-allocate 158 size_t size, ///< Size required 159 const char *file, ///< File of call 160 int lineno ///< Line number of call 161 ); 157 162 158 163 /// Memory re-allocation. psRealloc sends file and line number to p_psRealloc. … … 166 171 */ 167 172 #ifdef DOXYGEN 168 void psFree(void *ptr, ///< Pointer to free, if NULL, function returns immediately. 169 ); 170 #else 171 172 void p_psFree(void *ptr, ///< Pointer to free 173 const char *file, ///< File of call 174 int lineno ///< Line number of call 175 ); 173 void psFree( 174 void *ptr, ///< Pointer to free, if NULL, function returns immediately. 175 ); 176 #else 177 178 void p_psFree( 179 void *ptr, ///< Pointer to free 180 const char *file, ///< File of call 181 int lineno ///< Line number of call 182 ); 176 183 177 184 /// Free memory. psFree sends file and line number to p_psFree. … … 194 201 * @ingroup memTracing 195 202 */ 196 int psMemCheckLeaks(psMemoryId id0, ///< don't list blocks with id < id0 197 psMemBlock* ** arr, ///< pointer to array of pointers to leaked blocks, or NULL 198 FILE * fd ///< print list of leaks to fd (or NULL) 199 ); 203 int psMemCheckLeaks( 204 psMemoryId id0, ///< don't list blocks with id < id0 205 psMemBlock* ** arr, ///< pointer to array of pointers to leaked blocks, or NULL 206 FILE * fd ///< print list of leaks to fd (or NULL) 207 ); 200 208 201 209 /** Check for memory corruption. Scans all currently allocated memory buffers and checks for corruptions, … … 204 212 * @ingroup memTracing 205 213 */ 206 int psMemCheckCorruption(bool abort_on_error ///< Abort on detecting corruption? 207 ); 214 int psMemCheckCorruption( 215 bool abort_on_error ///< Abort on detecting corruption? 216 ); 208 217 209 218 /** Return reference counter … … 211 220 * @ingroup memRefCount 212 221 */ 213 psReferenceCount psMemGetRefCounter(void *vptr ///< Pointer to get refCounter for 214 ); 222 psReferenceCount psMemGetRefCounter( 223 void *vptr ///< Pointer to get refCounter for 224 ); 215 225 216 226 /** Increment reference counter and return the pointer … … 219 229 */ 220 230 #ifdef DOXYGEN 221 void *psMemIncrRefCounter(void *vptr ///< Pointer to increment refCounter, and return 222 ); 223 #else 224 225 void *p_psMemIncrRefCounter(void *vptr, ///< Pointer to increment refCounter, and return 226 const char *file, ///< File of call 227 int lineno ///< Line number of call 228 ); 231 void *psMemIncrRefCounter( 232 void *vptr ///< Pointer to increment refCounter, and return 233 ); 234 #else 235 236 void *p_psMemIncrRefCounter( 237 void *vptr, ///< Pointer to increment refCounter, and return 238 const char *file, ///< File of call 239 int lineno ///< Line number of call 240 ); 229 241 230 242 #define psMemIncrRefCounter(vptr) p_psMemIncrRefCounter(vptr, __FILE__, __LINE__) … … 234 246 * 235 247 * @ingroup memRefCount 236 */ 237 #ifdef DOXYGEN 238 void *psMemDecrRefCounter(void *vptr ///< Pointer to decrement refCounter, and return 239 ); 240 #else 241 242 void *p_psMemDecrRefCounter(void *vptr, ///< Pointer to decrement refCounter, and return 243 const char *file, ///< File of call 244 int lineno ///< Line number of call 245 ); 248 * 249 * @return void* the pointer deremented in refCount, or NULL if pointer is 250 * fully dereferenced. 251 */ 252 #ifdef DOXYGEN 253 void* psMemDecrRefCounter( 254 void* vptr ///< Pointer to decrement refCounter, and return 255 ); 256 #else 257 258 void *p_psMemDecrRefCounter( 259 void *vptr, ///< Pointer to decrement refCounter, and return 260 const char *file, ///< File of call 261 int lineno ///< Line number of call 262 ); 246 263 247 264 #define psMemDecrRefCounter(vptr) p_psMemDecrRefCounter(vptr, __FILE__, __LINE__) 248 265 #endif 249 266 250 /** Set callback for problems 251 * @ingroup memCallback 252 */ 253 psMemProblemCallback psMemProblemCallbackSet(psMemProblemCallback func ///< Function to run 254 ); 255 256 /** Set callback for out-of-memory 257 * 258 * @ingroup memCallback 259 */ 260 psMemExhaustedCallback psMemExhaustedCallbackSet(psMemExhaustedCallback func ///< Function to run 261 ); 267 /** Set callback for problems. 268 * 269 * At various occasions, the memory manager can check the state of the memory 270 * stack. If any of these checks discover that the memory stack is corrupted, 271 * the psMemProblemCallback is called. 272 273 * @ingroup memCallback 274 * 275 * @return psMemProblemCallback old psMemProblemCallback function 276 */ 277 psMemProblemCallback psMemProblemCallbackSet( 278 psMemProblemCallback func ///< Function to run at memory problem detection 279 ); 280 281 /** Set callback for out-of-memory. 282 * 283 * If not enough memory is available to satisfy a request by psAlloc or 284 * psRealloc, these functions attempt to find an alternative solution by 285 * calling the psMemExhaustedCallback, a function which may be set by the 286 * programmer in appropriate circumstances, rather than immediately fail. 287 * The typical use of such a feature may be when a program needs a large 288 * chunk of memory to do an operation, but the exact size is not critical. 289 * This feature gives the programmer the opportunity to make a smaller 290 * request and try again, limiting the size of the operating buffer. 291 * 292 * @ingroup memCallback 293 * 294 * @return psMemExhaustedCallback old psMemExhaustedCallback function 295 */ 296 psMemExhaustedCallback psMemExhaustedCallbackSet( 297 psMemExhaustedCallback func ///< Function to run at memory exhaustion 298 ); 262 299 263 300 /** Set call back for when a particular memory block is allocated 264 301 * 265 * @ingroup memCallback 266 */ 267 psMemAllocateCallback psMemAllocateCallbackSet(psMemAllocateCallback func ///< Function to run 268 ); 302 * A private variable, p_psMemAllocateID, can be used to trace the allocation 303 * and freeing of specific memory blocks. If p_psMemAllocateID is set and a 304 * memory block with that ID is allocated, psMemAllocateCallback is called 305 * just before memory is returned to the calling function. 306 * 307 * @ingroup memCallback 308 * 309 * @return psMemAllocateCallback old psMemAllocateCallback function 310 */ 311 psMemAllocateCallback psMemAllocateCallbackSet( 312 psMemAllocateCallback func ///< Function to run at memory allocation of specific mem block 313 ); 269 314 270 315 /** Set call back for when a particular memory block is freed 271 316 * 272 * @ingroup memCallback 273 */ 274 psMemFreeCallback psMemFreeCallbackSet(psMemFreeCallback func ///< Function to run 275 ); 317 * A private variable, p_psMemFreeID, can be used to trace the freeing of 318 * specific memory blocks. If p_psMemFreeID is set and the memory block with 319 * the ID is about to be freed, the psMemFreeCallback callback is called just 320 * before the memory block is freed. 321 * 322 * @ingroup memCallback 323 * 324 * @return psMemFreeCallback old psMemFreeCallback function 325 */ 326 psMemFreeCallback psMemFreeCallbackSet( 327 psMemFreeCallback func ///< Function to run at memory free of specific mem block 328 ); 276 329 277 330 /** get next memory ID 278 331 * 279 332 * @ingroup memCallback 333 * 334 * @return psMemoryId the next memory ID to be used 280 335 */ 281 336 psMemoryId psMemGetId(void); 282 337 283 /** set p_psMemAllocateID to id 284 * 285 * @ingroup memCallback 286 */ 287 psMemoryId psMemAllocateCallbackSetID(psMemoryId id ///< ID to set 288 ); 338 /** set p_psMemAllocateID to specific id 339 * 340 * A private variable, p_psMemAllocateID, can be used to trace the allocation 341 * and freeing of specific memory blocks. If p_psMemAllocateID is set and a 342 * memory block with that ID is allocated, psMemAllocateCallback is called 343 * just before memory is returned to the calling function. 344 * 345 * @ingroup memCallback 346 * 347 * @return psMemoryId 348 * 349 * @see psMemAllocateCallbackSet 350 */ 351 psMemoryId psMemAllocateCallbackSetID( 352 psMemoryId id ///< ID to set 353 ); 289 354 290 355 /** set p_psMemFreeID to id 291 356 * 292 * @ingroup memCallback 293 */ 294 psMemoryId psMemFreeCallbackSetID(psMemoryId id ///< ID to set 295 ); 357 * A private variable, p_psMemFreeID, can be used to trace the freeing of 358 * specific memory blocks. If p_psMemFreeID is set and the memory block with 359 * the ID is about to be freed, the psMemFreeCallback callback is called just 360 * before the memory block is freed. 361 * 362 * @ingroup memCallback 363 * 364 * @return psMemoryId the old p_psMemFreeID 365 * 366 * @see psMemFreeCallbackSet 367 */ 368 psMemoryId psMemFreeCallbackSetID( 369 psMemoryId id ///< ID to set 370 ); 296 371 297 372 //@} End of Memory Management Functions … … 306 381 #pragma GCC poison malloc realloc calloc free 307 382 #else 308 # define malloc(S) _Pragma("error Use of malloc is not allowed. Use psAlloc instead.")309 # define realloc(P,S) _Pragma("error Use of realloc is not allowed. Use psRealloc instead.")310 # define calloc(S) _Pragma("error Use of calloc is not allowed. Use psAlloc instead.")311 # define free(P) _Pragma("error Use of free is not allowed. Use psFree instead.")312 # endif313 # endif314 315 # endif383 #define malloc(S) _Pragma("error Use of malloc is not allowed. Use psAlloc instead.") 384 #define realloc(P,S) _Pragma("error Use of realloc is not allowed. Use psRealloc instead.") 385 #define calloc(S) _Pragma("error Use of calloc is not allowed. Use psAlloc instead.") 386 #define free(P) _Pragma("error Use of free is not allowed. Use psFree instead.") 387 #endif 388 #endif 389 390 #endif 316 391 // doxygen skip 317 392
Note:
See TracChangeset
for help on using the changeset viewer.
