mirror of
https://github.com/espressif/tlsf.git
synced 2025-11-16 04:24:45 +00:00
tlsf: update calculation of fl index max
The calculation of fl index max is changed to always be the smallest number that includes the size of the registered memory. The control_construct() function now checks for minimum size as the control structure parameters are calculated. There is no longer a minimum configuration for fl index max so the tlsf_config enum is striped down to remove unecessary compile time values. the tlsf_size() function will fail if no tlsf pointer is passed as parameter since there is no way to calculate a default tlsf size anymore.
This commit is contained in:
70
tlsf.c
70
tlsf.c
@@ -584,16 +584,19 @@ static inline __attribute__((always_inline)) void* block_prepare_used(control_t*
|
||||
}
|
||||
|
||||
/* Clear structure and point all empty lists at the null block. */
|
||||
static void control_construct(control_t* control, size_t bytes)
|
||||
static control_t* control_construct(control_t* control, size_t bytes)
|
||||
{
|
||||
int i, j;
|
||||
|
||||
control->block_null.next_free = &control->block_null;
|
||||
control->block_null.prev_free = &control->block_null;
|
||||
// check that the requested size can at least hold the control_t. This will allow us
|
||||
// to fill in the field of control_t necessary to determine the final size of
|
||||
// the metadata overhead and check that the requested size can hold
|
||||
// this data and at least a block of minimum size
|
||||
if (bytes < sizeof(control_t))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Find the closest power of two for first layer */
|
||||
i = (bytes - 1) / (16 * 1024);
|
||||
control->fl_index_max = FL_INDEX_MAX_MIN + sizeof(i) * 8 - __builtin_clz(i);
|
||||
control->fl_index_max = 32 - __builtin_clz(bytes);
|
||||
|
||||
/* Adapt second layer to the pool */
|
||||
if (bytes <= 16 * 1024) control->sl_index_count_log2 = 3;
|
||||
@@ -604,11 +607,26 @@ static void control_construct(control_t* control, size_t bytes)
|
||||
control->sl_index_count = 1 << control->sl_index_count_log2;
|
||||
control->fl_index_count = control->fl_index_max - control->fl_index_shift + 1;
|
||||
control->small_block_size = 1 << control->fl_index_shift;
|
||||
|
||||
// the total size fo the metadata overhead is the size of the control_t
|
||||
// added to the size of the sl_bitmaps and the size of blocks
|
||||
control->size = sizeof(control_t) + (sizeof(*control->sl_bitmap) * control->fl_index_count) +
|
||||
(sizeof(*control->blocks) * (control->fl_index_count * control->sl_index_count));
|
||||
|
||||
// check that the requested size can hold the whole control structure and
|
||||
// a small block at least
|
||||
if (bytes < control->size + block_size_min)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
control->block_null.next_free = &control->block_null;
|
||||
control->block_null.prev_free = &control->block_null;
|
||||
|
||||
control->fl_bitmap = 0;
|
||||
control->sl_bitmap = align_ptr(control + 1, sizeof(*control->sl_bitmap));
|
||||
control->blocks = align_ptr(control->sl_bitmap + control->fl_index_count, sizeof(*control->blocks));
|
||||
control->size = (void*) (control->blocks + control->sl_index_count * control->fl_index_count) - (void*) control;
|
||||
|
||||
|
||||
/* SL_INDEX_COUNT must be <= number of bits in sl_bitmap's storage type. */
|
||||
tlsf_assert(sizeof(unsigned int) * CHAR_BIT >= control->sl_index_count); //CHAR_BIT less than sl_index_count");
|
||||
@@ -616,14 +634,16 @@ static void control_construct(control_t* control, size_t bytes)
|
||||
/* Ensure we've properly tuned our sizes. */
|
||||
tlsf_assert(ALIGN_SIZE == control->small_block_size / control->sl_index_count); //ALIGN_SIZE does not match");
|
||||
|
||||
for (i = 0; i < control->fl_index_count; ++i)
|
||||
for (int i = 0; i < control->fl_index_count; ++i)
|
||||
{
|
||||
control->sl_bitmap[i] = 0;
|
||||
for (j = 0; j < control->sl_index_count; ++j)
|
||||
for (int j = 0; j < control->sl_index_count; ++j)
|
||||
{
|
||||
control->blocks[i * control->sl_index_count + j] = &control->block_null;
|
||||
}
|
||||
}
|
||||
|
||||
return control;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -791,16 +811,12 @@ size_t tlsf_fit_size(tlsf_t tlsf, size_t size)
|
||||
*/
|
||||
size_t tlsf_size(tlsf_t tlsf)
|
||||
{
|
||||
if (tlsf)
|
||||
if (tlsf == NULL)
|
||||
{
|
||||
control_t* control = tlsf_cast(control_t*, tlsf);
|
||||
return control->size;
|
||||
}
|
||||
|
||||
/* no tlsf, we'll just return a min size */
|
||||
return sizeof(control_t) +
|
||||
sizeof(int) * SL_INDEX_COUNT_MIN +
|
||||
sizeof(block_header_t*) * SL_INDEX_COUNT_MIN * FL_INDEX_COUNT_MIN;
|
||||
return 0;
|
||||
}
|
||||
control_t* control = tlsf_cast(control_t*, tlsf);
|
||||
return control->size;
|
||||
}
|
||||
|
||||
size_t tlsf_align_size(void)
|
||||
@@ -946,15 +962,17 @@ tlsf_t tlsf_create(void* mem, size_t max_bytes)
|
||||
return 0;
|
||||
}
|
||||
|
||||
control_construct(tlsf_cast(control_t*, mem), max_bytes);
|
||||
|
||||
return tlsf_cast(tlsf_t, mem);
|
||||
control_t* control_ptr = control_construct(tlsf_cast(control_t*, mem), max_bytes);
|
||||
return tlsf_cast(tlsf_t, control_ptr);
|
||||
}
|
||||
|
||||
tlsf_t tlsf_create_with_pool(void* mem, size_t pool_bytes, size_t max_bytes)
|
||||
{
|
||||
tlsf_t tlsf = tlsf_create(mem, max_bytes ? max_bytes : pool_bytes);
|
||||
tlsf_add_pool(tlsf, (char*)mem + tlsf_size(tlsf), pool_bytes - tlsf_size(tlsf));
|
||||
if (tlsf != NULL)
|
||||
{
|
||||
tlsf_add_pool(tlsf, (char*)mem + tlsf_size(tlsf), pool_bytes - tlsf_size(tlsf));
|
||||
}
|
||||
return tlsf;
|
||||
}
|
||||
|
||||
@@ -1133,6 +1151,12 @@ void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size)
|
||||
const size_t combined = cursize + block_size(next) + block_header_overhead;
|
||||
const size_t adjust = adjust_request_size(tlsf, size, ALIGN_SIZE);
|
||||
|
||||
// if adjust if equal to 0, the size is too big
|
||||
if (adjust == 0)
|
||||
{
|
||||
return p;
|
||||
}
|
||||
|
||||
tlsf_assert(!block_is_free(block) && "block already marked as free");
|
||||
|
||||
/*
|
||||
|
||||
@@ -35,35 +35,9 @@ extern "C" {
|
||||
|
||||
enum tlsf_config
|
||||
{
|
||||
/* 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, 3 is for very small pools.
|
||||
*/
|
||||
SL_INDEX_COUNT_LOG2_MIN = 3,
|
||||
|
||||
/* All allocation sizes and addresses are aligned to 4 bytes. */
|
||||
ALIGN_SIZE_LOG2 = 2,
|
||||
ALIGN_SIZE = (1 << ALIGN_SIZE_LOG2),
|
||||
|
||||
/*
|
||||
** We support allocations of sizes up to (1 << FL_INDEX_MAX) bits.
|
||||
** However, because we linearly subdivide the second-level lists, and
|
||||
** our minimum size granularity is 4 bytes, it doesn't make sense to
|
||||
** create first-level lists for sizes smaller than SL_INDEX_COUNT * 4,
|
||||
** or (1 << (SL_INDEX_COUNT_LOG2 + 2)) bytes, as there we will be
|
||||
** trying to split size ranges into more slots than we have available.
|
||||
** Instead, we calculate the minimum threshold size, and place all
|
||||
** blocks below that size into the 0th first-level list.
|
||||
** Values below are the absolute minimum to accept a pool addition.
|
||||
*/
|
||||
|
||||
/* Tunning the first level, we can reduce TLSF pool overhead
|
||||
* in exchange of manage a pool smaller than 4GB
|
||||
*/
|
||||
FL_INDEX_MAX_MIN = 14,
|
||||
SL_INDEX_COUNT_MIN = (1 << SL_INDEX_COUNT_LOG2_MIN),
|
||||
FL_INDEX_SHIFT_MIN = (SL_INDEX_COUNT_LOG2_MIN + ALIGN_SIZE_LOG2),
|
||||
FL_INDEX_COUNT_MIN = (FL_INDEX_MAX_MIN - FL_INDEX_SHIFT_MIN + 1),
|
||||
};
|
||||
|
||||
/*
|
||||
@@ -112,7 +86,6 @@ static const size_t block_start_offset =
|
||||
*/
|
||||
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_MIN;
|
||||
|
||||
/* The TLSF control structure. */
|
||||
typedef struct control_t
|
||||
|
||||
Reference in New Issue
Block a user