<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>
"); //-->

博客專(zhuān)欄

EEPW首頁(yè) > 博客 > 干貨 | 分享一個(gè)實(shí)用的、可應用于單片機的內存管理模塊

干貨 | 分享一個(gè)實(shí)用的、可應用于單片機的內存管理模塊

發(fā)布人:傳感器技術(shù) 時(shí)間:2021-07-11 來(lái)源:工程師 發(fā)布文章

本次給大家分享一位大佬寫(xiě)的應用于單片機內存管理模塊mem_malloc,這個(gè)mem_malloc的使用不會(huì )產(chǎn)生內存碎片,可以高效利用單片機ram空間。

mem_malloc代碼倉庫:

?

https://github.com/chenqy2018/mem_malloc

?
mem_malloc介紹

一般單片機的內存都比較小,而且沒(méi)有MMU,malloc 與free的使用容易造成內存碎片。而且可能因為空間不足而分配失敗,從而導致系統崩潰,因此應該慎用,或者自己實(shí)現內存管理。

mem_malloc就是一個(gè)不會(huì )產(chǎn)生內存碎片的、適合單片機使用的內存管理模塊。其與使用malloc的區別如

「算法原理:」

定義一個(gè)數組作為動(dòng)態(tài)分配的堆空間,低地址空間保存管理數據,高地址空間實(shí)際分配給用戶(hù)的緩存(類(lèi)似堆棧使用,分配是往中間靠攏),free時(shí)移動(dòng)高地址用戶(hù)空間(以時(shí)間換空間),使得未使用的空間都是連續的。

mem_malloc測試驗證

下面以小熊派IOT開(kāi)發(fā)板來(lái)做實(shí)驗。

實(shí)驗過(guò)程很簡(jiǎn)單。準備一份開(kāi)發(fā)板帶串口打印的工程,下載mem_malloc,把mem_malloc.c、mem_malloc.h復制到工程目錄下,并添加到工程里:

然后進(jìn)行編譯,編譯過(guò)程可能會(huì )報錯:

..\Src\mem_malloc.c(119): error:  #852: expression must be a pointer to a complete object type

這份代碼在不同編譯器下編譯情況不同。gcc下編譯不會(huì )報錯,在keil下編譯報如上錯誤。

keil編譯器更嚴格些。報錯原因是對mem_block結構體的mem_ptr成員進(jìn)行操作,而mem_ptr成員的類(lèi)型是void*,而mem_ptr成員參與運算時(shí)的增、減偏移量取決于mem_ptr的類(lèi)型,所以這里我們需要指定類(lèi)型。

我們把相關(guān)報錯代碼修改如:

圖片

再次編譯就正常了。

下面簡(jiǎn)單看一下mem_malloc的代碼。

「mem_malloc.h:」

#ifndef __MEM_MALLOC_H__
#define __MEM_MALLOC_H__

#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h> 
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#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,進(jìn)行獲取。

下面對mem_malloc進(jìn)行測試驗證。

測試代碼作者也有給出,這里我們簡(jiǎn)單測試即可,進(jìn)行了一些刪減及增加了一些注釋?zhuān)?/p>

#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(110); // 給申請一塊10個(gè)字節的內存,標記內存塊id為1
 test_malloc(28); // 給申請一塊8個(gè)字節的內存,標記內存塊id為2
 test_malloc(320); // 給申請一塊20個(gè)字節的內存,標記內存塊id為2

 test_free(2);  // 釋放id為2的內存塊的內存

 test_malloc(470); // 申請一塊70個(gè)字節的內存

 test_free(1);       // 釋放id為1的內存塊內存

 test_buffer(320); // 獲取id為3的內存塊地址,并往這個(gè)內存塊重新寫(xiě)入0xf0+i的數據

 test_realloc(310); // 重新分配內存,并往這個(gè)內存塊重新寫(xiě)入0xa0+i的數據
 
 for(int i=0; i<10; i++)  // 釋放所有內存塊內存,已釋放的不再重新釋放
  test_free(i);


*博客內容為網(wǎng)友個(gè)人發(fā)布,僅代表博主個(gè)人觀(guān)點(diǎn),如有侵權請聯(lián)系工作人員刪除。



關(guān)鍵詞: 技術(shù)

相關(guān)推薦

技術(shù)專(zhuān)區

關(guān)閉
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放
<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>