memhandler.c File Reference

#include "../headers/memhandler.h"

Go to the source code of this file.

Data Structures

struct  ptr_node
struct  ptr_lst

Functions

static ptr_nodeptr_node_alloc (void *ptr)
static void add_bloc (void *bloc)
static void remove_bloc (void *bloc)
void * my_malloc (size_t s)
void * my_calloc (size_t nb, size_t s)
void * my_realloc (void *ptr, size_t s)
void my_free (void *bloc)
int get_number_of_objects_in_memory (void)
void print_number_of_objects_in_memory (void)
void free_all (void)
void my_exit (void)
void print_ptr_lst (void)

Variables

static ptr_lstST_lst_alloc = NULL


Function Documentation

static void add_bloc ( void *  bloc  )  [static]

## FUNCTION: static add_bloc

## SPECIFICATION: Add an allocated pointer (bloc) to the list ST_lst_alloc.

This function is supposed to be called by my_malloc, my_calloc or my_realloc functions only.

## PARAMETRES: @ void *bloc: The pointer to remove

## RETURN:

Definition at line 289 of file memhandler.c.

References ptr_lst::first, ptr_lst::last, my_exit(), ptr_lst::n_ptr, ptr_node::next, and ptr_node_alloc().

Referenced by my_calloc(), my_malloc(), and my_realloc().

00290 {       
00291         #ifdef M_MEM_DEBUG
00292                 if(ST_fdebug) fprintf(ST_fdebug, "> Adding bloc %p\n", bloc) ;
00293         #endif
00294 
00295         /* First check if the list exists, if not create it. */
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         /* Now add the bloc to the chained list */
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 }

void free_all ( void   ) 

## FUNCTION: free_all

## SPECIFICATION: Free all pointers allocated and present in the list ST_lst_alloc. If a NULL pointer is found, ignore it.

## PARAMETRES: void

## RETURN: void

Definition at line 427 of file memhandler.c.

References ptr_lst::first, ptr_node::next, and ptr_node::ptr.

Referenced by main(), and my_exit().

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 }

int get_number_of_objects_in_memory ( void   ) 

Definition at line 401 of file memhandler.c.

References ptr_lst::n_ptr.

00401                                          {
00402     if(ST_lst_alloc) {
00403         return(ST_lst_alloc->n_ptr);
00404     }
00405     return(0);
00406 }

void* my_calloc ( size_t  nb,
size_t  s 
)

## FUNCTION: my_calloc

## SPECIFICATION: Allocate memory for nb bloc of size s using calloc standart function.

## PARAMETRES: @ size_t nb : Number of bloc to allocate @ size_t s : Size of the bloc to allocate

## RETURN: void *: Pointer to the allocated bloc

Definition at line 153 of file memhandler.c.

References add_bloc(), and my_exit().

Referenced by fill_vvertices(), get_pocket_pvertices(), rpdb_open(), sort_pockets(), str_split(), and test_pdb_line().

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 }

void my_exit ( void   ) 

## FUNCTION: my_exit

## SPECIFICATION: Before exiting the programm, just free all allocated pointers (if any). This allows to exit the programme whenever one wants during the execution without dealing with memory allocation/desallocation.

This implies to use my_malloc and my_free functions instead of standart functions. Only memory blocs allocated with those functions will be freed when my_exit is called.

## PARAMETRES: void

## RETURN: void

Definition at line 483 of file memhandler.c.

References free_all().

Referenced by add_bloc(), my_calloc(), my_malloc(), my_realloc(), and ptr_node_alloc().

00484 {
00485         free_all() ;
00486 
00487         exit(1) ;
00488 }

void my_free ( void *  bloc  ) 

## FUNCTION: my_free

## SPECIFICATION: Free memory for the given bloc, and remove this pointer from the list.

## PARAMETRES: @ void *bloc: Pointer to free.

## RETURN:

Definition at line 255 of file memhandler.c.

References remove_bloc().

Referenced by c_lst_pocket_free(), c_lst_vertices_free(), check_fparams(), check_fpocket(), check_pockets(), clusterPockets(), desc_pocket(), dropPocket(), f_readl(), free_dparams(), free_fparams(), free_mdconcat(), free_mdparams(), free_pdb_atoms(), free_s_vsort(), free_tab_str(), free_tparams(), free_vert_lst(), get_actual_pocket_DEPRECATED(), get_explicit_desc(), init_md_grid(), load_vvertices(), mdpocket_characterize(), mergePockets(), rpdb_open(), set_pockets_descriptors(), sort_pockets(), test_pdb_line(), test_set(), write_pocket_pdb(), and write_pocket_pdb_DB().

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 }

void* my_malloc ( size_t  s  ) 

## FUNCTION: my_malloc

## SPECIFICATION: Allocate memory for a bloc of size s.

## PARAMETRES: @ size_t s : Size of the bloc to allocate

## RETURN: void *: Pointer to the allocated bloc

Definition at line 121 of file memhandler.c.

References add_bloc(), and my_exit().

Referenced by add_complexe(), add_prot(), add_snapshot(), alloc_first_md_concat(), alloc_pocket(), allocate_s_desc(), c_lst_pockets_alloc(), c_lst_vertices_alloc(), check_fparams(), check_fpocket(), f_readl(), fill_vvertices(), float_get_min_max_from_pockets(), get_fpocket_args(), get_mol_atm_neigh(), get_mol_ctd_atm_neigh(), get_mol_vert_neigh(), get_pocket_contacted_atms(), get_sorted_list(), get_vert_contacted_atms(), get_wanted_atom_ids(), init_def_dparams(), init_def_fparams(), init_def_mdparams(), init_def_tparams(), init_md_concat(), init_md_grid(), load_vvertices(), mdpocket_characterize(), node_pocket_alloc(), node_vertice_alloc(), pck_final_clust(), realloc_md_concat(), rpdb_open(), set_ASA(), set_pockets_descriptors(), write_pocket_pdb(), and write_pocket_pdb_DB().

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 }

void* my_realloc ( void *  ptr,
size_t  s 
)

## FUNCTION: my_realloc

## SPECIFICATION: Allocate memory for a bloc of size s using calloc standart function.

## PARAMETRES: @ size_t s : Size of the bloc to allocate @ int exit : Whether we exit the programm if malloc fails.

## RETURN: void *: Pointer to the allocated bloc

Definition at line 184 of file memhandler.c.

References add_bloc(), my_exit(), and remove_bloc().

Referenced by add_complexe(), add_prot(), add_snapshot(), get_mol_atm_neigh(), get_mol_ctd_atm_neigh(), get_mol_vert_neigh(), get_pocket_contacted_atms(), get_wanted_atom_ids(), load_vvertices(), realloc_md_concat(), and write_pocket_pdb().

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         /* If the newly allocated pointer is different from previou s
00200            one, remove previous from the list and add new one. Else, 
00201            ptr doesn't need to be added. */
00202                         remove_bloc(tmp) ;
00203                         add_bloc(ptr) ;
00204                 }
00205         }
00206 
00207         return ptr ;
00208 }

void print_number_of_objects_in_memory ( void   ) 

Definition at line 408 of file memhandler.c.

References ptr_lst::n_ptr.

00408                                             {
00409     if(ST_lst_alloc) {
00410         printf("\nHaving %d objects in memory\n",ST_lst_alloc->n_ptr);
00411     }
00412 }

void print_ptr_lst ( void   ) 

## FUNCTION: print_ptr_lst

## SPECIFICATION: Print allocated pointers stored in ST_lst_alloc for debugging purpose.

## PARAMETRES: void

## RETURN: void

Definition at line 502 of file memhandler.c.

References ptr_lst::first, ptr_lst::last, ptr_lst::n_ptr, ptr_node::next, and ptr_node::ptr.

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 }

static ptr_node * ptr_node_alloc ( void *  ptr  )  [static]

## FONCTION: ptr_node_alloc

## SPECIFICATION: Allocate a simple chained node.

## PARAMETRES: @ void *ptr: Pointer

## RETURN: ptr_node*

Definition at line 225 of file memhandler.c.

References my_exit(), ptr_node::next, and ptr_node::ptr.

Referenced by add_bloc().

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 }

static void remove_bloc ( void *  bloc  )  [static]

## FUNCTION: static remove_bloc

## SPECIFICATION: Remove the given pointer (node) from the list ST_lst_alloc. Donc free the memory.

This function is supposed to be called by my_free function and not another.

## PARAMETRES: @ void *bloc: The pointer to remove.

## RETURN: void

Definition at line 343 of file memhandler.c.

References ptr_lst::first, ptr_lst::last, ptr_lst::n_ptr, ptr_node::next, and ptr_node::ptr.

Referenced by my_free(), and my_realloc().

00344 {
00345 #ifdef M_MEM_DEBUG
00346         size_t i = 0 ;
00347 #endif
00348         /* First check if the list exists, if not create it. */
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                         /* Remove bloc */
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 }


Variable Documentation

ptr_lst* ST_lst_alloc = NULL [static]

Definition at line 97 of file memhandler.c.


Generated on Mon Jun 7 16:44:23 2010 for fpocket by  doxygen 1.5.6