[feat][tlsf] add tlsf component

This commit is contained in:
jzlv 2022-12-08 11:02:49 +08:00
parent d9adf21997
commit 05e403062c
15 changed files with 1765 additions and 55 deletions

View file

@ -6,8 +6,9 @@ sdk_add_subdirectory_ifdef(CONFIG_BFLOG bflog)
sdk_add_subdirectory_ifdef(CONFIG_LUA lua)
sdk_add_subdirectory_ifdef(CONFIG_LVGL lvgl)
sdk_add_subdirectory_ifdef(CONFIG_LWIP lwip)
sdk_add_subdirectory_ifdef(CONFIG_BLE ble)
sdk_add_subdirectory_ifdef(CONFIG_BLUETOOTH bluetooth)
sdk_add_subdirectory_ifdef(CONFIG_XZ xz)
sdk_add_subdirectory_ifdef(CONFIG_TINYMAIX TinyMaix)
sdk_add_subdirectory_ifdef(CONFIG_TENSORFLOWLITE TensorFlowLite)
sdk_add_subdirectory_ifdef(CONFIG_TLSF tlsf)
add_subdirectory(utils)

View file

@ -0,0 +1,4 @@
sdk_generate_library()
sdk_library_add_sources(tlsf.c bflb_tlsf.c)
sdk_add_include_directories(.)
sdk_add_compile_definitions(-DCONFIG_TLSF)

View file

@ -0,0 +1,59 @@
#include "tlsf.h"
#include "bflb_tlsf.h"
static tlsf_t tlsf_ptr = 0;
int bflb_mmheap_init(void *begin_addr, uint32_t size)
{
tlsf_ptr = (tlsf_t)tlsf_create_with_pool(begin_addr, size);
if (tlsf_ptr == 0) {
return -1;
}
return 0;
}
void *bflb_malloc(size_t nbytes)
{
void *ptr;
ptr = tlsf_malloc(tlsf_ptr, nbytes);
return ptr;
}
void bflb_free(void *ptr)
{
tlsf_free(tlsf_ptr, ptr);
}
void *bflb_realloc(void *ptr, size_t nbytes)
{
if (tlsf_ptr) {
ptr = tlsf_realloc(tlsf_ptr, ptr, nbytes);
}
return ptr;
}
void *bflb_calloc(size_t count, size_t nbytes)
{
void *ptr = NULL;
size_t total_size;
total_size = count * nbytes;
ptr = tlsf_malloc(tlsf_ptr, nbytes);
if (ptr != NULL) {
/* clean memory */
memset(ptr, 0, total_size);
}
return ptr;
}
void *bflb_malloc_align(size_t align, size_t size)
{
void *ptr = NULL;
ptr = tlsf_memalign(tlsf_ptr, align, size);
return ptr;
}

View file

@ -0,0 +1,14 @@
#ifndef BFLB_TLSF_H
#define BFLB_TLSF_H
#include <stdint.h>
#include <string.h>
int bflb_mmheap_init(void *begin_addr, uint32_t size);
void *bflb_malloc(size_t nbytes);
void bflb_free(void *ptr);
void *bflb_realloc(void *ptr, size_t nbytes);
void *bflb_calloc(size_t count, size_t size);
void *bflb_malloc_align(size_t align, size_t size);
#endif

1286
components/tlsf/tlsf.c Normal file

File diff suppressed because it is too large Load diff

90
components/tlsf/tlsf.h Normal file
View file

@ -0,0 +1,90 @@
#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.
*/
#include <stddef.h>
#if defined(__cplusplus)
extern "C" {
#endif
/* 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);
#if defined(__cplusplus)
};
#endif
#endif