diff options
author | Ratakor <ratakor@disroot.org> | 2023-06-25 15:55:57 +0200 |
---|---|---|
committer | Ratakor <ratakor@disroot.org> | 2023-06-25 15:55:57 +0200 |
commit | 62ac361d518a8a07238d1531858693c9157006b9 (patch) | |
tree | a09b89befb6f5f1d8b538399301b436ba083a66d | |
parent | 6ff15dc0f668dfd124800a815a2e288f1709464b (diff) |
Simplify free and some renames
Simplify __free() by directly copying the struct instead of copying all
element of a struct to another.
Rename dfree(), dmalloc(), etc... to __free(), __malloc() so it's
clearer that they must not be used.
-rw-r--r-- | README.md | 7 | ||||
-rw-r--r-- | dalloc.c | 42 | ||||
-rw-r--r-- | dalloc.h | 29 |
3 files changed, 35 insertions, 43 deletions
@@ -13,14 +13,11 @@ overflow and memory leak. It will also output a recap at the end of the program. By defining `EXITSEGV` all exit() call will be replaced by a segmentation fault which can be very useful to check where an overflow occur with a real debugger. -Functions ---------- -dfree(), dmalloc(), etc... should NEVER be used, use the classic functions and -enable dalloc with `-DDALLOC` when debugging. - strdup, strndup and reallocarray are not standard so you'll probably need to define `_DEFAULT_SOURCE` or equivalent to use them outside of dalloc. +Functions +--------- #### dalloc_check_overflow(void) Output all memory overflow to stderr and return the sum of all overflow. @@ -138,9 +138,9 @@ dalloc_sighandler(int sig) } void -dfree(void *p, char *file, int line) +__free(void *p, char *file, int line) { - size_t i = 0, j; + size_t i = 0; if (p == NULL) return; @@ -164,14 +164,8 @@ dfree(void *p, char *file, int line) exit(EXIT_STATUS); } - for (i++; i < npointers; i++) { - pointers[i - 1].p = pointers[i].p; - pointers[i - 1].siz = pointers[i].siz; - for (j = 0; j < PATH_MAX && pointers[i].file[j] != '\0'; j++) - pointers[i - 1].file[j] = pointers[i].file[j]; - pointers[i - 1].file[j] = '\0'; - pointers[i - 1].line = pointers[i].line; - } + for (i++; i < npointers; i++) + pointers[i - 1] = pointers[i]; npointers--; free(p); pthread_mutex_unlock(&dalloc_mutex); @@ -179,7 +173,7 @@ dfree(void *p, char *file, int line) void * -dmalloc(size_t siz, char *file, int line) +__malloc(size_t siz, char *file, int line) { void *p = NULL; size_t i; @@ -219,7 +213,7 @@ dmalloc(size_t siz, char *file, int line) } void * -dcalloc(size_t nmemb, size_t siz, char *file, int line) +__calloc(size_t nmemb, size_t siz, char *file, int line) { void *p; @@ -233,24 +227,24 @@ dcalloc(size_t nmemb, size_t siz, char *file, int line) } siz *= nmemb; - p = dmalloc(siz, file, line); + p = __malloc(siz, file, line); memset(p, 0, siz); return p; } void * -drealloc(void *p, size_t siz, char *file, int line) +__realloc(void *p, size_t siz, char *file, int line) { void *np; size_t i = 0, osiz; if (p == NULL) - return dmalloc(siz, file, line); + return __malloc(siz, file, line); if (siz == 0) { - dfree(p, file, line); + __free(p, file, line); return NULL; } @@ -265,15 +259,15 @@ drealloc(void *p, size_t siz, char *file, int line) osiz = pointers[i].siz; pthread_mutex_unlock(&dalloc_mutex); - np = dmalloc(siz, file, line); + np = __malloc(siz, file, line); memcpy(np, p, MIN(osiz, siz)); - dfree(p, file, line); + __free(p, file, line); return np; } void * -dreallocarray(void *p, size_t nmemb, size_t siz, char *file, int line) +__reallocarray(void *p, size_t nmemb, size_t siz, char *file, int line) { if (siz != 0 && nmemb > -1 / siz) { fprintf(stderr, "%s:%d: dalloc: reallocarray: %s\n", @@ -281,24 +275,24 @@ dreallocarray(void *p, size_t nmemb, size_t siz, char *file, int line) exit(EXIT_STATUS); } - return drealloc(p, nmemb * siz, file, line); + return __realloc(p, nmemb * siz, file, line); } char * -dstrdup(const char *s, char *file, int line) +__strdup(const char *s, char *file, int line) { char *p; size_t siz; siz = strlen(s) + 1; - p = dmalloc(siz, file, line); + p = __malloc(siz, file, line); memcpy(p, s, siz); return p; } char * -dstrndup(const char *s, size_t n, char *file, int line) +__strndup(const char *s, size_t n, char *file, int line) { const char *end; char *p; @@ -306,7 +300,7 @@ dstrndup(const char *s, size_t n, char *file, int line) end = memchr(s, '\0', n); siz = (end ? (size_t)(end - s) : n) + 1; - p = dmalloc(siz, file, line); + p = __malloc(siz, file, line); memcpy(p, s, siz - 1); p[siz - 1] = '\0'; @@ -16,13 +16,13 @@ extern "C" { #endif /* __cplusplus */ #ifdef DALLOC -#define free(p) (dfree(p, __FILE__, __LINE__)) -#define malloc(siz) (dmalloc(siz, __FILE__, __LINE__)) -#define calloc(nmemb, siz) (dcalloc(nmemb, siz, __FILE__, __LINE__)) -#define realloc(p, siz) (drealloc(p, siz, __FILE__, __LINE__)) -#define reallocarray(p, n, siz) (dreallocarray(p, n, siz, __FILE__, __LINE__)) -#define strdup(s) (dstrdup(s, __FILE__, __LINE__)) -#define strndup(s, n) (dstrndup(s, n, __FILE__, __LINE__)) +#define free(p) (__free(p, __FILE__, __LINE__)) +#define malloc(siz) (__malloc(siz, __FILE__, __LINE__)) +#define calloc(nmemb, siz) (__calloc(nmemb, siz, __FILE__, __LINE__)) +#define realloc(p, siz) (__realloc(p, siz, __FILE__, __LINE__)) +#define reallocarray(p, n, siz) (__reallocarray(p, n, siz, __FILE__, __LINE__)) +#define strdup(s) (__strdup(s, __FILE__, __LINE__)) +#define strndup(s, n) (__strndup(s, n, __FILE__, __LINE__)) #endif /* DALLOC */ #ifdef EXITSEGV @@ -34,19 +34,20 @@ void dalloc_check_free(void); void dalloc_check_all(void); void dalloc_sighandler(int sig); -void dfree(void *p, char *file, int line); -void *dmalloc(size_t siz, char *file, int line) +void __free(void *p, char *file, int line); +void *__malloc(size_t siz, char *file, int line) __attribute_warn_unused_result__; -void *dcalloc(size_t nmemb, size_t siz, char *file, int line) +void *__calloc(size_t nmemb, size_t siz, char *file, int line) __attribute_warn_unused_result__; -void *drealloc(void *p, size_t siz, char *file, int line) +void *__realloc(void *p, size_t siz, char *file, int line) __attribute_warn_unused_result__; -void *dreallocarray(void *p, size_t nmemb, size_t siz, char *file, int line) +void *__reallocarray(void *p, size_t nmemb, size_t siz, char *file, int line) __attribute_warn_unused_result__; -char *dstrdup(const char *s, char *file, int line) +char *__strdup(const char *s, char *file, int line) __attribute_warn_unused_result__ __nonnull ((1)); -char *dstrndup(const char *s, size_t n, char *file, int line) +char *__strndup(const char *s, size_t n, char *file, int line) __attribute_warn_unused_result__ __nonnull ((1)); + void exitsegv(int dummy); #ifdef __cplusplus |