diff --git a/documentation/memory/figures/08smem_work4.png b/documentation/memory/figures/08smem_work4.png new file mode 100644 index 00000000000..92544169d03 Binary files /dev/null and b/documentation/memory/figures/08smem_work4.png differ diff --git a/documentation/memory/memory.md b/documentation/memory/memory.md index 892107fd751..2ac9fe30efb 100644 --- a/documentation/memory/memory.md +++ b/documentation/memory/memory.md @@ -43,7 +43,13 @@ Any or none of these memory heap management algorithms can be chosen when the sy ### Small Memory Management Algorithm -The small memory management algorithm is a simple memory allocation algorithm. Initially, it is a large piece of memory. When a memory block needs to be allocated, the matching memory block is segmented from the large memory block, and then this matching free memory block is returned to the heap management system. Each memory block contains data head for management use through which the used block and the free block are linked by a doubly linked list, as shown in the following figure: +The small memory management algorithm is a simple memory allocation algorithm. Initially, it is a large piece of memory. When a memory block needs to be allocated, the matching memory block is segmented from the large memory block, and then this matching free memory block is returned to the heap management system. Each memory block contains data head for management use through which the used block and the free block are linked by a doubly linked list. + + + +**Use this implementation before 4.1.0** + +As shown in the following figure: ![Small Memory Management Working Mechanism Diagram](figures/08smem_work.png) @@ -55,6 +61,22 @@ Each memory block (whether it is an allocated memory block or a free memory bloc The performance of memory management is mainly reflected in the allocation and release of memory. The small memory management algorithm can be embodied by the following examples. + + +**4.1.0 and later use this implementation** + +As shown in the following figure: + +![](figures/08smem_work4.png) + +**Heap Start**: The heap head address stores memory usage information, the heap head address pointer, the heap end address pointer, the minimum heap free address pointer, and the memory size. + +Each memory block (whether it is an allocated memory block or a free memory block) contains a data head, including: + +**pool_ptr**: Small memory object address. If the last bit of the memory block is 1, it is used. If the last bit of the memory block is 0, it is not used. When used, the structure members of the small memory algorithm can be quickly obtained by calc. + + + As shown in the following figure, the free list pointer lfree initially points to a 32-byte block of memory. When the user thread wants to allocate a 64-byte memory block, since the memory block pointed to by this lfree pointer is only 32 bytes and does not meet the requirements, the memory manager will continue to search for the next memory block. When the next memory block with 128 bytes is found, it meets the requirements of the allocation. Because this memory block is large, the allocator will split the memory block, and the remaining memory block (52 bytes) will remain in the lfree linked list, as shown in the following table which is after 64 bytes is allocated. ![Small Memory Management Algorithm Linked List Structure Diagram 1](figures/08smem_work2.png) diff --git a/examples/utest/testcases/kernel/mem_tc.c b/examples/utest/testcases/kernel/mem_tc.c index 028876dfa9f..aee9795117f 100644 --- a/examples/utest/testcases/kernel/mem_tc.c +++ b/examples/utest/testcases/kernel/mem_tc.c @@ -15,9 +15,6 @@ struct rt_small_mem_item { rt_ubase_t pool_ptr; /**< small memory object addr */ -#ifdef ARCH_CPU_64BIT - rt_uint32_t resv; -#endif /* ARCH_CPU_64BIT */ rt_size_t next; /**< next free item */ rt_size_t prev; /**< prev free item */ #ifdef RT_USING_MEMTRACE diff --git a/src/mem.c b/src/mem.c index 06c064c0e80..644eeb268c3 100644 --- a/src/mem.c +++ b/src/mem.c @@ -59,9 +59,6 @@ struct rt_small_mem_item { rt_ubase_t pool_ptr; /**< small memory object addr */ -#ifdef ARCH_CPU_64BIT - rt_uint32_t resv; -#endif /* ARCH_CPU_64BIT */ rt_size_t next; /**< next free item */ rt_size_t prev; /**< prev free item */ #ifdef RT_USING_MEMTRACE @@ -85,8 +82,6 @@ struct rt_small_mem rt_size_t mem_size_aligned; /**< aligned memory size */ }; -#define HEAP_MAGIC 0x1ea0 - #define MIN_SIZE (sizeof(rt_ubase_t) + sizeof(rt_size_t) + sizeof(rt_size_t)) #define MEM_MASK ((~(rt_size_t)0) - 1)