mirror of
https://github.com/moparisthebest/curl
synced 2025-03-11 07:39:50 -04:00
more fancy alloc, we store the size in each allocated block so that we
can destroy the full allocated area just before we free it
This commit is contained in:
parent
f1103b95cf
commit
cb85ca18ab
@ -74,20 +74,23 @@ void curl_memdebug(const char *logname)
|
|||||||
|
|
||||||
void *curl_domalloc(size_t wantedsize, int line, const char *source)
|
void *curl_domalloc(size_t wantedsize, int line, const char *source)
|
||||||
{
|
{
|
||||||
void *mem;
|
struct memdebug *mem;
|
||||||
size_t size;
|
size_t size;
|
||||||
|
|
||||||
/* alloc at least 64 bytes */
|
/* alloc at least 64 bytes */
|
||||||
size = wantedsize>64?wantedsize:64;
|
size = sizeof(struct memdebug)+wantedsize;
|
||||||
|
|
||||||
mem=(malloc)(size);
|
mem=(struct memdebug *)(malloc)(size);
|
||||||
if(mem)
|
if(mem) {
|
||||||
/* fill memory with junk */
|
/* fill memory with junk */
|
||||||
memset(mem, 0xA5, size);
|
memset(mem->mem, 0xA5, wantedsize);
|
||||||
|
mem->size = wantedsize;
|
||||||
|
}
|
||||||
|
|
||||||
if(logfile && source)
|
if(logfile && source)
|
||||||
fprintf(logfile, "MEM %s:%d malloc(%d) = %p\n",
|
fprintf(logfile, "MEM %s:%d malloc(%d) = %p\n",
|
||||||
source, line, wantedsize, mem);
|
source, line, wantedsize, mem->mem);
|
||||||
return mem;
|
return mem->mem;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *curl_dostrdup(const char *str, int line, const char *source)
|
char *curl_dostrdup(const char *str, int line, const char *source)
|
||||||
@ -109,35 +112,48 @@ char *curl_dostrdup(const char *str, int line, const char *source)
|
|||||||
if(logfile)
|
if(logfile)
|
||||||
fprintf(logfile, "MEM %s:%d strdup(%p) (%d) = %p\n",
|
fprintf(logfile, "MEM %s:%d strdup(%p) (%d) = %p\n",
|
||||||
source, line, str, len, mem);
|
source, line, str, len, mem);
|
||||||
|
|
||||||
return mem;
|
return mem;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *curl_dorealloc(void *ptr, size_t wantedsize,
|
void *curl_dorealloc(void *ptr, size_t wantedsize,
|
||||||
int line, const char *source)
|
int line, const char *source)
|
||||||
{
|
{
|
||||||
void *mem;
|
struct memdebug *mem;
|
||||||
|
|
||||||
size_t size = wantedsize>64?wantedsize:64;
|
size_t size = sizeof(struct memdebug)+wantedsize;
|
||||||
|
|
||||||
mem=(realloc)(ptr, size);
|
mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
|
||||||
|
|
||||||
|
mem=(struct memdebug *)(realloc)(mem, size);
|
||||||
if(logfile)
|
if(logfile)
|
||||||
fprintf(logfile, "MEM %s:%d realloc(%p, %d) = %p\n",
|
fprintf(logfile, "MEM %s:%d realloc(%p, %d) = %p\n",
|
||||||
source, line, ptr, wantedsize, mem);
|
source, line, ptr, wantedsize, mem?mem->mem:NULL);
|
||||||
return mem;
|
|
||||||
|
if(mem) {
|
||||||
|
mem->size = wantedsize;
|
||||||
|
return mem->mem;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void curl_dofree(void *ptr, int line, const char *source)
|
void curl_dofree(void *ptr, int line, const char *source)
|
||||||
{
|
{
|
||||||
|
struct memdebug *mem;
|
||||||
|
|
||||||
if(NULL == ptr) {
|
if(NULL == ptr) {
|
||||||
fprintf(stderr, "ILLEGAL free() on NULL at %s:%d\n",
|
fprintf(stderr, "ILLEGAL free() on NULL at %s:%d\n",
|
||||||
source, line);
|
source, line);
|
||||||
exit(2);
|
exit(2);
|
||||||
}
|
}
|
||||||
/* we know this is least 64 bytes, destroy this much */
|
mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
|
||||||
memset(ptr, 0x13, 64);
|
|
||||||
|
|
||||||
|
/* destroy */
|
||||||
|
memset(mem->mem, 0x13, mem->size);
|
||||||
|
|
||||||
/* free for real */
|
/* free for real */
|
||||||
(free)(ptr);
|
(free)(mem);
|
||||||
|
|
||||||
if(logfile)
|
if(logfile)
|
||||||
fprintf(logfile, "MEM %s:%d free(%p)\n", source, line, ptr);
|
fprintf(logfile, "MEM %s:%d free(%p)\n", source, line, ptr);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user