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"
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_info * | Heap_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} |
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 double eyecatcherType |
|
static |
void* Heap_findItem | ( | void * | p | ) |
heap_info* Heap_get_info | ( | void | ) |
|
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.
size | the size actually needed |
void Heap_unlink | ( | char * | file, |
int | line, | ||
void * | p | ||
) |
Remove an item from the recorded heap without actually freeing it. Use sparingly!
file | use the FILE macro to indicate which file this item was allocated in |
line | use the LINE macro to indicate which line this item was allocated at |
p | pointer to the item to be removed |
int HeapDump | ( | FILE * | file | ) |
int HeapDumpString | ( | FILE * | file, |
char * | str | ||
) |
|
static |
|
static |
Remove an item from the recorded heap without actually freeing it. Use sparingly!
file | use the FILE macro to indicate which file this item was allocated in |
line | use the LINE macro to indicate which line this item was allocated at |
p | pointer to the item to be removed |
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.
file | use the FILE macro to indicate which file this item was allocated in |
line | use the LINE macro to indicate which line this item was allocated at |
p | pointer to the item to be freed |
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.
file | use the FILE macro to indicate which file this item was allocated in |
line | use the LINE macro to indicate which line this item was allocated at |
size | the size of the item to be allocated |
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.
file | use the FILE macro to indicate which file this item was reallocated in |
line | use the LINE macro to indicate which line this item was reallocated at |
p | pointer to the item to be reallocated |
size | the new size of the item |
|
static |
|
static |
|
static |
|
static |