MemoryDebug.cpp (pymol-open-source-2.2.0) | : | MemoryDebug.cpp (pymol-open-source-2.3.0) | ||
---|---|---|---|---|
skipping to change at line 114 | skipping to change at line 114 | |||
} | } | |||
if(vla->auto_zero) { | if(vla->auto_zero) { | |||
start = ((char *) vla) + soffset; | start = ((char *) vla) + soffset; | |||
stop = ((char *) vla) + sizeof(VLARec) + (vla->unit_size * vla->size); | stop = ((char *) vla) + sizeof(VLARec) + (vla->unit_size * vla->size); | |||
MemoryZero(start, stop); | MemoryZero(start, stop); | |||
} | } | |||
} | } | |||
return ((void *) &(vla[1])); | return ((void *) &(vla[1])); | |||
} | } | |||
#ifdef _MemoryCache_ON | ||||
void *VLACacheExpand(PyMOLGlobals * G, void *ptr, unsigned int rec, int thread_i | ||||
ndex, | ||||
int block_id) | ||||
{ | ||||
VLARec *vla; | ||||
char *start, *stop; | ||||
unsigned int soffset = 0; | ||||
vla = &(((VLARec *) ptr)[-1]); | ||||
if(rec >= vla->size) { | ||||
if(vla->auto_zero) | ||||
soffset = sizeof(VLARec) + (vla->unit_size * vla->size); | ||||
vla->size = ((unsigned int) (rec * vla->grow_factor)) + 1; | ||||
if(vla->size <= rec) | ||||
vla->size = rec + 1; | ||||
vla = (void *) _MemoryCacheRealloc(G, vla, | ||||
(vla->unit_size * vla->size) + sizeof(VLA | ||||
Rec), | ||||
thread_index, block_id MD_FILE_LINE_Call) | ||||
; | ||||
if(!vla) { | ||||
printf("VLAExpand-ERR: realloc failed.\n"); | ||||
DieOutOfMemory(); | ||||
} | ||||
if(vla->auto_zero) { | ||||
start = ((char *) vla) + soffset; | ||||
stop = ((char *) vla) + sizeof(VLARec) + (vla->unit_size * vla->size); | ||||
MemoryZero(start, stop); | ||||
} | ||||
} | ||||
return ((void *) &(vla[1])); | ||||
} | ||||
#endif | ||||
#ifndef _MemoryDebug_ON | ||||
void *VLAMalloc(ov_size init_size, ov_size unit_size, unsigned int grow_factor, | void *VLAMalloc(ov_size init_size, ov_size unit_size, unsigned int grow_factor, | |||
int auto_zero) | int auto_zero) | |||
#else | ||||
void *_VLAMalloc(const char *file, int line, ov_size init_size, | ||||
ov_size unit_size, unsigned int grow_factor, int auto_zero) | ||||
#endif | ||||
{ | { | |||
VLARec *vla; | VLARec *vla; | |||
char *start, *stop; | char *start, *stop; | |||
#ifndef _MemoryDebug_ON | ||||
vla = (VLARec*) mmalloc((init_size * unit_size) + sizeof(VLARec)); | vla = (VLARec*) mmalloc((init_size * unit_size) + sizeof(VLARec)); | |||
#else | ||||
vla = (VLARec*) | ||||
MemoryDebugMalloc((init_size * unit_size) + sizeof(VLARec), file, line, _MDP | ||||
ointer); | ||||
#endif | ||||
if(!vla) { | if(!vla) { | |||
printf("VLAMalloc-ERR: malloc failed\n"); | printf("VLAMalloc-ERR: malloc failed\n"); | |||
DieOutOfMemory(); | DieOutOfMemory(); | |||
} | } | |||
vla->size = init_size; | vla->size = init_size; | |||
vla->unit_size = unit_size; | vla->unit_size = unit_size; | |||
vla->grow_factor = (1.0F + grow_factor * 0.1F); | vla->grow_factor = (1.0F + grow_factor * 0.1F); | |||
vla->auto_zero = auto_zero; | vla->auto_zero = auto_zero; | |||
if(vla->auto_zero) { | if(vla->auto_zero) { | |||
start = ((char *) vla) + sizeof(VLARec); | start = ((char *) vla) + sizeof(VLARec); | |||
stop = ((char *) vla) + sizeof(VLARec) + (vla->unit_size * vla->size); | stop = ((char *) vla) + sizeof(VLARec) + (vla->unit_size * vla->size); | |||
MemoryZero(start, stop); | MemoryZero(start, stop); | |||
} | } | |||
return ((void *) &(vla[1])); | return ((void *) &(vla[1])); | |||
} | } | |||
#ifdef _MemoryCache_ON | ||||
#ifndef _MemoryDebug_ON | ||||
void *VLACacheMalloc(PyMOLGlobals * G, unsigned int init_size, unsigned int unit | ||||
_size, | ||||
unsigned int grow_factor, int auto_zero, int thread, int id | ||||
) | ||||
#else | ||||
void *_VLACacheMalloc(PyMOLGlobals * G, const char *file, int line, | ||||
unsigned int init_size, unsigned int unit_size, | ||||
unsigned int grow_factor, int auto_zero, int thread, int i | ||||
d) | ||||
#endif | ||||
{ | ||||
VLARec *vla; | ||||
char *start, *stop; | ||||
vla = (void *) _MemoryCacheMalloc(G, (init_size * unit_size) + sizeof(VLARec), | ||||
thread, id MD_FILE_LINE_Nest); | ||||
if(!vla) { | ||||
printf("VLAMalloc-ERR: realloc failed\n"); | ||||
DieOutOfMemory(); | ||||
} | ||||
vla->size = init_size; | ||||
vla->unit_size = unit_size; | ||||
vla->grow_factor = (1.0F + grow_factor * 0.1F); | ||||
vla->auto_zero = auto_zero; | ||||
if(vla->auto_zero) { | ||||
start = ((char *) vla) + sizeof(VLARec); | ||||
stop = ((char *) vla) + sizeof(VLARec) + (vla->unit_size * vla->size); | ||||
MemoryZero(start, stop); | ||||
} | ||||
return ((void *) &(vla[1])); | ||||
} | ||||
#endif | ||||
void VLAFree(void *ptr) | void VLAFree(void *ptr) | |||
{ | { | |||
VLARec *vla; | VLARec *vla; | |||
if(!ptr) { | if(!ptr) { | |||
printf("VLAFree-ERR: tried to free NULL pointer!\n"); | printf("VLAFree-ERR: tried to free NULL pointer!\n"); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
vla = &(((VLARec *) ptr)[-1]); | vla = &(((VLARec *) ptr)[-1]); | |||
mfree(vla); | mfree(vla); | |||
} | } | |||
#ifdef _MemoryCache_ON | ||||
void VLACacheFree(PyMOLGlobals * G, void *ptr, int thread, int id, int force) | ||||
{ | ||||
VLARec *vla; | ||||
if(!ptr) { | ||||
printf("VLAFree-ERR: tried to free NULL pointer!\n"); | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
vla = &(((VLARec *) ptr)[-1]); | ||||
_MemoryCacheFree(G, vla, thread, id, force MD_FILE_LINE_Call); | ||||
} | ||||
#endif | ||||
unsigned int VLAGetSize(const void *ptr) | unsigned int VLAGetSize(const void *ptr) | |||
{ | { | |||
const VLARec *vla; | const VLARec *vla; | |||
vla = &((VLARec *) ptr)[-1]; | vla = &((VLARec *) ptr)[-1]; | |||
return (vla->size); | return (vla->size); | |||
} | } | |||
void *VLANewCopy(const void *ptr) | void *VLANewCopy(const void *ptr) | |||
{ | { | |||
if(ptr) { /* NULL protected */ | if(ptr) { /* NULL protected */ | |||
skipping to change at line 264 | skipping to change at line 176 | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} else { | } else { | |||
memcpy(new_vla, vla, size); | memcpy(new_vla, vla, size); | |||
} | } | |||
return ((void *) &(new_vla[1])); | return ((void *) &(new_vla[1])); | |||
} else { | } else { | |||
return NULL; | return NULL; | |||
} | } | |||
} | } | |||
#ifdef _MemoryCache_ON | ||||
void *VLACacheSetSize(PyMOLGlobals * G, void *ptr, unsigned int new_size, int gr | ||||
oup_id, | ||||
int block_id) | ||||
{ | ||||
VLARec *vla; | ||||
char *start = NULL; | ||||
char *stop; | ||||
unsigned int soffset = 0; | ||||
vla = &((VLARec *) ptr)[-1]; | ||||
if(vla->auto_zero) { | ||||
soffset = sizeof(VLARec) + (vla->unit_size * vla->size); | ||||
} | ||||
vla->size = new_size; | ||||
vla = | ||||
(void *) _MemoryCacheRealloc(G, vla, (vla->unit_size * vla->size) + sizeof(V | ||||
LARec), | ||||
group_id, block_id MD_FILE_LINE_Call); | ||||
if(!vla) { | ||||
printf("VLASetSize-ERR: realloc failed.\n"); | ||||
DieOutOfMemory(); | ||||
} | ||||
if(vla->auto_zero) { | ||||
start = ((char *) vla) + soffset; | ||||
stop = ((char *) vla) + sizeof(VLARec) + (vla->unit_size * vla->size); | ||||
if(start < stop) | ||||
MemoryZero(start, stop); | ||||
} | ||||
return ((void *) &(vla[1])); | ||||
} | ||||
void *VLACacheSetSizeForSure(PyMOLGlobals * G, void *ptr, unsigned int new_size, | ||||
int group_id, int block_id) | ||||
{ | ||||
VLARec *vla; | ||||
char *start = NULL; | ||||
char *stop; | ||||
unsigned int soffset = 0; | ||||
vla = &((VLARec *) ptr)[-1]; | ||||
if(vla->auto_zero) { | ||||
soffset = sizeof(VLARec) + (vla->unit_size * vla->size); | ||||
} | ||||
if(new_size < vla->size) { | ||||
vla->size = new_size; | ||||
vla = | ||||
(void *) _MemoryCacheShrinkForSure(G, vla, | ||||
(vla->unit_size * vla->size) + sizeof(V | ||||
LARec), | ||||
group_id, block_id MD_FILE_LINE_Call); | ||||
} else { | ||||
vla->size = new_size; | ||||
vla = | ||||
(void *) _MemoryCacheRealloc(G, vla, (vla->unit_size * vla->size) + sizeof | ||||
(VLARec), | ||||
group_id, block_id MD_FILE_LINE_Call); | ||||
} | ||||
if(!vla) { | ||||
printf("VLASetSize-ERR: realloc failed.\n"); | ||||
DieOutOfMemory(); | ||||
} | ||||
if(vla->auto_zero) { | ||||
start = ((char *) vla) + soffset; | ||||
stop = ((char *) vla) + sizeof(VLARec) + (vla->unit_size * vla->size); | ||||
if(start < stop) | ||||
MemoryZero(start, stop); | ||||
} | ||||
return ((void *) &(vla[1])); | ||||
} | ||||
#endif | ||||
void *VLASetSize(void *ptr, unsigned int new_size) | void *VLASetSize(void *ptr, unsigned int new_size) | |||
{ | { | |||
VLARec *vla; | VLARec *vla; | |||
char *start = NULL; | char *start = NULL; | |||
char *stop; | char *stop; | |||
unsigned int soffset = 0; | unsigned int soffset = 0; | |||
vla = &((VLARec *) ptr)[-1]; | vla = &((VLARec *) ptr)[-1]; | |||
if(vla->auto_zero) { | if(vla->auto_zero) { | |||
soffset = sizeof(VLARec) + (vla->unit_size * vla->size); | soffset = sizeof(VLARec) + (vla->unit_size * vla->size); | |||
} | } | |||
skipping to change at line 455 | skipping to change at line 301 | |||
DieOutOfMemory(); | DieOutOfMemory(); | |||
} | } | |||
if(vla->auto_zero) { | if(vla->auto_zero) { | |||
start = ((char *) vla) + soffset; | start = ((char *) vla) + soffset; | |||
stop = ((char *) vla) + sizeof(VLARec) + (vla->unit_size * vla->size); | stop = ((char *) vla) + sizeof(VLARec) + (vla->unit_size * vla->size); | |||
if(start < stop) | if(start < stop) | |||
MemoryZero(start, stop); | MemoryZero(start, stop); | |||
} | } | |||
return ((void *) &(vla[1])); | return ((void *) &(vla[1])); | |||
} | } | |||
#ifdef _MemoryDebug_ON | ||||
#ifndef NULL | ||||
#define NULL (void *) | ||||
#endif | ||||
#ifndef false | ||||
#define false 0 | ||||
#endif | ||||
#ifndef true | ||||
#define true 1 | ||||
#endif | ||||
#define _NO_GDB_ENTRY | ||||
#ifdef __cplusplus | ||||
extern "C" { | ||||
#endif | ||||
typedef struct DebugRec { | ||||
struct DebugRec *next; | ||||
char file[32], note[64]; | ||||
int line; | ||||
size_t size; | ||||
int type; | ||||
} DebugRec; | ||||
#define HASH(x) ((x>>11)&0x3FF) | ||||
static DebugRec *HashTable[1024]; | ||||
static int InitFlag = true; | ||||
static int Count; | ||||
static int MaxCount; | ||||
DebugRec *MemoryDebugRemove(void *ptr); | ||||
void MemoryDebugInit(void); | ||||
void MemoryDebugInit(void) { | ||||
int a; | ||||
for(a = 0; a < 1024; a++) | ||||
HashTable[a] = NULL; | ||||
InitFlag = false; | ||||
Count = 0; | ||||
MaxCount = 0; | ||||
} | ||||
int MemoryDebugUsage(void) { | ||||
int a; | ||||
unsigned int tot = 0; | ||||
DebugRec *rec; | ||||
if(InitFlag) | ||||
MemoryDebugInit(); | ||||
for(a = 0; a < 1024; a++) { | ||||
rec = HashTable[a]; | ||||
while(rec) { | ||||
rec = rec->next; | ||||
} | ||||
} | ||||
return (tot); | ||||
} | ||||
void MemoryDebugDump(void) { | ||||
int a; | ||||
int cnt = 0; | ||||
unsigned int tot = 0; | ||||
DebugRec *rec; | ||||
if(InitFlag) | ||||
MemoryDebugInit(); | ||||
for(a = 0; a < 1024; a++) { | ||||
rec = HashTable[a]; | ||||
while(rec) { | ||||
tot += rec->size; | ||||
printf("Memory: %12p %12p %8x %3.1f %s:%i\n", | ||||
(void *) (rec + 1), | ||||
((char *) (rec + 1) + rec->size), (unsigned int) rec->size, | ||||
rec->size / 1048576.0F, rec->file, rec->line); | ||||
rec = rec->next; | ||||
cnt++; | ||||
} | ||||
} | ||||
printf("Memory: %d blocks expected, %d found, %d maximum allocated.\n", | ||||
Count, cnt, MaxCount); | ||||
printf("Memory: current memory allocated %x bytes (%0.1f MB).\n", tot, | ||||
tot / (1024.0 * 1024)); | ||||
} | ||||
void MemoryDebugHashAdd(DebugRec * rec); | ||||
DebugRec *MemoryDebugHashRemove(void *ptr); | ||||
void MemoryDebugHashAdd(DebugRec * rec) { | ||||
int hash; | ||||
hash = (size_t) rec; | ||||
hash = HASH(hash); | ||||
rec->next = HashTable[hash]; | ||||
HashTable[hash] = rec; | ||||
} | ||||
DebugRec *MemoryDebugHashRemove(void *ptr) { | ||||
DebugRec *rec, *cur, *last; | ||||
int hash; | ||||
rec = (DebugRec *) ptr; | ||||
rec--; | ||||
hash = (size_t) rec; | ||||
hash = HASH(hash); | ||||
last = NULL; | ||||
cur = HashTable[hash]; | ||||
while(cur) { | ||||
if(cur == rec) { | ||||
if(last) | ||||
last->next = cur->next; | ||||
else | ||||
HashTable[hash] = cur->next; | ||||
break; | ||||
} | ||||
last = cur; | ||||
cur = cur->next; | ||||
} | ||||
return (cur); | ||||
} | ||||
void *MemoryDebugMalloc(size_t size, const char *file, int line, int type) { | ||||
DebugRec *rec; | ||||
if(InitFlag) | ||||
MemoryDebugInit(); | ||||
rec = (DebugRec *) malloc(sizeof(DebugRec) + size); | ||||
if(!rec) { | ||||
if(!size) | ||||
return (NULL); | ||||
else { | ||||
printf("MemoryDebugMalloc-ERR: alloc failed.\n"); | ||||
DieOutOfMemory(); | ||||
} | ||||
} | ||||
strcpy(rec->file, file); | ||||
rec->line = line; | ||||
rec->size = size; | ||||
rec->type = type; | ||||
MemoryDebugHashAdd(rec); | ||||
rec++; | ||||
Count++; | ||||
if(MaxCount < Count) | ||||
MaxCount = Count; | ||||
return ((void *) rec); | ||||
} | ||||
void *MemoryDebugCalloc(size_t num, size_t size, const char *file, int line, i | ||||
nt type) { | ||||
DebugRec *rec; | ||||
if(InitFlag) | ||||
MemoryDebugInit(); | ||||
rec = (DebugRec *) calloc(1, sizeof(DebugRec) + size * num); | ||||
if(!rec) | ||||
return (NULL); | ||||
strcpy(rec->file, file); | ||||
rec->line = line; | ||||
rec->size = size; | ||||
rec->type = type; | ||||
MemoryDebugHashAdd(rec); | ||||
rec++; | ||||
Count++; | ||||
if(MaxCount < Count) | ||||
MaxCount = Count; | ||||
return ((void *) rec); | ||||
} | ||||
void *MemoryDebugRealloc(void *ptr, size_t size, const char *file, int line, i | ||||
nt type) { | ||||
DebugRec *rec; | ||||
if(InitFlag) | ||||
MemoryDebugInit(); | ||||
if((!ptr) && (!size)) { | ||||
printf("MemoryDebug-ERR: realloc given (NULL,zero) (%s:%i)\n", file, line) | ||||
; | ||||
#ifdef GDB_ENTRY | ||||
MemoryDebugDump(); | ||||
abort(); | ||||
#endif | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
if(!ptr) | ||||
return (MemoryDebugMalloc(size, file, line, type)); | ||||
else if(!size) { | ||||
MemoryDebugFree(ptr, file, line, type); | ||||
return (NULL); | ||||
} else { | ||||
rec = MemoryDebugHashRemove(ptr); | ||||
if(!rec) { | ||||
printf("MemoryDebug-ERR: realloc() corrupted tree or bad ptr! (%s:%i @%p | ||||
)\n", | ||||
file, line, ptr); | ||||
#ifdef GDB_ENTRY | ||||
MemoryDebugDump(); | ||||
abort(); | ||||
#endif | ||||
exit(EXIT_FAILURE); | ||||
} else { | ||||
if(rec->type != type) { | ||||
printf("MemoryDebug-ERR: ptr %p is of wrong type: %i!=%i (%s:%i)\n", | ||||
ptr, rec->type, type, file, line); | ||||
#ifdef GDB_ENTRY | ||||
MemoryDebugDump(); | ||||
abort(); | ||||
#endif | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
rec = (DebugRec *) realloc(rec, size + sizeof(DebugRec)); | ||||
if(!rec) { | ||||
printf("MemoryDebug-ERR: realloc() failed reallocation! (%s:%i)\n", fi | ||||
le, line); | ||||
#ifdef GDB_ENTRY | ||||
MemoryDebugDump(); | ||||
abort(); | ||||
#endif | ||||
DieOutOfMemory(); | ||||
} else { | ||||
MemoryDebugHashAdd(rec); | ||||
rec->size = size; | ||||
rec++; | ||||
return ((void *) rec); | ||||
} | ||||
} | ||||
} | ||||
return (ptr); | ||||
} | ||||
void *MemoryDebugReallocForSure(void *ptr, size_t size, const char *file, | ||||
int line, int type) { | ||||
DebugRec *rec, *new_rec; | ||||
if(InitFlag) | ||||
MemoryDebugInit(); | ||||
if((!ptr) && (!size)) { | ||||
printf("MemoryDebug-ERR: realloc given (NULL,zero) (%s:%i)\n", file, line) | ||||
; | ||||
#ifdef GDB_ENTRY | ||||
MemoryDebugDump(); | ||||
abort(); | ||||
#endif | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
if(!ptr) | ||||
return (MemoryDebugMalloc(size, file, line, type)); | ||||
else if(!size) { | ||||
MemoryDebugFree(ptr, file, line, type); | ||||
return (NULL); | ||||
} else { | ||||
rec = MemoryDebugHashRemove(ptr); | ||||
if(!rec) { | ||||
printf("MemoryDebug-ERR: realloc() corrupted tree or bad ptr! (%s:%i @%p | ||||
)\n", | ||||
file, line, ptr); | ||||
#ifdef GDB_ENTRY | ||||
MemoryDebugDump(); | ||||
abort(); | ||||
#endif | ||||
exit(EXIT_FAILURE); | ||||
} else { | ||||
if(rec->type != type) { | ||||
printf("MemoryDebug-ERR: ptr %p is of wrong type: %i!=%i (%s:%i)\n", | ||||
ptr, rec->type, type, file, line); | ||||
#ifdef GDB_ENTRY | ||||
MemoryDebugDump(); | ||||
abort(); | ||||
#endif | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
new_rec = (DebugRec*) malloc(size + sizeof(DebugRec)); | ||||
if(new_rec) | ||||
memcpy(new_rec, rec, size + sizeof(DebugRec)); | ||||
free(rec); | ||||
rec = new_rec; | ||||
if(!rec) { | ||||
printf("MemoryDebug-ERR: realloc() failed reallocation! (%s:%i)\n", fi | ||||
le, line); | ||||
#ifdef GDB_ENTRY | ||||
MemoryDebugDump(); | ||||
abort(); | ||||
#endif | ||||
DieOutOfMemory(); | ||||
} else { | ||||
MemoryDebugHashAdd(rec); | ||||
rec->size = size; | ||||
rec++; | ||||
return ((void *) rec); | ||||
} | ||||
} | ||||
} | ||||
return (ptr); | ||||
} | ||||
void *MemoryDebugReallocForSureSafe(void *ptr, size_t size, size_t old_size, | ||||
const char *file, int line, int type) { | ||||
DebugRec *rec, *new_rec; | ||||
if(InitFlag) | ||||
MemoryDebugInit(); | ||||
if((!ptr) && (!size)) { | ||||
printf("MemoryDebug-ERR: realloc given (NULL,zero) (%s:%i)\n", file, line) | ||||
; | ||||
#ifdef GDB_ENTRY | ||||
MemoryDebugDump(); | ||||
abort(); | ||||
#endif | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
if(!ptr) | ||||
return (MemoryDebugMalloc(size, file, line, type)); | ||||
else if(!size) { | ||||
MemoryDebugFree(ptr, file, line, type); | ||||
return (NULL); | ||||
} else { | ||||
rec = MemoryDebugHashRemove(ptr); | ||||
if(!rec) { | ||||
printf("MemoryDebug-ERR: realloc() corrupted tree or bad ptr! (%s:%i @%p | ||||
)\n", | ||||
file, line, ptr); | ||||
#ifdef GDB_ENTRY | ||||
MemoryDebugDump(); | ||||
abort(); | ||||
#endif | ||||
exit(EXIT_FAILURE); | ||||
} else { | ||||
if(rec->type != type) { | ||||
printf("MemoryDebug-ERR: ptr %p is of wrong type: %i!=%i (%s:%i)\n", | ||||
ptr, rec->type, type, file, line); | ||||
#ifdef GDB_ENTRY | ||||
MemoryDebugDump(); | ||||
abort(); | ||||
#endif | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
if(old_size > size) { | ||||
new_rec = (DebugRec*) malloc(size + sizeof(DebugRec)); | ||||
if(new_rec) | ||||
memcpy(new_rec, rec, size + sizeof(DebugRec)); | ||||
free(rec); | ||||
rec = new_rec; | ||||
} else { | ||||
rec = (DebugRec*) realloc(rec, size + sizeof(DebugRec)); | ||||
} | ||||
if(!rec) { | ||||
printf("MemoryDebug-ERR: realloc() failed reallocation! (%s:%i)\n", fi | ||||
le, line); | ||||
#ifdef GDB_ENTRY | ||||
MemoryDebugDump(); | ||||
abort(); | ||||
#endif | ||||
DieOutOfMemory(); | ||||
} else { | ||||
MemoryDebugHashAdd(rec); | ||||
rec->size = size; | ||||
rec++; | ||||
return ((void *) rec); | ||||
} | ||||
} | ||||
} | ||||
return (ptr); | ||||
} | ||||
void MemoryDebugQuietFree(void *ptr, int type) { | ||||
DebugRec *rec; | ||||
if(InitFlag) | ||||
MemoryDebugInit(); | ||||
if(!ptr) { | ||||
printf("MemoryDebug-ERR: MemoryDebugQuietFree() given NULL pointer\n"); | ||||
} | ||||
rec = MemoryDebugHashRemove(ptr); | ||||
if(rec) { | ||||
if(rec->type != type) { | ||||
printf("MemoryDebug-ERR: ptr %p is of wrong type: %i!=%i (allocated %s:% | ||||
i)\n", | ||||
ptr, rec->type, type, rec->file, rec->line); | ||||
#ifdef GDB_ENTRY | ||||
MemoryDebugDump(); | ||||
abort(); | ||||
#endif | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
free(rec); | ||||
Count--; | ||||
} else | ||||
free(ptr); | ||||
} | ||||
void MemoryDebugFree(void *ptr, const char *file, int line, int type) { | ||||
DebugRec *rec; | ||||
if(!ptr) | ||||
return; | ||||
if(InitFlag) | ||||
MemoryDebugInit(); | ||||
rec = MemoryDebugHashRemove(ptr); | ||||
if(rec) { | ||||
if(rec->type != type) { | ||||
printf("MemoryDebug-ERR: ptr %p is of wrong type: %i!=%i (%s:%i)\n", | ||||
ptr, rec->type, type, file, line); | ||||
#ifdef GDB_ENTRY | ||||
MemoryDebugDump(); | ||||
abort(); | ||||
#endif | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
free(rec); | ||||
} else { | ||||
printf("MemoryDebug-ERR: free(): corrupted tree or bad ptr! (%s:%i @%p)\n" | ||||
, | ||||
file, line, ptr); | ||||
#ifdef GDB_ENTRY | ||||
MemoryDebugDump(); | ||||
abort(); | ||||
#endif | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
Count--; | ||||
} | ||||
// strdup | ||||
char *MemoryDebugStrDup(const char *s, const char *file, int line, int type) { | ||||
size_t n = strlen(s) + 1; | ||||
void *ptr = MemoryDebugMalloc(n, file, line, type); | ||||
return (char*)memcpy(ptr, s, n); | ||||
} | ||||
#ifdef __cplusplus | ||||
} | ||||
#endif | ||||
#endif | ||||
End of changes. 8 change blocks. | ||||
165 lines changed or deleted | 0 lines changed or added |