mm: Integrate TLSF manager
can be enabled by CONFIG_MM_TLSF_MANAGER=y Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com> Signed-off-by: anjiahao <anjiahao@xiaomi.com>
This commit is contained in:
parent
f5e8c30808
commit
a6428f4c27
@ -12,6 +12,11 @@ config MM_DEFAULT_MANAGER
|
||||
---help---
|
||||
NuttX original memory manager strategy.
|
||||
|
||||
config MM_TLSF_MANAGER
|
||||
bool "TLSF heap manager"
|
||||
---help---
|
||||
TLSF memory manager strategy.
|
||||
|
||||
config MM_CUSTOMIZE_MANAGER
|
||||
bool "Customized heap manager"
|
||||
---help---
|
||||
|
@ -32,6 +32,7 @@ include circbuf/Make.defs
|
||||
include mempool/Make.defs
|
||||
include kasan/Make.defs
|
||||
include ubsan/Make.defs
|
||||
include tlsf/Make.defs
|
||||
include map/Make.defs
|
||||
|
||||
BINDIR ?= bin
|
||||
@ -48,7 +49,7 @@ KBIN = libkmm$(LIBEXT)
|
||||
BIN ?= libmm$(LIBEXT)
|
||||
|
||||
all: $(BIN)
|
||||
.PHONY: clean distclean
|
||||
.PHONY: context depend clean distclean
|
||||
|
||||
$(AOBJS): $(BINDIR)$(DELIM)$(DELIM)%$(OBJEXT): %.S
|
||||
$(call ASSEMBLE, $<, $@)
|
||||
@ -73,6 +74,8 @@ endif
|
||||
|
||||
# Dependencies
|
||||
|
||||
context::
|
||||
|
||||
makedepfile: $(CSRCS:.c=.ddc) $(ASRCS:.S=.dds)
|
||||
$(call CATFILE, bin/Make.dep, $^)
|
||||
$(call DELFILE, $^)
|
||||
@ -101,7 +104,7 @@ clean:
|
||||
|
||||
# Deep clean -- removes all traces of the configuration
|
||||
|
||||
distclean: clean
|
||||
distclean:: clean
|
||||
$(Q) $(MAKE) -C bin distclean
|
||||
$(Q) $(MAKE) -C kbin distclean
|
||||
$(call DELFILE, bin$(DELIM)Make.dep)
|
||||
|
1
mm/tlsf/.gitignore
vendored
Normal file
1
mm/tlsf/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/tlsf
|
316
mm/tlsf/0001-Add-TLSF_API-and-tlsf_printf.patch
Normal file
316
mm/tlsf/0001-Add-TLSF_API-and-tlsf_printf.patch
Normal file
@ -0,0 +1,316 @@
|
||||
From f413f7d60212a925078748e800f381ced51b9e9a Mon Sep 17 00:00:00 2001
|
||||
From: Nodir Temirkhodjaev <nodir.temir@gmail.com>
|
||||
Date: Fri, 3 Jan 2020 14:41:23 +0500
|
||||
Subject: [PATCH 1/8] Add TLSF_API and tlsf_printf.
|
||||
|
||||
Needed for static building.
|
||||
---
|
||||
tlsf.c | 58 +++++++++++++++++++++++++++++++---------------------------
|
||||
tlsf.h | 46 ++++++++++++++++++++++++++--------------------
|
||||
2 files changed, 57 insertions(+), 47 deletions(-)
|
||||
|
||||
diff --git a/tlsf.c tlsf/tlsf/tlsf.c
|
||||
index af57573..e344dd5 100644
|
||||
--- a/tlsf.c
|
||||
+++ tlsf/tlsf/tlsf.c
|
||||
@@ -13,6 +13,10 @@
|
||||
#define tlsf_decl static
|
||||
#endif
|
||||
|
||||
+#if !defined(tlsf_printf)
|
||||
+#define tlsf_printf printf
|
||||
+#endif
|
||||
+
|
||||
/*
|
||||
** Architecture-specific bit manipulation routines.
|
||||
**
|
||||
@@ -841,7 +845,7 @@ static void integrity_walker(void* ptr, size_t size, int used, void* user)
|
||||
integ->status += status;
|
||||
}
|
||||
|
||||
-int tlsf_check(tlsf_t tlsf)
|
||||
+TLSF_API int tlsf_check(tlsf_t tlsf)
|
||||
{
|
||||
int i, j;
|
||||
|
||||
@@ -898,10 +902,10 @@ int tlsf_check(tlsf_t tlsf)
|
||||
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));
|
||||
+ tlsf_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_API void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void* user)
|
||||
{
|
||||
tlsf_walker pool_walker = walker ? walker : default_walker;
|
||||
block_header_t* block =
|
||||
@@ -918,7 +922,7 @@ void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void* user)
|
||||
}
|
||||
}
|
||||
|
||||
-size_t tlsf_block_size(void* ptr)
|
||||
+TLSF_API size_t tlsf_block_size(void* ptr)
|
||||
{
|
||||
size_t size = 0;
|
||||
if (ptr)
|
||||
@@ -929,7 +933,7 @@ size_t tlsf_block_size(void* ptr)
|
||||
return size;
|
||||
}
|
||||
|
||||
-int tlsf_check_pool(pool_t pool)
|
||||
+TLSF_API int tlsf_check_pool(pool_t pool)
|
||||
{
|
||||
/* Check that the blocks are physically correct. */
|
||||
integrity_t integ = { 0, 0 };
|
||||
@@ -942,22 +946,22 @@ int tlsf_check_pool(pool_t pool)
|
||||
** 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)
|
||||
+TLSF_API size_t tlsf_size(void)
|
||||
{
|
||||
return sizeof(control_t);
|
||||
}
|
||||
|
||||
-size_t tlsf_align_size(void)
|
||||
+TLSF_API size_t tlsf_align_size(void)
|
||||
{
|
||||
return ALIGN_SIZE;
|
||||
}
|
||||
|
||||
-size_t tlsf_block_size_min(void)
|
||||
+TLSF_API size_t tlsf_block_size_min(void)
|
||||
{
|
||||
return block_size_min;
|
||||
}
|
||||
|
||||
-size_t tlsf_block_size_max(void)
|
||||
+TLSF_API size_t tlsf_block_size_max(void)
|
||||
{
|
||||
return block_size_max;
|
||||
}
|
||||
@@ -967,17 +971,17 @@ size_t tlsf_block_size_max(void)
|
||||
** tlsf_add_pool, equal to the overhead of a free block and the
|
||||
** sentinel block.
|
||||
*/
|
||||
-size_t tlsf_pool_overhead(void)
|
||||
+TLSF_API size_t tlsf_pool_overhead(void)
|
||||
{
|
||||
return 2 * block_header_overhead;
|
||||
}
|
||||
|
||||
-size_t tlsf_alloc_overhead(void)
|
||||
+TLSF_API size_t tlsf_alloc_overhead(void)
|
||||
{
|
||||
return block_header_overhead;
|
||||
}
|
||||
|
||||
-pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes)
|
||||
+TLSF_API pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes)
|
||||
{
|
||||
block_header_t* block;
|
||||
block_header_t* next;
|
||||
@@ -987,7 +991,7 @@ pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes)
|
||||
|
||||
if (((ptrdiff_t)mem % ALIGN_SIZE) != 0)
|
||||
{
|
||||
- printf("tlsf_add_pool: Memory must be aligned by %u bytes.\n",
|
||||
+ tlsf_printf("tlsf_add_pool: Memory must be aligned by %u bytes.\n",
|
||||
(unsigned int)ALIGN_SIZE);
|
||||
return 0;
|
||||
}
|
||||
@@ -995,11 +999,11 @@ pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes)
|
||||
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",
|
||||
+ tlsf_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",
|
||||
+ tlsf_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
|
||||
@@ -1026,7 +1030,7 @@ pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes)
|
||||
return mem;
|
||||
}
|
||||
|
||||
-void tlsf_remove_pool(tlsf_t tlsf, pool_t pool)
|
||||
+TLSF_API 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);
|
||||
@@ -1046,7 +1050,7 @@ void tlsf_remove_pool(tlsf_t tlsf, pool_t pool)
|
||||
*/
|
||||
|
||||
#if _DEBUG
|
||||
-int test_ffs_fls()
|
||||
+static int test_ffs_fls()
|
||||
{
|
||||
/* Verify ffs/fls work properly. */
|
||||
int rv = 0;
|
||||
@@ -1067,13 +1071,13 @@ int test_ffs_fls()
|
||||
|
||||
if (rv)
|
||||
{
|
||||
- printf("test_ffs_fls: %x ffs/fls tests failed.\n", rv);
|
||||
+ tlsf_printf("test_ffs_fls: %x ffs/fls tests failed.\n", rv);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
#endif
|
||||
|
||||
-tlsf_t tlsf_create(void* mem)
|
||||
+TLSF_API tlsf_t tlsf_create(void* mem)
|
||||
{
|
||||
#if _DEBUG
|
||||
if (test_ffs_fls())
|
||||
@@ -1084,7 +1088,7 @@ tlsf_t tlsf_create(void* mem)
|
||||
|
||||
if (((tlsfptr_t)mem % ALIGN_SIZE) != 0)
|
||||
{
|
||||
- printf("tlsf_create: Memory must be aligned to %u bytes.\n",
|
||||
+ tlsf_printf("tlsf_create: Memory must be aligned to %u bytes.\n",
|
||||
(unsigned int)ALIGN_SIZE);
|
||||
return 0;
|
||||
}
|
||||
@@ -1094,25 +1098,25 @@ tlsf_t tlsf_create(void* mem)
|
||||
return tlsf_cast(tlsf_t, mem);
|
||||
}
|
||||
|
||||
-tlsf_t tlsf_create_with_pool(void* mem, size_t bytes)
|
||||
+TLSF_API 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_destroy(tlsf_t tlsf)
|
||||
+TLSF_API void tlsf_destroy(tlsf_t tlsf)
|
||||
{
|
||||
/* Nothing to do. */
|
||||
(void)tlsf;
|
||||
}
|
||||
|
||||
-pool_t tlsf_get_pool(tlsf_t tlsf)
|
||||
+TLSF_API pool_t tlsf_get_pool(tlsf_t tlsf)
|
||||
{
|
||||
return tlsf_cast(pool_t, (char*)tlsf + tlsf_size());
|
||||
}
|
||||
|
||||
-void* tlsf_malloc(tlsf_t tlsf, size_t size)
|
||||
+TLSF_API void* tlsf_malloc(tlsf_t tlsf, size_t size)
|
||||
{
|
||||
control_t* control = tlsf_cast(control_t*, tlsf);
|
||||
const size_t adjust = adjust_request_size(size, ALIGN_SIZE);
|
||||
@@ -1120,7 +1124,7 @@ void* tlsf_malloc(tlsf_t tlsf, size_t size)
|
||||
return block_prepare_used(control, block, adjust);
|
||||
}
|
||||
|
||||
-void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t size)
|
||||
+TLSF_API void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t size)
|
||||
{
|
||||
control_t* control = tlsf_cast(control_t*, tlsf);
|
||||
const size_t adjust = adjust_request_size(size, ALIGN_SIZE);
|
||||
@@ -1177,7 +1181,7 @@ void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t size)
|
||||
return block_prepare_used(control, block, adjust);
|
||||
}
|
||||
|
||||
-void tlsf_free(tlsf_t tlsf, void* ptr)
|
||||
+TLSF_API void tlsf_free(tlsf_t tlsf, void* ptr)
|
||||
{
|
||||
/* Don't attempt to free a NULL pointer. */
|
||||
if (ptr)
|
||||
@@ -1205,7 +1209,7 @@ void tlsf_free(tlsf_t tlsf, void* ptr)
|
||||
** - an extended buffer size will leave the newly-allocated area with
|
||||
** contents undefined
|
||||
*/
|
||||
-void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size)
|
||||
+TLSF_API void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size)
|
||||
{
|
||||
control_t* control = tlsf_cast(control_t*, tlsf);
|
||||
void* p = 0;
|
||||
diff --git a/tlsf.h tlsf/tlsf/tlsf.h
|
||||
index e9b5a91..c2c4161 100644
|
||||
--- a/tlsf.h
|
||||
+++ tlsf/tlsf/tlsf.h
|
||||
@@ -40,6 +40,12 @@
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
+/* Definition of the TLSF_API. */
|
||||
+/* Provide the ability to override linkage features of the interface. */
|
||||
+#if !defined(TLSF_API)
|
||||
+#define TLSF_API
|
||||
+#endif
|
||||
+
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
@@ -50,38 +56,38 @@ 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);
|
||||
+TLSF_API tlsf_t tlsf_create(void* mem);
|
||||
+TLSF_API tlsf_t tlsf_create_with_pool(void* mem, size_t bytes);
|
||||
+TLSF_API void tlsf_destroy(tlsf_t tlsf);
|
||||
+TLSF_API 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);
|
||||
+TLSF_API pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes);
|
||||
+TLSF_API 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);
|
||||
+TLSF_API void* tlsf_malloc(tlsf_t tlsf, size_t bytes);
|
||||
+TLSF_API void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t bytes);
|
||||
+TLSF_API void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size);
|
||||
+TLSF_API void tlsf_free(tlsf_t tlsf, void* ptr);
|
||||
|
||||
/* Returns internal block size, not original request size */
|
||||
-size_t tlsf_block_size(void* ptr);
|
||||
+TLSF_API 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);
|
||||
+TLSF_API size_t tlsf_size(void);
|
||||
+TLSF_API size_t tlsf_align_size(void);
|
||||
+TLSF_API size_t tlsf_block_size_min(void);
|
||||
+TLSF_API size_t tlsf_block_size_max(void);
|
||||
+TLSF_API size_t tlsf_pool_overhead(void);
|
||||
+TLSF_API 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);
|
||||
+TLSF_API 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);
|
||||
+TLSF_API int tlsf_check(tlsf_t tlsf);
|
||||
+TLSF_API int tlsf_check_pool(pool_t pool);
|
||||
|
||||
#if defined(__cplusplus)
|
||||
};
|
||||
--
|
||||
2.34.1
|
||||
|
31
mm/tlsf/0002-Define-_DEBUG-to-0-if-not-done-yet.patch
Normal file
31
mm/tlsf/0002-Define-_DEBUG-to-0-if-not-done-yet.patch
Normal file
@ -0,0 +1,31 @@
|
||||
From 9d731cb125205ead7b80ab6ddb89c250978a86eb Mon Sep 17 00:00:00 2001
|
||||
From: Xiang Xiao <xiaoxiang@xiaomi.com>
|
||||
Date: Wed, 10 Mar 2021 01:05:42 +0800
|
||||
Subject: [PATCH 2/8] Define _DEBUG to 0 if not done yet
|
||||
|
||||
to avoid the preprocess warning
|
||||
|
||||
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
|
||||
Change-Id: I4ae1eb8533563d377ec8614f0c9428c8734e1f2c
|
||||
---
|
||||
tlsf.c | 4 ++++
|
||||
1 file changed, 4 insertions(+)
|
||||
|
||||
diff --git a/tlsf.c tlsf/tlsf/tlsf.c
|
||||
index e344dd5..ea8d640 100644
|
||||
--- a/tlsf.c
|
||||
+++ tlsf/tlsf/tlsf.c
|
||||
@@ -7,6 +7,10 @@
|
||||
|
||||
#include "tlsf.h"
|
||||
|
||||
+#if !defined(_DEBUG)
|
||||
+#define _DEBUG 0
|
||||
+#endif
|
||||
+
|
||||
#if defined(__cplusplus)
|
||||
#define tlsf_decl inline
|
||||
#else
|
||||
--
|
||||
2.34.1
|
||||
|
@ -0,0 +1,95 @@
|
||||
From d2d18a9ed8836dac252f7c4c61541c2a9e4ebbf0 Mon Sep 17 00:00:00 2001
|
||||
From: Xiang Xiao <xiaoxiang@xiaomi.com>
|
||||
Date: Wed, 10 Mar 2021 02:30:33 +0800
|
||||
Subject: [PATCH 3/8] Support customize FL_INDEX_MAX to reduce the memory
|
||||
overhead
|
||||
|
||||
user can define the max pool size through TLSF_MAX_POOL_SIZE
|
||||
|
||||
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
|
||||
Change-Id: I021b816f65c1bc5c1025969bc6cc458029f3bc88
|
||||
---
|
||||
tlsf.c | 46 +++++++++++++++++++++++++++++-----------------
|
||||
1 file changed, 29 insertions(+), 17 deletions(-)
|
||||
|
||||
diff --git a/tlsf.c tlsf/tlsf/tlsf.c
|
||||
index ea8d640..66daf33 100644
|
||||
--- a/tlsf.c
|
||||
+++ tlsf/tlsf/tlsf.c
|
||||
@@ -48,6 +48,29 @@
|
||||
#define TLSF_64BIT
|
||||
#endif
|
||||
|
||||
+/*
|
||||
+** Returns one plus the index of the most significant 1-bit of n,
|
||||
+** or if n is zero, returns zero.
|
||||
+*/
|
||||
+#ifdef TLSF_64BIT
|
||||
+#define TLSF_FLS(n) ((n) & 0xffffffff00000000ull ? 32 + TLSF_FLS32((size_t)(n) >> 32) : TLSF_FLS32(n))
|
||||
+#else
|
||||
+#define TLSF_FLS(n) TLSF_FLS32(n)
|
||||
+#endif
|
||||
+
|
||||
+#define TLSF_FLS32(n) ((n) & 0xffff0000 ? 16 + TLSF_FLS16((n) >> 16) : TLSF_FLS16(n))
|
||||
+#define TLSF_FLS16(n) ((n) & 0xff00 ? 8 + TLSF_FLS8 ((n) >> 8) : TLSF_FLS8 (n))
|
||||
+#define TLSF_FLS8(n) ((n) & 0xf0 ? 4 + TLSF_FLS4 ((n) >> 4) : TLSF_FLS4 (n))
|
||||
+#define TLSF_FLS4(n) ((n) & 0xc ? 2 + TLSF_FLS2 ((n) >> 2) : TLSF_FLS2 (n))
|
||||
+#define TLSF_FLS2(n) ((n) & 0x2 ? 1 + TLSF_FLS1 ((n) >> 1) : TLSF_FLS1 (n))
|
||||
+#define TLSF_FLS1(n) ((n) & 0x1 ? 1 : 0)
|
||||
+
|
||||
+/*
|
||||
+** Returns round up value of log2(n).
|
||||
+** Note: it is used at compile time.
|
||||
+*/
|
||||
+#define TLSF_LOG2_CEIL(n) ((n) & (n - 1) ? TLSF_FLS(n) : TLSF_FLS(n) - 1)
|
||||
+
|
||||
/*
|
||||
** gcc 3.4 and above have builtin support, specialized for architecture.
|
||||
** Some compilers masquerade as gcc; patchlevel test filters them out.
|
||||
@@ -155,29 +178,16 @@ tlsf_decl int tlsf_fls(unsigned int word)
|
||||
#else
|
||||
/* Fall back to generic implementation. */
|
||||
|
||||
-tlsf_decl int tlsf_fls_generic(unsigned int word)
|
||||
-{
|
||||
- int bit = 32;
|
||||
-
|
||||
- if (!word) bit -= 1;
|
||||
- if (!(word & 0xffff0000)) { word <<= 16; bit -= 16; }
|
||||
- if (!(word & 0xff000000)) { word <<= 8; bit -= 8; }
|
||||
- if (!(word & 0xf0000000)) { word <<= 4; bit -= 4; }
|
||||
- if (!(word & 0xc0000000)) { word <<= 2; bit -= 2; }
|
||||
- if (!(word & 0x80000000)) { word <<= 1; bit -= 1; }
|
||||
-
|
||||
- return bit;
|
||||
-}
|
||||
-
|
||||
/* Implement ffs in terms of fls. */
|
||||
tlsf_decl int tlsf_ffs(unsigned int word)
|
||||
{
|
||||
- return tlsf_fls_generic(word & (~word + 1)) - 1;
|
||||
+ const unsigned int reverse = word & (~word + 1);
|
||||
+ return TLSF_FLS32(reverse) - 1;
|
||||
}
|
||||
|
||||
tlsf_decl int tlsf_fls(unsigned int word)
|
||||
{
|
||||
- return tlsf_fls_generic(word) - 1;
|
||||
+ return TLSF_FLS32(word) - 1;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -242,7 +252,9 @@ enum tlsf_private
|
||||
** blocks below that size into the 0th first-level list.
|
||||
*/
|
||||
|
||||
-#if defined (TLSF_64BIT)
|
||||
+#if defined (TLSF_MAX_POOL_SIZE)
|
||||
+ FL_INDEX_MAX = TLSF_LOG2_CEIL(TLSF_MAX_POOL_SIZE),
|
||||
+#elif defined (TLSF_64BIT)
|
||||
/*
|
||||
** TODO: We can increase this to support larger sizes, at the expense
|
||||
** of more overhead in the TLSF structure.
|
||||
--
|
||||
2.34.1
|
||||
|
127
mm/tlsf/0004-Add-tlsf_extend_pool-function.patch
Normal file
127
mm/tlsf/0004-Add-tlsf_extend_pool-function.patch
Normal file
@ -0,0 +1,127 @@
|
||||
From 76f37069b107ebc4dab50aa2ed553a3f751dc671 Mon Sep 17 00:00:00 2001
|
||||
From: Xiang Xiao <xiaoxiang@xiaomi.com>
|
||||
Date: Tue, 9 Mar 2021 21:54:16 +0800
|
||||
Subject: [PATCH 4/8] Add tlsf_extend_pool function
|
||||
|
||||
could work with sbrk to extend the pool size dynamically
|
||||
|
||||
Change-Id: I4f2cda419f88a31bc478e74813ebcd0d1275617c
|
||||
---
|
||||
tlsf.c | 56 ++++++++++++++++++++++++++++++++++++++++++--------------
|
||||
tlsf.h | 1 +
|
||||
2 files changed, 43 insertions(+), 14 deletions(-)
|
||||
|
||||
diff --git a/tlsf.c tlsf/tlsf/tlsf.c
|
||||
index 66daf33..6fd281a 100644
|
||||
--- a/tlsf.c
|
||||
+++ tlsf/tlsf/tlsf.c
|
||||
@@ -997,17 +997,18 @@ TLSF_API size_t tlsf_alloc_overhead(void)
|
||||
return block_header_overhead;
|
||||
}
|
||||
|
||||
-TLSF_API pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes)
|
||||
+TLSF_API pool_t tlsf_extend_pool(tlsf_t tlsf, void* mem, size_t bytes, size_t incr)
|
||||
{
|
||||
block_header_t* block;
|
||||
block_header_t* next;
|
||||
|
||||
+ control_t* control = tlsf_cast(control_t*, tlsf);
|
||||
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)
|
||||
{
|
||||
- tlsf_printf("tlsf_add_pool: Memory must be aligned by %u bytes.\n",
|
||||
+ tlsf_printf("tlsf_extend_pool: Memory must be aligned by %u bytes.\n",
|
||||
(unsigned int)ALIGN_SIZE);
|
||||
return 0;
|
||||
}
|
||||
@@ -1015,27 +1016,49 @@ TLSF_API pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes)
|
||||
if (pool_bytes < block_size_min || pool_bytes > block_size_max)
|
||||
{
|
||||
#if defined (TLSF_64BIT)
|
||||
- tlsf_printf("tlsf_add_pool: Memory size must be between 0x%x and 0x%x00 bytes.\n",
|
||||
+ tlsf_printf("tlsf_extend_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
|
||||
- tlsf_printf("tlsf_add_pool: Memory size must be between %u and %u bytes.\n",
|
||||
+ tlsf_printf("tlsf_extend_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);
|
||||
+ if (incr > 0 && incr < tlsf_block_size_min())
|
||||
+ {
|
||||
+ tlsf_printf("tlsf_extend_pool: Increased size must be at least %u bytes.\n",
|
||||
+ (unsigned int)tlsf_block_size_min());
|
||||
+ return 0;
|
||||
+ }
|
||||
+
|
||||
+ if (incr == 0) /* Initialize the pool */
|
||||
+ {
|
||||
+ /*
|
||||
+ ** 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(control, block);
|
||||
+ }
|
||||
+ else /* Extend the pool */
|
||||
+ {
|
||||
+ /* Extend the sentinel block */
|
||||
+ const size_t new_bytes = align_down((bytes + incr) -
|
||||
+ (pool_overhead + pool_bytes) - block_header_overhead, ALIGN_SIZE);
|
||||
+
|
||||
+ block = offset_to_block(mem, pool_bytes);
|
||||
+ block_set_size(block, new_bytes);
|
||||
+ block_set_free(block);
|
||||
+ block = block_merge_prev(control, block);
|
||||
+ block_insert(control, block);
|
||||
+ }
|
||||
|
||||
/* Split the block to create a zero-size sentinel block. */
|
||||
next = block_link_next(block);
|
||||
@@ -1046,6 +1069,11 @@ TLSF_API pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes)
|
||||
return mem;
|
||||
}
|
||||
|
||||
+TLSF_API pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes)
|
||||
+{
|
||||
+ return tlsf_extend_pool(tlsf, mem, bytes, 0);
|
||||
+}
|
||||
+
|
||||
TLSF_API void tlsf_remove_pool(tlsf_t tlsf, pool_t pool)
|
||||
{
|
||||
control_t* control = tlsf_cast(control_t*, tlsf);
|
||||
diff --git a/tlsf.h tlsf/tlsf/tlsf.h
|
||||
index c2c4161..085e053 100644
|
||||
--- a/tlsf.h
|
||||
+++ tlsf/tlsf/tlsf.h
|
||||
@@ -63,6 +63,7 @@ TLSF_API pool_t tlsf_get_pool(tlsf_t tlsf);
|
||||
|
||||
/* Add/remove memory pools. */
|
||||
TLSF_API pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes);
|
||||
+TLSF_API pool_t tlsf_extend_pool(tlsf_t tlsf, void* mem, size_t bytes, size_t incr);
|
||||
TLSF_API void tlsf_remove_pool(tlsf_t tlsf, pool_t pool);
|
||||
|
||||
/* malloc/memalign/realloc/free replacements. */
|
||||
--
|
||||
2.34.1
|
||||
|
@ -0,0 +1,26 @@
|
||||
From be043f1f50a0b30c3817c262d516083e409283d7 Mon Sep 17 00:00:00 2001
|
||||
From: Juan Carrano <j.carrano@fu-berlin.de>
|
||||
Date: Mon, 23 Apr 2018 13:55:42 +0200
|
||||
Subject: [PATCH 5/8] Fix warnining on implicit pointer conversion.
|
||||
|
||||
Change-Id: I2a208a0a4c835e752fe827acd3d5adb1aa2be626
|
||||
---
|
||||
tlsf.c | 2 +-
|
||||
1 file changed, 1 insertion(+), 1 deletion(-)
|
||||
|
||||
diff --git a/tlsf.c tlsf/tlsf/tlsf.c
|
||||
index 6fd281a..536bdff 100644
|
||||
--- a/tlsf.c
|
||||
+++ tlsf/tlsf/tlsf.c
|
||||
@@ -918,7 +918,7 @@ TLSF_API int tlsf_check(tlsf_t tlsf)
|
||||
static void default_walker(void* ptr, size_t size, int used, void* user)
|
||||
{
|
||||
(void)user;
|
||||
- tlsf_printf("\t%p %s size: %x (%p)\n", ptr, used ? "used" : "free", (unsigned int)size, block_from_ptr(ptr));
|
||||
+ tlsf_printf("\t%p %s size: %x (%p)\n", ptr, used ? "used" : "free", (unsigned int)size, (void *)block_from_ptr(ptr));
|
||||
}
|
||||
|
||||
TLSF_API void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void* user)
|
||||
--
|
||||
2.34.1
|
||||
|
47
mm/tlsf/Make.defs
Normal file
47
mm/tlsf/Make.defs
Normal file
@ -0,0 +1,47 @@
|
||||
############################################################################
|
||||
# mm/tlsf/Make.defs
|
||||
#
|
||||
# Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
# contributor license agreements. See the NOTICE file distributed with
|
||||
# this work for additional information regarding copyright ownership. The
|
||||
# ASF licenses this file to you 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.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
# tlfs memory allocator
|
||||
|
||||
ifeq ($(CONFIG_MM_TLSF_MANAGER),y)
|
||||
|
||||
TLSF = tlsf/tlsf/.git
|
||||
$(TLSF):
|
||||
$(Q) echo "Downloading: tlsf"
|
||||
$(Q) git clone git@github.com:mattconte/tlsf.git tlsf/tlsf
|
||||
$(Q) patch -p0 < tlsf/0001-Add-TLSF_API-and-tlsf_printf.patch
|
||||
$(Q) patch -p0 < tlsf/0002-Define-_DEBUG-to-0-if-not-done-yet.patch
|
||||
$(Q) patch -p0 < tlsf/0003-Support-customize-FL_INDEX_MAX-to-reduce-the-memory-.patch
|
||||
$(Q) patch -p0 < tlsf/0004-Add-tlsf_extend_pool-function.patch
|
||||
$(Q) patch -p0 < tlsf/0005-Fix-warnining-on-implicit-pointer-conversion.patch
|
||||
context::$(TLSF)
|
||||
|
||||
distclean::
|
||||
$(Q) rm -rf tlsf/tlsf
|
||||
|
||||
CSRCS += mm_tlsf.c tlsf.c
|
||||
|
||||
CFLAGS += ${shell $(DEFINE) "$(CC)" tlsf_printf=if(0)}
|
||||
|
||||
# Add the tlsf directory to the build
|
||||
|
||||
DEPPATH += --dep-path tlsf --dep-path tlsf/tlsf
|
||||
VPATH += :tlsf:tlsf/tlsf
|
||||
endif
|
1111
mm/tlsf/mm_tlsf.c
Normal file
1111
mm/tlsf/mm_tlsf.c
Normal file
File diff suppressed because it is too large
Load Diff
@ -164,4 +164,11 @@ else
|
||||
CLEANDIRS += openamp
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_MM_TLSF_MANAGER),y)
|
||||
KERNDEPDIRS += mm
|
||||
CONTEXTDIRS += mm
|
||||
else
|
||||
CLEANDIRS += mm
|
||||
endif
|
||||
|
||||
CLEANDIRS += $(KERNDEPDIRS) $(USERDEPDIRS)
|
||||
|
Loading…
x
Reference in New Issue
Block a user