32

FreeRTOS移植SLAB内存管理

 4 years ago
source link: https://www.taterli.com/7122/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

之前一直用连续内存管理,要不碎片化严重,要不分配效率低下,最近读内核源码发现slab/slub/slob这个内存管理器,虽然现在已经多年没人维护,主流已经切换为slub,但是在单片机只有几MB片外内存来说,SLAB还是不错的,而且,已经有rtt移植了slab,就更方便我继续参考移植.

在STM32H769NI和LPC4357FET256测试OK.

驱动文件:

https://gist.github.com/nickfox-taterli/b1509673edc25ece10c6d8e3a927af48

头文件

#ifndef _BSP_SLAB_H_
#define _BSP_SLAB_H_

#include "<单片机特定头>.h"
#include "string.h"

#define SLAB_MEM_STATS
#define MM_PAGE_SIZE 4096
#define MM_PAGE_MASK                 (MM_PAGE_SIZE - 1)
#define MM_PAGE_BITS                 12

void slab_system_heap_init(void *begin_addr, void *end_addr);
void *slab_malloc(uint32_t size);
void *slab_realloc(void *ptr, uint32_t size);
void *slab_calloc(uint32_t count, uint32_t size);
void slab_free(void *ptr);

#ifdef SLAB_MEM_STATS
void slab_memory_info(uint32_t *total,uint32_t *used,uint32_t *max_used);
#endif

#endif

使用示例:

// SDRAM地址范围:0xA0000000 - 0xA2000000
slab_system_heap_init((void *)0xA0000000, (void *)0xA2000000);

for (;;)
{
    slab_memory_info(&total, &used, &max_used);
    buf = slab_malloc(3000);
    slab_memory_info(&total, &used, &max_used);
    slab_free(buf);
    vTaskDelay(1000);
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK