博客專欄

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

        干貨 | 分享一個實用的、可應用于單片機的內存管理模塊

        發布人:傳感器技術 時間:2021-07-11 來源:工程師 發布文章

        本次給大家分享一位大佬寫的應用于單片機內存管理模塊mem_malloc,這個mem_malloc的使用不會產生內存碎片,可以高效利用單片機ram空間。

        mem_malloc代碼倉庫:

        ?

        https://github.com/chenqy2018/mem_malloc

        ?
        mem_malloc介紹

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

        mem_malloc就是一個不會產生內存碎片的、適合單片機使用的內存管理模塊。其與使用malloc的區別如

        「算法原理:」

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

        mem_malloc測試驗證

        下面以小熊派IOT開發板來做實驗。

        實驗過程很簡單。準備一份開發板帶串口打印的工程,下載mem_malloc,把mem_malloc.c、mem_malloc.h復制到工程目錄下,并添加到工程里:

        然后進行編譯,編譯過程可能會報錯:

        ..\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 <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,進行獲取。

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

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

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

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

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

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


        *博客內容為網友個人發布,僅代表博主個人觀點,如有侵權請聯系工作人員刪除。



        關鍵詞: 技術

        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 兰西县| 太谷县| 沭阳县| 雷波县| 孝昌县| 汝城县| 托克逊县| 田东县| 将乐县| 碌曲县| 化隆| 铁岭市| 灌云县| 惠东县| 阳春市| 比如县| 德格县| 文山县| 福贡县| 阿克| 德令哈市| 印江| 香格里拉县| 治多县| 海口市| 连江县| 维西| 来凤县| 宁安市| 贺兰县| 句容市| 大关县| 永吉县| 长顺县| 介休市| 松原市| 白玉县| 贡嘎县| 陇川县| 同仁县| 吉木乃县|