?https://github.com/chenqy2018/mem_malloc?
mem_malloc介紹
一般單片機的內存都比較小,而且沒有MMU,malloc 與free的使用容易造成內存碎片。而且可能因為空間不足而分配失敗,從而導致系統(tǒng)崩潰,因此應該慎用,或者自己實現(xiàn)內存管理。
mem_malloc測試驗證
下面以小熊派IOT開發(fā)板來做實驗。
..\Src\mem_malloc.c(119): error: #852: expression must be a pointer to a complete object type
這份代碼在不同編譯器下編譯情況不同。gcc下編譯不會報錯,在keil下編譯報如上錯誤。
keil編譯器更嚴格些。報錯原因是對mem_block結構體的mem_ptr成員進行操作,而mem_ptr成員的類型是void*,而mem_ptr成員參與運算時的增、減偏移量取決于mem_ptr的類型,所以這里我們需要指定類型。
我們把相關報錯代碼修改如:
再次編譯就正常了。
下面簡單看一下mem_malloc的代碼。
「mem_malloc.h:」
#ifndef __MEM_MALLOC_H__
#define __MEM_MALLOC_H__
#ifdef __cplusplus
extern "C" {
#endif
#include
#include
#include
#include
#include
#pragma pack(1)
typedef struct mem_block
{
void *mem_ptr;
unsigned int mem_size;
unsigned int mem_index;
}mem_block;
#pragma pack()
#define MEM_SIZE 128
void print_mem_info(void);
void print_hex(char *data, int len);
void print_mem_hex(int size);
int mem_malloc(unsigned int msize);
int mem_realloc(int id, unsigned int msize);
void *mem_buffer(int id);
int mem_free(int id);
#ifdef __cplusplus
}
#endif
#endif
「mem_malloc.c:」
暫不貼出,感興趣的小伙伴可以在上面的倉庫地址自行下載閱讀。在本公眾號后臺回復:mem_malloc,進行獲取。
下面對mem_malloc進行測試驗證。
測試代碼作者也有給出,這里我們簡單測試即可,進行了一些刪減及增加了一些注釋:
#include "mem_malloc.h"
char mem_id[10]={0}; // 10塊內存塊
void test_malloc(int i, int size)
{
printf("------test_malloc-------\n");
mem_id[i] = mem_malloc(size);
if(mem_id[i] == 0)
{
printf("malloc --- fail\n");
printf("size=%d\n", size);
}
else
{
char *p = mem_buffer(mem_id[i]);
memset(p, i, size);
printf("p = 0x%x, i=%d, id=%d, size=%d\n", (int)p, i, mem_id[i], size);
}
print_mem_hex(MEM_SIZE);
}
void test_buffer(int i, int size)
{
printf("------test_buffer-------\n");
printf("i=%d, id = %d, size=%d\n", i, mem_id[i], size);
char *p = mem_buffer(mem_id[i]);
if(p != NULL)
{
memset(p, 0xf0 i, size);
print_mem_hex(MEM_SIZE);
}
else
{
printf("test_buffer---fail\n");
}
}
void test_realloc(int i, int size)
{
printf("------test_realloc-------\n");
printf("i=%d, id = %d, size=%d\n", i, mem_id[i], size);
int ret = mem_realloc(mem_id[i], size);
if(ret)
{
char *p = mem_buffer(mem_id[i]);
memset(p, 0xa0 i, size);
print_mem_hex(MEM_SIZE);
}
else
{
printf("test_realloc---fail\n");
}
}
void test_free(int i)
{
printf("------test_free-------\n");
printf("i=%d, id = %d\n", i, mem_id[i]);
if(mem_free(mem_id[i]))
print_mem_hex( MEM_SIZE);
}
void main(void)
{
print_mem_info(); // 打印內存信息
test_malloc(1, 10); // 給申請一塊10個字節(jié)的內存,標記內存塊id為1
test_malloc(2, 8); // 給申請一塊8個字節(jié)的內存,標記內存塊id為2
test_malloc(3, 20); // 給申請一塊20個字節(jié)的內存,標記內存塊id為2
test_free(2); // 釋放id為2的內存塊的內存
test_malloc(4, 70); // 申請一塊70個字節(jié)的內存
test_free(1); // 釋放id為1的內存塊內存
test_buffer(3, 20); // 獲取id為3的內存塊地址,并往這個內存塊重新寫入0xf0 i的數(shù)據(jù)
test_realloc(3, 10); // 重新分配內存,并往這個內存塊重新寫入0xa0 i的數(shù)據(jù)
for(int i=0; i<10; i ) // 釋放所有內存塊內存,已釋放的不再重新釋放
test_free(i);
}
運行結果及解析:
這里設定一個128字節(jié)的數(shù)組作為堆空間使用。其中數(shù)組前面存放的是申請到的內存塊的信息,包括內存塊地址、大小、索引信息,這三個數(shù)據(jù)各占4個字節(jié),共12個字節(jié)。這里有設計到一個大小端模式的問題,STM32平臺為小端模式,即數(shù)據(jù)的低位存儲在內存的低地址中。
申請的內存塊從128字節(jié)的尾部開始分配,再次申請的內存塊依次往前移,釋放的內存,則整體內存塊往后移動,內存塊之前不留空隙,即不產生內存碎片。
以上就是本次的分享,如有錯誤,歡迎指出,謝謝!
???????????????? END ????????????????關注我的微信公眾號,回復“加群”按規(guī)則加入技術交流群。
單片機、開發(fā)工具、編程技術、行業(yè)資訊等相關內容。孵化編程,樂于分享,講原理,摳細節(jié),究根源,歡迎關注。" data-from="0"> 歡迎關注我的視頻號:
點擊“閱讀原文”查看更多分享,歡迎點分享、收藏、點贊、在看。