Skip to content

Commit

Permalink
[UMM-53] Move heap variables into umm_heap_config struct
Browse files Browse the repository at this point in the history
  • Loading branch information
rhempel committed May 1, 2021
1 parent 42be1cd commit d277f75
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/umm_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
UMM_HEAP_INFO ummHeapInfo;

void *umm_info(void *ptr, bool force) {
if (umm_heap == NULL) {
if (UMM_HEAP == NULL) {
umm_init();
}

Expand Down Expand Up @@ -163,7 +163,7 @@ size_t umm_free_heap_size(void) {
#ifndef UMM_INLINE_METRICS
umm_info(NULL, false);
#endif
return (size_t)ummHeapInfo.freeBlocks * sizeof(umm_block);
return (size_t)ummHeapInfo.freeBlocks * UMM_BLOCKSIZE;
}

size_t umm_max_free_block_size(void) {
Expand Down
2 changes: 1 addition & 1 deletion src/umm_integrity.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ bool umm_integrity_check(void) {
uint16_t prev;
uint16_t cur;

if (umm_heap == NULL) {
if (UMM_HEAP == NULL) {
umm_init();
}

Expand Down
38 changes: 24 additions & 14 deletions src/umm_malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,27 @@ UMM_H_ATTPACKPRE typedef struct umm_block_t {

/* ------------------------------------------------------------------------- */

umm_block *umm_heap = NULL;
uint16_t umm_numblocks = 0;
struct umm_heap_config {
umm_block *pheap;
size_t heap_size;
uint16_t numblocks;
};

#define UMM_NUMBLOCKS (umm_numblocks)
struct umm_heap_config umm_heap_current;
// struct umm_heap_config umm_heaps[UMM_NUM_HEAPS];

#define UMM_HEAP (umm_heap_current.pheap)
#define UMM_HEAPSIZE (umm_heap_current.heap_size)
#define UMM_NUMBLOCKS (umm_heap_current.numblocks)

#define UMM_BLOCKSIZE (sizeof(umm_block))
#define UMM_BLOCK_LAST (UMM_NUMBLOCKS - 1)

/* -------------------------------------------------------------------------
* These macros evaluate to the address of the block and data respectively
*/

#define UMM_BLOCK(b) (umm_heap[b])
#define UMM_BLOCK(b) (UMM_HEAP[b])
#define UMM_DATA(b) (UMM_BLOCK(b).body.data)

/* -------------------------------------------------------------------------
Expand Down Expand Up @@ -137,7 +147,7 @@ static uint16_t umm_blocks(size_t size) {

size -= (1 + (sizeof(((umm_block *)0)->body)));

return 2 + size / (sizeof(umm_block));
return 2 + size / (UMM_BLOCKSIZE);
}

/* ------------------------------------------------------------------------ */
Expand Down Expand Up @@ -236,9 +246,9 @@ static uint16_t umm_assimilate_down(uint16_t c, uint16_t freemask) {

void umm_init(void) {
/* init heap pointer and size, and memset it to 0 */
umm_heap = (umm_block *)UMM_MALLOC_CFG_HEAP_ADDR;
umm_numblocks = (UMM_MALLOC_CFG_HEAP_SIZE / sizeof(umm_block));
memset(umm_heap, 0x00, UMM_MALLOC_CFG_HEAP_SIZE);
UMM_HEAP = (umm_block *)UMM_MALLOC_CFG_HEAP_ADDR;
UMM_NUMBLOCKS = (UMM_MALLOC_CFG_HEAP_SIZE / UMM_BLOCKSIZE);
memset(UMM_HEAP, 0x00, UMM_MALLOC_CFG_HEAP_SIZE);

/* setup initial blank heap structure */
UMM_FRAGMENTATION_METRIC_INIT();
Expand Down Expand Up @@ -304,7 +314,7 @@ static void umm_free_core(void *ptr) {

/* Figure out which block we're in. Note the use of truncated division... */

c = (((void *)ptr) - (void *)(&(umm_heap[0]))) / sizeof(umm_block);
c = (((void *)ptr) - (void *)(&(UMM_HEAP[0]))) / UMM_BLOCKSIZE;

DBGLOG_DEBUG("Freeing block %6i\n", c);

Expand Down Expand Up @@ -341,7 +351,7 @@ static void umm_free_core(void *ptr) {

void umm_free(void *ptr) {

if (umm_heap == NULL) {
if (UMM_HEAP == NULL) {
umm_init();
}

Expand Down Expand Up @@ -482,7 +492,7 @@ void *umm_malloc(size_t size) {

void *ptr = NULL;

if (umm_heap == NULL) {
if (UMM_HEAP == NULL) {
umm_init();
}

Expand Down Expand Up @@ -523,7 +533,7 @@ void *umm_realloc(void *ptr, size_t size) {

size_t curSize;

if (umm_heap == NULL) {
if (UMM_HEAP == NULL) {
umm_init();
}

Expand Down Expand Up @@ -568,15 +578,15 @@ void *umm_realloc(void *ptr, size_t size) {

/* Figure out which block we're in. Note the use of truncated division... */

c = (((void *)ptr) - (void *)(&(umm_heap[0]))) / sizeof(umm_block);
c = (((void *)ptr) - (void *)(&(UMM_HEAP[0]))) / UMM_BLOCKSIZE;

/* Figure out how big this block is ... the free bit is not set :-) */

blockSize = (UMM_NBLOCK(c) - c);

/* Figure out how many bytes are in this block */

curSize = (blockSize * sizeof(umm_block)) - (sizeof(((umm_block *)0)->header));
curSize = (blockSize * UMM_BLOCKSIZE) - (sizeof(((umm_block *)0)->header));

/* Protect the critical section... */
UMM_CRITICAL_ENTRY();
Expand Down
15 changes: 15 additions & 0 deletions src/umm_malloc_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
* We have not verified the checks below for 64 bit machines
* because this library is targeted for 32 bit machines.
*
* UMM_NUM_HEAPS
*
* Set to the maximum number of heaps that can be defined by the
* application - defaults to 1.
*
* UMM_BEST_FIT (default)
*
* Set this if you want to use a best-fit algorithm for allocating new blocks.
Expand Down Expand Up @@ -139,6 +144,16 @@

/* -------------------------------------------------------------------------- */

#ifndef UMM_NUM_HEAPS
#define UMM_NUM_HEAPS (1)
#endif

#if (UMM_NUM_HEAPS < 1)
#error UMM_NUM_HEAPS must be at least 1!
#endif

/* -------------------------------------------------------------------------- */

#ifdef UMM_BEST_FIT
#ifdef UMM_FIRST_FIT
#error Both UMM_BEST_FIT and UMM_FIRST_FIT are defined - pick one!
Expand Down
4 changes: 2 additions & 2 deletions src/umm_poison.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ static void *get_unpoisoned(void *ptr) {
ptr -= (sizeof(UMM_POISONED_BLOCK_LEN_TYPE) + UMM_POISON_SIZE_BEFORE);

/* Figure out which block we're in. Note the use of truncated division... */
c = (((void *)ptr) - (void *)(&(umm_heap[0]))) / sizeof(umm_block);
c = (((void *)ptr) - (void *)(&(UMM_HEAP[0]))) / UMM_BLOCKSIZE;

check_poison_block(&UMM_BLOCK(c));
}
Expand Down Expand Up @@ -207,7 +207,7 @@ bool umm_poison_check(void) {
bool ok = true;
unsigned short int cur;

if (umm_heap == NULL) {
if (UMM_HEAP == NULL) {
umm_init();
}

Expand Down

0 comments on commit d277f75

Please sign in to comment.