tlsf: third encounter, now with aligment by 8

This commit is contained in:
SG
2024-06-11 15:19:30 +01:00
parent 7d50f4b5a7
commit d4b70a6aa6
9 changed files with 1359 additions and 21 deletions

View File

@@ -1,6 +1,6 @@
#include <furi.h>
#include <tlsf.h>
#include <tlsf_block_functions.h>
// #include <tlsf_block_functions.h>
#include <FreeRTOS.h>
#include <task.h>
#include <m-dict.h>
@@ -45,7 +45,7 @@ static void memmgr_heap_init(void) {
__attribute__((constructor)) static void memmgr_init(void) {
size_t pool_size = (size_t)&__heap_end__ - (size_t)&__heap_start__;
tlsf = tlsf_create_with_pool((void*)&__heap_start__, pool_size, pool_size);
tlsf = tlsf_create_with_pool((void*)&__heap_start__, pool_size);
memmgr_heap_init();
}
@@ -116,8 +116,7 @@ size_t memmgr_heap_get_thread_memory(FuriThreadId thread_id) {
MemmgrHeapAllocDict_next(alloc_dict_it)) {
MemmgrHeapAllocDict_itref_t* data = MemmgrHeapAllocDict_ref(alloc_dict_it);
if(data->key != 0) {
block_header_t* block = block_from_ptr((uint8_t*)data->key);
if(!block_is_free(block)) {
if(!tlsf_pointer_is_free((uint8_t*)data->key)) {
leftovers += data->value;
}
}
@@ -129,7 +128,7 @@ size_t memmgr_heap_get_thread_memory(FuriThreadId thread_id) {
return leftovers;
}
static bool tlsf_walker_max_free(void* ptr, size_t size, int used, void* user) {
static void tlsf_walker_max_free(void* ptr, size_t size, int used, void* user) {
UNUSED(ptr);
bool free = !used;
@@ -137,8 +136,6 @@ static bool tlsf_walker_max_free(void* ptr, size_t size, int used, void* user) {
if(free && size > *max_free_block_size) {
*max_free_block_size = size;
}
return true;
}
size_t memmgr_heap_get_max_free_block(void) {
@@ -159,9 +156,9 @@ typedef struct {
void* context;
} BlockWalkerWrapper;
static bool tlsf_walker_wrapper(void* ptr, size_t size, int used, void* user) {
static void tlsf_walker_wrapper(void* ptr, size_t size, int used, void* user) {
BlockWalkerWrapper* wrapper = (BlockWalkerWrapper*)user;
return wrapper->walker(ptr, size, used, wrapper->context);
wrapper->walker(ptr, size, used, wrapper->context);
}
void memmgr_heap_walk_blocks(BlockWalker walker, void* context) {
@@ -336,7 +333,8 @@ extern void* pvPortRealloc(void* pv, size_t xSize) {
}
size_t xPortGetFreeHeapSize(void) {
return memmgr_get_heap_size() - heap_used - tlsf_size(tlsf);
return memmgr_get_heap_size() - heap_used - tlsf_struct_size();
return 0;
}
size_t xPortGetTotalHeapSize(void) {
@@ -344,5 +342,6 @@ size_t xPortGetTotalHeapSize(void) {
}
size_t xPortGetMinimumEverFreeHeapSize(void) {
return memmgr_get_heap_size() - heap_max_used - tlsf_size(tlsf);
return memmgr_get_heap_size() - heap_max_used - tlsf_struct_size();
return 0;
}

View File

@@ -13,9 +13,9 @@ env.Append(
libs = env.BuildModules(
[
"tlsf",
"mlib",
"stm32wb",
"tlsf",
"freertos",
"print",
"microtar",

View File

@@ -11,7 +11,9 @@ libenv = env.Clone(FW_LIB_NAME="tlsf")
libenv.ApplyLibFlags()
libenv.Append(
CPPDEFINES=[],
CPPDEFINES=[
"CONFIG_MALLOC_ALIGNMENT=4",
],
)
sources = [File("tlsf/tlsf.c")]

1130
lib/tlsf/tlsf.c Normal file

File diff suppressed because it is too large Load Diff

96
lib/tlsf/tlsf.h Normal file
View File

@@ -0,0 +1,96 @@
#ifndef INCLUDED_tlsf
#define INCLUDED_tlsf
/*
** 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.
*/
#if defined(__cplusplus)
extern "C" {
#endif
#include <core/check.h>
#define tlsf_assert furi_check
/* 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);
void tlsf_destroy(tlsf_t tlsf);
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 bytes);
void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t bytes);
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);
/* Memory statistics. */
size_t tlsf_struct_size(void);
bool tlsf_pointer_is_free(void* ptr);
#if defined(__cplusplus)
};
#endif
#endif

72
lib/tlsf/tlsfbits.h Normal file
View File

@@ -0,0 +1,72 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef INCLUDED_tlsfbits
#define INCLUDED_tlsfbits
#include <stdint.h>
typedef uint32_t u32;
#define BITS_PER_BYTE 8
#define BITS_PER_LONG (sizeof(long) * BITS_PER_BYTE)
#define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE)
#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_TYPE(long))
#define BITS_TO_U64(nr) DIV_ROUND_UP(nr, BITS_PER_TYPE(u64))
#define BITS_TO_U32(nr) DIV_ROUND_UP(nr, BITS_PER_TYPE(u32))
#define BITS_TO_BYTES(nr) DIV_ROUND_UP(nr, BITS_PER_TYPE(char))
#define BYTES_TO_BITS(nb) (((BITS_PER_LONG * (nb)) / sizeof(long)))
#ifdef CONFIG_64BIT
#define TLSF_64BIT
#endif
/*
** 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 int tlsf_ffs(unsigned int word)
{
return ffs(word) - 1;
}
static int tlsf_fls(unsigned int word)
{
return fls(word) - 1;
}
/* Possibly 64-bit version of tlsf_fls. */
#if defined (TLSF_64BIT)
static int tlsf_fls_sizet(size_t size)
{
int high = (int)(size >> 32);
int bits = 0;
if (high)
{
bits = 32 + tlsf_fls(high);
}
else
{
bits = tlsf_fls((int)size & 0xffffffff);
}
return bits;
}
#else
#define tlsf_fls_sizet tlsf_fls
#endif
#endif

View File

@@ -1,5 +1,5 @@
entry,status,name,type,params
Version,+,64.2,,
Version,+,65.0,,
Header,+,applications/services/bt/bt_service/bt.h,,
Header,+,applications/services/bt/bt_service/bt_keys_storage.h,,
Header,+,applications/services/cli/cli.h,,
@@ -520,9 +520,7 @@ Function,-,acosh,double,double
Function,-,acoshf,float,float
Function,-,acoshl,long double,long double
Function,-,acosl,long double,long double
Function,-,aligned_alloc,void*,"size_t, size_t"
Function,+,aligned_free,void,void*
Function,+,aligned_malloc,void*,"size_t, size_t"
Function,+,aligned_alloc,void*,"size_t, size_t"
Function,-,arc4random,__uint32_t,
Function,-,arc4random_buf,void,"void*, size_t"
Function,-,arc4random_uniform,__uint32_t,__uint32_t
@@ -2007,7 +2005,8 @@ Function,+,memchr,void*,"const void*, int, size_t"
Function,+,memcmp,int,"const void*, const void*, size_t"
Function,+,memcpy,void*,"void*, const void*, size_t"
Function,-,memmem,void*,"const void*, size_t, const void*, size_t"
Function,-,memmgr_alloc_from_pool,void*,size_t
Function,+,memmgr_aux_pool_alloc,void*,size_t
Function,+,memmgr_aux_pool_get_free,size_t,
Function,+,memmgr_get_free_heap,size_t,
Function,+,memmgr_get_minimum_free_heap,size_t,
Function,+,memmgr_get_total_heap,size_t,
@@ -2015,8 +2014,7 @@ Function,+,memmgr_heap_disable_thread_trace,void,FuriThreadId
Function,+,memmgr_heap_enable_thread_trace,void,FuriThreadId
Function,+,memmgr_heap_get_max_free_block,size_t,
Function,+,memmgr_heap_get_thread_memory,size_t,FuriThreadId
Function,+,memmgr_heap_printf_free_blocks,void,
Function,-,memmgr_pool_get_free,size_t,
Function,+,memmgr_heap_walk_blocks,void,"BlockWalker, void*"
Function,-,memmgr_pool_get_max_block,size_t,
Function,+,memmove,void*,"void*, const void*, size_t"
Function,-,mempcpy,void*,"void*, const void*, size_t"
1 entry status name type params
2 Version + 64.2 65.0
3 Header + applications/services/bt/bt_service/bt.h
4 Header + applications/services/bt/bt_service/bt_keys_storage.h
5 Header + applications/services/cli/cli.h
520 Function - acoshf float float
521 Function - acoshl long double long double
522 Function - acosl long double long double
523 Function - + aligned_alloc void* size_t, size_t
Function + aligned_free void void*
Function + aligned_malloc void* size_t, size_t
524 Function - arc4random __uint32_t
525 Function - arc4random_buf void void*, size_t
526 Function - arc4random_uniform __uint32_t __uint32_t
2005 Function + memcmp int const void*, const void*, size_t
2006 Function + memcpy void* void*, const void*, size_t
2007 Function - memmem void* const void*, size_t, const void*, size_t
2008 Function - + memmgr_alloc_from_pool memmgr_aux_pool_alloc void* size_t
2009 Function + memmgr_aux_pool_get_free size_t
2010 Function + memmgr_get_free_heap size_t
2011 Function + memmgr_get_minimum_free_heap size_t
2012 Function + memmgr_get_total_heap size_t
2014 Function + memmgr_heap_enable_thread_trace void FuriThreadId
2015 Function + memmgr_heap_get_max_free_block size_t
2016 Function + memmgr_heap_get_thread_memory size_t FuriThreadId
2017 Function + memmgr_heap_printf_free_blocks memmgr_heap_walk_blocks void BlockWalker, void*
Function - memmgr_pool_get_free size_t
2018 Function - memmgr_pool_get_max_block size_t
2019 Function + memmove void* void*, const void*, size_t
2020 Function - mempcpy void* void*, const void*, size_t

View File

@@ -597,7 +597,7 @@ Function,-,acosh,double,double
Function,-,acoshf,float,float
Function,-,acoshl,long double,long double
Function,-,acosl,long double,long double
Function,-,aligned_alloc,void*,"size_t, size_t"
Function,+,aligned_alloc,void*,"size_t, size_t"
Function,-,arc4random,__uint32_t,
Function,-,arc4random_buf,void,"void*, size_t"
Function,-,arc4random_uniform,__uint32_t,__uint32_t
1 entry status name type params
597 Function - acoshf float float
598 Function - acoshl long double long double
599 Function - acosl long double long double
600 Function - + aligned_alloc void* size_t, size_t
601 Function - arc4random __uint32_t
602 Function - arc4random_buf void void*, size_t
603 Function - arc4random_uniform __uint32_t __uint32_t

View File

@@ -286,6 +286,47 @@ void MemManage_Handler(void) {
}
void BusFault_Handler(void) {
furi_log_puts("\r\n" _FURI_LOG_CLR_E "Bus fault:\r\n");
if(FURI_BIT(SCB->CFSR, SCB_CFSR_LSPERR_Pos)) {
furi_log_puts(" - lazy stacking for exception entry\r\n");
}
if(FURI_BIT(SCB->CFSR, SCB_CFSR_STKERR_Pos)) {
furi_log_puts(" - stacking for exception entry\r\n");
}
if(FURI_BIT(SCB->CFSR, SCB_CFSR_UNSTKERR_Pos)) {
furi_log_puts(" - unstacking for exception return\r\n");
}
if(FURI_BIT(SCB->CFSR, SCB_CFSR_IMPRECISERR_Pos)) {
furi_log_puts(" - imprecise data access\r\n");
}
if(FURI_BIT(SCB->CFSR, SCB_CFSR_PRECISERR_Pos)) {
furi_log_puts(" - precise data access\r\n");
}
if(FURI_BIT(SCB->CFSR, SCB_CFSR_IBUSERR_Pos)) {
furi_log_puts(" - instruction\r\n");
}
if(FURI_BIT(SCB->CFSR, SCB_CFSR_BFARVALID_Pos)) {
uint32_t busfault_address = SCB->BFAR;
furi_log_puts(" -- at 0x");
char tmp_str[] = "0xFFFFFFFF";
itoa(busfault_address, tmp_str, 16);
furi_log_puts(tmp_str);
furi_log_puts("\r\n");
if(busfault_address == (uint32_t)NULL) {
furi_log_puts(" -- NULL pointer dereference\r\n");
}
}
furi_log_puts(_FURI_LOG_CLR_RESET);
furi_crash("BusFault");
}