Changeset 648 for trunk/psLib/src/sysUtils/psMemory.c
- Timestamp:
- May 12, 2004, 9:05:02 AM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/sysUtils/psMemory.c (modified) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sysUtils/psMemory.c
r639 r648 8 8 * @author Robert Lupton, Princeton University 9 9 * 10 * @version $Revision: 1.1 7$ $Name: not supported by cvs2svn $11 * @date $Date: 2004-05-1 1 20:11:17$10 * @version $Revision: 1.18 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2004-05-12 19:05:02 $ 12 12 * 13 13 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 20 20 #include <stdio.h> 21 21 #include <stdbool.h> 22 #include <stdint.h> 22 23 23 24 #include "psMemory.h" … … 34 35 * Unique ID for allocated blocks 35 36 */ 36 static unsigned longmemid = 0;37 static psMemoryId memid = 0; 37 38 38 39 /** … … 62 63 const char *file, int lineno) 63 64 { 64 if (ptr->refCounter < = 0) {65 if (ptr->refCounter < 1) { 65 66 psError(__func__, 66 67 "Block %ld allocated at %s:%d freed more than once at %s:%d\n", … … 95 96 * Call the callbacks when these IDs are allocated/freed 96 97 */ 97 longp_psMemAllocateID = 0; // notify user this block is allocated98 longp_psMemFreeID = 0; // notify user this block is freed99 100 long psMemAllocateCallbackSetID(longid) // set p_psMemAllocateID to id101 { 102 longold = p_psMemAllocateID;98 psMemoryId p_psMemAllocateID = 0; // notify user this block is allocated 99 psMemoryId p_psMemFreeID = 0; // notify user this block is freed 100 101 psMemoryId psMemAllocateCallbackSetID(psMemoryId id) // set p_psMemAllocateID to id 102 { 103 psMemoryId old = p_psMemAllocateID; 103 104 p_psMemAllocateID = id; 104 105 … … 106 107 } 107 108 108 long psMemFreeCallbackSetID(longid) // set p_psMemFreeID to id109 { 110 longold = p_psMemFreeID;109 psMemoryId psMemFreeCallbackSetID(psMemoryId id) // set p_psMemFreeID to id 110 { 111 psMemoryId old = p_psMemFreeID; 111 112 p_psMemFreeID = id; 112 113 … … 120 121 * isn't resignalled) 121 122 */ 122 static longmemAllocateCallbackDefault(const psMemBlock *ptr)123 { 124 static intincr = 0; // "p_psMemAllocateID += incr"123 static psMemoryId memAllocateCallbackDefault(const psMemBlock *ptr) 124 { 125 static psMemoryId incr = 0; // "p_psMemAllocateID += incr" 125 126 126 127 return incr; 127 128 } 128 129 129 static longmemFreeCallbackDefault(const psMemBlock *ptr)130 { 131 static intincr = 0; // "p_psMemFreeID += incr"130 static psMemoryId memFreeCallbackDefault(const psMemBlock *ptr) 131 { 132 static psMemoryId incr = 0; // "p_psMemFreeID += incr" 132 133 133 134 return incr; … … 169 170 * Return memory ID counter for next block to be allocated 170 171 */ 171 intpsMemGetId(void)172 psMemoryId psMemGetId(void) 172 173 { 173 174 return memid + 1; … … 199 200 200 201 if (m->startblock != P_PS_MEMMAGIC || m->endblock != P_PS_MEMMAGIC) { 201 psError(funcName, "psMemCheckCorruption: memory block %ld is corrupted ( %p %p)",202 psError(funcName, "psMemCheckCorruption: memory block %ld is corrupted (buffer underflow %p %p)", 202 203 m->id,m->startblock,m->endblock); 204 return(1); 205 } 206 if (*(void**)((int8_t*)(m+1)+m->userMemorySize) != P_PS_MEMMAGIC) { 207 psError(funcName, "psMemCheckCorruption: memory block %ld is corrupted (buffer overflow, %p)", 208 m->id,*(void**)((int8_t*)(m+1)+m->userMemorySize)); 203 209 return(1); 204 210 } … … 236 242 void *p_psAlloc(size_t size, const char *file, int lineno) 237 243 { 238 psMemBlock *ptr = malloc(sizeof(psMemBlock) + size );244 psMemBlock *ptr = malloc(sizeof(psMemBlock) + size + sizeof(void*)); 239 245 240 246 if (ptr == NULL) { … … 246 252 } 247 253 248 *( unsigned long*)&ptr->id = ++memid;254 *(psMemoryId*)&ptr->id = ++memid; 249 255 ptr->file = file; 250 256 *(unsigned int*)&ptr->lineno = lineno; … … 252 258 ptr->endblock = P_PS_MEMMAGIC; 253 259 ptr->refCounter = 1; // one user so far 260 ptr->userMemorySize = size; 261 *(void**)((int8_t*)(ptr+1)+size) = P_PS_MEMMAGIC; 254 262 255 263 // need exclusive access of the memory block list now... … … 287 295 isBlockLast = (ptr == lastMemBlockAllocated); 288 296 289 ptr = (psMemBlock*)realloc(ptr, sizeof(psMemBlock) + size );297 ptr = (psMemBlock*)realloc(ptr, sizeof(psMemBlock) + size + sizeof(void*)); 290 298 291 299 if (ptr == NULL) { … … 293 301 size, file, lineno); 294 302 } 303 304 ptr->userMemorySize = size; 305 *(void**)((int8_t*)(ptr+1)+size) = P_PS_MEMMAGIC; 295 306 296 307 if (isBlockLast) { … … 363 374 */ 364 375 int psMemCheckLeaks( 365 int id0,// don't list blocks with id < id0376 psMemoryId id0, // don't list blocks with id < id0 366 377 psMemBlock ***arr, // pointer to array of pointers to leaked blocks, or NULL 367 378 FILE *fd) // print list of leaks to fd (or NULL) … … 416 427 * Reference counting APIs 417 428 */ 418 int psMemGetRefCounter(void *vptr) // return refCounter429 psReferenceCount psMemGetRefCounter(void *vptr) // return refCounter 419 430 { 420 431 psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
Note:
See TracChangeset
for help on using the changeset viewer.
