memhandler.h File Reference

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>

Go to the source code of this file.

Defines

#define M_EXIT   1
#define M_CONTINUE   0

Functions

void * my_malloc (size_t nb)
void * my_calloc (size_t nb, size_t s)
void * my_realloc (void *ptr, size_t nb)
void my_free (void *bloc)
void my_exit (void)
void print_number_of_objects_in_memory (void)
void free_all (void)
void print_ptr_lst (void)


Define Documentation

#define M_CONTINUE   0

continue flag

Definition at line 49 of file memhandler.h.

#define M_EXIT   1

exit flag

Definition at line 48 of file memhandler.h.


Function Documentation

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 }

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 }


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