Classes | Typedefs | Functions | Variables
Heap.c File Reference

functions to manage the heap with the goal of eliminating memory leaks More...

#include "Tree.h"
#include "Log.h"
#include "StackTrace.h"
#include "Thread.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stddef.h>
#include "Heap.h"
Include dependency graph for Heap.c:

Go to the source code of this file.

Classes

struct  storageElement
 

Typedefs

typedef double eyecatcherType
 

Functions

static void checkEyecatchers (char *file, int line, void *p, size_t size)
 
void * Heap_findItem (void *p)
 
heap_infoHeap_get_info (void)
 
int Heap_initialize (void)
 
static size_t Heap_roundup (size_t size)
 
void Heap_terminate (void)
 
void Heap_unlink (char *file, int line, void *p)
 
int HeapDump (FILE *file)
 
int HeapDumpString (FILE *file, char *str)
 
static void HeapScan (enum LOG_LEVELS log_level)
 
static int Internal_heap_unlink (char *file, int line, void *p)
 
void myfree (char *file, int line, void *p)
 
void * mymalloc (char *file, int line, size_t size)
 
void * myrealloc (char *file, int line, void *p, size_t size)
 
static int ptrCompare (void *a, void *b, int value)
 

Variables

static const char * errmsg = "Memory allocation error"
 
static eyecatcherType eyecatcher = (eyecatcherType)0x8888888888888888
 
static Tree heap
 
static mutex_type heap_mutex = &heap_mutex_store
 
static pthread_mutex_t heap_mutex_store = PTHREAD_MUTEX_INITIALIZER
 
static heap_info state = {0, 0}
 

Detailed Description

functions to manage the heap with the goal of eliminating memory leaks

For any module to use these functions transparently, simply include the Heap.h header file. Malloc and free will be redefined, but will behave in exactly the same way as normal, so no recoding is necessary.

Definition in file Heap.c.

Typedef Documentation

typedef double eyecatcherType

Definition at line 60 of file Heap.c.

Function Documentation

static void checkEyecatchers ( char *  file,
int  line,
void *  p,
size_t  size 
)
static

Definition at line 222 of file Heap.c.

void* Heap_findItem ( void *  p)

Utility to find an item in the heap. Lets you know if the heap already contains the memory location in question.

Parameters
ppointer to a memory location
Returns
pointer to the storage element if found, or NULL

Definition at line 368 of file Heap.c.

heap_info* Heap_get_info ( void  )

Access to heap state

Returns
pointer to the heap state structure

Definition at line 432 of file Heap.c.

int Heap_initialize ( void  )

Heap initialization.

Definition at line 406 of file Heap.c.

static size_t Heap_roundup ( size_t  size)
static

Round allocation size up to a multiple of the size of an int. Apart from possibly reducing fragmentation, on the old v3 gcc compilers I was hitting some weird behaviour, which might have been errors in sizeof() used on structures and related to packing. In any case, this fixes that too.

Parameters
sizethe size actually needed
Returns
the rounded up size

Definition at line 98 of file Heap.c.

void Heap_terminate ( void  )

Heap termination.

Definition at line 417 of file Heap.c.

void Heap_unlink ( char *  file,
int  line,
void *  p 
)

Remove an item from the recorded heap without actually freeing it. Use sparingly!

Parameters
fileuse the FILE macro to indicate which file this item was allocated in
lineuse the LINE macro to indicate which line this item was allocated at
ppointer to the item to be removed

Definition at line 300 of file Heap.c.

int HeapDump ( FILE *  file)

Dump the state of the heap

Parameters
filefile handle to dump the heap contents to

Definition at line 462 of file Heap.c.

int HeapDumpString ( FILE *  file,
char *  str 
)

Dump a string from the heap so that it can be displayed conveniently

Parameters
filefile handle to dump the heap contents to
strthe string to dump, could be NULL

Definition at line 443 of file Heap.c.

static void HeapScan ( enum LOG_LEVELS  log_level)
static

Scans the heap and reports any items currently allocated. To be used at shutdown if any heap items have not been freed.

Definition at line 383 of file Heap.c.

static int Internal_heap_unlink ( char *  file,
int  line,
void *  p 
)
static

Remove an item from the recorded heap without actually freeing it. Use sparingly!

Parameters
fileuse the FILE macro to indicate which file this item was allocated in
lineuse the LINE macro to indicate which line this item was allocated at
ppointer to the item to be removed

Definition at line 245 of file Heap.c.

void myfree ( char *  file,
int  line,
void *  p 
)

Frees a block of memory. A direct replacement for free, but checks that a item is in the allocates list first.

Parameters
fileuse the FILE macro to indicate which file this item was allocated in
lineuse the LINE macro to indicate which line this item was allocated at
ppointer to the item to be freed

Definition at line 277 of file Heap.c.

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

Allocates a block of memory. A direct replacement for malloc, but keeps track of items allocated in a list, so that free can check that a item is being freed correctly and that we can check that all memory is freed at shutdown.

Parameters
fileuse the FILE macro to indicate which file this item was allocated in
lineuse the LINE macro to indicate which line this item was allocated at
sizethe size of the item to be allocated
Returns
pointer to the allocated item, or NULL if there was an error

Definition at line 158 of file Heap.c.

void* myrealloc ( char *  file,
int  line,
void *  p,
size_t  size 
)

Reallocates a block of memory. A direct replacement for realloc, but keeps track of items allocated in a list, so that free can check that a item is being freed correctly and that we can check that all memory is freed at shutdown. We have to remove the item from the tree, as the memory is in order and so it needs to be reinserted in the correct place.

Parameters
fileuse the FILE macro to indicate which file this item was reallocated in
lineuse the LINE macro to indicate which line this item was reallocated at
ppointer to the item to be reallocated
sizethe new size of the item
Returns
pointer to the allocated item, or NULL if there was an error

Definition at line 320 of file Heap.c.

static int ptrCompare ( void *  a,
void *  b,
int  value 
)
static

List callback function for comparing storage elements

Parameters
apointer to the current content in the tree (storageElement*)
bpointer to the memory to free
Returns
boolean indicating whether a and b are equal

Definition at line 114 of file Heap.c.

Variable Documentation

const char* errmsg = "Memory allocation error"
static

Definition at line 80 of file Heap.c.

eyecatcherType eyecatcher = (eyecatcherType)0x8888888888888888
static

Definition at line 61 of file Heap.c.

Tree heap
static

Tree that holds the allocation records

Definition at line 79 of file Heap.c.

mutex_type heap_mutex = &heap_mutex_store
static

Definition at line 55 of file Heap.c.

pthread_mutex_t heap_mutex_store = PTHREAD_MUTEX_INITIALIZER
static

Definition at line 54 of file Heap.c.

heap_info state = {0, 0}
static

global heap state information

Definition at line 58 of file Heap.c.



plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 04:02:48