标签:min ati ptr 第一个 结果 过程 cts 释放内存 连接
RT-Thread 操作系统在内存管理上,根据上层应用及系统资源的不同,有针对性地提供了不同的内存分配管理算法。总体上可分为两类:内存堆管理与内存池管理,而内存堆管理又根据具体内存设备划分为三种情况:
第一种是针对小内存块的分配管理(小内存管理算法);
第二种是针对大内存块的分配管理(slab 管理算法);
第三种是针对多内存堆的分配情况(memheap 管理算法)
struct heap_mem { /* magic and used flag */ rt_uint16_t magic; rt_uint16_t used; rt_size_t next, prev; #ifdef RT_USING_MEMTRACE rt_uint8_t thread[4]; /* thread name */ #endif };
/** * @ingroup SystemInit * * This function will initialize system heap memory. * * @param begin_addr the beginning address of system heap memory. * @param end_addr the end address of system heap memory. */ void rt_system_heap_init(void *begin_addr, void *end_addr)
/* * The initialized memory pool will be: * +-----------------------------------+--------------------------+ * | whole freed memory block | Used Memory Block Tailer | * +-----------------------------------+--------------------------+ * * block_list --> whole freed memory block * * The length of Used Memory Block Tailer is 0, * which is prevents block merging across list */ rt_err_t rt_memheap_init(struct rt_memheap *memheap, const char *name, void *start_addr, rt_uint32_t size)
/** * Allocate a block of memory with a minimum of ‘size‘ bytes. * * @param size is the minimum size of the requested block in bytes. * * @return pointer to allocated memory or NULL if no free memory was found. */ void *rt_malloc(rt_size_t size)
应用程序使用完从内存分配器中申请的内存后,必须及时释放,否则会造成内存泄漏
/** * This function will release the previously allocated memory block by * rt_malloc. The released memory block is taken back to system heap. * * @param rmem the address of memory which will be released */ void rt_free(void *rmem)
/** * This function will change the previously allocated memory block. * * @param rmem pointer to memory allocated by rt_malloc * @param newsize the required new size * * @return the changed memory block address */ void *rt_realloc(void *rmem, rt_size_t newsize)
/** * This function will contiguously allocate enough space for count objects * that are size bytes of memory each and returns a pointer to the allocated * memory. * * The allocated memory is filled with bytes of value zero. * * @param count number of objects to allocate * @param size size of the objects to allocate * * @return pointer to allocated memory / NULL pointer if there is an error */ void *rt_calloc(rt_size_t count, rt_size_t size)
/** * This function will set a hook function, which will be invoked when a memory * block is allocated from heap memory. * * @param hook the hook function */ void rt_malloc_sethook(void (*hook)(void *ptr, rt_size_t size)) { rt_malloc_hook = hook; }
在释放内存时,用户可设置一个钩子函数;
/** * This function will set a hook function, which will be invoked when a memory * block is released to heap memory. * * @param hook the hook function */ void rt_free_sethook(void (*hook)(void *ptr)) { rt_free_hook = hook; }
#include <rtthread.h> #define THREAD_PRIORITY 25 #define THREAD_STACK_SIZE 512 #define THREAD_TIMESLICE 5 /* 线程入口 */ void thread1_entry(void *parameter) { int i; char *ptr = RT_NULL; /* 内存块的指针 */ for (i = 0; ; i++) { /* 每次分配 (1 << i) 大小字节数的内存空间 */ ptr = rt_malloc(1 << i); /* 如果分配成功 */ if (ptr != RT_NULL) { rt_kprintf("get memory :%d byte\n", (1 << i)); /* 释放内存块 */ rt_free(ptr); rt_kprintf("free memory :%d byte\n", (1 << i)); ptr = RT_NULL; } else { rt_kprintf("try to get %d byte memory failed!\n", (1 << i)); return; } } } int dynmem_sample(void) { rt_thread_t tid = RT_NULL; /* 创建线程 1 */ tid = rt_thread_create("thread1", thread1_entry, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); if (tid != RT_NULL) rt_thread_startup(tid); return 0; } /* 导出到 msh 命令列表中 */ MSH_CMD_EXPORT(dynmem_sample, dynmem sample);
运行结果:
\ | / - RT - Thread Operating System / | \ 3.1.0 build Aug 24 2018 2006 - 2018 Copyright by rt-thread team msh >dynmem_sample msh >get memory :1 byte free memory :1 byte get memory :2 byte free memory :2 byte … get memory :16384 byte free memory :16384 byte get memory :32768 byte free memory :32768 byte try to get 65536 byte memory failed!
在 RT-Thread 实时操作系统中,内存池控制块由结构体 struct rt_mempool 表示
struct rt_mempool { struct rt_object parent; /**< inherit from rt_object */ void *start_address; /**< memory pool start */ rt_size_t size; /**< size of memory pool */ rt_size_t block_size; /**< size of memory blocks */ rt_uint8_t *block_list; /**< memory blocks list */ rt_size_t block_total_count; /**< numbers of memory block */ rt_size_t block_free_count; /**< numbers of free memory block */ rt_list_t suspend_thread; /**< threads pended on this resource */ rt_size_t suspend_thread_count; /**< numbers of thread pended on this resource */ }; typedef struct rt_mempool *rt_mp_t;
/** * This function will create a mempool object and allocate the memory pool from * heap. * * @param name the name of memory pool * @param block_count the count of blocks in memory pool * @param block_size the size for each block * * @return the created mempool object */ rt_mp_t rt_mp_create(const char *name, rt_size_t block_count, rt_size_t block_size)
删除内存池将删除内存池对象并释放申请的内存
/** * This function will delete a memory pool and release the object memory. * * @param mp the memory pool object * * @return RT_EOK */ rt_err_t rt_mp_delete(rt_mp_t mp)
/** * This function will initialize a memory pool object, normally which is used * for static object. * * @param mp the memory pool object * @param name the name of memory pool * @param start the star address of memory pool * @param size the total size of memory pool * @param block_size the size for each block * * @return RT_EOK */ rt_err_t rt_mp_init(struct rt_mempool *mp, const char *name, void *start, rt_size_t size, rt_size_t block_size)
脱离内存池将把内存池对象从内核对象管理器中脱离
/** * This function will detach a memory pool from system object management. * * @param mp the memory pool object * * @return RT_EOK */ rt_err_t rt_mp_detach(struct rt_mempool *mp)
/** * This function will allocate a block from memory pool * * @param mp the memory pool object * @param time the waiting time * * @return the allocated memory block or RT_NULL on allocated failed */ void *rt_mp_alloc(rt_mp_t mp, rt_int32_t time)
任何内存块使用完后都必须被释放,否则会造成内存泄露;
/** * This function will release a memory block * * @param block the address of memory block to be released */ void rt_mp_free(void *block)
#include <rtthread.h> static rt_uint8_t *ptr[50]; static rt_uint8_t mempool[4096]; static struct rt_mempool mp; #define THREAD_PRIORITY 25 #define THREAD_STACK_SIZE 512 #define THREAD_TIMESLICE 5 /* 指向线程控制块的指针 */ static rt_thread_t tid1 = RT_NULL; static rt_thread_t tid2 = RT_NULL; /* 线程 1 入口 */ static void thread1_mp_alloc(void *parameter) { int i; for (i = 0 ; i < 50 ; i++) { if (ptr[i] == RT_NULL) { /* 试图申请内存块 50 次,当申请不到内存块时, 线程 1 挂起,转至线程 2 运行 */ ptr[i] = rt_mp_alloc(&mp, RT_WAITING_FOREVER); if (ptr[i] != RT_NULL) rt_kprintf("allocate No.%d\n", i); } } } /* 线程 2 入口,线程 2 的优先级比线程 1 低,应该线程 1 先获得执行。*/ static void thread2_mp_release(void *parameter) { int i; rt_kprintf("thread2 try to release block\n"); for (i = 0; i < 50 ; i++) { /* 释放所有分配成功的内存块 */ if (ptr[i] != RT_NULL) { rt_kprintf("release block %d\n", i); rt_mp_free(ptr[i]); ptr[i] = RT_NULL; } } } int mempool_sample(void) { int i; for (i = 0; i < 50; i ++) ptr[i] = RT_NULL; /* 初始化内存池对象 */ rt_mp_init(&mp, "mp1", &mempool[0], sizeof(mempool), 80); /* 创建线程 1:申请内存池 */ tid1 = rt_thread_create("thread1", thread1_mp_alloc, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); if (tid1 != RT_NULL) rt_thread_startup(tid1); /* 创建线程 2:释放内存池 */ tid2 = rt_thread_create("thread2", thread2_mp_release, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE); if (tid2 != RT_NULL) rt_thread_startup(tid2); return 0; } /* 导出到 msh 命令列表中 */ MSH_CMD_EXPORT(mempool_sample, mempool sample);
运行结果:
\ | / - RT - Thread Operating System / | \ 3.1.0 build Aug 24 2018 2006 - 2018 Copyright by rt-thread team msh >mempool_sample msh >allocate No.0 allocate No.1 allocate No.2 allocate No.3 allocate No.4 … allocate No.46 allocate No.47 thread2 try to release block release block 0 allocate No.48 release block 1 allocate No.49 release block 2 release block 3 release block 4 release block 5 … release block 47 release block 48 release block 49
标签:min ati ptr 第一个 结果 过程 cts 释放内存 连接
原文地址:https://www.cnblogs.com/icefree/p/10822971.html