MQTT C Client Libraries Internals
|
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"
Data Structures | |
struct | storageElement |
Each item on the heap is recorded with this structure. More... | |
Typedefs | |
typedef double | eyecatcherType |
Functions | |
static size_t | Heap_roundup (size_t size) |
Round allocation size up to a multiple of the size of an int. More... | |
static int | ptrCompare (void *a, void *b, int value) |
List callback function for comparing storage elements. More... | |
static void | checkEyecatchers (char *file, int line, void *p, size_t size) |
static int | Internal_heap_unlink (char *file, int line, void *p) |
Remove an item from the recorded heap without actually freeing it. More... | |
static void | HeapScan (enum LOG_LEVELS log_level) |
Scans the heap and reports any items currently allocated. More... | |
void * | mymalloc (char *file, int line, size_t size) |
Allocates a block of memory. More... | |
void | myfree (char *file, int line, void *p) |
Frees a block of memory. More... | |
void | Heap_unlink (char *file, int line, void *p) |
Remove an item from the recorded heap without actually freeing it. More... | |
void * | myrealloc (char *file, int line, void *p, size_t size) |
Reallocates a block of memory. More... | |
void * | Heap_findItem (void *p) |
Utility to find an item in the heap. More... | |
int | Heap_initialize (void) |
Heap initialization. | |
void | Heap_terminate (void) |
Heap termination. | |
heap_info * | Heap_get_info (void) |
Access to heap state. More... | |
int | HeapDumpString (FILE *file, char *str) |
Dump a string from the heap so that it can be displayed conveniently. More... | |
int | HeapDump (FILE *file) |
Dump the state of the heap. More... | |
Variables | |
static pthread_mutex_t | heap_mutex_store = PTHREAD_MUTEX_INITIALIZER |
static mutex_type | heap_mutex = &heap_mutex_store |
static heap_info | state = {0, 0} |
global heap state information | |
static eyecatcherType | eyecatcher = (eyecatcherType)0x8888888888888888 |
static Tree | heap |
Tree that holds the allocation records. | |
static const char * | errmsg = "Memory allocation error" |
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.
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.
p | pointer to a memory location |
heap_info* Heap_get_info | ( | void | ) |
Access to heap state.
|
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 | ) |
Dump the state of the heap.
file | file handle to dump the heap contents to |
int HeapDumpString | ( | FILE * | file, |
char * | str | ||
) |
Dump a string from the heap so that it can be displayed conveniently.
file | file handle to dump the heap contents to |
str | the string to dump, could be NULL |
|
static |
Scans the heap and reports any items currently allocated.
To be used at shutdown if any heap items have not been freed.
|
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 |
List callback function for comparing storage elements.
a | pointer to the current content in the tree (storageElement*) |
b | pointer to the memory to free |