diff options
Diffstat (limited to 'mem')
| -rw-r--r-- | mem/LICENSE.txt | 21 | ||||
| -rw-r--r-- | mem/mem.cpp | 80 | ||||
| -rw-r--r-- | mem/mem.h | 11 |
3 files changed, 112 insertions, 0 deletions
diff --git a/mem/LICENSE.txt b/mem/LICENSE.txt new file mode 100644 index 0000000..af4ad2e --- /dev/null +++ b/mem/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License + +Copyright (c) 2015-2018 Bibhas Bhattacharya + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/mem/mem.cpp b/mem/mem.cpp new file mode 100644 index 0000000..0442d4e --- /dev/null +++ b/mem/mem.cpp @@ -0,0 +1,80 @@ +#define _CRT_SECURE_NO_WARNINGS + +#include <stdlib.h> +#include <stdio.h> +#pragma warning(disable : 4996) +void *debug_malloc(size_t size, const char* file, int line) { + void *p = malloc(size); + + if (p == NULL) { + return NULL; + } + + char buff[256]; + + sprintf(buff, "%p.mem", p); + + FILE *f = fopen(buff, "w"); + + fprintf(f, "File: %s\nLine: %d\nSize: %zu bytes\n", file, line, size); + fclose(f); + + return p; +} + +void *debug_calloc(size_t count, size_t size, const char* file, int line) { + void *p = calloc(count, size); + + if (p == NULL) { + return NULL; + } + + char buff[256]; + + sprintf(buff, "%p.mem", p); + + FILE *f = fopen(buff, "w"); + + fprintf(f, "File: %s\nLine: %d\nSize: %zu bytes\n", file, line, + count * size); + fclose(f); + + return p; +} + +void *debug_realloc(void *ptr, size_t size, const char* file, int line) { + void *p = realloc(ptr, size); + + if (p == NULL) { + return NULL; + } + + char buff[256]; + //Delete the old pointer record + sprintf(buff, "%p.mem", ptr); + if (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); + + FILE *f = fopen(buff, "w"); + + fprintf(f, "File: %s\nLine: %d\nSize: %zu bytes\n", file, line, size); + fclose(f); + + return p; +} + +void debug_free(void *p, const char* file, int line) { + char buff[256]; + + sprintf(buff, "%p.mem", p); + if (unlink(buff) < 0) { + printf("Double free: %p File: %s Line: %d\n", p, file, line); + } + + free(p); +} + diff --git a/mem/mem.h b/mem/mem.h new file mode 100644 index 0000000..cc6597b --- /dev/null +++ b/mem/mem.h @@ -0,0 +1,11 @@ +void *debug_malloc(size_t size, const char* file, int line); +void *debug_calloc(size_t count, size_t size, const char* file, int line); +void *debug_realloc(void *ptr, size_t size, const char* file, int line); +void debug_free(void *p, const char* file, int line); + +#ifdef TS_DEBUG +#define malloc(s) debug_malloc(s, __FILE__, __LINE__) +#define calloc(c, s) debug_calloc(c, s, __FILE__, __LINE__) +#define realloc(p, s) debug_realloc(p, s, __FILE__, __LINE__) +#define free(p) debug_free(p, __FILE__, __LINE__) +#endif |
