goinfre/libs/libdebugmalloc/libxmallocsimple.c

goinfre/libs/libdebugmalloc/libxmallocsimple.c

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