PAPI 7.1.0.0
Loading...
Searching...
No Matches
papi_memory.c File Reference
Include dependency graph for papi_memory.c:

Go to the source code of this file.

Macros

#define IN_MEM_FILE
 
#define MEM_PROLOG   (2*sizeof(void *))
 
#define MEM_EPILOG   4
 
#define MEM_EPILOG_1   0xC
 
#define MEM_EPILOG_2   0xA
 
#define MEM_EPILOG_3   0xC
 
#define MEM_EPILOG_4   0xA
 

Functions

static pmem_tget_mem_ptr (void *ptr)
 
static pmem_tinit_mem_ptr (void *, int, char *, int)
 
static void insert_mem_ptr (pmem_t *)
 
static void remove_mem_ptr (pmem_t *)
 
static int set_epilog (pmem_t *mem_ptr)
 
void * _papi_realloc (char *file, int line, void *ptr, size_t size)
 
void * _papi_calloc (char *file, int line, size_t nmemb, size_t size)
 
void * _papi_malloc (char *file, int line, size_t size)
 
char * _papi_strdup (char *file, int line, const char *s)
 
int _papi_valid_free (char *file, int line, void *ptr)
 
void _papi_free (char *file, int line, void *ptr)
 
void _papi_mem_print_info (void *ptr)
 
void _papi_mem_print_stats ()
 
int _papi_mem_overhead (int type)
 
void _papi_mem_cleanup_all ()
 
static int _papi_mem_check_buf_overflow (pmem_t *tmp)
 
int _papi_mem_check_all_overflow ()
 

Variables

static pmem_tmem_head = NULL
 

Detailed Description

Author
Kevin London londo.nosp@m.n@cs.nosp@m..utk..nosp@m.edu PAPI memory allocation provides for checking and maintenance of all memory allocated through this interface. Implemented as a series of wrappers around standard C memory allocation routines, _papi_malloc and associated functions add a prolog and optional epilog to each malloc'd pointer. The prolog, sized to preserve memory alignment, contains a pointer to a linked list of pmem_t structures that describe every block of memory allocated through these calls. The optional epilog is enabled if DEBUG is defined, and contains a distinctive pattern that allows checking for pointer overflow.

Definition in file papi_memory.c.

Macro Definition Documentation

◆ IN_MEM_FILE

#define IN_MEM_FILE

Definition at line 16 of file papi_memory.c.

◆ MEM_EPILOG

#define MEM_EPILOG   4

Define the amount of extra memory at the end of the alloc'd pointer. Also define the contents: 0xCACA

Definition at line 38 of file papi_memory.c.

◆ MEM_EPILOG_1

#define MEM_EPILOG_1   0xC

Definition at line 39 of file papi_memory.c.

◆ MEM_EPILOG_2

#define MEM_EPILOG_2   0xA

Definition at line 40 of file papi_memory.c.

◆ MEM_EPILOG_3

#define MEM_EPILOG_3   0xC

Definition at line 41 of file papi_memory.c.

◆ MEM_EPILOG_4

#define MEM_EPILOG_4   0xA

Definition at line 42 of file papi_memory.c.

◆ MEM_PROLOG

#define MEM_PROLOG   (2*sizeof(void *))

Define the amount of extra memory at the beginning of the alloc'd pointer. This is usually the size of a pointer, but in some cases needs to be bigger to preserve data alignment.

Definition at line 31 of file papi_memory.c.

Function Documentation

◆ _papi_calloc()

void * _papi_calloc ( char *  file,
int  line,
size_t  nmemb,
size_t  size 
)

Definition at line 113 of file papi_memory.c.

114{
115 void *ptr = _papi_malloc( file, line, size * nmemb );
116
117 if ( !ptr )
118 return ( NULL );
119 memset( ptr, 0, size * nmemb );
120 return ( ptr );
121}
void * _papi_malloc(char *file, int line, size_t size)
Definition: papi_memory.c:124
const char FILE * file
Definition: pscanf.h:13
Here is the call graph for this function:

◆ _papi_free()

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

Frees up the ptr

Definition at line 226 of file papi_memory.c.

227{
228 pmem_t *mem_ptr = get_mem_ptr( ptr );
229
230 if ( !mem_ptr ) {
231 ( void ) file;
232 ( void ) line;
233 return;
234 }
235
236 MEMDBG( "%p: Freeing %d bytes from File: %s Line: %d\n", mem_ptr->ptr,
237 mem_ptr->size, file, line );
238
240 remove_mem_ptr( mem_ptr );
243}
#define MEMDBG(format, args...)
Definition: papi_debug.h:71
#define MEMORY_LOCK
Definition: papi_internal.h:89
static pmem_t * get_mem_ptr(void *ptr)
Definition: papi_memory.c:344
static void remove_mem_ptr(pmem_t *)
Definition: papi_memory.c:405
int _papi_mem_check_all_overflow()
Definition: papi_memory.c:470
void * ptr
Definition: papi_memory.h:10
int size
Definition: papi_memory.h:11
inline_static int _papi_hwi_lock(int lck)
Definition: threads.h:69
inline_static int _papi_hwi_unlock(int lck)
Definition: threads.h:83
Here is the call graph for this function:

◆ _papi_malloc()

void * _papi_malloc ( char *  file,
int  line,
size_t  size 
)

Definition at line 124 of file papi_memory.c.

125{
126 void *ptr;
127 void **tmp;
128 pmem_t *mem_ptr;
129 size_t nsize = size + MEM_PROLOG;
130
131#ifdef DEBUG
132 nsize += MEM_EPILOG;
133#endif
134
135 if ( size == 0 ) {
136 MEMDBG( "Attempting to allocate %lu bytes from File: %s Line: %d\n",
137 ( unsigned long ) size, file, line );
138 return ( NULL );
139 }
140
141 ptr = ( void * ) malloc( nsize );
142
143 if ( !ptr )
144 return ( NULL );
145 else {
146 if ( ( mem_ptr =
147 init_mem_ptr( ( char * ) ptr + MEM_PROLOG, ( int ) size, file,
148 line ) ) == NULL ) {
149 free( ptr );
150 return ( NULL );
151 }
152 tmp = ptr;
153 *tmp = mem_ptr;
154 ptr = mem_ptr->ptr;
155 mem_ptr->ptr = ptr;
157 insert_mem_ptr( mem_ptr );
158 set_epilog( mem_ptr );
160
161 MEMDBG( "%p: Allocated %lu bytes from File: %s Line: %d\n",
162 mem_ptr->ptr, ( unsigned long ) size, file, line );
163 return ( ptr );
164 }
165 return ( NULL );
166}
double tmp
#define MEM_EPILOG
Definition: papi_memory.c:38
#define MEM_PROLOG
Definition: papi_memory.c:31
static pmem_t * init_mem_ptr(void *, int, char *, int)
Definition: papi_memory.c:358
static void insert_mem_ptr(pmem_t *)
Definition: papi_memory.c:383
static int set_epilog(pmem_t *mem_ptr)
Definition: papi_memory.c:420
Here is the call graph for this function:
Here is the caller graph for this function:

◆ _papi_mem_check_all_overflow()

int _papi_mem_check_all_overflow ( )

Definition at line 470 of file papi_memory.c.

471{
472 int fnd = 0;
473#ifdef DEBUG
474 pmem_t *tmp;
475
476 for ( tmp = mem_head; tmp; tmp = tmp->next ) {
478 fnd++;
479 }
480
481 if ( fnd ) {
482 LEAKDBG( "%d Total Buffer overflows detected!\n", fnd );
483 }
484#endif
485 return ( fnd );
486}
#define LEAKDBG(format, args...)
Definition: papi_debug.h:72
static pmem_t * mem_head
Definition: papi_memory.c:46
static int _papi_mem_check_buf_overflow(pmem_t *tmp)
Definition: papi_memory.c:438
Here is the call graph for this function:
Here is the caller graph for this function:

◆ _papi_mem_check_buf_overflow()

static int _papi_mem_check_buf_overflow ( pmem_t tmp)
static

Definition at line 438 of file papi_memory.c.

439{
440 int fnd = 0;
441 char *ptr;
442 char *tptr;
443
444 if ( !tmp )
445 return ( 0 );
446
447 tptr = tmp->ptr;
448 tptr += tmp->size;
449
450 /* Move to the buffer overflow padding */
451 ptr = ( ( char * ) tmp->ptr ) + tmp->size;
452 if ( *ptr++ != MEM_EPILOG_1 )
453 fnd = 1;
454 else if ( *ptr++ != MEM_EPILOG_2 )
455 fnd = 2;
456 else if ( *ptr++ != MEM_EPILOG_3 )
457 fnd = 3;
458 else if ( *ptr++ != MEM_EPILOG_4 )
459 fnd = 4;
460
461 if ( fnd ) {
462 LEAKDBG( "Buffer Overflow[%d] for %p allocated from %s at line %d\n",
463 fnd, tmp->ptr, tmp->file, tmp->line );
464 }
465 return ( fnd );
466}
#define MEM_EPILOG_1
Definition: papi_memory.c:39
#define MEM_EPILOG_2
Definition: papi_memory.c:40
#define MEM_EPILOG_3
Definition: papi_memory.c:41
#define MEM_EPILOG_4
Definition: papi_memory.c:42
Here is the caller graph for this function:

◆ _papi_mem_cleanup_all()

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.

304{
305 pmem_t *ptr = NULL, *tmp = NULL;
306#ifdef DEBUG
307 int cnt = 0;
308#endif
309
312
313 for ( ptr = mem_head; ptr; ptr = tmp ) {
314 tmp = ptr->next;
315#ifdef DEBUG
316 LEAKDBG( "MEMORY LEAK: %p of %d bytes, from File: %s Line: %d\n",
317 ptr->ptr, ptr->size, ptr->file, ptr->line );
318 cnt += ptr->size;
319#endif
320
321 remove_mem_ptr( ptr );
322 }
324#ifdef DEBUG
325 if ( 0 != cnt ) {
326 LEAKDBG( "TOTAL MEMORY LEAK: %d bytes.\n", cnt );
327 }
328#endif
329}
char file[DEBUG_FILE_LEN]
Definition: papi_memory.h:13
int line
Definition: papi_memory.h:14
struct pmem * next
Definition: papi_memory.h:16
Here is the call graph for this function:
Here is the caller graph for this function:

◆ _papi_mem_overhead()

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.

281{
282 pmem_t *ptr = NULL;
283 int size = 0;
284
286 for ( ptr = mem_head; ptr; ptr = ptr->next ) {
288 size += ptr->size;
289 if ( type & PAPI_MEM_OVERHEAD ) {
290 size += ( int ) sizeof ( pmem_t );
291 size += ( int ) MEM_PROLOG;
292#ifdef DEBUG
293 size += ( int ) MEM_EPILOG;
294#endif
295 }
296 }
298 return size;
299}
uint16_t type
#define PAPI_MEM_OVERHEAD
Definition: papi_memory.h:61
#define PAPI_MEM_LIB_OVERHEAD
Definition: papi_memory.h:60
int
Definition: sde_internal.h:89
Here is the call graph for this function:

◆ _papi_mem_print_info()

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.

248{
249 pmem_t *mem_ptr = get_mem_ptr( ptr );
250
251#ifdef DEBUG
252 fprintf( stderr, "%p: Allocated %d bytes from File: %s Line: %d\n", ptr,
253 mem_ptr->size, mem_ptr->file, mem_ptr->line );
254#else
255 fprintf( stderr, "%p: Allocated %d bytes\n", ptr, mem_ptr->size );
256#endif
257 return;
258}
FILE * stderr
Here is the call graph for this function:
Here is the caller graph for this function:

◆ _papi_mem_print_stats()

void _papi_mem_print_stats ( )

Print out all memory information

Definition at line 262 of file papi_memory.c.

263{
264 pmem_t *tmp = NULL;
265
267 for ( tmp = mem_head; tmp; tmp = tmp->next ) {
269 }
271}
void _papi_mem_print_info(void *ptr)
Definition: papi_memory.c:247
Here is the call graph for this function:

◆ _papi_realloc()

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.

78{
79 size_t nsize = size + MEM_PROLOG;
80 pmem_t *mem_ptr;
81 void *nptr;
82
83#ifdef DEBUG
84 nsize += MEM_EPILOG;
87#endif
88
89 if ( !ptr )
90 return ( _papi_malloc( file, line, size ) );
91
92 mem_ptr = get_mem_ptr( ptr );
93 nptr = ( pmem_t * ) realloc( ( ( char * ) ptr - MEM_PROLOG ), nsize );
94
95 if ( !nptr )
96 return ( NULL );
97
98 mem_ptr->size = ( int ) size;
99 mem_ptr->ptr = ( char * ) nptr + MEM_PROLOG;
100#ifdef DEBUG
101 strncpy( mem_ptr->file, file, DEBUG_FILE_LEN );
102 mem_ptr->file[DEBUG_FILE_LEN - 1] = '\0';
103 mem_ptr->line = line;
104 set_epilog( mem_ptr );
106#endif
107 MEMDBG( "%p: Re-allocated: %lu bytes from File: %s Line: %d\n",
108 mem_ptr->ptr, ( unsigned long ) size, file, line );
109 return ( mem_ptr->ptr );
110}
#define DEBUG_FILE_LEN
Definition: papi_memory.h:6
Here is the call graph for this function:

◆ _papi_strdup()

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

Definition at line 169 of file papi_memory.c.

170{
171 size_t size;
172 char *ptr;
173
174 if ( !s )
175 return ( NULL );
176
177 /* String Length +1 for \0 */
178 size = strlen( s ) + 1;
179 ptr = ( char * ) _papi_malloc( file, line, size );
180
181 if ( !ptr )
182 return ( NULL );
183
184 memcpy( ptr, s, size );
185 return ( ptr );
186}
double s
Definition: byte_profile.c:36
Here is the call graph for this function:

◆ _papi_valid_free()

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.

192{
193 pmem_t *tmp;
194 int valid = 0;
195
196 if ( !ptr ) {
197 ( void ) file;
198 ( void ) line;
199 return ( 0 );
200 }
201
203
204 for ( tmp = mem_head; tmp; tmp = tmp->next ) {
205 if ( ptr == tmp->ptr ) {
206 pmem_t *mem_ptr = get_mem_ptr( ptr );
207
208 if ( mem_ptr ) {
209 MEMDBG( "%p: Freeing %d bytes from File: %s Line: %d\n",
210 mem_ptr->ptr, mem_ptr->size, file, line );
211 remove_mem_ptr( mem_ptr );
213 }
214
215 valid = 1;
216 break;
217 }
218 }
219
221 return ( valid );
222}
uint8_t valid
Here is the call graph for this function:

◆ get_mem_ptr()

static pmem_t * get_mem_ptr ( void *  ptr)
static

Definition at line 344 of file papi_memory.c.

345{
346 pmem_t **tmp_ptr = ( pmem_t ** ) ( ( char * ) ptr - MEM_PROLOG );
347 pmem_t *mem_ptr;
348
349 if ( !tmp_ptr || !ptr )
350 return ( NULL );
351
352 mem_ptr = *tmp_ptr;
353 return ( mem_ptr );
354}
Here is the caller graph for this function:

◆ init_mem_ptr()

pmem_t * init_mem_ptr ( void *  ptr,
int  size,
char *  file,
int  line 
)
static

Definition at line 358 of file papi_memory.c.

359{
360 pmem_t *mem_ptr = NULL;
361 if ( ( mem_ptr = ( pmem_t * ) malloc( sizeof ( pmem_t ) ) ) == NULL )
362 return ( NULL );
363
364 mem_ptr->ptr = ptr;
365 mem_ptr->size = size;
366 mem_ptr->next = NULL;
367 mem_ptr->prev = NULL;
368#ifdef DEBUG
369 strncpy( mem_ptr->file, file, DEBUG_FILE_LEN );
370 mem_ptr->file[DEBUG_FILE_LEN - 1] = '\0';
371 mem_ptr->line = line;
372#else
373 ( void ) file; /*unused */
374 ( void ) line; /*unused */
375#endif
376 return ( mem_ptr );
377}
struct pmem * prev
Definition: papi_memory.h:17
Here is the caller graph for this function:

◆ insert_mem_ptr()

static void insert_mem_ptr ( pmem_t ptr)
static

Definition at line 383 of file papi_memory.c.

384{
385 if ( !ptr )
386 return;
387
388 if ( !mem_head ) {
389 mem_head = ptr;
390 ptr->next = NULL;
391 ptr->prev = NULL;
392 } else {
393 mem_head->prev = ptr;
394 ptr->next = mem_head;
395 mem_head = ptr;
396 }
397 return;
398}
Here is the caller graph for this function:

◆ remove_mem_ptr()

static void remove_mem_ptr ( pmem_t ptr)
static

Definition at line 405 of file papi_memory.c.

406{
407 if ( !ptr )
408 return;
409
410 if ( ptr->prev )
411 ptr->prev->next = ptr->next;
412 if ( ptr->next )
413 ptr->next->prev = ptr->prev;
414 if ( ptr == mem_head )
415 mem_head = ptr->next;
416 free( ptr );
417}
Here is the caller graph for this function:

◆ set_epilog()

static int set_epilog ( pmem_t mem_ptr)
static

Definition at line 420 of file papi_memory.c.

421{
422#ifdef DEBUG
423 char *chptr = ( char * ) mem_ptr->ptr + mem_ptr->size;
424 *chptr++ = MEM_EPILOG_1;
425 *chptr++ = MEM_EPILOG_2;
426 *chptr++ = MEM_EPILOG_3;
427 *chptr++ = MEM_EPILOG_4;
428 return ( _papi_mem_check_all_overflow( ) );
429#else
430 ( void ) mem_ptr; /*unused */
431#endif
432 return ( 0 );
433}
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ mem_head

pmem_t* mem_head = NULL
static

Definition at line 46 of file papi_memory.c.