Skip to content

Commit fe5d506

Browse files
committed
【增加】memheap.c
1 parent 464d0c5 commit fe5d506

File tree

1 file changed

+130
-11
lines changed

1 file changed

+130
-11
lines changed

src/memheap.c

Lines changed: 130 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@
3939
#define MEMITEM(ptr) (struct rt_memheap_item*)((rt_uint8_t*)ptr - RT_MEMHEAP_SIZE)
4040

4141
#ifdef RT_USING_MEMTRACE
42+
/**
43+
* @brief This function will set a new name for memheap.
44+
*
45+
* @param item is a pointer point to a memheap object.
46+
*
47+
* @param name is the new name to be set.
48+
*/
4249
rt_inline void rt_memheap_setname(struct rt_memheap_item *item, const char *name)
4350
{
4451
int index;
@@ -64,6 +71,13 @@ rt_inline void rt_memheap_setname(struct rt_memheap_item *item, const char *name
6471
}
6572
}
6673

74+
/**
75+
* @brief This function will set a new name for memheap.
76+
*
77+
* @param ptr is a pointer point to a memheap object.
78+
*
79+
* @param name is the new name to be set.
80+
*/
6781
void rt_mem_set_tag(void *ptr, const char *name)
6882
{
6983
struct rt_memheap_item *item;
@@ -76,16 +90,28 @@ void rt_mem_set_tag(void *ptr, const char *name)
7690
}
7791
#endif /* RT_USING_MEMTRACE */
7892

79-
/*
80-
* The initialized memory pool will be:
81-
* +-----------------------------------+--------------------------+
82-
* | whole freed memory block | Used Memory Block Tailer |
83-
* +-----------------------------------+--------------------------+
93+
/**
94+
* @brief This function initializes a piece of memory called memheap.
95+
*
96+
* @note The initialized memory pool will be:
97+
* +-----------------------------------+--------------------------+
98+
* | whole freed memory block | Used Memory Block Tailer |
99+
* +-----------------------------------+--------------------------+
100+
*
101+
* block_list --> whole freed memory block
102+
*
103+
* The length of Used Memory Block Tailer is 0,
104+
* which is prevents block merging across list
105+
*
106+
* @param memheap is a pointer of the memheap object.
107+
*
108+
* @param name is the name of the memheap.
109+
*
110+
* @param start_addr is the start address of the memheap.
84111
*
85-
* block_list --> whole freed memory block
112+
* @param size is the size of the memheap.
86113
*
87-
* The length of Used Memory Block Tailer is 0,
88-
* which is prevents block merging across list
114+
* @return RT_EOK
89115
*/
90116
rt_err_t rt_memheap_init(struct rt_memheap *memheap,
91117
const char *name,
@@ -165,6 +191,13 @@ rt_err_t rt_memheap_init(struct rt_memheap *memheap,
165191
}
166192
RTM_EXPORT(rt_memheap_init);
167193

194+
/**
195+
* @brief This function will remove a memheap from the system.
196+
*
197+
* @param heap is a pointer of memheap object.
198+
*
199+
* @return RT_EOK
200+
*/
168201
rt_err_t rt_memheap_detach(struct rt_memheap *heap)
169202
{
170203
RT_ASSERT(heap);
@@ -179,6 +212,15 @@ rt_err_t rt_memheap_detach(struct rt_memheap *heap)
179212
}
180213
RTM_EXPORT(rt_memheap_detach);
181214

215+
/**
216+
* @brief Allocate a block of memory with a minimum of 'size' bytes on memheap.
217+
*
218+
* @param heap is a pointer for memheap object.
219+
*
220+
* @param size is the minimum size of the requested block in bytes.
221+
*
222+
* @return the pointer to allocated memory or NULL if no free memory was found.
223+
*/
182224
void *rt_memheap_alloc(struct rt_memheap *heap, rt_size_t size)
183225
{
184226
rt_err_t result;
@@ -336,6 +378,18 @@ void *rt_memheap_alloc(struct rt_memheap *heap, rt_size_t size)
336378
}
337379
RTM_EXPORT(rt_memheap_alloc);
338380

381+
/**
382+
* @brief This function will change the size of previously allocated memory block.
383+
*
384+
* @param heap is a pointer to the memheap object, which will reallocate
385+
* memory from the block
386+
*
387+
* @param ptr is a pointer to start address of memory.
388+
*
389+
* @param newsize is the required new size.
390+
*
391+
* @return the changed memory block address.
392+
*/
339393
void *rt_memheap_realloc(struct rt_memheap *heap, void *ptr, rt_size_t newsize)
340394
{
341395
rt_err_t result;
@@ -556,6 +610,12 @@ void *rt_memheap_realloc(struct rt_memheap *heap, void *ptr, rt_size_t newsize)
556610
}
557611
RTM_EXPORT(rt_memheap_realloc);
558612

613+
/**
614+
* @brief This function will release the allocated memory block by
615+
* rt_malloc. The released memory block is taken back to system heap.
616+
*
617+
* @param ptr the address of memory which will be released.
618+
*/
559619
void rt_memheap_free(void *ptr)
560620
{
561621
rt_err_t result;
@@ -680,7 +740,13 @@ static void _memheap_dump_tag(struct rt_memheap_item *item)
680740

681741
rt_kprintf("%.*s", 2 * sizeof(void *), name);
682742
}
683-
743+
/**
744+
* @brief This function will print the memheap infomation.
745+
*
746+
* @param heap is the pointer to the memheap to get information.
747+
*
748+
* @return 0
749+
*/
684750
int rt_memheap_dump(struct rt_memheap *heap)
685751
{
686752
struct rt_memheap_item *item, *end;
@@ -757,6 +823,13 @@ MSH_CMD_EXPORT(memheaptrace, dump memory trace information);
757823
#ifdef RT_USING_MEMHEAP_AS_HEAP
758824
static struct rt_memheap _heap;
759825

826+
/**
827+
* @brief This function initializes a heap for system.
828+
*
829+
* @param begin_addr is the start address of the memory.
830+
*
831+
* @param end_addr is the end address of the memory.
832+
*/
760833
void rt_system_heap_init(void *begin_addr, void *end_addr)
761834
{
762835
RT_ASSERT((rt_uint32_t)end_addr > (rt_uint32_t)begin_addr);
@@ -768,6 +841,11 @@ void rt_system_heap_init(void *begin_addr, void *end_addr)
768841
(rt_uint32_t)end_addr - (rt_uint32_t)begin_addr);
769842
}
770843

844+
/**
845+
* @brief Allocate a block of memory with a minimum of 'size' bytes.
846+
*
847+
* @param size is the minimum size of the requested block in bytes.
848+
*/
771849
void *rt_malloc(rt_size_t size)
772850
{
773851
void *ptr;
@@ -804,7 +882,6 @@ void *rt_malloc(rt_size_t size)
804882
}
805883
}
806884

807-
808885
#ifdef RT_USING_MEMTRACE
809886
if (ptr == RT_NULL)
810887
{
@@ -826,12 +903,27 @@ void *rt_malloc(rt_size_t size)
826903
}
827904
RTM_EXPORT(rt_malloc);
828905

906+
/**
907+
* @brief This function will release the previously allocated memory block by
908+
* rt_malloc. The released memory block is taken back to system heap.
909+
*
910+
* @param rmem the address of memory which will be released.
911+
*/
829912
void rt_free(void *rmem)
830913
{
831914
rt_memheap_free(rmem);
832915
}
833916
RTM_EXPORT(rt_free);
834917

918+
/**
919+
* @brief This function will change the size of previously allocated memory block.
920+
*
921+
* @param rmem is the pointer to memory allocated by rt_malloc.
922+
*
923+
* @param newsize is the required new size.
924+
*
925+
* @return the changed memory block address.
926+
*/
835927
void *rt_realloc(void *rmem, rt_size_t newsize)
836928
{
837929
void *new_ptr;
@@ -892,6 +984,19 @@ void *rt_realloc(void *rmem, rt_size_t newsize)
892984
}
893985
RTM_EXPORT(rt_realloc);
894986

987+
/**
988+
* @brief This function will contiguously allocate enough space for count objects
989+
* that are size bytes of memory each and returns a pointer to the allocated
990+
* memory.
991+
*
992+
* @note The allocated memory is filled with bytes of value zero.
993+
*
994+
* @param count is the number of objects to allocate.
995+
*
996+
* @param size is the size of one object to allocate.
997+
*
998+
* @return pointer to allocated memory pointer.
999+
*/
8951000
void *rt_calloc(rt_size_t count, rt_size_t size)
8961001
{
8971002
void *ptr;
@@ -922,6 +1027,16 @@ void *rt_calloc(rt_size_t count, rt_size_t size)
9221027
}
9231028
RTM_EXPORT(rt_calloc);
9241029

1030+
/**
1031+
* @brief This function will caculate the total memory, the used memory, and
1032+
* the max used memory.
1033+
*
1034+
* @param total is a pointer to get the total size of the memory.
1035+
*
1036+
* @param used is a pointer to get the size of memory used.
1037+
*
1038+
* @param max_used is a pointer to get the maximum memory used.
1039+
*/
9251040
void rt_memory_info(rt_uint32_t *total,
9261041
rt_uint32_t *used,
9271042
rt_uint32_t *max_used)
@@ -940,12 +1055,16 @@ void rt_memory_info(rt_uint32_t *total,
9401055

9411056
#ifdef RT_USING_MEMTRACE
9421057

1058+
/**
1059+
* @brief This function will print the used memheap infomation.
1060+
*
1061+
* @param memheap is a pointer of the memheap object.
1062+
*/
9431063
void dump_used_memheap(struct rt_memheap *mh)
9441064
{
9451065
struct rt_memheap_item *header_ptr;
9461066
rt_uint32_t block_size;
9471067

948-
9491068
rt_kprintf("\nmemory heap address:\n");
9501069
rt_kprintf("heap_ptr: 0x%08x\n", mh->start_addr);
9511070
rt_kprintf("free : 0x%08x\n", mh->available_size);

0 commit comments

Comments
 (0)