diff --git a/.gitlab/ci/rules.yml b/.gitlab/ci/rules.yml index 5aad86a7ce..c356292c50 100644 --- a/.gitlab/ci/rules.yml +++ b/.gitlab/ci/rules.yml @@ -172,6 +172,7 @@ - "components/spiffs/spiffs" - "components/tinyusb/tinyusb" - "components/unity/unity" + - "components/heap/tlsf" - "examples/peripherals/secure_element/atecc608_ecdsa/components/esp-cryptoauthlib" - ".gitmodules" diff --git a/.gitmodules b/.gitmodules index 802e4b04a7..23c0bc2d55 100644 --- a/.gitmodules +++ b/.gitmodules @@ -86,3 +86,7 @@ [submodule "components/bt/controller/lib_esp32c2/esp32c2-bt-lib"] path = components/bt/controller/lib_esp32c2/esp32c2-bt-lib url = ../../espressif/esp32c2-bt-lib.git + +[submodule "components/heap/tlsf"] + path = components/heap/tlsf + url = ../../espressif/tlsf.git diff --git a/components/heap/CMakeLists.txt b/components/heap/CMakeLists.txt index 66e32e9cee..1ef484ae4f 100644 --- a/components/heap/CMakeLists.txt +++ b/components/heap/CMakeLists.txt @@ -3,8 +3,16 @@ set(srcs "heap_caps_init.c" "multi_heap.c") +set(includes "include") + if(NOT CONFIG_HEAP_TLSF_USE_ROM_IMPL) - list(APPEND srcs "heap_tlsf.c") + set(priv_includes "tlsf") + list(APPEND srcs "tlsf/tlsf.c") + if(NOT CMAKE_BUILD_EARLY_EXPANSION) + set_source_files_properties(tlsf/tlsf.c + PROPERTIES COMPILE_FLAGS + "-include ../tlsf_platform.h") + endif() endif() if(NOT CONFIG_HEAP_POISONING_DISABLED) @@ -31,7 +39,8 @@ endif() idf_component_register(SRCS "${srcs}" - INCLUDE_DIRS include + INCLUDE_DIRS ${includes} + PRIV_INCLUDE_DIRS ${priv_includes} LDFRAGMENTS linker.lf PRIV_REQUIRES soc) diff --git a/components/heap/heap_private.h b/components/heap/heap_private.h index 5e172c034b..1d2065cd47 100644 --- a/components/heap/heap_private.h +++ b/components/heap/heap_private.h @@ -1,16 +1,8 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #pragma once #include diff --git a/components/heap/heap_task_info.c b/components/heap/heap_task_info.c index 5914e66465..54c6fb346c 100644 --- a/components/heap/heap_task_info.c +++ b/components/heap/heap_task_info.c @@ -1,16 +1,8 @@ -// Copyright 2018 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #include #include diff --git a/components/heap/heap_tlsf.c b/components/heap/heap_tlsf.c deleted file mode 100644 index e5f826011f..0000000000 --- a/components/heap/heap_tlsf.c +++ /dev/null @@ -1,949 +0,0 @@ -/* -** Two Level Segregated Fit memory allocator, version 3.1. -** Written by Matthew Conte -** http://tlsf.baisoku.org -** -** Based on the original documentation by Miguel Masmano: -** http://www.gii.upv.es/tlsf/main/docs -** -** This implementation was written to the specification -** of the document, therefore no GPL restrictions apply. -** -** Copyright (c) 2006-2016, Matthew Conte -** All rights reserved. -** -** Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in the -** documentation and/or other materials provided with the distribution. -** * Neither the name of the copyright holder nor the -** names of its contributors may be used to endorse or promote products -** derived from this software without specific prior written permission. -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -** DISCLAIMED. IN NO EVENT SHALL MATTHEW CONTE BE LIABLE FOR ANY -** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ -#include "multi_heap_config.h" -#include "multi_heap.h" -#include "multi_heap_internal.h" -#include "heap_tlsf_config.h" -#include "heap_tlsf.h" - -/* -** Architecture-specific bit manipulation routines. -** -** TLSF achieves O(1) cost for malloc and free operations by limiting -** the search for a free block to a free list of guaranteed size -** adequate to fulfill the request, combined with efficient free list -** queries using bitmasks and architecture-specific bit-manipulation -** routines. -** -** Most modern processors provide instructions to count leading zeroes -** in a word, find the lowest and highest set bit, etc. These -** specific implementations will be used when available, falling back -** to a reasonably efficient generic implementation. -** -** NOTE: TLSF spec relies on ffs/fls returning value 0..31. -** ffs/fls return 1-32 by default, returning 0 for error. -*/ -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; -} - -/* -** Set assert macro, if it has not been provided by the user. -*/ -#if !defined (tlsf_assert) -#define tlsf_assert assert -#endif - -/* -** Static assertion mechanism. -*/ -#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] - -/* 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); -tlsf_static_assert(sizeof(size_t) * CHAR_BIT <= 64); - -/* SL_INDEX_COUNT must be <= number of bits in sl_bitmap's storage type. */ -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); - -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); -} - -/* -** Adjust an allocation size to be aligned to word size, and no smaller -** than internal minimum. -*/ -static inline __attribute__((__always_inline__)) size_t adjust_request_size(size_t size, size_t align) -{ - size_t adjust = 0; - if (size) - { - const size_t aligned = align_up(size, align); - - /* aligned sized must not exceed block_size_max or we'll go out of bounds on sl_bitmap */ - if (aligned < block_size_max) - { - adjust = tlsf_max(aligned, block_size_min); - } - } - return adjust; -} - -/* -** TLSF utility functions. In most cases, these are direct translations of -** the documentation found in the white paper. -*/ - -static inline __attribute__((__always_inline__)) void mapping_insert(size_t size, int* fli, int* sli) -{ - int fl, sl; - if (size < SMALL_BLOCK_SIZE) - { - /* Store small blocks in first list. */ - fl = 0; - sl = tlsf_cast(int, size) >> 2; - } - else - { - fl = tlsf_fls(size); - sl = tlsf_cast(int, size >> (fl - SL_INDEX_COUNT_LOG2)) ^ (1 << SL_INDEX_COUNT_LOG2); - fl -= (FL_INDEX_SHIFT - 1); - } - *fli = fl; - *sli = sl; -} - -/* This version rounds up to the next block size (for allocations) */ -static inline __attribute__((__always_inline__)) void mapping_search(size_t size, int* fli, int* sli) -{ - if (size >= SMALL_BLOCK_SIZE) - { - const size_t round = (1 << (tlsf_fls(size) - SL_INDEX_COUNT_LOG2)) - 1; - size += round; - } - mapping_insert(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; - - /* - ** First, search for a block in the list associated with the given - ** fl/sl index. - */ - 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 = control->fl_bitmap & (~0U << (fl + 1)); - if (!fl_map) - { - /* No free blocks available, memory has been exhausted. */ - 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 the first block in the free list. */ - return control->blocks[fl][sl]; -} - -/* Remove a free block from the free list.*/ -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 this block is the head of the free list, set new head. */ - if (control->blocks[fl][sl] == block) - { - control->blocks[fl][sl] = next; - - /* If the new head is null, clear the bitmap. */ - if (next == &control->block_null) - { - control->sl_bitmap[fl] &= ~(1 << sl); - - /* If the second bitmap is now empty, clear the fl bitmap. */ - if (!control->sl_bitmap[fl]) - { - control->fl_bitmap &= ~(1 << fl); - } - } - } -} - -/* Insert a free block into the free block list. */ -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][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"); - /* - ** Insert the new block at the head of the list, and mark the first- - ** and second-level bitmaps appropriately. - */ - control->blocks[fl][sl] = block; - control->fl_bitmap |= (1 << fl); - control->sl_bitmap[fl] |= (1 << sl); -} - -/* Remove a given block from the free list. */ -static inline __attribute__((__always_inline__)) void block_remove(control_t* control, block_header_t* block) -{ - int fl, sl; - mapping_insert(block_size(block), &fl, &sl); - remove_free_block(control, block, fl, sl); -} - -/* Insert a given block into the free list. */ -static inline __attribute__((__always_inline__)) void block_insert(control_t* control, block_header_t* block) -{ - int fl, sl; - mapping_insert(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; -} - -/* Split a block into two, the second of which is free. */ -static inline __attribute__((__always_inline__)) block_header_t* block_split(block_header_t* block, size_t size) -{ - /* Calculate the amount of space left in the remaining block. - * REMINDER: remaining pointer's first field is `prev_phys_block` but this field is part of the - * previous physical block. */ - block_header_t* remaining = - offset_to_block(block_to_ptr(block), size - block_header_overhead); - - /* `size` passed as an argument is the first block's new size, thus, the remaining block's size - * is `block_size(block) - size`. However, the block's data must be precedeed by the data size. - * This field is NOT part of the size, so it has to be substracted from the calculation. */ - 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); - - /** - * Here is the final outcome of this function: - * - * block remaining (block_ptr + size - BHO) - * + + - * | | - * v v - * +----------------------------------------------------------------------+ - * |0000| |xxxxxxxxxxxxxxxxxxxxxx|xxxx| |###########################| - * |0000| |xxxxxxxxxxxxxxxxxxxxxx|xxxx| |###########################| - * |0000| |xxxxxxxxxxxxxxxxxxxxxx|xxxx| |###########################| - * |0000| |xxxxxxxxxxxxxxxxxxxxxx|xxxx| |###########################| - * +----------------------------------------------------------------------+ - * | | | | - * + +<------------------------->+ +<-------------------------> - * BHO `size` (argument) bytes BHO `remain_size` bytes - * - * Where BHO = block_header_overhead, - * 0: part of the memory owned by a `block`'s previous neighbour, - * x: part of the memory owned by `block`. - * #: part of the memory owned by `remaining`. - */ - - return remaining; -} - -/* Absorb a free block's storage into an adjacent previous free block. */ -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"); - /* Note: Leaves flags untouched. */ - prev->size += block_size(block) + block_header_overhead; - block_link_next(prev); - -#ifdef MULTI_HEAP_POISONING_SLOW - /* next_block header needs to be replaced with a fill pattern */ - multi_heap_internal_poison_fill_region(block, sizeof(block_header_t), true /* free */); -#endif - - return prev; -} - -/* Merge a just-freed block with an adjacent previous free block. */ -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; -} - -/* Merge a just-freed block with an adjacent free 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; -} - -/* Trim any trailing block space off the end of a block, return to pool. */ -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); - } -} - -/* Trim any trailing block space off the end of a used block, return to pool. */ -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)) - { - /* If the next block is free, we must coalesce. */ - 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)) - { - /* We want to split `block` in two: the first block will be freed and the - * second block will be returned. */ - remaining_block = block_split(block, size - block_header_overhead); - - /* `remaining_block` is the second block, mark its predecessor (first - * block) as free. */ - block_set_prev_free(remaining_block); - - block_link_next(block); - - /* Put back the first block into the free memory list. */ - 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(size, &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(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; -} - -/* Clear structure and point all empty lists at the null block. */ -static void control_construct(control_t* control) -{ - int i, j; - - control->block_null.next_free = &control->block_null; - control->block_null.prev_free = &control->block_null; - - control->fl_bitmap = 0; - for (i = 0; i < FL_INDEX_COUNT; ++i) - { - control->sl_bitmap[i] = 0; - for (j = 0; j < SL_INDEX_COUNT; ++j) - { - control->blocks[i][j] = &control->block_null; - } - } -} - -/* -** Debugging utilities. -*/ - -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; - - /* 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 = 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][j]; - - /* Check that first- and second-level lists agree. */ - 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; - } - - /* Check that there is at least one free block. */ - 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(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) -{ - /* Check that the blocks are physically correct. */ - integrity_t integ = { 0, 0 }; - tlsf_walk_pool(pool, integrity_walker, &integ); - - return integ.status; -} - -/* -** Size of the TLSF structures in a given memory block passed to -** tlsf_create, equal to the size of a control_t -*/ -size_t tlsf_size(void) -{ - return sizeof(control_t); -} - -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(void) -{ - return block_size_max; -} - -/* -** 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; -} - -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 > 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_overhead); - 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_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(block_size(block), &fl, &sl); - remove_free_block(control, block, fl, sl); -} - -/* -** TLSF main interface. -*/ - - -tlsf_t tlsf_create(void* mem) -{ -#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)); - - return tlsf_cast(tlsf_t, mem); -} - -pool_t tlsf_get_pool(tlsf_t tlsf) -{ - return tlsf_cast(pool_t, (char*)tlsf + tlsf_size()); -} - -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()); - 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(size, ALIGN_SIZE); - block_header_t* block = block_locate_free(control, adjust); - return block_prepare_used(control, block, adjust); -} - -/** - * @brief Allocate memory of at least `size` bytes where byte at `data_offset` will be aligned to `alignment`. - * - * This function will allocate memory pointed by `ptr`. However, the byte at `data_offset` of - * this piece of memory (i.e., byte at `ptr` + `data_offset`) will be aligned to `alignment`. - * This function is useful for allocating memory that will internally have a header, and the - * usable memory following the header (i.e. `ptr` + `data_offset`) must be aligned. - * - * For example, a call to `multi_heap_aligned_alloc_impl_offs(heap, 64, 256, 20)` will return a - * pointer `ptr` to free memory of minimum 64 bytes, where `ptr + 20` is aligned on `256`. - * So `(ptr + 20) % 256` equals 0. - * - * @param tlsf TLSF structure to allocate memory from. - * @param align Alignment for the returned pointer's offset. - * @param size Minimum size, in bytes, of the memory to allocate INCLUDING - * `data_offset` bytes. - * @param data_offset Offset to be aligned on `alignment`. This can be 0, in - * this case, the returned pointer will be aligned on - * `alignment`. If it is not a multiple of CPU word size, - * it will be aligned up to the closest multiple of it. - * - * @return pointer to free memory. - */ -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(size, ALIGN_SIZE); - const size_t off_adjust = align_up(data_offset, 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 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. - */ - const size_t gap_minimum = sizeof(block_header_t) + off_adjust; - /* The offset is included in both `adjust` and `gap_minimum`, so we - ** need to subtract it once. - */ - const size_t size_with_gap = adjust_request_size(adjust + align + gap_minimum - off_adjust, align); - - /* - ** If alignment is less than or equal to base alignment, we're done, because - ** we are guaranteed that the size is at least sizeof(block_header_t), enough - ** to store next blocks' metadata. Plus, all pointers allocated will all be - ** aligned on a 4-byte bound, so ptr + data_offset will also have this - ** alignment constraint. Thus, the gap is not required. - ** 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 = 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); - - 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 size is too small or if there is no gap but we need one, - ** offset to next aligned boundary. - ** NOTE: No need for a gap if the alignment required is less than or is - ** equal to ALIGN_SIZE. - */ - if ((gap && gap < gap_minimum) || (!gap && off_adjust && align > ALIGN_SIZE)) - { - 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); - } - } - - /* Preparing the block will also the trailing free memory. */ - return block_prepare_used(control, block, adjust); -} - -/** - * @brief Same as `tlsf_memalign_offs` function but with a 0 offset. - * The pointer returned is aligned on `align`. - */ -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) -{ - /* Don't attempt to free a NULL pointer. */ - 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); - } -} - -/* -** The TLSF block information provides us with enough information to -** provide a reasonably intelligent implementation of realloc, growing or -** shrinking the currently allocated block as required. -** -** This routine handles the somewhat esoteric edge cases of realloc: -** - a non-zero size with a null pointer will behave like malloc -** - a zero size with a non-null pointer will behave like free -** - a request that cannot be satisfied will leave the original buffer -** untouched -** - an extended buffer size will leave the newly-allocated area with -** contents undefined -*/ -void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size) -{ - control_t* control = tlsf_cast(control_t*, tlsf); - void* p = 0; - - /* Zero-size requests are treated as free. */ - if (ptr && size == 0) - { - tlsf_free(tlsf, ptr); - } - /* Requests with NULL pointers are treated as malloc. */ - 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(size, ALIGN_SIZE); - - tlsf_assert(!block_is_free(block) && "block already marked as free"); - - /* - ** If the next block is used, or when combined with the current - ** block, does not offer enough space, we must reallocate and copy. - */ - 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 - { - /* Do we need to expand to the next block? */ - if (adjust > cursize) - { - block_merge_next(control, block); - block_mark_as_used(block); - } - - /* Trim the resulting block and return the original pointer. */ - block_trim_used(control, block, adjust); - p = ptr; - } - } - - return p; -} diff --git a/components/heap/heap_tlsf.h b/components/heap/heap_tlsf.h deleted file mode 100644 index 24768f3055..0000000000 --- a/components/heap/heap_tlsf.h +++ /dev/null @@ -1,135 +0,0 @@ -/* -** Two Level Segregated Fit memory allocator, version 3.1. -** Written by Matthew Conte -** http://tlsf.baisoku.org -** -** Based on the original documentation by Miguel Masmano: -** http://www.gii.upv.es/tlsf/main/docs -** -** This implementation was written to the specification -** of the document, therefore no GPL restrictions apply. -** -** Copyright (c) 2006-2016, Matthew Conte -** All rights reserved. -** -** Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in the -** documentation and/or other materials provided with the distribution. -** * Neither the name of the copyright holder nor the -** names of its contributors may be used to endorse or promote products -** derived from this software without specific prior written permission. -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -** DISCLAIMED. IN NO EVENT SHALL MATTHEW CONTE BE LIABLE FOR ANY -** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#pragma once -#include -#include -#include -#include -#include -#include -#include -#include "heap_tlsf_config.h" - -#if defined(__cplusplus) -extern "C" { -#endif - -/* -** Cast and min/max macros. -*/ -#define tlsf_cast(t, exp) ((t) (exp)) -#define tlsf_min(a, b) ((a) < (b) ? (a) : (b)) -#define tlsf_max(a, b) ((a) > (b) ? (a) : (b)) - -/* A type used for casting when doing pointer arithmetic. */ -typedef ptrdiff_t tlsfptr_t; - -typedef struct block_header_t -{ - /* Points to the previous physical block. */ - struct block_header_t* prev_phys_block; - - /* The size of this block, excluding the block header. */ - size_t size; - - /* Next and previous free blocks. */ - struct block_header_t* next_free; - struct block_header_t* prev_free; -} block_header_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; - - /* Bitmaps for free lists. */ - unsigned int fl_bitmap; - unsigned int sl_bitmap[FL_INDEX_COUNT]; - - /* Head of free lists. */ - block_header_t* blocks[FL_INDEX_COUNT][SL_INDEX_COUNT]; -} control_t; - -#include "heap_tlsf_block_functions.h" - -/* tlsf_t: a TLSF structure. Can contain 1 to N pools. */ -/* pool_t: a block of memory that TLSF can manage. */ -typedef void* tlsf_t; -typedef void* pool_t; - -/* Create/destroy a memory pool. */ -tlsf_t tlsf_create(void* mem); -tlsf_t tlsf_create_with_pool(void* mem, size_t bytes); -pool_t tlsf_get_pool(tlsf_t tlsf); - -/* Add/remove memory pools. */ -pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes); -void tlsf_remove_pool(tlsf_t tlsf, pool_t pool); - -/* malloc/memalign/realloc/free replacements. */ -void* tlsf_malloc(tlsf_t tlsf, size_t size); -void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t size); -void* tlsf_memalign_offs(tlsf_t tlsf, size_t align, size_t size, size_t offset); -void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size); -void tlsf_free(tlsf_t tlsf, void* ptr); - -/* Returns internal block size, not original request size */ -size_t tlsf_block_size(void* ptr); - -/* Overheads/limits of internal structures. */ -size_t tlsf_size(void); -size_t tlsf_align_size(void); -size_t tlsf_block_size_min(void); -size_t tlsf_block_size_max(void); -size_t tlsf_pool_overhead(void); -size_t tlsf_alloc_overhead(void); - -/* Debugging. */ -typedef void (*tlsf_walker)(void* ptr, size_t size, int used, void* user); -void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void* user); -/* Returns nonzero if any internal consistency check fails. */ -int tlsf_check(tlsf_t tlsf); -int tlsf_check_pool(pool_t pool); - -/* Used for heap tlsf in ROM IMPL */ -void tlsf_poison_fill_pfunc_set(void *pfunc); - -#if defined(__cplusplus) -}; -#endif diff --git a/components/heap/heap_tlsf_block_functions.h b/components/heap/heap_tlsf_block_functions.h deleted file mode 100644 index 77119e39a2..0000000000 --- a/components/heap/heap_tlsf_block_functions.h +++ /dev/null @@ -1,172 +0,0 @@ -/* -** Two Level Segregated Fit memory allocator, version 3.1. -** Written by Matthew Conte -** http://tlsf.baisoku.org -** -** Based on the original documentation by Miguel Masmano: -** http://www.gii.upv.es/tlsf/main/docs -** -** This implementation was written to the specification -** of the document, therefore no GPL restrictions apply. -** -** Copyright (c) 2006-2016, Matthew Conte -** All rights reserved. -** -** Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in the -** documentation and/or other materials provided with the distribution. -** * Neither the name of the copyright holder nor the -** names of its contributors may be used to endorse or promote products -** derived from this software without specific prior written permission. -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -** DISCLAIMED. IN NO EVENT SHALL MATTHEW CONTE BE LIABLE FOR ANY -** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#pragma once - -/* -** Data structures and associated constants. -*/ - -/* -** Since block sizes are always at least a multiple of 4, the two least -** significant bits of the size field are used to store the block status: -** - bit 0: whether block is busy or free -** - bit 1: whether previous block is busy or free -*/ -#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. -*/ -#define block_header_overhead (sizeof(size_t)) - -/* User data starts directly after the size field in a used block. */ -#define block_start_offset (offsetof(block_header_t, size) + sizeof(size_t)) - -/* -** 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. -*/ -#define block_size_min (sizeof(block_header_t) - sizeof(block_header_t*)) -#define block_size_max (tlsf_cast(size_t, 1) << FL_INDEX_MAX) - -/* -** block_header_t member functions. -*/ -static inline __attribute__((__always_inline__)) size_t block_size(const block_header_t* block) -{ - return block->size & ~(block_header_free_bit | block_header_prev_free_bit); -} - -static inline __attribute__((__always_inline__)) void block_set_size(block_header_t* block, size_t size) -{ - const size_t oldsize = block->size; - block->size = size | (oldsize & (block_header_free_bit | block_header_prev_free_bit)); -} - -static inline __attribute__((__always_inline__)) int block_is_last(const block_header_t* block) -{ - return block_size(block) == 0; -} - -static inline __attribute__((__always_inline__)) int block_is_free(const block_header_t* block) -{ - return tlsf_cast(int, block->size & block_header_free_bit); -} - -static inline __attribute__((__always_inline__)) void block_set_free(block_header_t* block) -{ - block->size |= block_header_free_bit; -} - -static inline __attribute__((__always_inline__)) void block_set_used(block_header_t* block) -{ - block->size &= ~block_header_free_bit; -} - -static inline __attribute__((__always_inline__)) int block_is_prev_free(const block_header_t* block) -{ - return tlsf_cast(int, block->size & block_header_prev_free_bit); -} - -static inline __attribute__((__always_inline__)) void block_set_prev_free(block_header_t* block) -{ - block->size |= block_header_prev_free_bit; -} - -static inline __attribute__((__always_inline__)) void block_set_prev_used(block_header_t* block) -{ - block->size &= ~block_header_prev_free_bit; -} - -static inline __attribute__((__always_inline__)) block_header_t* block_from_ptr(const void* ptr) -{ - return tlsf_cast(block_header_t*, - tlsf_cast(unsigned char*, ptr) - block_start_offset); -} - -static inline __attribute__((__always_inline__)) void* block_to_ptr(const block_header_t* block) -{ - return tlsf_cast(void*, - tlsf_cast(unsigned char*, block) + block_start_offset); -} - -/* Return location of next block after block of given size. */ -static inline __attribute__((__always_inline__)) block_header_t* offset_to_block(const void* ptr, size_t size) -{ - return tlsf_cast(block_header_t*, tlsf_cast(tlsfptr_t, ptr) + size); -} - -/* Return location of previous block. */ -static inline __attribute__((__always_inline__)) block_header_t* block_prev(const block_header_t* block) -{ - return block->prev_phys_block; -} - -/* Return location of next existing block. */ -static inline __attribute__((__always_inline__)) 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); - return next; -} - -/* Link a new block with its physical neighbor, return the neighbor. */ -static inline __attribute__((__always_inline__)) block_header_t* block_link_next(block_header_t* block) -{ - block_header_t* next = block_next(block); - next->prev_phys_block = block; - return next; -} - -static inline __attribute__((__always_inline__)) void block_mark_as_free(block_header_t* block) -{ - /* Link the block to the next block, first. */ - block_header_t* next = block_link_next(block); - block_set_prev_free(next); - block_set_free(block); -} - -static inline __attribute__((__always_inline__)) void block_mark_as_used(block_header_t* block) -{ - block_header_t* next = block_next(block); - block_set_prev_used(next); - block_set_used(block); -} diff --git a/components/heap/heap_tlsf_config.h b/components/heap/heap_tlsf_config.h deleted file mode 100644 index 48189b0ed5..0000000000 --- a/components/heap/heap_tlsf_config.h +++ /dev/null @@ -1,128 +0,0 @@ -/* -** Two Level Segregated Fit memory allocator, version 3.1. -** Written by Matthew Conte -** http://tlsf.baisoku.org -** -** Based on the original documentation by Miguel Masmano: -** http://www.gii.upv.es/tlsf/main/docs -** -** This implementation was written to the specification -** of the document, therefore no GPL restrictions apply. -** -** Copyright (c) 2006-2016, Matthew Conte -** All rights reserved. -** -** Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in the -** documentation and/or other materials provided with the distribution. -** * Neither the name of the copyright holder nor the -** names of its contributors may be used to endorse or promote products -** derived from this software without specific prior written permission. -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -** DISCLAIMED. IN NO EVENT SHALL MATTHEW CONTE BE LIABLE FOR ANY -** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#pragma once - -#ifdef ESP_PLATFORM - -#include "soc/soc.h" - -#if !CONFIG_SPIRAM -#define TLSF_MAX_POOL_SIZE (SOC_DIRAM_DRAM_HIGH - SOC_DIRAM_DRAM_LOW) -#else -#define TLSF_MAX_POOL_SIZE SOC_EXTRAM_DATA_SIZE -#endif - -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. - */ - SL_INDEX_COUNT_LOG2 = 5, - - /* 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. - */ - - /* Tunning the first level, we can reduce TLSF pool overhead - * in exchange of manage a pool smaller than 4GB - */ - #if (TLSF_MAX_POOL_SIZE <= (256 * 1024)) - FL_INDEX_MAX = 18, //Each pool can have up 256KB - #elif (TLSF_MAX_POOL_SIZE <= (512 * 1024)) - FL_INDEX_MAX = 19, //Each pool can have up 512KB - #elif (TLSF_MAX_POOL_SIZE <= (1 * 1024 * 1024)) - FL_INDEX_MAX = 20, //Each pool can have up 1MB - #elif (TLSF_MAX_POOL_SIZE <= (2 * 1024 * 1024)) - FL_INDEX_MAX = 21, //Each pool can have up 2MB - #elif (TLSF_MAX_POOL_SIZE <= (4 * 1024 * 1024)) - FL_INDEX_MAX = 22, //Each pool can have up 4MB - #elif (TLSF_MAX_POOL_SIZE <= (8 * 1024 * 1024)) - FL_INDEX_MAX = 23, //Each pool can have up 8MB - #elif (TLSF_MAX_POOL_SIZE <= (16 * 1024 * 1024)) - FL_INDEX_MAX = 24, //Each pool can have up 16MB - #elif (TLSF_MAX_POOL_SIZE <= (32 * 1024 * 1024)) - FL_INDEX_MAX = 25, //Each pool can have up 32MB - #else - #error "Higher TLSF pool sizes should be added for this new config" - #endif - - SL_INDEX_COUNT = (1 << SL_INDEX_COUNT_LOG2), - FL_INDEX_SHIFT = (SL_INDEX_COUNT_LOG2 + ALIGN_SIZE_LOG2), - FL_INDEX_COUNT = (FL_INDEX_MAX - FL_INDEX_SHIFT + 1), - - SMALL_BLOCK_SIZE = (1 << FL_INDEX_SHIFT), -}; -#else -enum tlsf_config -{ - //Specific configuration for host test. - - /* 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, - - /* All allocation sizes and addresses are aligned to 4 bytes. */ - ALIGN_SIZE_LOG2 = 2, - ALIGN_SIZE = (1 << ALIGN_SIZE_LOG2), - - /* Tunning the first level, we can reduce TLSF pool overhead - * in exchange of manage a pool smaller than 4GB - */ - FL_INDEX_MAX = 30, - - SL_INDEX_COUNT = (1 << SL_INDEX_COUNT_LOG2), - FL_INDEX_SHIFT = (SL_INDEX_COUNT_LOG2 + ALIGN_SIZE_LOG2), - FL_INDEX_COUNT = (FL_INDEX_MAX - FL_INDEX_SHIFT + 1), - - SMALL_BLOCK_SIZE = (1 << FL_INDEX_SHIFT), -}; -#endif diff --git a/components/heap/heap_trace_standalone.c b/components/heap/heap_trace_standalone.c index 138a8cd35f..b945b0530f 100644 --- a/components/heap/heap_trace_standalone.c +++ b/components/heap/heap_trace_standalone.c @@ -1,16 +1,8 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #include #include diff --git a/components/heap/multi_heap.c b/components/heap/multi_heap.c index 2f28b0c574..b87e923a3d 100644 --- a/components/heap/multi_heap.c +++ b/components/heap/multi_heap.c @@ -11,7 +11,8 @@ #include #include #include -#include "heap_tlsf.h" +#include "tlsf.h" +#include "tlsf_block_functions.h" #include "multi_heap.h" #include "multi_heap_internal.h" diff --git a/components/heap/multi_heap_config.h b/components/heap/multi_heap_config.h index 13d30cfd75..cd8a963fb6 100644 --- a/components/heap/multi_heap_config.h +++ b/components/heap/multi_heap_config.h @@ -1,16 +1,8 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #pragma once #ifdef ESP_PLATFORM diff --git a/components/heap/multi_heap_poisoning.c b/components/heap/multi_heap_poisoning.c index 45f95cde7c..b13b4f4a5a 100644 --- a/components/heap/multi_heap_poisoning.c +++ b/components/heap/multi_heap_poisoning.c @@ -21,7 +21,7 @@ /* Defines compile-time configuration macros */ #include "multi_heap_config.h" -#include "heap_tlsf.h" +#include "tlsf.h" #ifdef MULTI_HEAP_POISONING @@ -177,6 +177,22 @@ static bool verify_fill_pattern(void *data, size_t size, bool print_errors, bool } return valid; } + +/*! + * @brief Definition of the weak function declared in TLSF repository. + * The call of this function assures that the header of an absorbed + * block is filled with the correct pattern in case of comprehensive + * heap poisoning. + * + * @param start: pointer to the start of the memory region to fill + * @param size: size of the memory region to fill + * @param is_free: Indicate if the pattern to use the fill the region should be + * an after free or after allocation pattern. + */ +void block_absorb_post_hook(void *start, size_t size, bool is_free) +{ + multi_heap_internal_poison_fill_region(start, size, is_free); +} #endif void *multi_heap_aligned_alloc(multi_heap_handle_t heap, size_t size, size_t alignment) diff --git a/components/heap/test_multi_heap_host/Makefile b/components/heap/test_multi_heap_host/Makefile index 0827e34695..6c10f8035a 100644 --- a/components/heap/test_multi_heap_host/Makefile +++ b/components/heap/test_multi_heap_host/Makefile @@ -6,14 +6,14 @@ ifneq ($(filter clean,$(MAKECMDGOALS)),) endif SOURCE_FILES = $(abspath \ - ../multi_heap.c \ - ../heap_tlsf.c \ - ../multi_heap_poisoning.c \ test_multi_heap.cpp \ + ../multi_heap_poisoning.c \ + ../multi_heap.c \ + ../tlsf/tlsf.c \ main.cpp \ - ) + ) -INCLUDE_FLAGS = -I../include -I../../../tools/catch +INCLUDE_FLAGS = -I../include -I../../../tools/catch -I../tlsf GCOV ?= gcov diff --git a/components/heap/tlsf b/components/heap/tlsf new file mode 160000 index 0000000000..ff11688f24 --- /dev/null +++ b/components/heap/tlsf @@ -0,0 +1 @@ +Subproject commit ff11688f242b28b3918c2cdaa20738d12d73b5f4 diff --git a/components/heap/tlsf_platform.h b/components/heap/tlsf_platform.h new file mode 100644 index 0000000000..6b882be920 --- /dev/null +++ b/components/heap/tlsf_platform.h @@ -0,0 +1,49 @@ +/* + * SPDX-FileCopyrightText: 2017-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef ESP_PLATFORM +#include "soc/soc.h" + +#if !CONFIG_SPIRAM +#define TLSF_MAX_POOL_SIZE (SOC_DIRAM_DRAM_HIGH - SOC_DIRAM_DRAM_LOW) +#else +#define TLSF_MAX_POOL_SIZE SOC_EXTRAM_DATA_SIZE +#endif +#endif + +#if (TLSF_MAX_POOL_SIZE <= (256 * 1024)) +#define FL_INDEX_MAX_PLATFORM 18 //Each pool can have up 256KB +#elif (TLSF_MAX_POOL_SIZE <= (512 * 1024)) +#define FL_INDEX_MAX_PLATFORM 19 //Each pool can have up 512KB +#elif (TLSF_MAX_POOL_SIZE <= (1 * 1024 * 1024)) +#define FL_INDEX_MAX_PLATFORM 20 //Each pool can have up 1MB +#elif (TLSF_MAX_POOL_SIZE <= (2 * 1024 * 1024)) +#define FL_INDEX_MAX_PLATFORM 21 //Each pool can have up 2MB +#elif (TLSF_MAX_POOL_SIZE <= (4 * 1024 * 1024)) +#define FL_INDEX_MAX_PLATFORM 22 //Each pool can have up 4MB +#elif (TLSF_MAX_POOL_SIZE <= (8 * 1024 * 1024)) +#define FL_INDEX_MAX_PLATFORM 23 //Each pool can have up 8MB +#elif (TLSF_MAX_POOL_SIZE <= (16 * 1024 * 1024)) +#define FL_INDEX_MAX_PLATFORM 24 //Each pool can have up 16MB +#else +#error "Higher TLSF pool sizes should be added for this new config" +#endif + +/* Include from the TLSF submodule to force TLSF_INDEX_MAX_PLATFORM to be defined + * when the TLSF repository is compiled in the IDF environment. */ +#include "tlsf_common.h" + +#ifdef __cplusplus +} +#endif diff --git a/tools/ci/check_copyright_ignore.txt b/tools/ci/check_copyright_ignore.txt index 412824c4f3..f1894845ef 100644 --- a/tools/ci/check_copyright_ignore.txt +++ b/tools/ci/check_copyright_ignore.txt @@ -813,16 +813,8 @@ components/hal/twai_hal.c components/hal/twai_hal_iram.c components/hal/uart_hal_iram.c components/hal/usb_hal.c -components/heap/heap_private.h -components/heap/heap_task_info.c -components/heap/heap_tlsf.c -components/heap/heap_tlsf.h -components/heap/heap_tlsf_block_functions.h -components/heap/heap_tlsf_config.h -components/heap/heap_trace_standalone.c components/heap/include/esp_heap_trace.h components/heap/include/heap_memory_layout.h -components/heap/multi_heap_config.h components/heap/test/test_aligned_alloc_caps.c components/heap/test/test_allocator_timings.c components/heap/test/test_diram.c diff --git a/tools/ci/static-analysis-rules.yml b/tools/ci/static-analysis-rules.yml index a9cc044957..f729329f02 100644 --- a/tools/ci/static-analysis-rules.yml +++ b/tools/ci/static-analysis-rules.yml @@ -29,6 +29,7 @@ skip: - "components/spiffs/spiffs" - "components/tinyusb/tinyusb" - "components/unity/unity" + - "components/heap/tlsf" - "examples/peripherals/secure_element/atecc608_ecdsa/components/esp-cryptoauthlib" # disabled temporarily to pass the CI