papi_memory.h File Reference

Include dependency graph for papi_memory.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  pmem_t

Defines

#define DEBUG_FILE_LEN   20
#define PAPI_MEM_LIB_OVERHEAD   1
#define PAPI_MEM_OVERHEAD   2

Functions

void * _papi_malloc (char *, int, size_t)
void _papi_free (char *, int, void *)
void * _papi_realloc (char *, int, void *, size_t)
void * _papi_calloc (char *, int, size_t, size_t)
int _papi_valid_free (char *, int, void *)
char * _papi_strdup (char *, int, const char *s)
void _papi_mem_cleanup_all ()
void _papi_mem_print_info (void *ptr)
void _papi_mem_print_stats ()
int _papi_mem_overhead (int)
int _papi_mem_check_all_overflow ()

Define Documentation

#define DEBUG_FILE_LEN   20

Definition at line 6 of file papi_memory.h.

#define PAPI_MEM_LIB_OVERHEAD   1

Definition at line 60 of file papi_memory.h.

#define PAPI_MEM_OVERHEAD   2

Definition at line 61 of file papi_memory.h.


Function Documentation

void* _papi_calloc ( char *  ,
int  ,
size_t  ,
size_t   
)

Definition at line 113 of file papi_memory.c.

00114 {
00115     void *ptr = _papi_malloc( file, line, size * nmemb );
00116 
00117     if ( !ptr )
00118         return ( NULL );
00119     memset( ptr, 0, size * nmemb );
00120     return ( ptr );
00121 }

Here is the call graph for this function:

void _papi_free ( char *  file,
int  line,
void *  ptr 
)

Frees up the ptr

Definition at line 226 of file papi_memory.c.

00227 {
00228     pmem_t *mem_ptr = get_mem_ptr( ptr );
00229 
00230     if ( !mem_ptr ) {
00231         ( void ) file;
00232         ( void ) line;
00233         return;
00234     }
00235 
00236     MEMDBG( "%p: Freeing %d bytes from File: %s  Line: %d\n", mem_ptr->ptr,
00237             mem_ptr->size, file, line );
00238 
00239     _papi_hwi_lock( MEMORY_LOCK );
00240     remove_mem_ptr( mem_ptr );
00241     _papi_mem_check_all_overflow(  );
00242     _papi_hwi_unlock( MEMORY_LOCK );
00243 }

Here is the call graph for this function:

void* _papi_malloc ( char *  ,
int  ,
size_t   
)

Definition at line 124 of file papi_memory.c.

00125 {
00126     void *ptr;
00127     void **tmp;
00128     pmem_t *mem_ptr;
00129     size_t nsize = size + MEM_PROLOG;
00130 
00131 #ifdef DEBUG
00132     nsize += MEM_EPILOG;
00133 #endif
00134 
00135     if ( size == 0 ) {
00136         MEMDBG( "Attempting to allocate %lu bytes from File: %s  Line: %d\n",
00137                 ( unsigned long ) size, file, line );
00138         return ( NULL );
00139     }
00140 
00141     ptr = ( void * ) malloc( nsize );
00142 
00143     if ( !ptr )
00144         return ( NULL );
00145     else {
00146         if ( ( mem_ptr =
00147                init_mem_ptr( ( char * ) ptr + MEM_PROLOG, ( int ) size, file,
00148                              line ) ) == NULL ) {
00149             free( ptr );
00150             return ( NULL );
00151         }
00152         tmp = ptr;
00153         *tmp = mem_ptr;
00154         ptr = mem_ptr->ptr;
00155         mem_ptr->ptr = ptr;
00156         _papi_hwi_lock( MEMORY_LOCK );
00157         insert_mem_ptr( mem_ptr );
00158         set_epilog( mem_ptr );
00159         _papi_hwi_unlock( MEMORY_LOCK );
00160 
00161         MEMDBG( "%p: Allocated %lu bytes from File: %s  Line: %d\n",
00162                 mem_ptr->ptr, ( unsigned long ) size, file, line );
00163         return ( ptr );
00164     }
00165     return ( NULL );
00166 }

Here is the call graph for this function:

Here is the caller graph for this function:

int _papi_mem_check_all_overflow (  ) 

Definition at line 470 of file papi_memory.c.

00471 {
00472     int fnd = 0;
00473 #ifdef DEBUG
00474     pmem_t *tmp;
00475 
00476     for ( tmp = mem_head; tmp; tmp = tmp->next ) {
00477         if ( _papi_mem_check_buf_overflow( tmp ) )
00478             fnd++;
00479     }
00480 
00481     if ( fnd ) {
00482         LEAKDBG( "%d Total Buffer overflows detected!\n", fnd );
00483     }
00484 #endif
00485     return ( fnd );
00486 }

Here is the call graph for this function:

Here is the caller graph for this function:

void _papi_mem_cleanup_all (  ) 

Clean all memory up and print out memory leak information to stderr

Definition at line 303 of file papi_memory.c.

00304 {
00305     pmem_t *ptr = NULL, *tmp = NULL;
00306 #ifdef DEBUG
00307     int cnt = 0;
00308 #endif
00309 
00310     _papi_hwi_lock( MEMORY_LOCK );
00311     _papi_mem_check_all_overflow(  );
00312 
00313     for ( ptr = mem_head; ptr; ptr = tmp ) {
00314         tmp = ptr->next;
00315 #ifdef DEBUG
00316         LEAKDBG( "MEMORY LEAK: %p of %d bytes, from File: %s Line: %d\n",
00317                  ptr->ptr, ptr->size, ptr->file, ptr->line );
00318         cnt += ptr->size;
00319 #endif
00320 
00321         remove_mem_ptr( ptr );
00322     }
00323     _papi_hwi_unlock( MEMORY_LOCK );
00324 #ifdef DEBUG
00325     if ( 0 != cnt ) { 
00326         LEAKDBG( "TOTAL MEMORY LEAK: %d bytes.\n", cnt );
00327     }
00328 #endif
00329 }

Here is the call graph for this function:

Here is the caller graph for this function:

int _papi_mem_overhead ( int  type  ) 

Return the amount of memory overhead of the PAPI library and the memory system PAPI_MEM_LIB_OVERHEAD is the library overhead PAPI_MEM_OVERHEAD is the memory overhead They both can be | together This only includes "malloc'd memory"

Definition at line 280 of file papi_memory.c.

00281 {
00282     pmem_t *ptr = NULL;
00283     int size = 0;
00284 
00285     _papi_hwi_lock( MEMORY_LOCK );
00286     for ( ptr = mem_head; ptr; ptr = ptr->next ) {
00287         if ( type & PAPI_MEM_LIB_OVERHEAD )
00288             size += ptr->size;
00289         if ( type & PAPI_MEM_OVERHEAD ) {
00290             size += ( int ) sizeof ( pmem_t );
00291             size += ( int ) MEM_PROLOG;
00292 #ifdef DEBUG
00293             size += ( int ) MEM_EPILOG;
00294 #endif
00295         }
00296     }
00297     _papi_hwi_unlock( MEMORY_LOCK );
00298     return size;
00299 }

Here is the call graph for this function:

void _papi_mem_print_info ( void *  ptr  ) 

Print information about the memory including file and location it came from

Definition at line 247 of file papi_memory.c.

00248 {
00249     pmem_t *mem_ptr = get_mem_ptr( ptr );
00250 
00251 #ifdef DEBUG
00252     fprintf( stderr, "%p: Allocated %d bytes from File: %s  Line: %d\n", ptr,
00253              mem_ptr->size, mem_ptr->file, mem_ptr->line );
00254 #else
00255     fprintf( stderr, "%p: Allocated %d bytes\n", ptr, mem_ptr->size );
00256 #endif
00257     return;
00258 }

Here is the call graph for this function:

Here is the caller graph for this function:

void _papi_mem_print_stats (  ) 

Print out all memory information

Definition at line 262 of file papi_memory.c.

00263 {
00264     pmem_t *tmp = NULL;
00265 
00266     _papi_hwi_lock( MEMORY_LOCK );
00267     for ( tmp = mem_head; tmp; tmp = tmp->next ) {
00268         _papi_mem_print_info( tmp->ptr );
00269     }
00270     _papi_hwi_unlock( MEMORY_LOCK );
00271 }

Here is the call graph for this function:

void* _papi_realloc ( char *  file,
int  line,
void *  ptr,
size_t  size 
)

_papi_realloc -- given a pointer returned by _papi_malloc, returns a pointer to the related pmem_t structure describing this pointer. Checks for NULL pointers and returns NULL if error.

Definition at line 77 of file papi_memory.c.

00078 {
00079     size_t nsize = size + MEM_PROLOG;
00080     pmem_t *mem_ptr;
00081     void *nptr;
00082 
00083 #ifdef DEBUG
00084     nsize += MEM_EPILOG;
00085     _papi_hwi_lock( MEMORY_LOCK );
00086     _papi_mem_check_all_overflow(  );
00087 #endif
00088 
00089     if ( !ptr )
00090         return ( _papi_malloc( file, line, size ) );
00091 
00092     mem_ptr = get_mem_ptr( ptr );
00093     nptr = ( pmem_t * ) realloc( ( ( char * ) ptr - MEM_PROLOG ), nsize );
00094 
00095     if ( !nptr )
00096         return ( NULL );
00097 
00098     mem_ptr->size = ( int ) size;
00099     mem_ptr->ptr = ( char * ) nptr + MEM_PROLOG;
00100 #ifdef DEBUG
00101     strncpy( mem_ptr->file, file, DEBUG_FILE_LEN );
00102     mem_ptr->file[DEBUG_FILE_LEN - 1] = '\0';
00103     mem_ptr->line = line;
00104     set_epilog( mem_ptr );
00105     _papi_hwi_unlock( MEMORY_LOCK );
00106 #endif
00107     MEMDBG( "%p: Re-allocated: %lu bytes from File: %s  Line: %d\n",
00108             mem_ptr->ptr, ( unsigned long ) size, file, line );
00109     return ( mem_ptr->ptr );
00110 }

Here is the call graph for this function:

char* _papi_strdup ( char *  ,
int  ,
const char *  s 
)

Definition at line 169 of file papi_memory.c.

00170 {
00171     size_t size;
00172     char *ptr;
00173 
00174     if ( !s )
00175         return ( NULL );
00176 
00177     /* String Length +1 for \0 */
00178     size = strlen( s ) + 1;
00179     ptr = ( char * ) _papi_malloc( file, line, size );
00180 
00181     if ( !ptr )
00182         return ( NULL );
00183 
00184     memcpy( ptr, s, size );
00185     return ( ptr );
00186 }

Here is the call graph for this function:

int _papi_valid_free ( char *  file,
int  line,
void *  ptr 
)

Only frees the memory if PAPI malloced it returns 1 if pointer was valid; 0 if not

Definition at line 191 of file papi_memory.c.

00192 {
00193     pmem_t *tmp;
00194     int valid = 0;
00195 
00196     if ( !ptr ) {
00197         ( void ) file;
00198         ( void ) line;
00199         return ( 0 );
00200     }
00201 
00202     _papi_hwi_lock( MEMORY_LOCK );
00203 
00204     for ( tmp = mem_head; tmp; tmp = tmp->next ) {
00205         if ( ptr == tmp->ptr ) {
00206             pmem_t *mem_ptr = get_mem_ptr( ptr );
00207 
00208             if ( mem_ptr ) {
00209                 MEMDBG( "%p: Freeing %d bytes from File: %s  Line: %d\n",
00210                         mem_ptr->ptr, mem_ptr->size, file, line );
00211                 remove_mem_ptr( mem_ptr );
00212                 _papi_mem_check_all_overflow(  );
00213             }
00214 
00215             valid = 1;
00216             break;
00217         }
00218     }
00219 
00220     _papi_hwi_unlock( MEMORY_LOCK );
00221     return ( valid );
00222 }

Here is the call graph for this function:


Generated on 8 Sep 2016 for PAPI by  doxygen 1.6.1