Nilorea Library

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Macros Groups
n_debug_mem.c
Go to the documentation of this file.
1
7 #include "n_debug_mem.h"
8
10 static DEBUG_MEM_TABLE __debug_mem_table ;
11
12
17 int init_debug_mem()
18 {
19 __debug_mem_table . nb_malloc = 0 ;
20 __debug_mem_table . nb_free = 0 ;
21
22 /* A huge table because there could be a ton of memory allocation */
23 __debug_mem_table . table = new_ht( 1048576 );
24 if( ! __debug_mem_table . table )
25 {
26 n_log( LOG_ERR , "Unable to allocate a 1048576 item hash table" );
27 return FALSE ;
28 }
29
30 if( pthread_mutex_init( &__debug_mem_table . lock , NULL ) != 0 )
31 {
32 n_log( LOG_ERR , "Unable to initialize lock" );
33 return FALSE ;
34 }
35
36 return TRUE ;
37 } /* init_debuf_mem() */
38
39
40
50 DEBUG_MEM_ITEM *new_db_mem_item( void *ptr , size_t size , int line , char *file , char *func )
51 {
52 DEBUG_MEM_ITEM *item = NULL ;
53
54 item = (DEBUG_MEM_ITEM *)calloc( 1 , sizeof( DEBUG_MEM_ITEM ) );
55 if( !item )
56 {
57 n_log( LOG_ERR , "Unable to allocate a DEBUG_MEM_ITEM" );
58 return NULL ;
59 }
60 item -> ptr = ptr ;
61 item -> size = size ;
62 item -> line = line ;
63 item -> file = file ;
64 item -> func = func ;
65
66 return item ;
67 } /* new_db_item() */
68
69
70
75 void delete_db_mem_item( void *ptr )
76 {
77 DEBUG_MEM_ITEM *item = (DEBUG_MEM_ITEM *)ptr ;
78 if( item )
79 {
80 FreeNoLog( item -> file );
81 FreeNoLog( item -> func );
82 FreeNoLog( item );
83 }
84 } /* delete_db_mem_item */
85
86
87
96 void *db_mem_alloc( size_t size , int line , char *file , const char *func )
97 {
98 DEBUG_MEM_ITEM *item = NULL ;
99 char key[ 512 ] = "";
100 void *ptr = NULL ;
101
102 __debug_mem_table . nb_malloc ++ ;
103
104 ptr = calloc( 1 , size );
105 if( ptr )
106 {
107 item = new_db_mem_item( ptr , size , line , strdup( file ) , strdup( func ) );
108 sprintf( key , "%p" , ptr );
109 ht_put_ptr( __debug_mem_table . table , key , item , delete_db_mem_item );
110 n_log( LOG_DEBUG , "Added %p size %zu file %s func %s line %d" , item -> ptr , item -> size , item -> file , item -> func , item -> line );
111 }
112 else
113 {
114 n_log( LOG_ERR , "Unable to allocate %d oct at file %s func %s line %d " , size , file , func , line );
115 }
116 return ptr ;
117 }
118
119
120
126 void *db_malloc( size_t size )
127 {
128 return db_mem_alloc( size , __LINE__, __FILE__, __func__ );
129 } /* db_malloc */
130
131
132
137 void db_mem_free( void *ptr )
138 {
139 void *hashptr = NULL ;
140 char key[ 512 ] = "";
141
142 sprintf( key , "%p" , ptr );
143 ht_get_ptr( __debug_mem_table . table , key , &hashptr );
144 if( hashptr )
145 {
146 DEBUG_MEM_ITEM *item = hashptr ;
147 n_log( LOG_DEBUG , "Removed %p size %zu file %s func %s line %d" , item -> ptr , item -> size , item -> file , item -> func , item -> line );
148 delete_db_mem_item( item );
149 item = NULL ;
150 ht_remove( __debug_mem_table . table , key );
151 __debug_mem_table . nb_free ++ ;
152 }
153 Free( ptr );
154 } /* db_mem_free() */
155
156
157
163 int db_mem_dump_leaked( char *file )
164 {
165 FILE *out = NULL ;
166 LIST_NODE *node = NULL ;
167 HASH_NODE *hash_entry = NULL ;
168 DEBUG_MEM_ITEM *item = NULL ;
169 size_t len = 0 ;
170
171 out = fopen( file , "w" );
172 if( !out )
173 {
174 n_log( LOG_ERR ,"Unable to open %s" , file );
175 return FALSE ;
176 }
177
178 fprintf( out , "%d malloc / %d free\n" , __debug_mem_table . nb_malloc , __debug_mem_table . nb_free );
179
180 for( int it = 0 ; it < __debug_mem_table . table -> size ; it ++ )
181 {
182 node = __debug_mem_table . table -> hash_table[ it ] -> start ;
183 while( node )
184 {
185 hash_entry= (HASH_NODE *)node -> ptr ;
186 item = hash_entry -> data . ptr ;
187
188 if( item )
189 {
190 fprintf( out , "%p size %zu file %s func %s line %d dump:" , item -> ptr , item -> size , item -> file , item -> func , item -> line );
191 if( item -> size > DEBUG_MEM_DUMP_MAX_LEN )
192 len = DEBUG_MEM_DUMP_MAX_LEN ;
193 else
194 len = item -> size ;
195
196 fwrite( item -> ptr , len , 1 , out );
197 fprintf( out , "\n" );
198 delete_db_mem_item( item );
199 item = NULL ;
200 }
201 else
202 {
203 n_log( LOG_ERR ,"Unable to load item from node -> ptr" );
204 }
205 node = node -> next ;
206 }
207 }
208
209 fclose( out );
210 n_log( LOG_DEBUG , "Memory dump finished" );
211 return TRUE ;
212 } /*db_mem_dump_leaked() */