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:

Data Structures

struct  storageElement
 Each item on the heap is recorded with this structure. More...

Functions

static size_t Heap_roundup (size_t size)
 Round allocation size up to a multiple of the size of an int.
static int ptrCompare (void *a, void *b, int value)
 List callback function for comparing storage elements.
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.
static void HeapScan (enum LOG_LEVELS log_level)
 Scans the heap and reports any items currently allocated.
void * mymalloc (char *file, int line, size_t size)
 Allocates a block of memory.
void myfree (char *file, int line, void *p)
 Frees a block of memory.
void Heap_unlink (char *file, int line, void *p)
 Remove an item from the recorded heap without actually freeing it.
void * myrealloc (char *file, int line, void *p, size_t size)
 Reallocates a block of memory.
void * Heap_findItem (void *p)
 Utility to find an item in the heap.
int Heap_initialize (void)
 Heap initialization.
void Heap_terminate (void)
 Heap termination.
heap_infoHeap_get_info (void)
 Access to heap state.
int HeapDumpString (FILE *file, char *str)
 Dump a string from the heap so that it can be displayed conveniently.
int HeapDump (FILE *file)
 Dump the state of the heap.

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 int eyecatcher = 0x88888888
static Tree heap
 Tree that holds the allocation records.
static const char * errmsg = "Memory allocation error"

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.


Function Documentation

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:
p pointer to a memory location
Returns:
pointer to the storage element if found, or NULL

Here is the call graph for this function:

heap_info* Heap_get_info ( void   ) 

Access to heap state.

Returns:
pointer to the heap state structure
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:
size the size actually needed
Returns:
the rounded up size
void Heap_unlink ( char *  file,
int  line,
void *  p 
)

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

Use sparingly!

Parameters:
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

Here is the call graph for this function:

int HeapDump ( FILE *  file  ) 

Dump the state of the heap.

Parameters:
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.

Parameters:
file file handle to dump the heap contents to
str the string to dump, could be NULL
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.

Here is the call graph for this function:

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:
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

Here is the call graph for this function:

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:
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

Here is the call graph for this function:

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:
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
Returns:
pointer to the allocated item, or NULL if there was an error

Here is the call graph for this function:

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:
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
Returns:
pointer to the allocated item, or NULL if there was an error

Here is the call graph for this function:

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

List callback function for comparing storage elements.

Parameters:
a pointer to the current content in the tree (storageElement*)
b pointer to the memory to free
Returns:
boolean indicating whether a and b are equal
 All Data Structures Files Functions Variables Typedefs Defines

Generated on 2 Jun 2018 for MQTT C Client Libraries Internals by  doxygen 1.6.1