diff options
| author | Aldrik Ramaekers <aldrikboy@gmail.com> | 2024-03-14 21:58:58 +0100 |
|---|---|---|
| committer | Aldrik Ramaekers <aldrikboy@gmail.com> | 2024-03-14 21:58:58 +0100 |
| commit | eeed387a65d9dbdafddffab8e6f8b507262ffaf5 (patch) | |
| tree | 3a730603579c9719e1bd4fead5fd6a474806270f /mem/mem.cpp | |
| parent | b00e89d83ee95ea3129ee9bc31ba540ef0f42adf (diff) | |
building on unix
Diffstat (limited to 'mem/mem.cpp')
| -rw-r--r-- | mem/mem.cpp | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/mem/mem.cpp b/mem/mem.cpp index 0442d4e..43f5a89 100644 --- a/mem/mem.cpp +++ b/mem/mem.cpp @@ -2,6 +2,9 @@ #include <stdlib.h> #include <stdio.h> +#ifndef _WIN32 +#include <unistd.h> +#endif #pragma warning(disable : 4996) void *debug_malloc(size_t size, const char* file, int line) { void *p = malloc(size); @@ -12,10 +15,10 @@ void *debug_malloc(size_t size, const char* file, int line) { char buff[256]; - sprintf(buff, "%p.mem", p); + sprintf(buff, "bin/debug/mem/%p.mem", p); FILE *f = fopen(buff, "w"); - + if (!f) return p; fprintf(f, "File: %s\nLine: %d\nSize: %zu bytes\n", file, line, size); fclose(f); @@ -31,9 +34,10 @@ void *debug_calloc(size_t count, size_t size, const char* file, int line) { char buff[256]; - sprintf(buff, "%p.mem", p); + sprintf(buff, "bin/debug/mem/%p.mem", p); FILE *f = fopen(buff, "w"); + if (!f) return p; fprintf(f, "File: %s\nLine: %d\nSize: %zu bytes\n", file, line, count * size); @@ -51,15 +55,16 @@ void *debug_realloc(void *ptr, size_t size, const char* file, int line) { char buff[256]; //Delete the old pointer record - sprintf(buff, "%p.mem", ptr); - if (unlink(buff) < 0) { + sprintf(buff, "bin/debug/mem/%p.mem", ptr); + if (ptr != NULL && unlink(buff) < 0) { printf("Double free: %p File: %s Line: %d\n", ptr, file, line); } //Create the new pointer record - sprintf(buff, "%p.mem", p); + sprintf(buff, "bin/debug/mem/%p.mem", p); FILE *f = fopen(buff, "w"); + if (!f) return p; fprintf(f, "File: %s\nLine: %d\nSize: %zu bytes\n", file, line, size); fclose(f); @@ -70,8 +75,8 @@ void *debug_realloc(void *ptr, size_t size, const char* file, int line) { void debug_free(void *p, const char* file, int line) { char buff[256]; - sprintf(buff, "%p.mem", p); - if (unlink(buff) < 0) { + sprintf(buff, "bin/debug/mem/%p.mem", p); + if (p != NULL && unlink(buff) < 0) { printf("Double free: %p File: %s Line: %d\n", p, file, line); } |
