00001
00002 #include "../headers/memhandler.h"
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 typedef struct ptr_node
00080 {
00081 struct ptr_node *next ;
00082 void *ptr ;
00083
00084 } ptr_node ;
00085
00086
00087 typedef struct
00088 {
00089 ptr_node *first ;
00090 ptr_node *last ;
00091
00092 size_t n_ptr ;
00093
00094 } ptr_lst ;
00095
00096
00097 static ptr_lst *ST_lst_alloc = NULL ;
00098 #ifdef M_MEM_DEBUG
00099 static FILE *ST_fdebug = NULL ;
00100 #endif
00101
00102 static ptr_node* ptr_node_alloc(void *ptr) ;
00103 static void add_bloc(void *bloc) ;
00104 static void remove_bloc(void *bloc) ;
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121 void* my_malloc(size_t s)
00122 {
00123 void *bloc = malloc(s) ;
00124
00125 if(bloc == NULL) {
00126 fprintf(stderr, "! malloc failed in my_bloc_malloc. Programm will exit, as demanded.\n") ;
00127 my_exit() ;
00128 }
00129
00130 #ifdef M_MEM_DEBUG
00131 if(ST_fdebug) fprintf(ST_fdebug, "> (M)Allocation success at: <%p>\n", bloc) ;
00132 #endif
00133 add_bloc(bloc) ;
00134
00135 return bloc ;
00136 }
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153 void* my_calloc(size_t nb, size_t s)
00154 {
00155 void *bloc = calloc(nb, s) ;
00156
00157 if(bloc == NULL){
00158 fprintf(stderr, "! malloc failed in my_bloc_malloc. Programm will exit, as demanded.\n") ;
00159 my_exit() ;
00160 }
00161 #ifdef M_MEM_DEBUG
00162 if(ST_fdebug) fprintf(ST_fdebug, "> (C)Allocation success at: <%p>\n", bloc) ;
00163 #endif
00164 add_bloc(bloc) ;
00165
00166 return bloc ;
00167 }
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184 void* my_realloc(void *ptr, size_t s)
00185 {
00186 void *tmp = ptr ;
00187 ptr = realloc(ptr, s) ;
00188
00189 if(ptr == NULL){
00190 fprintf(stderr, "! malloc failed in my_bloc_malloc. Programm will exit, as demanded.\n") ;
00191 my_exit() ;
00192 }
00193 else {
00194 if(tmp && tmp != ptr) {
00195 #ifdef M_MEM_DEBUG
00196 if(ST_fdebug) fprintf(ST_fdebug, "> Realloc generates a new pointer: <%p> newly allocated at %p. \n", tmp, ptr) ;
00197 #endif
00198
00199
00200
00201
00202 remove_bloc(tmp) ;
00203 add_bloc(ptr) ;
00204 }
00205 }
00206
00207 return ptr ;
00208 }
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225 static ptr_node* ptr_node_alloc(void *ptr)
00226 {
00227 ptr_node *node = (ptr_node *) malloc(sizeof(ptr_node)) ;
00228
00229 if(node) {
00230 node->ptr = ptr ;
00231 node->next = NULL ;
00232 }
00233 else {
00234 fprintf(stderr, "! malloc failed in my_bloc_malloc. Programm will exit, as demanded.\n") ;
00235 my_exit() ;
00236 }
00237
00238 return node ;
00239 }
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 void my_free(void *bloc)
00256 {
00257 if(bloc) {
00258 #ifdef M_MEM_DEBUG
00259 if(ST_fdebug) fprintf(ST_fdebug, "> (my_free) Freeing bloc <%p>!\n", bloc) ;
00260 fflush(ST_fdebug) ;
00261 #endif
00262 remove_bloc(bloc) ;
00263 free(bloc) ;
00264 }
00265 else {
00266 #ifdef M_MEM_DEBUG
00267 if(ST_fdebug) fprintf(ST_fdebug, "! Cannot free a NULL variable!\n") ;
00268 #endif
00269 }
00270
00271 }
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289 static void add_bloc(void *bloc)
00290 {
00291 #ifdef M_MEM_DEBUG
00292 if(ST_fdebug) fprintf(ST_fdebug, "> Adding bloc %p\n", bloc) ;
00293 #endif
00294
00295
00296 if(!ST_lst_alloc) {
00297
00298 ST_lst_alloc = (ptr_lst*) malloc(sizeof(ptr_lst)) ;
00299 if(!ST_lst_alloc) {
00300 fprintf(stderr, "! malloc failed in add_bloc while creating the list. Programm will exit.\n") ;
00301 my_exit() ;
00302 }
00303 #ifdef M_MEM_DEBUG
00304 ST_fdebug = fopen("memory_debug.tmp", "w") ;
00305 #endif
00306
00307 ST_lst_alloc->first = NULL ;
00308 ST_lst_alloc->last = NULL ;
00309 ST_lst_alloc->n_ptr = 0 ;
00310 }
00311
00312
00313
00314 ptr_node *newn = ptr_node_alloc(bloc) ;
00315 if(ST_lst_alloc->first) {
00316 newn->next = ST_lst_alloc->first ;
00317 ST_lst_alloc->first = newn ;
00318 }
00319 else {
00320 ST_lst_alloc->first = newn ;
00321 ST_lst_alloc->last = newn ;
00322 }
00323
00324 ST_lst_alloc->n_ptr += 1 ;
00325 }
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343 static void remove_bloc(void *bloc)
00344 {
00345 #ifdef M_MEM_DEBUG
00346 size_t i = 0 ;
00347 #endif
00348
00349
00350 if(ST_lst_alloc) {
00351
00352 ptr_node *cur = ST_lst_alloc->first,
00353 *prev = cur ;
00354 #ifdef M_MEM_DEBUG
00355 if(!cur && ST_fdebug) fprintf(ST_fdebug, "> No element in the list, so cannot remove <%p>...\n", bloc) ;
00356 size_t prev_s = ST_lst_alloc->n_ptr ;
00357 #endif
00358 while(cur) {
00359 if(cur->ptr == bloc) {
00360
00361 #ifdef M_MEM_DEBUG
00362 if(ST_fdebug) fprintf(ST_fdebug, "> Removing <%p>...\n", cur->ptr) ;
00363 #endif
00364 if(cur == ST_lst_alloc->first) {
00365 ST_lst_alloc->first = cur->next ;
00366 }
00367 else if(cur == ST_lst_alloc->last) {
00368 ST_lst_alloc->last = prev ;
00369 prev->next = NULL ;
00370 }
00371 else {
00372 prev->next = cur->next ;
00373 }
00374
00375 cur->next = NULL ;
00376 free(cur) ;
00377 ST_lst_alloc->n_ptr -= 1 ;
00378
00379 break ;
00380 }
00381 #ifdef M_MEM_DEBUG
00382 i++ ;
00383 #endif
00384
00385 prev = cur ;
00386 cur = cur->next ;
00387 }
00388 #ifdef M_MEM_DEBUG
00389 if(i == prev_s && ST_fdebug) fprintf(ST_fdebug, "Memhandler: <%p> not found!\n", bloc) ;
00390 #endif
00391 }
00392
00393 #ifdef M_MEM_DEBUG
00394 else {
00395 if(ST_fdebug) fprintf(ST_fdebug, "! No bloc allocated -> cannot remove given argument from an empty list.\n") ;
00396 }
00397 #endif
00398 }
00399
00400
00401 int get_number_of_objects_in_memory(void){
00402 if(ST_lst_alloc) {
00403 return(ST_lst_alloc->n_ptr);
00404 }
00405 return(0);
00406 }
00407
00408 void print_number_of_objects_in_memory(void){
00409 if(ST_lst_alloc) {
00410 printf("\nHaving %d objects in memory\n",ST_lst_alloc->n_ptr);
00411 }
00412 }
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427 void free_all(void)
00428 {
00429 if(ST_lst_alloc) {
00430
00431 ptr_node *cur = ST_lst_alloc->first,
00432 *tmp = cur ;
00433 while(cur) {
00434 if(cur->ptr) {
00435 #ifdef M_MEM_DEBUG
00436 if(ST_fdebug) fprintf(ST_fdebug, "> Freeing <%p>.\n", cur->ptr) ;
00437 #endif
00438 free(cur->ptr) ;
00439 cur->ptr = NULL ;
00440 }
00441 #ifdef M_MEM_DEBUG
00442 else {
00443 if(ST_fdebug) fprintf(ST_fdebug, "! A NULL bloc has been found in free_all!! Ignoring this bloc...\n") ;
00444 }
00445 #endif
00446
00447 tmp = cur->next ;
00448 cur->next = NULL ;
00449 free(cur) ;
00450
00451 cur = tmp ;
00452 }
00453
00454 free(ST_lst_alloc) ;
00455 ST_lst_alloc = NULL ;
00456
00457 }
00458 #ifdef M_MEM_DEBUG
00459 else {
00460 if(ST_fdebug) fprintf(ST_fdebug, "! No bloc allocated -> cannot free memory...\n") ;
00461 }
00462 #endif
00463 }
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483 void my_exit(void)
00484 {
00485 free_all() ;
00486
00487 exit(1) ;
00488 }
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502 void print_ptr_lst(void)
00503 {
00504 if(ST_lst_alloc && ST_lst_alloc->n_ptr > 0) {
00505 int i = 0 ;
00506 ptr_node *cur = ST_lst_alloc->first ;
00507 fprintf(stdout, "\t==============\n\tLst of %d allocated ptr: \n", (int) ST_lst_alloc->n_ptr) ;
00508 fprintf(stdout, "\tFirst: <%p> - Last: <%p>\n", ST_lst_alloc->first->ptr, ST_lst_alloc->last->ptr) ;
00509
00510 while(cur) {
00511 if(cur->ptr) {
00512 fprintf(stdout, "\t<%p> <%d>\n", cur->ptr, (int)sizeof(cur->ptr)) ;
00513 }
00514 else {
00515 fprintf(stdout, "\t! A NULL bloc has been found in position %d.\n", i) ;
00516 }
00517
00518 cur = cur->next ;
00519 }
00520 }
00521 else {
00522 fprintf(stdout, "\t! No bloc allocated yet...\n") ;
00523 }
00524 }
00525