summaryrefslogtreecommitdiffstats
path: root/common/tlsf.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/tlsf.c')
-rw-r--r--common/tlsf.c476
1 files changed, 307 insertions, 169 deletions
diff --git a/common/tlsf.c b/common/tlsf.c
index ba68a5e688..ba2ed367c0 100644
--- a/common/tlsf.c
+++ b/common/tlsf.c
@@ -1,16 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <linux/stringify.h>
-
-#include "tlsf.h"
+#include <tlsf.h>
#include "tlsfbits.h"
+#include <linux/kasan.h>
-#ifdef DEBUG
-#define tlsf_assert(expr) \
- ((expr) ? (void)(0) : printf("%s\n", __stringify(expr)))
-#else
-#define tlsf_assert(expr) (void)(0)
+#define CHAR_BIT 8
+
+#ifndef CONFIG_KASAN
+#define __memcpy memcpy
#endif
/*
@@ -20,20 +20,18 @@
/* Public constants: may be modified. */
enum tlsf_public
{
- /* log2 of number of linear subdivisions of block sizes. */
+ /* log2 of number of linear subdivisions of block sizes. Larger
+ ** values require more memory in the control structure. Values of
+ ** 4 or 5 are typical.
+ */
SL_INDEX_COUNT_LOG2 = 5,
};
/* Private constants: do not modify. */
enum tlsf_private
{
-#if defined (TLSF_64BIT)
/* All allocation sizes and addresses are aligned to 8 bytes. */
ALIGN_SIZE_LOG2 = 3,
-#else
- /* All allocation sizes and addresses are aligned to 4 bytes. */
- ALIGN_SIZE_LOG2 = 2,
-#endif
ALIGN_SIZE = (1 << ALIGN_SIZE_LOG2),
/*
@@ -87,7 +85,6 @@ enum tlsf_private
#define tlsf_static_assert(exp) \
typedef char _tlsf_glue(static_assert, __LINE__) [(exp) ? 1 : -1]
-#ifndef __BAREBOX__
/* This code has been tested on 32- and 64-bit (LP/LLP) architectures. */
tlsf_static_assert(sizeof(int) * CHAR_BIT == 32);
tlsf_static_assert(sizeof(size_t) * CHAR_BIT >= 32);
@@ -98,7 +95,8 @@ tlsf_static_assert(sizeof(unsigned int) * CHAR_BIT >= SL_INDEX_COUNT);
/* Ensure we've properly tuned our sizes. */
tlsf_static_assert(ALIGN_SIZE == SMALL_BLOCK_SIZE / SL_INDEX_COUNT);
-#endif
+
+tlsf_static_assert(ALIGN_SIZE >= CONFIG_MALLOC_ALIGNMENT);
/*
** Data structures and associated constants.
@@ -121,6 +119,7 @@ typedef struct block_header_t
/* The size of this block, excluding the block header. */
size_t size;
+ u32 : BYTES_TO_BITS(ALIGN_SIZE - sizeof(size_t));
/* Next and previous free blocks. */
struct block_header_t* next_free;
@@ -133,31 +132,32 @@ typedef struct block_header_t
** - bit 0: whether block is busy or free
** - bit 1: whether previous block is busy or free
*/
-static const size_t block_header_free_bit = 1 << 0;
-static const size_t block_header_prev_free_bit = 1 << 1;
+#define block_header_free_bit (1 << 0)
+#define block_header_prev_free_bit (1 << 1)
/*
** The size of the block header exposed to used blocks is the size field.
** The prev_phys_block field is stored *inside* the previous free block.
*/
-static const size_t block_header_overhead = sizeof(size_t);
+#define block_header_shift offsetof(block_header_t, size)
+#define block_header_overhead ALIGN_SIZE
/* User data starts directly after the size field in a used block. */
-static const size_t block_start_offset =
- offsetof(block_header_t, size) + sizeof(size_t);
+#define block_start_offset (block_header_shift + block_header_overhead)
/*
** A free block must be large enough to store its header minus the size of
** the prev_phys_block field, and no larger than the number of addressable
** bits for FL_INDEX.
*/
-static const size_t block_size_min =
- sizeof(block_header_t) - sizeof(block_header_t*);
-static const size_t block_size_max = tlsf_cast(size_t, 1) << FL_INDEX_MAX;
+#define block_size_min (sizeof(block_header_t) - sizeof(block_header_t*))
+#define block_size_max (tlsf_cast(size_t, 1) << FL_INDEX_MAX)
+tlsf_static_assert(block_size_min % ALIGN_SIZE == 0);
+tlsf_static_assert(block_size_max % ALIGN_SIZE == 0);
-/* The TLSF pool structure. */
-typedef struct pool_t
+/* The TLSF control structure. */
+typedef struct control_t
{
/* Empty lists point at this block to indicate they are free. */
block_header_t block_null;
@@ -165,10 +165,12 @@ typedef struct pool_t
/* Bitmaps for free lists. */
unsigned int fl_bitmap;
unsigned int sl_bitmap[FL_INDEX_COUNT];
+ u32 : BYTES_TO_BITS(ALIGN_SIZE - sizeof(size_t));
/* Head of free lists. */
block_header_t* blocks[FL_INDEX_COUNT][SL_INDEX_COUNT];
-} pool_t;
+} control_t;
+tlsf_static_assert(sizeof(control_t) % ALIGN_SIZE == 0);
/* A type used for casting when doing pointer arithmetic. */
typedef ptrdiff_t tlsfptr_t;
@@ -190,7 +192,7 @@ static void block_set_size(block_header_t* block, size_t size)
static int block_is_last(const block_header_t* block)
{
- return 0 == block_size(block);
+ return block_size(block) == 0;
}
static int block_is_free(const block_header_t* block)
@@ -244,6 +246,7 @@ static block_header_t* offset_to_block(const void* ptr, size_t size)
/* Return location of previous block. */
static block_header_t* block_prev(const block_header_t* block)
{
+ tlsf_assert(block_is_prev_free(block) && "previous block must be free");
return block->prev_phys_block;
}
@@ -251,7 +254,7 @@ static block_header_t* block_prev(const block_header_t* block)
static block_header_t* block_next(const block_header_t* block)
{
block_header_t* next = offset_to_block(block_to_ptr(block),
- block_size(block) - block_header_overhead);
+ block_size(block) - block_header_shift);
tlsf_assert(!block_is_last(block));
return next;
}
@@ -306,10 +309,15 @@ static void* align_ptr(const void* ptr, size_t align)
static size_t adjust_request_size(size_t size, size_t align)
{
size_t adjust = 0;
- if (size && size < block_size_max)
+ if (size)
{
const size_t aligned = align_up(size, align);
- adjust = tlsf_max(aligned, block_size_min);
+
+ /* aligned sized must not exceed block_size_max or we'll go out of bounds on sl_bitmap */
+ if (aligned >= size && aligned < block_size_max)
+ {
+ adjust = tlsf_max(aligned, block_size_min);
+ }
}
return adjust;
}
@@ -341,7 +349,7 @@ static void mapping_insert(size_t size, int* fli, int* sli)
/* This version rounds up to the next block size (for allocations) */
static void mapping_search(size_t size, int* fli, int* sli)
{
- if (size >= (1 << SL_INDEX_COUNT_LOG2))
+ if (size >= SMALL_BLOCK_SIZE)
{
const size_t round = (1 << (tlsf_fls_sizet(size) - SL_INDEX_COUNT_LOG2)) - 1;
size += round;
@@ -349,7 +357,7 @@ static void mapping_search(size_t size, int* fli, int* sli)
mapping_insert(size, fli, sli);
}
-static block_header_t* search_suitable_block(pool_t* pool, int* fli, int* sli)
+static block_header_t* search_suitable_block(control_t* control, int* fli, int* sli)
{
int fl = *fli;
int sl = *sli;
@@ -358,31 +366,31 @@ static block_header_t* search_suitable_block(pool_t* pool, int* fli, int* sli)
** First, search for a block in the list associated with the given
** fl/sl index.
*/
- unsigned int sl_map = pool->sl_bitmap[fl] & (~0 << sl);
+ unsigned int sl_map = control->sl_bitmap[fl] & (~0U << sl);
if (!sl_map)
{
/* No block exists. Search in the next largest first-level list. */
- const unsigned int fl_map = pool->fl_bitmap & (~0 << (fl + 1));
+ const unsigned int fl_map = control->fl_bitmap & (~0U << (fl + 1));
if (!fl_map)
{
/* No free blocks available, memory has been exhausted. */
- return NULL;
+ return 0;
}
fl = tlsf_ffs(fl_map);
*fli = fl;
- sl_map = pool->sl_bitmap[fl];
+ sl_map = control->sl_bitmap[fl];
}
tlsf_assert(sl_map && "internal error - second level bitmap is null");
sl = tlsf_ffs(sl_map);
*sli = sl;
/* Return the first block in the free list. */
- return pool->blocks[fl][sl];
+ return control->blocks[fl][sl];
}
/* Remove a free block from the free list.*/
-static void remove_free_block(pool_t* pool, block_header_t* block, int fl, int sl)
+static void remove_free_block(control_t* control, block_header_t* block, int fl, int sl)
{
block_header_t* prev = block->prev_free;
block_header_t* next = block->next_free;
@@ -392,32 +400,32 @@ static void remove_free_block(pool_t* pool, block_header_t* block, int fl, int s
prev->next_free = next;
/* If this block is the head of the free list, set new head. */
- if (pool->blocks[fl][sl] == block)
+ if (control->blocks[fl][sl] == block)
{
- pool->blocks[fl][sl] = next;
+ control->blocks[fl][sl] = next;
/* If the new head is null, clear the bitmap. */
- if (next == &pool->block_null)
+ if (next == &control->block_null)
{
- pool->sl_bitmap[fl] &= ~(1 << sl);
+ control->sl_bitmap[fl] &= ~(1U << sl);
/* If the second bitmap is now empty, clear the fl bitmap. */
- if (!pool->sl_bitmap[fl])
+ if (!control->sl_bitmap[fl])
{
- pool->fl_bitmap &= ~(1 << fl);
+ control->fl_bitmap &= ~(1U << fl);
}
}
}
}
/* Insert a free block into the free block list. */
-static void insert_free_block(pool_t* pool, block_header_t* block, int fl, int sl)
+static void insert_free_block(control_t* control, block_header_t* block, int fl, int sl)
{
- block_header_t* current = pool->blocks[fl][sl];
+ block_header_t* current = control->blocks[fl][sl];
tlsf_assert(current && "free list cannot have a null entry");
tlsf_assert(block && "cannot insert a null entry into the free list");
block->next_free = current;
- block->prev_free = &pool->block_null;
+ block->prev_free = &control->block_null;
current->prev_free = block;
tlsf_assert(block_to_ptr(block) == align_ptr(block_to_ptr(block), ALIGN_SIZE)
@@ -426,25 +434,25 @@ static void insert_free_block(pool_t* pool, block_header_t* block, int fl, int s
** Insert the new block at the head of the list, and mark the first-
** and second-level bitmaps appropriately.
*/
- pool->blocks[fl][sl] = block;
- pool->fl_bitmap |= (1 << fl);
- pool->sl_bitmap[fl] |= (1 << sl);
+ control->blocks[fl][sl] = block;
+ control->fl_bitmap |= (1U << fl);
+ control->sl_bitmap[fl] |= (1U << sl);
}
/* Remove a given block from the free list. */
-static void block_remove(pool_t* pool, block_header_t* block)
+static void block_remove(control_t* control, block_header_t* block)
{
int fl, sl;
mapping_insert(block_size(block), &fl, &sl);
- remove_free_block(pool, block, fl, sl);
+ remove_free_block(control, block, fl, sl);
}
/* Insert a given block into the free list. */
-static void block_insert(pool_t* pool, block_header_t* block)
+static void block_insert(control_t* control, block_header_t* block)
{
int fl, sl;
mapping_insert(block_size(block), &fl, &sl);
- insert_free_block(pool, block, fl, sl);
+ insert_free_block(control, block, fl, sl);
}
static int block_can_split(block_header_t* block, size_t size)
@@ -457,7 +465,7 @@ static block_header_t* block_split(block_header_t* block, size_t size)
{
/* Calculate the amount of space left in the remaining block. */
block_header_t* remaining =
- offset_to_block(block_to_ptr(block), size - block_header_overhead);
+ offset_to_block(block_to_ptr(block), size - block_header_shift);
const size_t remain_size = block_size(block) - (size + block_header_overhead);
@@ -477,7 +485,7 @@ static block_header_t* block_split(block_header_t* block, size_t size)
/* Absorb a free block's storage into an adjacent previous free block. */
static block_header_t* block_absorb(block_header_t* prev, block_header_t* block)
{
- tlsf_assert(!block_is_last(prev) && "previous block can't be last!");
+ tlsf_assert(!block_is_last(prev) && "previous block can't be last");
/* Note: Leaves flags untouched. */
prev->size += block_size(block) + block_header_overhead;
block_link_next(prev);
@@ -485,14 +493,14 @@ static block_header_t* block_absorb(block_header_t* prev, block_header_t* block)
}
/* Merge a just-freed block with an adjacent previous free block. */
-static block_header_t* block_merge_prev(pool_t* pool, block_header_t* block)
+static block_header_t* block_merge_prev(control_t* control, block_header_t* block)
{
if (block_is_prev_free(block))
{
block_header_t* prev = block_prev(block);
tlsf_assert(prev && "prev physical block can't be null");
tlsf_assert(block_is_free(prev) && "prev block is not free though marked as such");
- block_remove(pool, prev);
+ block_remove(control, prev);
block = block_absorb(prev, block);
}
@@ -500,15 +508,15 @@ static block_header_t* block_merge_prev(pool_t* pool, block_header_t* block)
}
/* Merge a just-freed block with an adjacent free block. */
-static block_header_t* block_merge_next(pool_t* pool, block_header_t* block)
+static block_header_t* block_merge_next(control_t* control, block_header_t* block)
{
block_header_t* next = block_next(block);
tlsf_assert(next && "next physical block can't be null");
if (block_is_free(next))
{
- tlsf_assert(!block_is_last(block) && "previous block can't be last!");
- block_remove(pool, next);
+ tlsf_assert(!block_is_last(block) && "previous block can't be last");
+ block_remove(control, next);
block = block_absorb(block, next);
}
@@ -516,7 +524,7 @@ static block_header_t* block_merge_next(pool_t* pool, block_header_t* block)
}
/* Trim any trailing block space off the end of a block, return to pool. */
-static void block_trim_free(pool_t* pool, block_header_t* block, size_t size)
+static void block_trim_free(control_t* control, block_header_t* block, size_t size)
{
tlsf_assert(block_is_free(block) && "block must be free");
if (block_can_split(block, size))
@@ -524,12 +532,12 @@ static void block_trim_free(pool_t* pool, block_header_t* block, size_t size)
block_header_t* remaining_block = block_split(block, size);
block_link_next(block);
block_set_prev_free(remaining_block);
- block_insert(pool, remaining_block);
+ block_insert(control, remaining_block);
}
}
/* Trim any trailing block space off the end of a used block, return to pool. */
-static void block_trim_used(pool_t* pool, block_header_t* block, size_t size)
+static void block_trim_used(control_t* control, block_header_t* block, size_t size, size_t used)
{
tlsf_assert(!block_is_free(block) && "block must be used");
if (block_can_split(block, size))
@@ -538,12 +546,16 @@ static void block_trim_used(pool_t* pool, block_header_t* block, size_t size)
block_header_t* remaining_block = block_split(block, size);
block_set_prev_used(remaining_block);
- remaining_block = block_merge_next(pool, remaining_block);
- block_insert(pool, remaining_block);
+ remaining_block = block_merge_next(control, remaining_block);
+ block_insert(control, remaining_block);
}
+
+ kasan_poison_shadow(&block->size, size + 2 * sizeof(size_t),
+ KASAN_KMALLOC_REDZONE);
+ kasan_unpoison_shadow(block_to_ptr(block), used);
}
-static block_header_t* block_trim_free_leading(pool_t* pool, block_header_t* block, size_t size)
+static block_header_t* block_trim_free_leading(control_t* control, block_header_t* block, size_t size)
{
block_header_t* remaining_block = block;
if (block_can_split(block, size))
@@ -553,59 +565,75 @@ static block_header_t* block_trim_free_leading(pool_t* pool, block_header_t* blo
block_set_prev_free(remaining_block);
block_link_next(block);
- block_insert(pool, block);
+ block_insert(control, block);
}
return remaining_block;
}
-static block_header_t* block_locate_free(pool_t* pool, size_t size)
+static block_header_t* block_locate_free(control_t* control, size_t size)
{
int fl = 0, sl = 0;
- block_header_t* block = NULL;
+ block_header_t* block = 0;
if (size)
{
mapping_search(size, &fl, &sl);
- block = search_suitable_block(pool, &fl, &sl);
+
+ /*
+ ** mapping_search can futz with the size, so for excessively large sizes it can sometimes wind up
+ ** with indices that are off the end of the block array.
+ ** So, we protect against that here, since this is the only callsite of mapping_search.
+ ** Note that we don't need to check sl, since it comes from a modulo operation that guarantees it's always in range.
+ */
+ if (fl < FL_INDEX_COUNT)
+ {
+ block = search_suitable_block(control, &fl, &sl);
+ }
}
if (block)
{
tlsf_assert(block_size(block) >= size);
- remove_free_block(pool, block, fl, sl);
+ remove_free_block(control, block, fl, sl);
}
return block;
}
-static void* block_prepare_used(pool_t* pool, block_header_t* block, size_t size)
+static void* block_prepare_used(control_t* control, block_header_t* block,
+ size_t size, size_t used)
{
- void* p = NULL;
+ void* p = 0;
if (block)
{
- block_trim_free(pool, block, size);
+ tlsf_assert(size && "size must be non-zero");
+ block_trim_free(control, block, size);
block_mark_as_used(block);
p = block_to_ptr(block);
+
+ kasan_poison_shadow(&block->size, size + 2 * sizeof(size_t),
+ KASAN_KMALLOC_REDZONE);
+ kasan_unpoison_shadow(p, used);
}
return p;
}
/* Clear structure and point all empty lists at the null block. */
-static void pool_construct(pool_t* pool)
+static void control_construct(control_t* control)
{
int i, j;
- pool->block_null.next_free = &pool->block_null;
- pool->block_null.prev_free = &pool->block_null;
+ control->block_null.next_free = &control->block_null;
+ control->block_null.prev_free = &control->block_null;
- pool->fl_bitmap = 0;
+ control->fl_bitmap = 0;
for (i = 0; i < FL_INDEX_COUNT; ++i)
{
- pool->sl_bitmap[i] = 0;
+ control->sl_bitmap[i] = 0;
for (j = 0; j < SL_INDEX_COUNT; ++j)
{
- pool->blocks[i][j] = &pool->block_null;
+ control->blocks[i][j] = &control->block_null;
}
}
}
@@ -631,6 +659,7 @@ static void integrity_walker(void* ptr, size_t size, int used, void* user)
const size_t this_block_size = block_size(block);
int status = 0;
+ (void)used;
tlsf_insist(integ->prev_status == this_prev_status && "prev status incorrect");
tlsf_insist(size == this_block_size && "block size incorrect");
@@ -638,27 +667,22 @@ static void integrity_walker(void* ptr, size_t size, int used, void* user)
integ->status += status;
}
-int tlsf_check_heap(tlsf_pool tlsf)
+int tlsf_check(tlsf_t tlsf)
{
int i, j;
- pool_t* pool = tlsf_cast(pool_t*, tlsf);
+ control_t* control = tlsf_cast(control_t*, tlsf);
int status = 0;
- /* Check that the blocks are physically correct. */
- integrity_t integ = { 0, 0 };
- tlsf_walk_heap(tlsf, integrity_walker, &integ);
- status = integ.status;
-
/* Check that the free lists and bitmaps are accurate. */
for (i = 0; i < FL_INDEX_COUNT; ++i)
{
for (j = 0; j < SL_INDEX_COUNT; ++j)
{
- const int fl_map = pool->fl_bitmap & (1 << i);
- const int sl_list = pool->sl_bitmap[i];
- const int sl_map = sl_list & (1 << j);
- const block_header_t* block = pool->blocks[i][j];
+ const int fl_map = control->fl_bitmap & (1U << i);
+ const int sl_list = control->sl_bitmap[i];
+ const int sl_map = sl_list & (1U << j);
+ const block_header_t* block = control->blocks[i][j];
/* Check that first- and second-level lists agree. */
if (!fl_map)
@@ -668,15 +692,15 @@ int tlsf_check_heap(tlsf_pool tlsf)
if (!sl_map)
{
- tlsf_insist(block == &pool->block_null && "block list must be null");
+ tlsf_insist(block == &control->block_null && "block list must be null");
continue;
}
/* Check that there is at least one free block. */
tlsf_insist(sl_list && "no free blocks in second-level map");
- tlsf_insist(block != &pool->block_null && "block should not be null");
+ tlsf_insist(block != &control->block_null && "block should not be null");
- while (block != &pool->block_null)
+ while (block != &control->block_null)
{
int fli, sli;
tlsf_insist(block_is_free(block) && "block should be free");
@@ -703,15 +727,15 @@ static void default_walker(void* ptr, size_t size, int used, void* user)
printf("\t%p %s size: %x (%p)\n", ptr, used ? "used" : "free", (unsigned int)size, block_from_ptr(ptr));
}
-void tlsf_walk_heap(tlsf_pool pool, tlsf_walker walker, void* user)
+void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void* user)
{
- tlsf_walker heap_walker = walker ? walker : default_walker;
+ tlsf_walker pool_walker = walker ? walker : default_walker;
block_header_t* block =
- offset_to_block(pool, sizeof(pool_t) - block_header_overhead);
+ offset_to_block(pool, -(int)block_header_shift);
while (block && !block_is_last(block))
{
- heap_walker(
+ pool_walker(
block_to_ptr(block),
block_size(block),
!block_is_free(block),
@@ -731,31 +755,125 @@ size_t tlsf_block_size(void* ptr)
return size;
}
+int tlsf_check_pool(pool_t pool)
+{
+ /* Check that the blocks are physically correct. */
+ integrity_t integ = { 0, 0 };
+ tlsf_walk_pool(pool, integrity_walker, &integ);
+
+ return integ.status;
+}
+
/*
-** Overhead of the TLSF structures in a given memory block passed to
-** tlsf_create, equal to the size of a pool_t plus overhead of the initial
-** free block and the sentinel block.
+** Size of the TLSF structures in a given memory block passed to
+** tlsf_create, equal to aligned size of a control_t
*/
-size_t tlsf_overhead(void)
+size_t tlsf_size(void)
+{
+ return align_up(sizeof(control_t), ALIGN_SIZE);
+}
+
+size_t tlsf_align_size(void)
{
- const size_t pool_overhead = sizeof(pool_t) + 2 * block_header_overhead;
- return pool_overhead;
+ return ALIGN_SIZE;
+}
+
+size_t tlsf_block_size_min(void)
+{
+ return block_size_min;
+}
+
+size_t tlsf_block_size_max(void)
+{
+ return block_size_max;
}
/*
-** TLSF main interface. Right out of the white paper.
+** Overhead of the TLSF structures in a given memory block passed to
+** tlsf_add_pool, equal to the overhead of a free block and the
+** sentinel block.
*/
+size_t tlsf_pool_overhead(void)
+{
+ return 2 * block_header_overhead;
+}
+
+size_t tlsf_alloc_overhead(void)
+{
+ return block_header_overhead;
+}
-tlsf_pool tlsf_create(void* mem, size_t bytes)
+pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes)
{
block_header_t* block;
block_header_t* next;
- const size_t pool_overhead = tlsf_overhead();
+ const size_t pool_overhead = tlsf_pool_overhead();
const size_t pool_bytes = align_down(bytes - pool_overhead, ALIGN_SIZE);
- pool_t* pool = tlsf_cast(pool_t*, mem);
-#ifdef DEBUG
+ if (((ptrdiff_t)mem % ALIGN_SIZE) != 0)
+ {
+ printf("tlsf_add_pool: Memory must be aligned by %u bytes.\n",
+ (unsigned int)ALIGN_SIZE);
+ return 0;
+ }
+
+ if (pool_bytes < block_size_min || pool_bytes > block_size_max)
+ {
+#if defined (TLSF_64BIT)
+ printf("tlsf_add_pool: Memory size must be between 0x%x and 0x%x00 bytes.\n",
+ (unsigned int)(pool_overhead + block_size_min),
+ (unsigned int)((pool_overhead + block_size_max) / 256));
+#else
+ printf("tlsf_add_pool: Memory size must be between %u and %u bytes.\n",
+ (unsigned int)(pool_overhead + block_size_min),
+ (unsigned int)(pool_overhead + block_size_max));
+#endif
+ return 0;
+ }
+
+ /*
+ ** Create the main free block. Offset the start of the block slightly
+ ** so that the prev_phys_block field falls outside of the pool -
+ ** it will never be used.
+ */
+ block = offset_to_block(mem, -(tlsfptr_t)block_header_shift);
+ block_set_size(block, pool_bytes);
+ block_set_free(block);
+ block_set_prev_used(block);
+ block_insert(tlsf_cast(control_t*, tlsf), block);
+
+ /* Split the block to create a zero-size sentinel block. */
+ next = block_link_next(block);
+ block_set_size(next, 0);
+ block_set_used(next);
+ block_set_prev_free(next);
+
+ return mem;
+}
+
+void tlsf_remove_pool(tlsf_t tlsf, pool_t pool)
+{
+ control_t* control = tlsf_cast(control_t*, tlsf);
+ block_header_t* block = offset_to_block(pool, -(int)block_header_shift);
+
+ int fl = 0, sl = 0;
+
+ tlsf_assert(block_is_free(block) && "block should be free");
+ tlsf_assert(!block_is_free(block_next(block)) && "next block should not be free");
+ tlsf_assert(block_size(block_next(block)) == 0 && "next block size should be zero");
+
+ mapping_insert(block_size(block), &fl, &sl);
+ remove_free_block(control, block, fl, sl);
+}
+
+/*
+** TLSF main interface.
+*/
+
+#ifdef _DEBUG
+int test_ffs_fls()
+{
/* Verify ffs/fls work properly. */
int rv = 0;
rv += (tlsf_ffs(0) == -1) ? 0 : 0x1;
@@ -771,74 +889,79 @@ tlsf_pool tlsf_create(void* mem, size_t bytes)
rv += (tlsf_fls_sizet(0x80000000) == 31) ? 0 : 0x100;
rv += (tlsf_fls_sizet(0x100000000) == 32) ? 0 : 0x200;
rv += (tlsf_fls_sizet(0xffffffffffffffff) == 63) ? 0 : 0x400;
+#endif
+
if (rv)
{
- printf("tlsf_create: %x ffs/fls tests failed!\n", rv);
- return 0;
+ printf("test_ffs_fls: %x ffs/fls tests failed.\n", rv);
}
-#endif
+ return rv;
+}
#endif
- if (pool_bytes < block_size_min || pool_bytes > block_size_max)
+tlsf_t tlsf_create(void* mem)
+{
+#ifdef _DEBUG
+ if (test_ffs_fls())
{
-#if defined (TLSF_64BIT)
- printf("tlsf_create: Pool size must be at least %d bytes.\n",
- (unsigned int)(pool_overhead + block_size_min));
-#else
- printf("tlsf_create: Pool size must be between %u and %u bytes.\n",
- (unsigned int)(pool_overhead + block_size_min),
- (unsigned int)(pool_overhead + block_size_max));
-#endif
- return NULL;
+ return 0;
}
+#endif
- /* Construct a valid pool object. */
- pool_construct(pool);
+ if (((tlsfptr_t)mem % ALIGN_SIZE) != 0)
+ {
+ printf("tlsf_create: Memory must be aligned to %u bytes.\n",
+ (unsigned int)ALIGN_SIZE);
+ return 0;
+ }
- /*
- ** Create the main free block. Offset the start of the block slightly
- ** so that the prev_phys_block field falls inside of the pool
- ** structure - it will never be used.
- */
- block = offset_to_block(
- tlsf_cast(void*, pool), sizeof(pool_t) - block_header_overhead);
- block_set_size(block, pool_bytes);
- block_set_free(block);
- block_set_prev_used(block);
- block_insert(pool, block);
+ control_construct(tlsf_cast(control_t*, mem));
- /* Split the block to create a zero-size pool sentinel block. */
- next = block_link_next(block);
- block_set_size(next, 0);
- block_set_used(next);
- block_set_prev_free(next);
+ return tlsf_cast(tlsf_t, mem);
+}
- return tlsf_cast(tlsf_pool, pool);
+tlsf_t tlsf_create_with_pool(void* mem, size_t bytes)
+{
+ tlsf_t tlsf = tlsf_create(mem);
+ tlsf_add_pool(tlsf, (char*)mem + tlsf_size(), bytes - tlsf_size());
+ kasan_poison_shadow(mem, bytes, KASAN_TAG_INVALID);
+ return tlsf;
}
-void tlsf_destroy(tlsf_pool pool)
+void tlsf_destroy(tlsf_t tlsf)
{
/* Nothing to do. */
- pool = pool;
+ (void)tlsf;
}
-void* tlsf_malloc(tlsf_pool tlsf, size_t size)
+pool_t tlsf_get_pool(tlsf_t tlsf)
{
- pool_t* pool = tlsf_cast(pool_t*, tlsf);
+ return tlsf_cast(pool_t, (char*)tlsf + tlsf_size());
+}
+
+void* tlsf_malloc(tlsf_t tlsf, size_t size)
+{
+ control_t* control = tlsf_cast(control_t*, tlsf);
const size_t adjust = adjust_request_size(size, ALIGN_SIZE);
- block_header_t* block = block_locate_free(pool, adjust);
- return block_prepare_used(pool, block, adjust);
+ block_header_t* block;
+
+ if (!adjust)
+ return NULL;
+
+ block = block_locate_free(control, adjust);
+
+ return block_prepare_used(control, block, adjust, size);
}
-void* tlsf_memalign(tlsf_pool tlsf, size_t align, size_t size)
+void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t size)
{
- pool_t* pool = tlsf_cast(pool_t*, tlsf);
+ control_t* control = tlsf_cast(control_t*, tlsf);
const size_t adjust = adjust_request_size(size, ALIGN_SIZE);
/*
** We must allocate an additional minimum block size bytes so that if
** our free block will leave an alignment gap which is smaller, we can
- ** trim a leading free block and release it back to the heap. We must
+ ** trim a leading free block and release it back to the pool. We must
** do this because the previous physical block is in use, therefore
** the prev_phys_block field is not valid, and we can't simply adjust
** the size of that block.
@@ -846,13 +969,21 @@ void* tlsf_memalign(tlsf_pool tlsf, size_t align, size_t size)
const size_t gap_minimum = sizeof(block_header_t);
const size_t size_with_gap = adjust_request_size(adjust + align + gap_minimum, align);
- /* If alignment is less than or equals base alignment, we're done. */
- const size_t aligned_size = (align <= ALIGN_SIZE) ? adjust : size_with_gap;
+ /*
+ ** If alignment is less than or equals base alignment, we're done.
+ ** If we requested 0 bytes, return null, as tlsf_malloc(0) does.
+ */
+ const size_t aligned_size = (adjust && align > ALIGN_SIZE) ? size_with_gap : adjust;
+
+ block_header_t* block;
+
+ if (!adjust || !size_with_gap)
+ return NULL;
- block_header_t* block = block_locate_free(pool, aligned_size);
+ block = block_locate_free(control, aligned_size);
/* This can't be a static assert. */
- tlsf_assert(sizeof(block_header_t) == block_size_min + block_header_overhead);
+ tlsf_assert(sizeof(block_header_t) == block_size_min + block_header_shift);
if (block)
{
@@ -877,24 +1008,26 @@ void* tlsf_memalign(tlsf_pool tlsf, size_t align, size_t size)
if (gap)
{
tlsf_assert(gap >= gap_minimum && "gap size too small");
- block = block_trim_free_leading(pool, block, gap);
+ block = block_trim_free_leading(control, block, gap);
}
}
- return block_prepare_used(pool, block, adjust);
+ return block_prepare_used(control, block, adjust, size);
}
-void tlsf_free(tlsf_pool tlsf, void* ptr)
+void tlsf_free(tlsf_t tlsf, void* ptr)
{
/* Don't attempt to free a NULL pointer. */
if (ptr)
{
- pool_t* pool = tlsf_cast(pool_t*, tlsf);
+ control_t* control = tlsf_cast(control_t*, tlsf);
block_header_t* block = block_from_ptr(ptr);
+ tlsf_assert(!block_is_free(block) && "block already marked as free");
+ kasan_poison_shadow(ptr, block_size(block), 0xff);
block_mark_as_free(block);
- block = block_merge_prev(pool, block);
- block = block_merge_next(pool, block);
- block_insert(pool, block);
+ block = block_merge_prev(control, block);
+ block = block_merge_next(control, block);
+ block_insert(control, block);
}
}
@@ -911,10 +1044,10 @@ void tlsf_free(tlsf_pool tlsf, void* ptr)
** - an extended buffer size will leave the newly-allocated area with
** contents undefined
*/
-void* tlsf_realloc(tlsf_pool tlsf, void* ptr, size_t size)
+void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size)
{
- pool_t* pool = tlsf_cast(pool_t*, tlsf);
- void* p = NULL;
+ control_t* control = tlsf_cast(control_t*, tlsf);
+ void* p = 0;
/* Zero-size requests are treated as free. */
if (ptr && size == 0)
@@ -935,6 +1068,11 @@ void* tlsf_realloc(tlsf_pool tlsf, void* ptr, size_t size)
const size_t combined = cursize + block_size(next) + block_header_overhead;
const size_t adjust = adjust_request_size(size, ALIGN_SIZE);
+ tlsf_assert(!block_is_free(block) && "block already marked as free");
+
+ if (!adjust)
+ return NULL;
+
/*
** If the next block is used, or when combined with the current
** block, does not offer enough space, we must reallocate and copy.
@@ -945,7 +1083,7 @@ void* tlsf_realloc(tlsf_pool tlsf, void* ptr, size_t size)
if (p)
{
const size_t minsize = tlsf_min(cursize, size);
- memcpy(p, ptr, minsize);
+ __memcpy(p, ptr, minsize);
tlsf_free(tlsf, ptr);
}
}
@@ -954,12 +1092,12 @@ void* tlsf_realloc(tlsf_pool tlsf, void* ptr, size_t size)
/* Do we need to expand to the next block? */
if (adjust > cursize)
{
- block_merge_next(pool, block);
+ block_merge_next(control, block);
block_mark_as_used(block);
}
/* Trim the resulting block and return the original pointer. */
- block_trim_used(pool, block, adjust);
+ block_trim_used(control, block, adjust, size);
p = ptr;
}
}