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