1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015 |
- #include "multi_heap_config.h"
- #include "multi_heap.h"
- #include "multi_heap_internal.h"
- #include "heap_tlsf_config.h"
- #include "heap_tlsf.h"
- #include "esp_log.h"
- typedef struct control_t
- {
-
- block_header_t block_null;
-
-
- unsigned int fl_index_count;
- unsigned int fl_index_shift;
- unsigned int fl_index_max;
- unsigned int sl_index_count;
- unsigned int sl_index_count_log2;
- unsigned int small_block_size;
- size_t size;
-
- unsigned int fl_bitmap;
- unsigned int *sl_bitmap;
-
- block_header_t** blocks;
- } control_t;
- static inline __attribute__((__always_inline__)) int tlsf_ffs(unsigned int word)
- {
- const unsigned int reverse = word & (~word + 1);
- const int bit = 32 - __builtin_clz(reverse);
- return bit - 1;
- }
- static inline __attribute__((__always_inline__)) int tlsf_fls(unsigned int word)
- {
- const int bit = word ? 32 - __builtin_clz(word) : 0;
- return bit - 1;
- }
- #if !defined (tlsf_assert)
- #define tlsf_assert assert
- #endif
- #define _tlsf_glue2(x, y) x ## y
- #define _tlsf_glue(x, y) _tlsf_glue2(x, y)
- #define tlsf_static_assert(exp) \
- typedef char _tlsf_glue(static_assert, __LINE__) [(exp) ? 1 : -1]
- tlsf_static_assert(sizeof(int) * CHAR_BIT == 32);
- tlsf_static_assert(sizeof(size_t) * CHAR_BIT >= 32);
- tlsf_static_assert(sizeof(size_t) * CHAR_BIT <= 64);
- static inline __attribute__((__always_inline__)) size_t align_up(size_t x, size_t align)
- {
- tlsf_assert(0 == (align & (align - 1)) && "must align to a power of two");
- return (x + (align - 1)) & ~(align - 1);
- }
- static inline __attribute__((__always_inline__)) size_t align_down(size_t x, size_t align)
- {
- tlsf_assert(0 == (align & (align - 1)) && "must align to a power of two");
- return x - (x & (align - 1));
- }
- static inline __attribute__((__always_inline__)) void* align_ptr(const void* ptr, size_t align)
- {
- const tlsfptr_t aligned =
- (tlsf_cast(tlsfptr_t, ptr) + (align - 1)) & ~(align - 1);
- tlsf_assert(0 == (align & (align - 1)) && "must align to a power of two");
- return tlsf_cast(void*, aligned);
- }
- static inline __attribute__((__always_inline__)) size_t adjust_request_size(tlsf_t tlsf, size_t size, size_t align)
- {
- size_t adjust = 0;
- if (size)
- {
- const size_t aligned = align_up(size, align);
-
- if (aligned < tlsf_block_size_max(tlsf))
- {
- adjust = tlsf_max(aligned, block_size_min);
- }
- }
- return adjust;
- }
- static inline __attribute__((__always_inline__)) void mapping_insert(control_t *control, size_t size, int* fli, int* sli)
- {
- int fl, sl;
- if (size < control->small_block_size)
- {
-
- fl = 0;
- sl = tlsf_cast(int, size) >> 2;
- }
- else
- {
- fl = tlsf_fls(size);
- sl = tlsf_cast(int, size >> (fl - control->sl_index_count_log2)) ^ (1 << control->sl_index_count_log2);
- fl -= (control->fl_index_shift - 1);
- }
- *fli = fl;
- *sli = sl;
- }
- static inline __attribute__((__always_inline__)) void mapping_search(control_t *control, size_t size, int* fli, int* sli)
- {
- if (size >= control->small_block_size)
- {
- const size_t round = (1 << (tlsf_fls(size) - control->sl_index_count_log2)) - 1;
- size += round;
- }
- mapping_insert(control, size, fli, sli);
- }
- static inline __attribute__((__always_inline__)) block_header_t* search_suitable_block(control_t* control, int* fli, int* sli)
- {
- int fl = *fli;
- int sl = *sli;
-
- unsigned int sl_map = control->sl_bitmap[fl] & (~0U << sl);
- if (!sl_map)
- {
-
- const unsigned int fl_map = control->fl_bitmap & (~0U << (fl + 1));
- if (!fl_map)
- {
-
- return 0;
- }
- fl = tlsf_ffs(fl_map);
- *fli = 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 control->blocks[fl*control->sl_index_count + sl];
- }
- static inline __attribute__((__always_inline__)) 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;
- tlsf_assert(prev && "prev_free field can not be null");
- tlsf_assert(next && "next_free field can not be null");
- next->prev_free = prev;
- prev->next_free = next;
-
- if (control->blocks[fl*control->sl_index_count + sl] == block)
- {
- control->blocks[fl*control->sl_index_count + sl] = next;
-
- if (next == &control->block_null)
- {
- control->sl_bitmap[fl] &= ~(1 << sl);
-
- if (!control->sl_bitmap[fl])
- {
- control->fl_bitmap &= ~(1 << fl);
- }
- }
- }
- }
- static inline __attribute__((__always_inline__)) void insert_free_block(control_t* control, block_header_t* block, int fl, int sl)
- {
- block_header_t* current = control->blocks[fl*control->sl_index_count + 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 = &control->block_null;
- current->prev_free = block;
- tlsf_assert(block_to_ptr(block) == align_ptr(block_to_ptr(block), ALIGN_SIZE)
- && "block not aligned properly");
-
- control->blocks[fl*control->sl_index_count + sl] = block;
- control->fl_bitmap |= (1 << fl);
- control->sl_bitmap[fl] |= (1 << sl);
- }
- static inline __attribute__((__always_inline__)) void block_remove(control_t* control, block_header_t* block)
- {
- int fl, sl;
- mapping_insert(control, block_size(block), &fl, &sl);
- remove_free_block(control, block, fl, sl);
- }
- static inline __attribute__((__always_inline__)) void block_insert(control_t* control, block_header_t* block)
- {
- int fl, sl;
- mapping_insert(control, block_size(block), &fl, &sl);
- insert_free_block(control, block, fl, sl);
- }
- static inline __attribute__((__always_inline__)) int block_can_split(block_header_t* block, size_t size)
- {
- return block_size(block) >= sizeof(block_header_t) + size;
- }
- static inline __attribute__((__always_inline__)) block_header_t* block_split(block_header_t* block, size_t size)
- {
-
- block_header_t* remaining =
- offset_to_block(block_to_ptr(block), size - block_header_overhead);
-
- const size_t remain_size = block_size(block) - (size + block_header_overhead);
- tlsf_assert(block_to_ptr(remaining) == align_ptr(block_to_ptr(remaining), ALIGN_SIZE)
- && "remaining block not aligned properly");
- tlsf_assert(block_size(block) == remain_size + size + block_header_overhead);
- block_set_size(remaining, remain_size);
- tlsf_assert(block_size(remaining) >= block_size_min && "block split with invalid size");
- block_set_size(block, size);
- block_mark_as_free(remaining);
-
- return remaining;
- }
- static inline __attribute__((__always_inline__)) 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");
-
- prev->size += block_size(block) + block_header_overhead;
- block_link_next(prev);
- #ifdef MULTI_HEAP_POISONING_SLOW
-
- multi_heap_internal_poison_fill_region(block, sizeof(block_header_t), true );
- #endif
- return prev;
- }
- static inline __attribute__((__always_inline__)) 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(control, prev);
- block = block_absorb(prev, block);
- }
- return block;
- }
- static inline __attribute__((__always_inline__)) 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(control, next);
- block = block_absorb(block, next);
- }
- return block;
- }
- static inline __attribute__((__always_inline__)) 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))
- {
- block_header_t* remaining_block = block_split(block, size);
- block_link_next(block);
- block_set_prev_free(remaining_block);
- block_insert(control, remaining_block);
- }
- }
- static inline __attribute__((__always_inline__)) void block_trim_used(control_t* control, block_header_t* block, size_t size)
- {
- tlsf_assert(!block_is_free(block) && "block must be used");
- if (block_can_split(block, size))
- {
-
- block_header_t* remaining_block = block_split(block, size);
- block_set_prev_used(remaining_block);
- remaining_block = block_merge_next(control, remaining_block);
- block_insert(control, remaining_block);
- }
- }
- static inline __attribute__((__always_inline__)) 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))
- {
-
- remaining_block = block_split(block, size - block_header_overhead);
-
- block_set_prev_free(remaining_block);
- block_link_next(block);
-
- block_insert(control, block);
- }
- return remaining_block;
- }
- static inline __attribute__((__always_inline__)) block_header_t* block_locate_free(control_t* control, size_t size)
- {
- int fl = 0, sl = 0;
- block_header_t* block = 0;
- if (size)
- {
- mapping_search(control, size, &fl, &sl);
-
- if (fl < control->fl_index_count)
- {
- block = search_suitable_block(control, &fl, &sl);
- }
- }
- if (block)
- {
- tlsf_assert(block_size(block) >= size);
- remove_free_block(control, block, fl, sl);
- }
- return block;
- }
- static inline __attribute__((__always_inline__)) void* block_prepare_used(control_t* control, block_header_t* block, size_t size)
- {
- void* p = 0;
- if (block)
- {
- tlsf_assert(size && "size must be non-zero");
- block_trim_free(control, block, size);
- block_mark_as_used(block);
- p = block_to_ptr(block);
- }
- return p;
- }
- static void 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;
-
- i = (bytes - 1) / (16 * 1024);
- control->fl_index_max = FL_INDEX_MAX_MIN + sizeof(i) * 8 - __builtin_clz(i);
-
- if (bytes <= 16 * 1024) control->sl_index_count_log2 = 3;
- else if (bytes <= 256 * 1024) control->sl_index_count_log2 = 4;
- else control->sl_index_count_log2 = 5;
-
- control->fl_index_shift = (control->sl_index_count_log2 + ALIGN_SIZE_LOG2);
- 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;
- 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;
-
- ESP_EARLY_LOGW( "REMOVE", "NEW POOL of %d bytes, ctrl_size: %d sli_c:%d fli_c:%d small_b %d max_b:%d",
- bytes,
- control->size, control->sl_index_count, control->fl_index_count,
- control->small_block_size, 1 << control->fl_index_max );
-
- tlsf_assert(sizeof(unsigned int) * CHAR_BIT >= control->sl_index_count && "CHAR_BIT less than sl_index_count");
-
- 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)
- {
- control->sl_bitmap[i] = 0;
- for (j = 0; j < control->sl_index_count; ++j)
- {
- control->blocks[i*control->sl_index_count + j] = &control->block_null;
- }
- }
- }
- typedef struct integrity_t
- {
- int prev_status;
- int status;
- } integrity_t;
- #define tlsf_insist(x) { tlsf_assert(x); if (!(x)) { status--; } }
- static void integrity_walker(void* ptr, size_t size, int used, void* user)
- {
- block_header_t* block = block_from_ptr(ptr);
- integrity_t* integ = tlsf_cast(integrity_t*, user);
- const int this_prev_status = block_is_prev_free(block) ? 1 : 0;
- const int this_status = block_is_free(block) ? 1 : 0;
- 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");
- integ->prev_status = this_status;
- integ->status += status;
- }
- int tlsf_check(tlsf_t tlsf)
- {
- int i, j;
- control_t* control = tlsf_cast(control_t*, tlsf);
- int status = 0;
-
- for (i = 0; i < control->fl_index_count; ++i)
- {
- for (j = 0; j < control->sl_index_count; ++j)
- {
- const int fl_map = control->fl_bitmap & (1 << i);
- const int sl_list = control->sl_bitmap[i];
- const int sl_map = sl_list & (1 << j);
- const block_header_t* block = control->blocks[i*control->sl_index_count + j];
-
- if (!fl_map)
- {
- tlsf_insist(!sl_map && "second-level map must be null");
- }
- if (!sl_map)
- {
- tlsf_insist(block == &control->block_null && "block list must be null");
- continue;
- }
-
- tlsf_insist(sl_list && "no free blocks in second-level map");
- tlsf_insist(block != &control->block_null && "block should not be null");
- while (block != &control->block_null)
- {
- int fli, sli;
- tlsf_insist(block_is_free(block) && "block should be free");
- tlsf_insist(!block_is_prev_free(block) && "blocks should have coalesced");
- tlsf_insist(!block_is_free(block_next(block)) && "blocks should have coalesced");
- tlsf_insist(block_is_prev_free(block_next(block)) && "block should be free");
- tlsf_insist(block_size(block) >= block_size_min && "block not minimum size");
- mapping_insert(control, block_size(block), &fli, &sli);
- tlsf_insist(fli == i && sli == j && "block size indexed in wrong list");
- block = block->next_free;
- }
- }
- }
- return status;
- }
- #undef tlsf_insist
- static void default_walker(void* ptr, size_t size, int used, void* user)
- {
- (void)user;
- printf("\t%p %s size: %x (%p)\n", ptr, used ? "used" : "free", (unsigned int)size, block_from_ptr(ptr));
- }
- void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void* user)
- {
- tlsf_walker pool_walker = walker ? walker : default_walker;
- block_header_t* block =
- offset_to_block(pool, -(int)block_header_overhead);
- while (block && !block_is_last(block))
- {
- pool_walker(
- block_to_ptr(block),
- block_size(block),
- !block_is_free(block),
- user);
- block = block_next(block);
- }
- }
- size_t tlsf_block_size(void* ptr)
- {
- size_t size = 0;
- if (ptr)
- {
- const block_header_t* block = block_from_ptr(ptr);
- size = block_size(block);
- }
- return size;
- }
- int tlsf_check_pool(pool_t pool)
- {
-
- integrity_t integ = { 0, 0 };
- tlsf_walk_pool(pool, integrity_walker, &integ);
- return integ.status;
- }
- size_t tlsf_fit_size(tlsf_t tlsf, size_t size)
- {
-
- if (size)
- {
- control_t* control = tlsf_cast(control_t*, tlsf);
- size_t sl_interval = (1 << ((sizeof(size_t) * 8 - 1) - __builtin_clz(size))) / control->sl_index_count;
- return size & ~(sl_interval - 1);
- }
-
- return 0;
- }
- size_t tlsf_size(tlsf_t tlsf)
- {
- if (tlsf)
- {
- control_t* control = tlsf_cast(control_t*, tlsf);
- return control->size;
- }
-
-
- return sizeof(control_t) +
- sizeof(int) * SL_INDEX_COUNT_MIN +
- sizeof(block_header_t*) * SL_INDEX_COUNT_MIN * FL_INDEX_COUNT_MIN;
- }
- size_t tlsf_align_size(void)
- {
- return ALIGN_SIZE;
- }
- size_t tlsf_block_size_min(void)
- {
- return block_size_min;
- }
- size_t tlsf_block_size_max(tlsf_t tlsf)
- {
- control_t* control = tlsf_cast(control_t*, tlsf);
- return tlsf_cast(size_t, 1) << control->fl_index_max;
- }
- size_t tlsf_pool_overhead(void)
- {
- return 2 * block_header_overhead;
- }
- size_t tlsf_alloc_overhead(void)
- {
- return block_header_overhead;
- }
- 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_pool_overhead();
- const size_t pool_bytes = align_down(bytes - pool_overhead, ALIGN_SIZE);
- 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 > tlsf_block_size_max(tlsf))
- {
- #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 + tlsf_block_size_max(tlsf)) / 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 + tlsf_block_size_max(tlsf)));
- #endif
- return 0;
- }
-
- block = offset_to_block(mem, -(tlsfptr_t)block_header_overhead);
- block_set_size(block, pool_bytes);
- block_set_free(block);
- block_set_prev_used(block);
- block_insert(tlsf_cast(control_t*, tlsf), 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_overhead);
- 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(control, block_size(block), &fl, &sl);
- remove_free_block(control, block, fl, sl);
- }
- tlsf_t tlsf_create(void* mem, size_t max_bytes)
- {
- #if _DEBUG
- if (test_ffs_fls())
- {
- return 0;
- }
- #endif
- if (((tlsfptr_t)mem % ALIGN_SIZE) != 0)
- {
- printf("tlsf_create: Memory must be aligned to %u bytes.\n",
- (unsigned int)ALIGN_SIZE);
- return 0;
- }
- control_construct(tlsf_cast(control_t*, mem), max_bytes);
- return tlsf_cast(tlsf_t, mem);
- }
- pool_t tlsf_get_pool(tlsf_t tlsf)
- {
- return tlsf_cast(pool_t, (char*)tlsf + tlsf_size(tlsf));
- }
- 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));
- return tlsf;
- }
- void* tlsf_malloc(tlsf_t tlsf, size_t size)
- {
- control_t* control = tlsf_cast(control_t*, tlsf);
- size_t adjust = adjust_request_size(tlsf, size, ALIGN_SIZE);
- block_header_t* block = block_locate_free(control, adjust);
- return block_prepare_used(control, block, adjust);
- }
- void* tlsf_memalign_offs(tlsf_t tlsf, size_t align, size_t size, size_t data_offset)
- {
- control_t* control = tlsf_cast(control_t*, tlsf);
- const size_t adjust = adjust_request_size(tlsf, size, ALIGN_SIZE);
- const size_t off_adjust = align_up(data_offset, ALIGN_SIZE);
-
- const size_t gap_minimum = sizeof(block_header_t) + off_adjust;
-
- const size_t size_with_gap = adjust_request_size(tlsf, adjust + align + gap_minimum - off_adjust, align);
-
- const size_t aligned_size = (adjust && align > ALIGN_SIZE) ? size_with_gap : adjust;
- block_header_t* block = block_locate_free(control, aligned_size);
-
- tlsf_assert(sizeof(block_header_t) == block_size_min + block_header_overhead);
- if (block)
- {
- void* ptr = block_to_ptr(block);
- void* aligned = align_ptr(ptr, align);
- size_t gap = tlsf_cast(size_t,
- tlsf_cast(tlsfptr_t, aligned) - tlsf_cast(tlsfptr_t, ptr));
-
- if ((gap && gap < gap_minimum) || (!gap && off_adjust))
- {
- const size_t gap_remain = gap_minimum - gap;
- const size_t offset = tlsf_max(gap_remain, align);
- const void* next_aligned = tlsf_cast(void*,
- tlsf_cast(tlsfptr_t, aligned) + offset);
- aligned = align_ptr(next_aligned, align);
- gap = tlsf_cast(size_t,
- tlsf_cast(tlsfptr_t, aligned) - tlsf_cast(tlsfptr_t, ptr));
- }
- if (gap)
- {
- tlsf_assert(gap >= gap_minimum && "gap size too small");
- block = block_trim_free_leading(control, block, gap - off_adjust);
- }
- }
-
- return block_prepare_used(control, block, adjust);
- }
- void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t size)
- {
- return tlsf_memalign_offs(tlsf, align, size, 0);
- }
- void tlsf_free(tlsf_t tlsf, void* ptr)
- {
-
- if (ptr)
- {
- 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");
- block_mark_as_free(block);
- block = block_merge_prev(control, block);
- block = block_merge_next(control, block);
- block_insert(control, block);
- }
- }
- void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size)
- {
- control_t* control = tlsf_cast(control_t*, tlsf);
- void* p = 0;
-
- if (ptr && size == 0)
- {
- tlsf_free(tlsf, ptr);
- }
-
- else if (!ptr)
- {
- p = tlsf_malloc(tlsf, size);
- }
- else
- {
- block_header_t* block = block_from_ptr(ptr);
- block_header_t* next = block_next(block);
- const size_t cursize = block_size(block);
- const size_t combined = cursize + block_size(next) + block_header_overhead;
- const size_t adjust = adjust_request_size(tlsf, size, ALIGN_SIZE);
- tlsf_assert(!block_is_free(block) && "block already marked as free");
-
- if (adjust > cursize && (!block_is_free(next) || adjust > combined))
- {
- p = tlsf_malloc(tlsf, size);
- if (p)
- {
- const size_t minsize = tlsf_min(cursize, size);
- memcpy(p, ptr, minsize);
- tlsf_free(tlsf, ptr);
- }
- }
- else
- {
-
- if (adjust > cursize)
- {
- block_merge_next(control, block);
- block_mark_as_used(block);
- }
-
- block_trim_used(control, block, adjust);
- p = ptr;
- }
- }
- return p;
- }
|