_____________________________________________________________________________
/ / Index / Goinfre / Resume / Links / Contact / Sitemap / .: v0.6.1.0 |^|
|\__/-------+ +--------+-------+---------+---------+-----------------+#|
| 1| /goinfre/libs/libdebugmalloc/libxmalloc.c |#|
| 2| ========================================== |#|
| 3| |#|
| 4| [ raw ] [ download ] |#|
| 5| ` `` ````````````````````` |#|
| 6| |#|
| 7| /** |#|
| 8| * libxmalloc.c |#|
| 9| * |#|
| 10| * Copyright (c) Manfred Touron 2008x |#|
| 11| */ |#|
| 12| |#|
| 13| #include <sys/types.h> |#|
| 14| #include <stdio.h> |#|
| 15| #include <dlfcn.h> |#|
| 16| |#|
| 17| # define _XM_PROTO , char *, int, char * |#|
| 18| # define _XM_ARGS , file, line, func |#|
| 19| # define _XM_PROTO_F , char *file, int line, char *func |#|
| 20| # define _XM_PRINTF "(%s:%d %s)" |#|
| 21| |#|
| 22| # define REAL(FUNC, RET_TYPE, ARGS_PROTO, ARGS_PARAM) |#|
| 23| \ |#|
| 24| RET_TYPE _xm_real_ ## FUNC ARGS_PROTO \ |#|
| 25| { \ |#|
| 26| static RET_TYPE (*func)ARGS_PROTO; \ |#|
| 27| \ |#|
| 28| if (!func) \ |#|
| 29| func = (RET_TYPE (*)ARGS_PROTO)dlsym(RTLD_NEXT, #FUNC); |#|
| 30| \ |#|
| 31| return (func ARGS_PARAM); \ |#|
| 32| } \ |#|
| 33| RET_TYPE FUNC ARGS_PROTO \ |#|
| 34| { \ |#|
| 35| _xmalloc_initptr(); \ |#|
| 36| return (_xm_real_ ## FUNC ARGS_PARAM); \ |#|
| 37| } |#|
| 38| |#|
| 39| typedef struct s_xmalloc |#|
| 40| { |#|
| 41| void *ptr; |#|
| 42| char *type; |#|
| 43| size_t size; |#|
| 44| char *file; |#|
| 45| int line; |#|
| 46| char *func; |#|
| 47| struct s_xmalloc *next; |#|
| 48| } t_xmalloc; |#|
| 49| |#|
| 50| extern void (*_f_xmalloc_add)(void *, char *, size_t _XM_PROTO); |#|
| 51| extern void (*_f_xmalloc_show)(void); |#|
| 52| extern void (*_f_xmalloc_del)(void * _XM_PROTO); |#|
| 53| |#|
| 54| t_xmalloc *g_xmalloc = NULL; |#|
| 55| |#|
| 56| REAL(malloc, void *, (size_t size), (size)) |#|
| 57| REAL(calloc, void *, (size_t number, size_t size), (number, size)) |#|
| 58| REAL(realloc, void *, (void *ptr, size_t size), (ptr, size)) |#|
| 59| REAL(reallocf, void *, (void *ptr, size_t size), (ptr, size)) |#|
| 60| |#|
| 61| void _xm_real_free(void *ptr) |#|
| 62| { |#|
| 63| static void (*func)(void *); |#|
| 64| |#|
| 65| if (!func) |#|
| 66| func = (void (*)(void *))dlsym(RTLD_NEXT, "free"); |#|
| 67| func(ptr); |#|
| 68| return ; |#|
| 69| } |#|
| 70| |#|
| 71| void free(void *ptr) |#|
| 72| { |#|
| 73| _xmalloc_initptr(); |#|
| 74| _xm_real_free(ptr); |#|
| 75| } |#|
| 76| |#|
| 77| void _xmalloc_add(void *ptr, char *type, size_t size |#|
| 78| _XM_PROTO_F) |#|
| 79| { |#|
| 80| t_xmalloc *new; |#|
| 81| |#|
| 82| new = _xm_real_malloc(sizeof(*new)); |#|
| 83| new->ptr = ptr; |#|
| 84| new->size = size; |#|
| 85| new->type = type; |#|
| 86| new->file = file; |#|
| 87| new->line = line; |#|
| 88| new->func = func; |#|
| 89| new->next = g_xmalloc; |#|
| 90| g_xmalloc = new; |#|
| 91| return ; |#|
| 92| } |#|
| 93| |#|
| 94| void _xmalloc_show(void) |#|
| 95| { |#|
| 96| t_xmalloc *tmp; |#|
| 97| int c; |#|
| 98| |#|
| 99| c = 0; |#|
|100| printf("+===================================" |#|
|101| "====================================+\n"); |#|
|102| for (tmp = g_xmalloc; tmp; tmp = tmp->next) |#|
|103| printf("| %03d %-10s(%d) - %p " _XM_PRINTF |#|
|104| "\r\t\t\t\t\t\t\t\t\t|\n", |#|
|105| ++c, tmp->type, tmp->size, tmp->ptr, |#|
|106| tmp->file, tmp->line, tmp->func); |#|
|107| printf("+===================================" | |
|108| "====================================+\n" | |
|109| "Unfreed mallocs: %d\n", c); | |
|110| return; | |
|111| } | |
|112| | |
|113| void _xmalloc_del(void *ptr _XM_PROTO_F) | |
|114| { | |
|115| t_xmalloc *tmp; | |
|116| t_xmalloc *tmp2; | |
|117| | |
|118| if (ptr == NULL) | |
|119| return ; | |
|120| if (g_xmalloc->ptr == ptr) | |
|121| { | |
|122| tmp2 = g_xmalloc; | |
|123| g_xmalloc = tmp2->next; | |
|124| _xm_real_free(tmp2); | |
|125| return ; | |
|126| } | |
|127| else | |
|128| for (tmp = g_xmalloc; tmp; tmp = tmp->next) | |
|129| if (tmp->next && tmp->next->ptr == ptr) | |
|130| { | |
|131| tmp2 = tmp->next; | |
|132| tmp->next = tmp->next->next; | |
|133| _xm_real_free(tmp2); | |
|134| return ; | |
|135| } | |
|136| printf("chunk is already free - %p " _XM_PRINTF "\n", ptr); | |
|137| return ; | |
|138| } | |
|139| | |
|140| void _xmalloc_atexit(void) | |
|141| { | |
|142| _xmalloc_show(); | |
|143| } | |
|144| | |
|145| int _xmalloc_initptr(void) | |
|146| { | |
|147| static int inited = 0; | |
|148| | |
|149| if (!inited) | |
|150| { | |
|151| inited = 1; | |
|152| atexit(_xmalloc_atexit); | |
|153| _f_xmalloc_add = _xmalloc_add; | |
|154| _f_xmalloc_show = _xmalloc_show; | |
|155| _f_xmalloc_del = _xmalloc_del; | |
|156| } | |
|157| return (0); | |
|158| } | |
|159| | |
+---+ | |
\_ \______ mo5.so - normal - 1337.so - rev.so - video_r.so +---------------+ |
|__ \_____ plain.so - color.so - comment.so / moul 2008 (c) |V|
\-----\______\________________________________________/-----------------+-+