MemoryDebug.h (pymol-open-source-2.2.0) | : | MemoryDebug.h (pymol-open-source-2.3.0) | ||
---|---|---|---|---|
skipping to change at line 20 | skipping to change at line 20 | |||
H* ------------------------------------------------------------------- | H* ------------------------------------------------------------------- | |||
I* Additional authors of this source file include: | I* Additional authors of this source file include: | |||
-* | -* | |||
-* | -* | |||
-* | -* | |||
Z* ------------------------------------------------------------------- | Z* ------------------------------------------------------------------- | |||
*/ | */ | |||
#ifndef _H_MemoryDebug | #ifndef _H_MemoryDebug | |||
#define _H_MemoryDebug | #define _H_MemoryDebug | |||
#include <vector> | ||||
#include "os_std.h" | #include "os_std.h" | |||
#include "PyMOLGlobals.h" | #include "PyMOLGlobals.h" | |||
/* This file can be included by C and C++ programs for | ||||
debugging of malloc, realloc, free in C and in addition, | ||||
of new and delete in C++ | ||||
it also includes a two functions for variable length arrays | ||||
In order to use the debugging system, you must do the following... | ||||
* use mmalloc for malloc | ||||
* use mcalloc for calloc | ||||
* use mrealloc for mrealloc | ||||
* use mfree for free | ||||
*/ | ||||
/* ==================== Master Switch ============================= | ||||
* Define _MemoryDebug_ON to enable the debugging system...*/ | ||||
/* WARNING!!! MemoryDebug is not thread safe...it must be disabled | ||||
for stable multi-threaded operation within the PyMOL core */ | ||||
#define _MemoryDebug_OFF | ||||
/* ================================================================ | /* ================================================================ | |||
* Don't touch below unless you know what you are doing */ | * Don't touch below unless you know what you are doing */ | |||
void UtilMemCpy(void *dst, void *src, unsigned int *size); | ||||
typedef struct VLARec { | typedef struct VLARec { | |||
ov_size size, unit_size; | ov_size size, unit_size; | |||
float grow_factor; | float grow_factor; | |||
int auto_zero; | int auto_zero; | |||
} VLARec; | } VLARec; | |||
/* NOTE: in VLACheck, rec is a zero based array index, not a record count */ | /* NOTE: in VLACheck, rec is a zero based array index, not a record count */ | |||
#define VLACheck(ptr,type,rec) (ptr=(type*)(((((ov_size)rec)>=((VLARec*)(ptr))[- 1].size) ? VLAExpand(ptr,((ov_size)rec)) : (ptr)))) | #define VLACheck(ptr,type,rec) VLACheck2<type>(ptr, rec) | |||
#define VLAlloc(type,init_size) (type*)VLAMalloc(init_size,sizeof(type),5,0) | #define VLAlloc(type,init_size) (type*)VLAMalloc(init_size,sizeof(type),5,0) | |||
#define VLACalloc(type,init_size) (type*)VLAMalloc(init_size,sizeof(type),5,1) | #define VLACalloc(type,init_size) (type*)VLAMalloc(init_size,sizeof(type),5,1) | |||
#define VLASize(ptr,type,size) {ptr=(type*)VLASetSize(ptr,size);} | #define VLASize(ptr,type,size) VLASize2<type>(ptr,size) | |||
#define VLASizeForSure(ptr,type,size) {ptr=(type*)VLASetSizeForSure(ptr,size);} | #define VLASizeForSure(ptr,type,size) VLASizeForSure2<type>(ptr,size) | |||
#define VLAFreeP(ptr) {if(ptr) {VLAFree(ptr);ptr=NULL;}} | ||||
#define VLACopy(ptr,type) (type*)VLANewCopy(ptr); | #define VLACopy(ptr,type) (type*)VLANewCopy(ptr); | |||
#define VLAInsert(ptr,type,index,count) {ptr=(type*)VLAInsertRaw(ptr,index,count );} | #define VLAInsert(ptr,type,index,count) {ptr=(type*)VLAInsertRaw(ptr,index,count );} | |||
#define VLADelete(ptr,type,index,count) {ptr=(type*)VLADeleteRaw(ptr,index,count );} | #define VLADelete(ptr,type,index,count) {ptr=(type*)VLADeleteRaw(ptr,index,count );} | |||
#define Alloc(type,size) (type*)mmalloc(sizeof(type)*(size)) | #define Alloc(type,size) (type*)mmalloc(sizeof(type)*(size)) | |||
#define Calloc(type,size) (type*)mcalloc(sizeof(type),size) | #define Calloc(type,size) (type*)mcalloc(sizeof(type),size) | |||
#define Realloc(ptr,type,size) (type*)mrealloc(ptr,sizeof(type)*(size)) | #define Realloc(ptr,type,size) (type*)mrealloc(ptr,sizeof(type)*(size)) | |||
#define FreeP(ptr) {if(ptr) {mfree(ptr);ptr=NULL;}} | #define FreeP(ptr) {if(ptr) {mfree(ptr);ptr=NULL;}} | |||
#define DeleteP(ptr) {if(ptr) {delete ptr;ptr=NULL;}} | #define DeleteP(ptr) {if(ptr) {delete ptr;ptr=NULL;}} | |||
#define DeleteAP(ptr) {if(ptr) {delete[] ptr;ptr=NULL;}} | #define DeleteAP(ptr) {if(ptr) {delete[] ptr;ptr=NULL;}} | |||
void *VLAExpand(void *ptr, ov_size rec); /* NOTE: rec is index (total-1) */ | void *VLAExpand(void *ptr, ov_size rec); /* NOTE: rec is index (total-1) */ | |||
void *MemoryReallocForSure(void *ptr, unsigned int newSize); | void *MemoryReallocForSure(void *ptr, unsigned int newSize); | |||
void *MemoryReallocForSureSafe(void *ptr, unsigned int newSize, unsigned int old Size); | void *MemoryReallocForSureSafe(void *ptr, unsigned int newSize, unsigned int old Size); | |||
void *VLADeleteRaw(void *ptr, int index, unsigned int count); | void *VLADeleteRaw(void *ptr, int index, unsigned int count); | |||
void *VLAInsertRaw(void *ptr, int index, unsigned int count); | void *VLAInsertRaw(void *ptr, int index, unsigned int count); | |||
#ifndef _MemoryDebug_ON | ||||
void *VLAMalloc(ov_size init_size, ov_size unit_size, unsigned int grow_factor, int auto_zero); /*growfactor 1-10 */ | void *VLAMalloc(ov_size init_size, ov_size unit_size, unsigned int grow_factor, int auto_zero); /*growfactor 1-10 */ | |||
#else | ||||
#define VLAMalloc(a,b,c,d) _VLAMalloc(__FILE__,__LINE__,a,b,c,d) | ||||
void *_VLAMalloc(const char *file, int line, ov_size init_size, ov_size unit_siz | ||||
e, unsigned int grow_factor, int auto_zero); /*growfactor 1-10 */ | ||||
#endif | ||||
void VLAFree(void *ptr); | void VLAFree(void *ptr); | |||
void *VLASetSize(void *ptr, unsigned int newSize); | void *VLASetSize(void *ptr, unsigned int newSize); | |||
void *VLASetSizeForSure(void *ptr, unsigned int newSize); | void *VLASetSizeForSure(void *ptr, unsigned int newSize); | |||
unsigned int VLAGetSize(const void *ptr); | unsigned int VLAGetSize(const void *ptr); | |||
void *VLANewCopy(const void *ptr); | void *VLANewCopy(const void *ptr); | |||
void MemoryZero(char *p, char *q); | void MemoryZero(char *p, char *q); | |||
#ifndef _MemoryDebug_ON | ||||
/* _MemoryDebug_ON not defined */ | ||||
#define mcalloc calloc | #define mcalloc calloc | |||
#define mmalloc malloc | #define mmalloc malloc | |||
#define mrealloc realloc | #define mrealloc realloc | |||
#define mfree free | #define mfree free | |||
#define mstrdup strdup | #define mstrdup strdup | |||
#define ReallocForSure(ptr,type,size) (type*)MemoryReallocForSure(ptr,sizeof(typ e)*(size)) | #define ReallocForSure(ptr,type,size) (type*)MemoryReallocForSure(ptr,sizeof(typ e)*(size)) | |||
#define ReallocForSureSafe(ptr,type,size,old_size) (type*)MemoryReallocForSure(p tr,sizeof(type)*(size),sizeof(type)*(old_size)) | #define ReallocForSureSafe(ptr,type,size,old_size) (type*)MemoryReallocForSure(p tr,sizeof(type)*(size),sizeof(type)*(old_size)) | |||
#define MemoryDebugDump() | ||||
#ifdef __cplusplus | #ifdef __cplusplus | |||
#define mnew new | #define mnew new | |||
#endif | #endif | |||
#define MD_FILE_LINE_Call | #define MD_FILE_LINE_Call | |||
#define MD_FILE_LINE_Decl | #define MD_FILE_LINE_Decl | |||
#define MD_FILE_LINE_Nest | #define MD_FILE_LINE_Nest | |||
#define MD_FILE_LINE_PTR_Call | #define MD_FILE_LINE_PTR_Call | |||
#else | ||||
/* _MemoryDebug_ON is defined */ | ||||
#ifdef __cplusplus | ||||
extern "C" { | ||||
#endif | ||||
#define _MDPointer 1 | ||||
#define MD_FILE_LINE_Call ,__FILE__,__LINE__ | ||||
#define MD_FILE_LINE_Decl ,const char *file,int line | ||||
#define MD_FILE_LINE_Nest ,file,line | ||||
#define MD_FILE_LINE_PTR_Call ,__FILE__,__LINE__,_MDPointer | ||||
#define mmalloc(x) MemoryDebugMalloc(x,__FILE__,__LINE__,_MDPointer) | ||||
#define mcalloc(x,y) MemoryDebugCalloc(x,y,__FILE__,__LINE__,_MDPointer) | ||||
#define mrealloc(x,y) MemoryDebugRealloc(x,y,__FILE__,__LINE__,_MDPointer) | ||||
#define mfree(x) MemoryDebugFree(x,__FILE__,__LINE__,_MDPointer) | ||||
#define mstrdup(x) MemoryDebugStrDup(x,__FILE__,__LINE__,_MDPointer) | ||||
#define ReallocForSure(ptr,type,size) (type*)MemoryDebugReallocForSure(ptr,sizeo | ||||
f(type)*(size),__FILE__,__LINE__,_MDPointer) | ||||
#define ReallocForSureSafe(ptr,type,size,old_size) (type*)MemoryDebugReallocForS | ||||
ureSafe(ptr,sizeof(type)*(size),\ | ||||
sizeof(type)*(old_size),__FILE__,__LINE__,_MDPointer) | ||||
void *MemoryDebugMalloc(size_t size, const char *file, int line, int type); | ||||
void *MemoryDebugCalloc(size_t nmemb, size_t size, const char *file, int line, | ||||
int type); | ||||
void *MemoryDebugRealloc(void *ptr, size_t size, const char *file, int line, i | ||||
nt type); | ||||
void *MemoryDebugReallocForSure(void *ptr, size_t size, const char *file, | ||||
int line, int type); | ||||
void *MemoryDebugReallocForSureSafe(void *ptr, size_t size, size_t old_size, | ||||
const char *file, int line, int type); | ||||
void MemoryDebugFree(void *ptr, const char *file, int line, int type); | ||||
void MemoryDebugQuietFree(void *ptr, int type); | ||||
char *MemoryDebugStrDup(const char *s, const char *file, int line, int type); | ||||
void MemoryDebugDump(void); | ||||
int MemoryDebugUsage(void); | ||||
#ifdef __cplusplus | ||||
} void *operator new(size_t size, const char *file, int line); | ||||
#define mnew new(__FILE__,__LINE__) | ||||
#endif | ||||
#endif | ||||
inline unsigned int VLAGetByteSize(const void *ptr) { | inline unsigned int VLAGetByteSize(const void *ptr) { | |||
const VLARec *vla = ((const VLARec *) ptr) - 1; | const VLARec *vla = ((const VLARec *) ptr) - 1; | |||
return vla->size * vla->unit_size; | return vla->size * vla->unit_size; | |||
} | } | |||
/* | /* | |||
* Templated version of the `VLACopy` macro | * Templated version of the `VLACopy` macro | |||
*/ | */ | |||
template <typename T> | template <typename T> | |||
T * VLACopy2(const T * vla) { | T * VLACopy2(const T * vla) { | |||
return VLACopy((void*)vla, T); | return VLACopy((void*)vla, T); | |||
} | } | |||
/* | ||||
* @brief std::vector version of VLACheck. Checks to see if index i is valid for | ||||
insertion. | ||||
* If not, a resize will be attempted. | ||||
* @param vec: vector whose size will be check for valid insertion at index i | ||||
* @param i: index for position where an element may be inserted into vec | ||||
* Note: Use of this function should be limited. Used for safe replacement of VL | ||||
ACheck | ||||
* Note: This function can throw. | ||||
*/ | ||||
template <typename T> | ||||
void VecCheck(std::vector<T> &vec, std::size_t i){ | ||||
if(i >= vec.size()){ | ||||
vec.resize(i + 1); | ||||
} | ||||
} | ||||
template <typename T> | ||||
T* VLACheck2(T*& ptr, size_t pos) { | ||||
if (pos >= ((VLARec*) ptr)[-1].size) { | ||||
ptr = static_cast<T*>(VLAExpand(ptr, pos)); | ||||
} | ||||
return ptr; | ||||
} | ||||
template <typename T> | ||||
void VLASize2(T*& ptr, size_t size) { | ||||
ptr = static_cast<T*>(VLASetSize(ptr, size)); | ||||
} | ||||
template <typename T> | ||||
void VLASizeForSure2(T*& ptr, size_t size) { | ||||
ptr = static_cast<T*>(VLASetSizeForSure(ptr, size)); | ||||
} | ||||
template <typename T> | ||||
void VLAFreeP(T*& ptr) { | ||||
if (ptr) { | ||||
VLAFree(ptr); | ||||
ptr = nullptr; | ||||
} | ||||
} | ||||
#endif | #endif | |||
// vi:sw=2:expandtab | ||||
End of changes. 12 change blocks. | ||||
97 lines changed or deleted | 49 lines changed or added |