Changeset 11694 for trunk/psLib/src/sys/psMemory.h
- Timestamp:
- Feb 7, 2007, 3:59:28 PM (19 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/sys/psMemory.h (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sys/psMemory.h
r11674 r11694 15 15 * @ingroup MemoryManagement 16 16 * 17 * @version $Revision: 1.6 7$ $Name: not supported by cvs2svn $18 * @date $Date: 2007-02-0 7 01:15:49$17 * @version $Revision: 1.68 $ $Name: not supported by cvs2svn $ 18 * @date $Date: 2007-02-08 01:59:28 $ 19 19 * 20 20 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 131 131 size_t size ///< Size required 132 132 ); 133 #else // #ifdef DOXYGEN133 #else // ifdef DOXYGEN 134 134 void *p_psAlloc( 135 135 const char *file, ///< File of caller … … 137 137 const char *func, ///< Function name of caller 138 138 size_t size ///< Size required 139 #ifdef __GNUC__139 #ifdef __GNUC__ 140 140 ) __attribute__((malloc)); 141 # else // __GNUC__ 142 ); 143 #endif // __GNUC__ 144 145 /// Memory allocation. psAlloc sends file and line number to p_psAlloc. 146 #ifndef SWIG 147 #define psAlloc(size) \ 148 p_psAlloc(__FILE__, __LINE__, __func__, size) 149 #endif // ! SWIG 150 #endif // ! DOXYGEN 151 152 153 /** Set the deallocator routine 154 * 155 * A deallocator routine can optionally be assigned to a memory block to 156 * ensure that associated memory blocks also get freed, e.g., memory buffers 157 * referenced within a struct. 158 * 159 */ 160 #ifdef DOXYGEN 161 void psMemSetDeallocator( 162 void *ptr, ///< the memory block to operate on 163 psFreeFunc freeFunc ///< the function to be executed at deallocation 164 ); 165 #else // ifdef DOXYGEN 166 void p_psMemSetDeallocator( 167 const char *file, ///< File of caller 168 unsigned int lineno, ///< Line number of caller 169 const char *func, ///< Function name of caller 170 void *ptr, ///< the memory block to operate on 171 psFreeFunc freeFunc ///< the function to be executed at deallocation 172 ); 173 #ifndef SWIG 174 #define psMemSetDeallocator(ptr, freeFunc) \ 175 p_psMemSetDeallocator(__FILE__, __LINE__, __func__, ptr, freeFunc) 176 #endif // ! SWIG 177 #endif // ifdef DOXYGEN 178 179 180 /** Get the deallocator routine 181 * 182 * This function returns the deallocator for a memory block. A deallocator 183 * routine can optionally be assigned to a memory block to ensure that 184 * associated memory blocks also get freed, e.g., memory buffers referenced 185 * within a struct. 186 * 187 * @return psFreeFunc the routine to be called at deallocation. 188 */ 189 #ifdef DOXYGEN 190 psFreeFunc psMemGetDeallocator( 191 void *ptr ///< the memory block 192 ); 193 #else // ifdef DOXYGEN 194 psFreeFunc p_psMemGetDeallocator( 195 const char *file, ///< File of caller 196 unsigned int lineno, ///< Line number of caller 197 const char *func, ///< Function name of caller 198 void *ptr ///< the memory block 199 ); 200 #ifndef SWIG 201 #define psMemGetDeallocator(ptr) \ 202 p_psMemGetDeallocator(__FILE__, __LINE__, __func__, ptr) 203 #endif // ! SWIG 204 #endif // ifdef DOXYGEN 205 206 207 /** Activate or Deactivate thread safety and mutex locking in the memory 208 * management. 209 * 210 * psMemThreadSafety shall turn on thread safety in the memory management 211 * functions if safe is true, and deactivate all mutex locking in the memory 212 * management functions if safe is false. The function shall return the 213 * previous value of the thread safety. Note that the default behaviour of 214 * the library shall be for the locking to be performed. 215 * 216 * @return bool: The previous value of the thread safety. 217 */ 218 bool psMemSetThreadSafety( 219 bool safe ///< boolean for turning on/off thread safety 220 ); 221 222 /** Get the current state of thread safety and mutex locking in the memory 223 * management. 224 * 225 * psMemGetThreadSafety shall return the current state of thread safety in the 226 * memory management system. 227 * 228 * @return bool: The current state of thread safety. 229 */ 230 bool psMemGetThreadSafety(void); 231 232 233 /** Set the memory as persistent so that it is ignored when detecting memory 234 * leaks. 235 * 236 * Used to mark a memory block as persistent data within the library, 237 * i.e., non user-level data used to hold psLib's state or cache data. Such 238 * examples of this class of memory is psTrace's trace-levels and dynamic 239 * error codes. 240 * 241 * Memory marked as persistent is excluded from memory leak checks. 242 * 243 */ 244 #ifdef DOXYGEN 245 void psMemSetPersistent( 246 void *ptr, ///< the memory block to operate on 247 bool value, ///< true if memory is persistent, otherwise false 248 ); 249 #else // #ifdef DOXYGEN 250 void p_psMemSetPersistent( 251 const char *file, ///< File of caller 252 unsigned int lineno, ///< Line number of caller 253 const char *func, ///< Function name of caller 254 void *ptr, ///< the memory block to operate on 255 bool value ///< true if memory is persistent, otherwise false 256 ); 257 #ifndef SWIG 258 #define psMemSetPersistent(ptr, value) \ 259 p_psMemSetPersistent(__FILE__, __LINE__, __func__, ptr, value) 260 #endif // ! SWIG 261 #endif // DOXYGEN 262 263 264 /** Set whether allocated memory is persistent 265 * 266 * Set whether allocated memory is persistent. The defeault is false. 267 * 268 * @return bool: The previous value of whether all allocated memory is 269 * persistent 270 */ 271 bool p_psMemAllocatePersistent(bool is_persistent); ///< Should all memory allocated be persistent? 272 273 274 /** Get the memory's persistent flag. 275 * 276 * Checks if a memory block has been marked as persistent by 277 * p_psMemSetPresistent. 278 * 279 * Memory marked as persistent is excluded from memory leak checks. 280 * 281 * @return bool true if memory is marked persistent, otherwise false. 282 */ 283 #ifdef DOXYGEN 284 bool psMemGetPersistent( 285 void *ptr, ///< the memory block to check. 286 ); 287 #else // ifdef DOXYGEN 288 bool p_psMemGetPersistent( 289 const char *file, ///< File of caller 290 unsigned int lineno, ///< Line number of caller 291 const char *func, ///< Function name of caller 292 void *ptr ///< the memory block to check. 293 ); 294 #ifndef SWIG 295 #define psMemGetPersistent(ptr) \ 296 p_psMemGetPersistent(__FILE__, __LINE__, __func__, ptr) 297 #endif // ! SWIG 298 #endif // DOXYGEN 299 300 301 /** Memory re-allocation. This operates much like realloc(), but is guaranteed 302 * to return a non-NULL value. 303 * 304 * @return void * pointer to resized buffer. This will not be NULL. 305 * @see psAlloc, psFree 306 */ 307 #ifdef DOXYGEN 308 void *psRealloc( 309 void *ptr, ///< Pointer to re-allocate 310 size_t size ///< Size required 311 ); 312 #else // #ifdef DOXYGEN 313 void *p_psRealloc( 314 const char *file, ///< File of caller 315 unsigned int lineno, ///< Line number of caller 316 const char *func, ///< Function name of caller 317 void *ptr, ///< Pointer to re-allocate 318 size_t size ///< Size required 319 #ifdef __GNUC__ 320 ) __attribute__((malloc)); 321 # else // __GNUC__ 322 ); 323 #endif // __GNUC__ 324 #ifndef SWIG 325 #define psRealloc(ptr, size) \ 326 p_psRealloc(__FILE__, __LINE__, __func__, ptr, size) 327 #endif // ! SWIG 328 #endif // ! DOXYGEN 329 330 331 /** Free memory. This operates much like free(). 332 * 333 * @see psAlloc, psRealloc 334 */ 335 #ifdef DOXYGEN 336 void psFree( 337 void *ptr ///< Pointer to free, if NULL, function returns immediately. 338 ); 339 #else // #ifdef DOXYGEN 340 /// Free memory. psFree sends file and line number to p_psFree. 341 #ifndef SWIG 342 #define psFree(ptr) \ 141 # else // ifdef __GNUC__ 142 ); 143 #endif // ifdef __GNUC__ 144 #ifndef SWIG 145 #define psAlloc(size) \ 146 p_psAlloc(__FILE__, __LINE__, __func__, size) 147 #endif // ifndef SWIG 148 #endif // ifdef DOXYGEN 149 150 151 /** Set the deallocator routine 152 * 153 * A deallocator routine can optionally be assigned to a memory block to 154 * ensure that associated memory blocks also get freed, e.g., memory buffers 155 * referenced within a struct. 156 * 157 */ 158 #ifdef DOXYGEN 159 void psMemSetDeallocator( 160 void *ptr, ///< the memory block to operate on 161 psFreeFunc freeFunc ///< the function to be executed at deallocation 162 ); 163 #else // ifdef DOXYGEN 164 void p_psMemSetDeallocator( 165 const char *file, ///< File of caller 166 unsigned int lineno, ///< Line number of caller 167 const char *func, ///< Function name of caller 168 void *ptr, ///< the memory block to operate on 169 psFreeFunc freeFunc ///< the function to be executed at deallocation 170 ); 171 #ifndef SWIG 172 #define psMemSetDeallocator(ptr, freeFunc) \ 173 p_psMemSetDeallocator(__FILE__, __LINE__, __func__, ptr, freeFunc) 174 #endif // ifndef SWIG 175 #endif // ifdef DOXYGEN 176 177 178 /** Get the deallocator routine 179 * 180 * This function returns the deallocator for a memory block. A deallocator 181 * routine can optionally be assigned to a memory block to ensure that 182 * associated memory blocks also get freed, e.g., memory buffers referenced 183 * within a struct. 184 * 185 * @return psFreeFunc the routine to be called at deallocation. 186 */ 187 #ifdef DOXYGEN 188 psFreeFunc psMemGetDeallocator( 189 void *ptr ///< the memory block 190 ); 191 #else // ifdef DOXYGEN 192 psFreeFunc p_psMemGetDeallocator( 193 const char *file, ///< File of caller 194 unsigned int lineno, ///< Line number of caller 195 const char *func, ///< Function name of caller 196 void *ptr ///< the memory block 197 ); 198 #ifndef SWIG 199 #define psMemGetDeallocator(ptr) \ 200 p_psMemGetDeallocator(__FILE__, __LINE__, __func__, ptr) 201 #endif // ifndef SWIG 202 #endif // ifdef DOXYGEN 203 204 205 /** Activate or Deactivate thread safety and mutex locking in the memory 206 * management. 207 * 208 * psMemThreadSafety shall turn on thread safety in the memory management 209 * functions if safe is true, and deactivate all mutex locking in the memory 210 * management functions if safe is false. The function shall return the 211 * previous value of the thread safety. Note that the default behaviour of 212 * the library shall be for the locking to be performed. 213 * 214 * @return bool: The previous value of the thread safety. 215 */ 216 bool psMemSetThreadSafety( 217 bool safe ///< boolean for turning on/off thread safety 218 ); 219 220 221 /** Get the current state of thread safety and mutex locking in the memory 222 * management. 223 * 224 * psMemGetThreadSafety shall return the current state of thread safety in the 225 * memory management system. 226 * 227 * @return bool: The current state of thread safety. 228 */ 229 bool psMemGetThreadSafety(void); 230 231 232 /** Set the memory as persistent so that it is ignored when detecting memory 233 * leaks. 234 * 235 * Used to mark a memory block as persistent data within the library, 236 * i.e., non user-level data used to hold psLib's state or cache data. Such 237 * examples of this class of memory is psTrace's trace-levels and dynamic 238 * error codes. 239 * 240 * Memory marked as persistent is excluded from memory leak checks. 241 * 242 */ 243 #ifdef DOXYGEN 244 void psMemSetPersistent( 245 void *ptr, ///< the memory block to operate on 246 bool value, ///< true if memory is persistent, otherwise false 247 ); 248 #else // ifdef DOXYGEN 249 void p_psMemSetPersistent( 250 const char *file, ///< File of caller 251 unsigned int lineno, ///< Line number of caller 252 const char *func, ///< Function name of caller 253 void *ptr, ///< the memory block to operate on 254 bool value ///< true if memory is persistent, otherwise false 255 ); 256 #ifndef SWIG 257 #define psMemSetPersistent(ptr, value) \ 258 p_psMemSetPersistent(__FILE__, __LINE__, __func__, ptr, value) 259 #endif // idndef SWIG 260 #endif // ifdef DOXYGEN 261 262 263 /** Set whether allocated memory is persistent 264 * 265 * Set whether allocated memory is persistent. The defeault is false. 266 * 267 * @return bool: The previous value of whether all allocated memory is 268 * persistent 269 */ 270 bool p_psMemAllocatePersistent(bool is_persistent); ///< Should all memory allocated be persistent? 271 272 273 /** Get the memory's persistent flag. 274 * 275 * Checks if a memory block has been marked as persistent by 276 * p_psMemSetPresistent. 277 * 278 * Memory marked as persistent is excluded from memory leak checks. 279 * 280 * @return bool true if memory is marked persistent, otherwise false. 281 */ 282 #ifdef DOXYGEN 283 bool psMemGetPersistent( 284 void *ptr, ///< the memory block to check. 285 ); 286 #else // ifdef DOXYGEN 287 bool p_psMemGetPersistent( 288 const char *file, ///< File of caller 289 unsigned int lineno, ///< Line number of caller 290 const char *func, ///< Function name of caller 291 void *ptr ///< the memory block to check. 292 ); 293 #ifndef SWIG 294 #define psMemGetPersistent(ptr) \ 295 p_psMemGetPersistent(__FILE__, __LINE__, __func__, ptr) 296 #endif // ifndef SWIG 297 #endif // ifdef DOXYGEN 298 299 300 /** Memory re-allocation. This operates much like realloc(), but is guaranteed 301 * to return a non-NULL value. 302 * 303 * @return void * pointer to resized buffer. This will not be NULL. 304 * @see psAlloc, psFree 305 */ 306 #ifdef DOXYGEN 307 void *psRealloc( 308 void *ptr, ///< Pointer to re-allocate 309 size_t size ///< Size required 310 ); 311 #else // ifdef DOXYGEN 312 void *p_psRealloc( 313 const char *file, ///< File of caller 314 unsigned int lineno, ///< Line number of caller 315 const char *func, ///< Function name of caller 316 void *ptr, ///< Pointer to re-allocate 317 size_t size ///< Size required 318 #ifdef __GNUC__ 319 ) __attribute__((malloc)); 320 # else // ifdef __GNUC__ 321 ); 322 #endif // ifdef __GNUC__ 323 #ifndef SWIG 324 #define psRealloc(ptr, size) \ 325 p_psRealloc(__FILE__, __LINE__, __func__, ptr, size) 326 #endif // ifndef SWIG 327 #endif // ifdef DOXYGEN 328 329 330 /** Free memory. This operates much like free(). 331 * 332 * @see psAlloc, psRealloc 333 */ 334 #ifdef DOXYGEN 335 void psFree( 336 void *ptr ///< Pointer to free, if NULL, function returns immediately. 337 ); 338 #else // ifdef DOXYGEN 339 #ifndef SWIG 340 #define psFree(ptr) \ 343 341 psMemDecrRefCounter(ptr) 344 #endif // !SWIG345 #endif // !DOXYGEN346 347 348 /** Check for memory leaks. This scans for allocated memory buffers not freed349 * with an ID not less than id0. This is used to check for memory leaks by: -#350 * before a block of code to be checked, store the current ID count via351 * psGetMemId -# after the block of code to be checked, call this function352 * using the ID stored above. If all memory in the block that was allocated353 * has been freed, this call should output nothing and return 0.354 *355 * If memory leaks are found, the Memory Problem callback will be called as356 * well.357 *358 * @return int number of memory blocks found as 'leaks', i.e., the number of359 * currently allocated memory blocks above id0 that have not been freed. @see360 * psAlloc, psFree, psgetMemId, psMemProblemCallbackSet361 */362 #ifdef DOXYGEN363 int psMemCheckLeaks(364 psMemId id0, ///< don't list blocks with id < id0365 psMemBlock ***array, ///< pointer to array of pointers to leaked blocks, or NULL366 FILE * fd, ///< print list of leaks to fd (or NULL)367 bool persistence ///< make check across all object even persistent ones368 );369 #else // ifdef DOXYGEN370 int p_psMemCheckLeaks(371 const char *file, ///< File of caller372 unsigned int lineno, ///< Line number of caller373 const char *func, ///< Function name of caller374 psMemId id0, ///< don't list blocks with id < id0375 psMemBlock ***array, ///< pointer to array of pointers to leaked blocks, or NULL376 FILE * fd, ///< print list of leaks to fd (or NULL)377 bool persistence ///< make check across all object even persistent ones378 );379 #ifndef SWIG380 #define psMemCheckLeaks(id0, array, fd, persistence) \381 p_psMemCheckLeaks(__FILE__, __LINE__, __func__, id0, array, fd, persistence)382 #endif // ifndef SWIG383 #endif // ifdef DOXYGEN384 385 386 /** Check for memory corruption. Scans all currently allocated memory buffers387 * and checks for corruptions, i.e., invalid markers that signify a buffer388 * under/overflow.389 *390 * @return int391 *392 */393 #ifdef DOXYGEN394 int psMemCheckCorruption(395 FILE *output, ///< FILE to write corrupted blocks too396 bool abort_on_error ///< Abort on detecting corruption?397 );398 #else // ifdef DOXYGEN399 int p_psMemCheckCorruption(400 const char *file, ///< File of caller401 unsigned int lineno, ///< Line number of caller402 const char *func, ///< Function name of caller403 FILE *output, ///< FILE to write corrupted blocks too404 bool abort_on_error ///< Abort on detecting corruption?405 );406 #ifndef SWIG407 #define psMemCheckCorruption(output, abort_on_error) \408 p_psMemCheckCorruption(__FILE__, __LINE__, __func__, output, abort_on_error)409 #endif // ifndef SWIG410 #endif // ifdef DOXYGEN411 412 413 /** Return reference counter414 *415 * @return psReferenceCount416 *417 */418 #ifdef DOXYGEN419 psReferenceCount psMemGetRefCounter(420 void *ptr ///< Pointer to get refCounter for421 );422 423 #else // ifdef DOXYGEN424 psReferenceCount p_psMemGetRefCounter(425 const char *file, ///< File of call426 unsigned int lineno, ///< Line number of call427 const char *func, ///< Function name of caller428 void *ptr ///< Pointer to get refCounter for429 );430 #ifndef SWIG431 #define psMemGetRefCounter(ptr) \432 p_psMemGetRefCounter(__FILE__, __LINE__, __func__, ptr)433 #endif // !SWIG434 #endif // !DOXYGEN435 436 437 /** Increment reference counter and return the pointer438 *439 * @return void *440 *441 */442 #ifdef DOXYGEN443 void *psMemIncrRefCounter(444 void *ptr ///< Pointer to increment refCounter, and return445 );446 #else // ifdef DOXYGEN447 void *p_psMemIncrRefCounter(448 const char *file, ///< File of call449 unsigned int lineno, ///< Line number of call450 const char *func, ///< Function name of caller451 void *ptr ///< Pointer to increment refCounter, and return452 );453 #ifndef SWIG454 #define psMemIncrRefCounter(ptr) \455 p_psMemIncrRefCounter(__FILE__, __LINE__, __func__, ptr)456 #endif // !SWIG457 #endif // !DOXYGEN458 459 460 /** Decrement reference counter and return the pointer461 *462 *463 * @return void * the pointer deremented in refCount, or NULL if pointer is464 * fully dereferenced.465 */466 #ifdef DOXYGEN467 void *psMemDecrRefCounter(468 void *ptr ///< Pointer to decrement refCounter, and return469 );470 #else // DOXYGEN471 void *p_psMemDecrRefCounter(472 const char *file, ///< File of call473 unsigned int lineno, ///< Line number of call474 const char *func, ///< Function name of caller475 void *ptr ///< Pointer to decrement refCounter, and return476 );477 #ifndef SWIG478 #define psMemDecrRefCounter(ptr) \479 p_psMemDecrRefCounter(__FILE__, __LINE__, __func__, ptr)480 #endif // !SWIG481 #endif // !DOXYGEN482 483 484 #if 0 // psMemSetRefCounter485 /** Set reference counter and return the pointer486 *487 * @return void * the pointer with refCount set, or NULL if pointer is488 * fully dereferenced.489 */490 #ifdef DOXYGEN491 void * psMemSetRefCounter(492 void * ptr, ///< Pointer to decrement refCounter, and return493 psReferenceCount count ///< New reference count494 );495 #else // DOXYGEN496 void * p_psMemSetRefCounter(497 void * vptr, ///< Pointer to decrement refCounter, and return498 psReferenceCount count, ///< New reference count499 const char *file, ///< File of call500 psS32 lineno ///< Line number of call501 );502 503 #ifndef SWIG504 #define psMemSetRefCounter(vptr, count) p_psMemSetRefCounter(vptr, count, __FILE__, __LINE__)505 #endif // !SWIG506 507 #endif // !DOXYGEN508 #endif // psMemSetRefCounter509 510 /** Set callback for out-of-memory.511 *512 * If not enough memory is available to satisfy a request by psAlloc or513 * psRealloc, these functions attempt to find an alternative solution by514 * calling the psMemExhaustedCallback, a function which may be set by the515 * programmer in appropriate circumstances, rather than immediately fail.516 * The typical use of such a feature may be when a program needs a large517 * chunk of memory to do an operation, but the exact size is not critical.518 * This feature gives the programmer the opportunity to make a smaller519 * request and try again, limiting the size of the operating buffer.520 *521 * @return psMemExhaustedCallback old psMemExhaustedCallback function522 */523 psMemExhaustedCallback psMemExhaustedCallbackSet(524 psMemExhaustedCallback func ///< Function to run at memory exhaustion525 );526 527 528 /** Set call back for when a particular memory block is allocated529 *530 * A private variable, p_psMemAllocID, can be used to trace the allocation531 * and freeing of specific memory blocks. If p_psMemAllocID is set and a532 * memory block with that ID is allocated, psMemAllocCallback is called533 * just before memory is returned to the calling function.534 *535 * @return psMemAllocCallback old psMemAllocCallback function536 */537 psMemAllocCallback psMemAllocCallbackSet(538 psMemAllocCallback func ///< Function to run at memory allocation of specific mem block539 );540 541 542 /** Set call back for when a particular memory block is freed543 *544 * A private variable, p_psMemFreeID, can be used to trace the freeing of545 * specific memory blocks. If p_psMemFreeID is set and the memory block with546 * the ID is about to be freed, the psMemFreeCallback callback is called just547 * before the memory block is freed.548 *549 * @return psMemFreeCallback old psMemFreeCallback function550 */551 psMemFreeCallback psMemFreeCallbackSet(552 psMemFreeCallback func ///< Function to run at memory free of specific mem block553 );554 555 556 /** get next memory ID557 *558 * @return psMemId the next memory ID to be used559 */560 psMemId psMemGetId(void);561 562 563 /** get the last memory ID used564 *565 * @return psMemId the last memory ID used566 */567 psMemId psMemGetLastId(void);568 569 570 /** set p_psMemAllocID to specific id571 *572 * A private variable, p_psMemAllocID, can be used to trace the allocation573 * and freeing of specific memory blocks. If p_psMemAllocID is set and a574 * memory block with that ID is allocated, psMemAllocCallback is called575 * just before memory is returned to the calling function.576 *577 * @return psMemId578 *579 * @see psMemAllocCallbackSet580 */581 psMemId psMemAllocCallbackSetID(582 psMemId id ///< ID to set583 );584 585 586 /** set p_psMemFreeID to id587 *588 * A private variable, p_psMemFreeID, can be used to trace the freeing of589 * specific memory blocks. If p_psMemFreeID is set and the memory block with590 * the ID is about to be freed, the psMemFreeCallback callback is called just591 * before the memory block is freed.592 *593 * @return psMemId the old p_psMemFreeID594 *595 * @see psMemFreeCallbackSet596 */597 psMemId psMemFreeCallbackSetID(598 psMemId id ///< ID to set599 );600 601 602 /** return statistics on memory usage603 *604 * @return the total amount of memory owned by psLib; if non-NULL also provide605 * a breakdown into allocated and allocated-and-persistent606 */607 size_t psMemStats(const bool print, ///< print details as they're found?608 size_t *allocated, ///< memory that's currently allocated (but not persistent)609 size_t *persistent); ///< persistent memory that's currently allocated610 611 /** print detailed information about a psMemBlock612 *613 * This function prints a detailed description of a psMemBlock to output.614 *615 * @return the return status of fprintf()616 */617 int psMemBlockPrint(618 FILE *output, ///< FILE to write information too619 const psMemBlock *memBlock ///< psMemBlock to be examined620 );621 622 623 /// @} end of SysUtils624 625 #ifndef DOXYGEN626 627 /*628 * Ensure that any program using malloc/realloc/free will fail to compile629 */630 #ifndef PS_ALLOW_MALLOC631 #ifdef __GNUC__632 #pragma GCC poison malloc realloc calloc free633 #else //__GNUC__634 #define malloc(S) _Pragma("error Use of malloc is not allowed. Use psAlloc instead.")635 #define realloc(P,S) _Pragma("error Use of realloc is not allowed. Use psRealloc instead.")636 #define calloc(S) _Pragma("error Use of calloc is not allowed. Use psAlloc instead.")637 #define free(P) _Pragma("error Use of free is not allowed. Use psFree instead.")638 #endif // !__GNUC__639 #endif // #ifndef PS_ALLOW_MALLOC640 641 #endif // #ifndef DOXYGEN642 #endif // #ifndef PS_MEMORY_H342 #endif // ifndef SWIG 343 #endif // ifdef DOXYGEN 344 345 346 /** Check for memory leaks. This scans for allocated memory buffers not freed 347 * with an ID not less than id0. This is used to check for memory leaks by: -# 348 * before a block of code to be checked, store the current ID count via 349 * psGetMemId -# after the block of code to be checked, call this function 350 * using the ID stored above. If all memory in the block that was allocated 351 * has been freed, this call should output nothing and return 0. 352 * 353 * If memory leaks are found, the Memory Problem callback will be called as 354 * well. 355 * 356 * @return int number of memory blocks found as 'leaks', i.e., the number of 357 * currently allocated memory blocks above id0 that have not been freed. @see 358 * psAlloc, psFree, psgetMemId, psMemProblemCallbackSet 359 */ 360 #ifdef DOXYGEN 361 int psMemCheckLeaks( 362 psMemId id0, ///< don't list blocks with id < id0 363 psMemBlock ***array, ///< pointer to array of pointers to leaked blocks, or NULL 364 FILE * fd, ///< print list of leaks to fd (or NULL) 365 bool persistence ///< make check across all object even persistent ones 366 ); 367 #else // ifdef DOXYGEN 368 int p_psMemCheckLeaks( 369 const char *file, ///< File of caller 370 unsigned int lineno, ///< Line number of caller 371 const char *func, ///< Function name of caller 372 psMemId id0, ///< don't list blocks with id < id0 373 psMemBlock ***array, ///< pointer to array of pointers to leaked blocks, or NULL 374 FILE * fd, ///< print list of leaks to fd (or NULL) 375 bool persistence ///< make check across all object even persistent ones 376 ); 377 #ifndef SWIG 378 #define psMemCheckLeaks(id0, array, fd, persistence) \ 379 p_psMemCheckLeaks(__FILE__, __LINE__, __func__, id0, array, fd, persistence) 380 #endif // ifndef SWIG 381 #endif // ifdef DOXYGEN 382 383 384 /** Check for memory corruption. Scans all currently allocated memory buffers 385 * and checks for corruptions, i.e., invalid markers that signify a buffer 386 * under/overflow. 387 * 388 * @return int 389 * 390 */ 391 #ifdef DOXYGEN 392 int psMemCheckCorruption( 393 FILE *output, ///< FILE to write corrupted blocks too 394 bool abort_on_error ///< Abort on detecting corruption? 395 ); 396 #else // ifdef DOXYGEN 397 int p_psMemCheckCorruption( 398 const char *file, ///< File of caller 399 unsigned int lineno, ///< Line number of caller 400 const char *func, ///< Function name of caller 401 FILE *output, ///< FILE to write corrupted blocks too 402 bool abort_on_error ///< Abort on detecting corruption? 403 ); 404 #ifndef SWIG 405 #define psMemCheckCorruption(output, abort_on_error) \ 406 p_psMemCheckCorruption(__FILE__, __LINE__, __func__, output, abort_on_error) 407 #endif // ifndef SWIG 408 #endif // ifdef DOXYGEN 409 410 411 /** Return reference counter 412 * 413 * @return psReferenceCount 414 * 415 */ 416 #ifdef DOXYGEN 417 psReferenceCount psMemGetRefCounter( 418 void *ptr ///< Pointer to get refCounter for 419 ); 420 421 #else // ifdef DOXYGEN 422 psReferenceCount p_psMemGetRefCounter( 423 const char *file, ///< File of call 424 unsigned int lineno, ///< Line number of call 425 const char *func, ///< Function name of caller 426 void *ptr ///< Pointer to get refCounter for 427 ); 428 #ifndef SWIG 429 #define psMemGetRefCounter(ptr) \ 430 p_psMemGetRefCounter(__FILE__, __LINE__, __func__, ptr) 431 #endif // !SWIG 432 #endif // !DOXYGEN 433 434 435 /** Increment reference counter and return the pointer 436 * 437 * @return void * 438 * 439 */ 440 #ifdef DOXYGEN 441 void *psMemIncrRefCounter( 442 void *ptr ///< Pointer to increment refCounter, and return 443 ); 444 #else // ifdef DOXYGEN 445 void *p_psMemIncrRefCounter( 446 const char *file, ///< File of call 447 unsigned int lineno, ///< Line number of call 448 const char *func, ///< Function name of caller 449 void *ptr ///< Pointer to increment refCounter, and return 450 ); 451 #ifndef SWIG 452 #define psMemIncrRefCounter(ptr) \ 453 p_psMemIncrRefCounter(__FILE__, __LINE__, __func__, ptr) 454 #endif // ifndef SWIG 455 #endif // ifdef DOXYGEN 456 457 458 /** Decrement reference counter and return the pointer 459 * 460 * 461 * @return void * the pointer deremented in refCount, or NULL if pointer is 462 * fully dereferenced. 463 */ 464 #ifdef DOXYGEN 465 void *psMemDecrRefCounter( 466 void *ptr ///< Pointer to decrement refCounter, and return 467 ); 468 #else // DOXYGEN 469 void *p_psMemDecrRefCounter( 470 const char *file, ///< File of call 471 unsigned int lineno, ///< Line number of call 472 const char *func, ///< Function name of caller 473 void *ptr ///< Pointer to decrement refCounter, and return 474 ); 475 #ifndef SWIG 476 #define psMemDecrRefCounter(ptr) \ 477 p_psMemDecrRefCounter(__FILE__, __LINE__, __func__, ptr) 478 #endif // ifndef SWIG 479 #endif // ifdef DOXYGEN 480 481 482 #if 0 // psMemSetRefCounter 483 /** Set reference counter and return the pointer 484 * 485 * @return void * the pointer with refCount set, or NULL if pointer is 486 * fully dereferenced. 487 */ 488 #ifdef DOXYGEN 489 void * psMemSetRefCounter( 490 void * ptr, ///< Pointer to decrement refCounter, and return 491 psReferenceCount count ///< New reference count 492 ); 493 #else // DOXYGEN 494 void * p_psMemSetRefCounter( 495 void * vptr, ///< Pointer to decrement refCounter, and return 496 psReferenceCount count, ///< New reference count 497 const char *file, ///< File of call 498 psS32 lineno ///< Line number of call 499 ); 500 501 #ifndef SWIG 502 #define psMemSetRefCounter(vptr, count) p_psMemSetRefCounter(vptr, count, __FILE__, __LINE__) 503 #endif // !SWIG 504 505 #endif // !DOXYGEN 506 #endif // psMemSetRefCounter 507 508 /** Set callback for out-of-memory. 509 * 510 * If not enough memory is available to satisfy a request by psAlloc or 511 * psRealloc, these functions attempt to find an alternative solution by 512 * calling the psMemExhaustedCallback, a function which may be set by the 513 * programmer in appropriate circumstances, rather than immediately fail. 514 * The typical use of such a feature may be when a program needs a large 515 * chunk of memory to do an operation, but the exact size is not critical. 516 * This feature gives the programmer the opportunity to make a smaller 517 * request and try again, limiting the size of the operating buffer. 518 * 519 * @return psMemExhaustedCallback old psMemExhaustedCallback function 520 */ 521 psMemExhaustedCallback psMemExhaustedCallbackSet( 522 psMemExhaustedCallback func ///< Function to run at memory exhaustion 523 ); 524 525 526 /** Set call back for when a particular memory block is allocated 527 * 528 * A private variable, p_psMemAllocID, can be used to trace the allocation 529 * and freeing of specific memory blocks. If p_psMemAllocID is set and a 530 * memory block with that ID is allocated, psMemAllocCallback is called 531 * just before memory is returned to the calling function. 532 * 533 * @return psMemAllocCallback old psMemAllocCallback function 534 */ 535 psMemAllocCallback psMemAllocCallbackSet( 536 psMemAllocCallback func ///< Function to run at memory allocation of specific mem block 537 ); 538 539 540 /** Set call back for when a particular memory block is freed 541 * 542 * A private variable, p_psMemFreeID, can be used to trace the freeing of 543 * specific memory blocks. If p_psMemFreeID is set and the memory block with 544 * the ID is about to be freed, the psMemFreeCallback callback is called just 545 * before the memory block is freed. 546 * 547 * @return psMemFreeCallback old psMemFreeCallback function 548 */ 549 psMemFreeCallback psMemFreeCallbackSet( 550 psMemFreeCallback func ///< Function to run at memory free of specific mem block 551 ); 552 553 554 /** get next memory ID 555 * 556 * @return psMemId the next memory ID to be used 557 */ 558 psMemId psMemGetId(void); 559 560 561 /** get the last memory ID used 562 * 563 * @return psMemId the last memory ID used 564 */ 565 psMemId psMemGetLastId(void); 566 567 568 /** set p_psMemAllocID to specific id 569 * 570 * A private variable, p_psMemAllocID, can be used to trace the allocation 571 * and freeing of specific memory blocks. If p_psMemAllocID is set and a 572 * memory block with that ID is allocated, psMemAllocCallback is called 573 * just before memory is returned to the calling function. 574 * 575 * @return psMemId 576 * 577 * @see psMemAllocCallbackSet 578 */ 579 psMemId psMemAllocCallbackSetID( 580 psMemId id ///< ID to set 581 ); 582 583 584 /** set p_psMemFreeID to id 585 * 586 * A private variable, p_psMemFreeID, can be used to trace the freeing of 587 * specific memory blocks. If p_psMemFreeID is set and the memory block with 588 * the ID is about to be freed, the psMemFreeCallback callback is called just 589 * before the memory block is freed. 590 * 591 * @return psMemId the old p_psMemFreeID 592 * 593 * @see psMemFreeCallbackSet 594 */ 595 psMemId psMemFreeCallbackSetID( 596 psMemId id ///< ID to set 597 ); 598 599 600 /** return statistics on memory usage 601 * 602 * @return the total amount of memory owned by psLib; if non-NULL also provide 603 * a breakdown into allocated and allocated-and-persistent 604 */ 605 size_t psMemStats(const bool print, ///< print details as they're found? 606 size_t *allocated, ///< memory that's currently allocated (but not persistent) 607 size_t *persistent); ///< persistent memory that's currently allocated 608 609 /** print detailed information about a psMemBlock 610 * 611 * This function prints a detailed description of a psMemBlock to output. 612 * 613 * @return the return status of fprintf() 614 */ 615 int psMemBlockPrint( 616 FILE *output, ///< FILE to write information too 617 const psMemBlock *memBlock ///< psMemBlock to be examined 618 ); 619 620 621 /// @} end of SysUtils 622 623 #ifndef DOXYGEN 624 625 /* 626 * Ensure that any program using malloc/realloc/free will fail to compile 627 */ 628 #ifndef PS_ALLOW_MALLOC 629 #ifdef __GNUC__ 630 #pragma GCC poison malloc realloc calloc free 631 #else // ifdef __GNUC__ 632 #define malloc(S) _Pragma("error Use of malloc is not allowed. Use psAlloc instead.") 633 #define realloc(P,S) _Pragma("error Use of realloc is not allowed. Use psRealloc instead.") 634 #define calloc(S) _Pragma("error Use of calloc is not allowed. Use psAlloc instead.") 635 #define free(P) _Pragma("error Use of free is not allowed. Use psFree instead.") 636 #endif // ifdef __GNUC__ 637 #endif // ifndef PS_ALLOW_MALLOC 638 639 #endif // #ifndef DOXYGEN 640 #endif // #ifndef PS_MEMORY_H
Note:
See TracChangeset
for help on using the changeset viewer.
