Skip to content

Commit

Permalink
[mem] Remove useless code And Update mem documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
wdfk-prog authored and mysterywolf committed Apr 19, 2024
1 parent 9229aee commit d8dcc05
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
Binary file added documentation/memory/figures/08smem_work4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 23 additions & 1 deletion documentation/memory/memory.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check failure on line 54 in documentation/memory/memory.md

View workflow job for this annotation

GitHub Actions / Check Spelling

`smem` is not a recognized word. (unrecognized-spelling)

Expand All @@ -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.

Check failure on line 80 in documentation/memory/memory.md

View workflow job for this annotation

GitHub Actions / Check Spelling

`lfree` is not a recognized word. (unrecognized-spelling)

![Small Memory Management Algorithm Linked List Structure Diagram 1](figures/08smem_work2.png)
Expand Down
3 changes: 0 additions & 3 deletions examples/utest/testcases/kernel/mem_tc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions src/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit d8dcc05

Please sign in to comment.