40 #define __FILE_ID__ "ymemory" 42 #define _CRT_SECURE_NO_DEPRECATE 43 #define YMEMORY_ALLOW_MALLOC 65 const char *malloc_file;
68 const char *free_file;
73 YMEM_ENTRY *yMap = NULL;
80 static void ymemdumpentry(YMEM_ENTRY *entry,
const char* prefix)
82 dbglog(
"%s %lX: alloc %s:%d of %db, free %s:%d\n",prefix,entry->ptr,entry->malloc_file,entry->malloc_line,entry->malloc_size,entry->free_file,entry->free_line);
86 static void ymemdump(
void)
92 dbglog(
"ySafeMemoryDump: %d/%d entry\n\n", yMapUsed, yMapSize);
95 for(i=0, entry=yMap; i< yMapUsed ; i++,entry++){
96 if(entry->state == YMEM_MALLOCED){
97 ymemdumpentry(entry,
"");
98 dbglog(
"%s : %d of %db (0x%x)\n", entry->malloc_file, entry->malloc_line, entry->malloc_size, entry->ptr);
99 total+= entry->malloc_size;
103 dbglog(
"total: %db (%d Kb) on %d entry\n\n",total,(
int)(total/1024),count);
106 for(i=0, entry=yMap; i< yMapUsed ; i++,entry++){
107 if(entry->state == YMEM_FREED){
108 dbglog(
"alloc %s:%d of %db, free %s:%d\n",entry->malloc_file,entry->malloc_line,entry->malloc_size,entry->free_file,entry->free_line);
122 yMap = malloc(nbentry *
sizeof(YMEM_ENTRY));
125 memset(yMap,0,nbentry *
sizeof(YMEM_ENTRY));
131 void *ySafeMalloc(
const char *file,u32 line,u32 size)
138 if(yMapUsed < yMapSize){
143 for(i=0; i< yMapSize;i++){
144 if(yMap[i].state == YMEM_FREED)
148 dbglog(
"No more entry available for ySafeMalloc\n\n");
158 dbglog(
"No more memory available (unable to allocate %d bytes)\n\n",size);
164 memset(entry,0,
sizeof(YMEM_ENTRY));
165 entry->state = YMEM_MALLOCED;
166 entry->malloc_file = file;
167 entry->malloc_line = line;
169 entry->malloc_size = size;
170 if(yMapUsed < yMapSize)
177 void ySafeFree(
const char *file,u32 line,
void *ptr)
183 for(i=0, entry=yMap; i< yMapUsed ; i++,entry++){
184 YASSERT(entry->state != YMEM_NOT_USED);
185 if(entry->ptr == ptr)
189 dbglog(
"Free of unallocated pointer 0x%x at %s:%d\n\n",ptr,file,line);
193 if(entry->state == YMEM_FREED){
194 dbglog(
"Free of allready freed pointer (0x%x) at %s:%d\n",ptr,file,line);
195 dbglog(
"was allocated at %s:%d size =%d freed at %s:%d\n\n",
196 entry->malloc_file, entry->malloc_line, entry->malloc_size, entry->free_file,entry->free_line);
201 entry->free_file = file;
202 entry->free_line = line;
203 entry->state = YMEM_FREED;
209 void ySafeTrace(
const char *file,u32 line,
void *ptr)
215 for(i=0, entry=yMap; i< yMapUsed ; i++,entry++){
216 YASSERT(entry->state != YMEM_NOT_USED);
217 if(entry->ptr == ptr)
221 dbglog(
"Update trace of unallocated pointer 0x%x at %s:%d\n\n",ptr,file,line);
225 if(entry->state == YMEM_FREED){
226 dbglog(
"Update trace of allready freed pointer (0x%x) at %s:%d\n",ptr,file,line);
227 dbglog(
"was allocated at %s:%d size =%d freed at %s:%d\n\n",
228 entry->malloc_file, entry->malloc_line, entry->malloc_size, entry->free_file,entry->free_line);
232 ymemdumpentry(entry,
"trace");
233 entry->malloc_file = file;
234 entry->malloc_line = line;
244 for(i=0, entry=yMap; i< yMapUsed ; i++,entry++){
245 if(entry->state == YMEM_MALLOCED && entry->ptr!=discard){
252 dbglog(
"No memory leak detected\n");
263 yMapSize = yMapUsed = 0;
270 static unsigned ystrnlen(
const char *src,
unsigned maxlen)
273 for (len=0 ; *src && len < maxlen ;len++,src++);
288 memcpy(tmp, src, len + 1);
295 return ystrncat_s(dst, dstsize, src, dstsize);
298 int ysprintf_s(
char *dst,
unsigned dstsize,
const char *fmt ,...)
302 va_start( args, fmt );
349 if(dstlen+1 > dstsize){
353 return ystrncpy_s(dst+dstlen, dstsize-dstlen, src, len);
357 int yvsprintf_s (
char *dst,
unsigned dstsize,
const char * fmt, va_list arg )
360 #if defined(_MSC_VER) && (_MSC_VER <= MSC_VS2003) 361 len = _vsnprintf(dst,dstsize,fmt,arg);
363 len = vsnprintf(dst,dstsize,fmt,arg);
365 if(len <0 || len >=(
long)dstsize){
373 int ymemfind(
const u8 *haystack, u32 haystack_len,
const u8 *needle, u32 needle_len)
379 while (needle_pos < needle_len && (abspos + needle_pos)<haystack_len && needle[needle_pos] == haystack[abspos + needle_pos]) {
382 if (needle_pos == needle_len) {
388 }
while (abspos + needle_len < haystack_len);
static unsigned ystrnlen(const char *src, unsigned maxlen)
#define ySafeMemoryStop()
void yInitializeCriticalSection(yCRITICAL_SECTION *cs)
YRETCODE ystrncat_s(char *dst, unsigned dstsize, const char *src, unsigned len)
int yvsprintf_s(char *dst, unsigned dstsize, const char *fmt, va_list arg)
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
char * ystrdup_s(const char *src)
int ysprintf_s(char *dst, unsigned dstsize, const char *fmt,...)
#define ySafeMemoryInit(nbentry)
YRETCODE ystrncpy_s(char *dst, unsigned dstsize, const char *src, unsigned arglen)
#define ySafeMemoryDump(discard)
void yEnterCriticalSection(yCRITICAL_SECTION *cs)
void yDeleteCriticalSection(yCRITICAL_SECTION *cs)
int ymemfind(const u8 *haystack, u32 haystack_len, const u8 *needle, u32 needle_len)
YRETCODE ystrcat_s(char *dst, unsigned dstsize, const char *src)
YRETCODE ystrcpy_s(char *dst, unsigned dstsize, const char *src)