MQTT C Client Libraries Internals: Heap.c File Reference

| MQTT C Client Libraries Internals

Data Structures | Macros | 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 <inttypes.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...

## Macros
#define | PRIeyecatcher PRIx64
| print eyecatcher in HEX notation

## Typedefs
typedef uint64_t | 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"

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

## ◆ Heap_findItem()

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_get_info()

heap_info* Heap_get_info | ( | void |  | ) |

Access to heap state.

Returns
  pointer to the heap state structure

## ◆ Heap_roundup()

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

## ◆ Heap_unlink()

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:

## ◆ HeapDump()

int HeapDump | ( | FILE * | file | ) |

Dump the state of the heap.

Parameters

  file | file handle to dump the heap contents to

## ◆ HeapDumpString()

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

## ◆ HeapScan()

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:

## ◆ Internal_heap_unlink()

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

## ◆ myfree()

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:

## ◆ mymalloc()

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:

## ◆ myrealloc()

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:

## ◆ ptrCompare()

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