1 /* (CC-BY-NC-SA) ROBIN KRENS - ROBIN @ ROBINKRENS.NL
4 * 2019/7/20 - ROBIN KRENS
8 * Fixed-sized memory pool allocation. A so called * memory-pool
9 * (i.e. 4kB * 12 blocks) can be initialized. Note: this allocator
10 * is for use on processors without an MMU (memory management
11 * unit). A MPU (memory protection unit), if available can be used
12 * to protect certain zones.
14 * This work is based on an article of Ben Kenwright.
16 * Preconditions: programmer should make sure the SRAM entry point
17 * + (blocks * blocksize) is free.
20 * KERNEL: can initialize a big pool for all user tasks
22 * USER TASKS/PROCESS: can use this to dynamically allocate their
23 * own memory (i.e. heap)
37 unsigned short blocks;
38 unsigned short block_size;
39 unsigned short free_blocks;
40 unsigned short blocks_alloc;
41 uint32_t * SRAM_entry;
49 void pool_init(size_t size_arg, unsigned int blocks_arg, uint32_t * entry_SRAM) {
51 mem.blocks = blocks_arg;
52 mem.block_size = size_arg;
53 mem.SRAM_entry = entry_SRAM;
54 memset(entry_SRAM, 0x00, (sizeof(char) * (size_arg * blocks_arg)));
55 mem.free_blocks = blocks_arg;
56 mem.m_next = mem.SRAM_entry;
59 /* uint32_t * a = alloc();
60 uint32_t * b = alloc();
61 uint32_t * c = alloc();
63 uint32_t * d = alloc();
73 /* void deletepool() {
74 mem.SRAM_entry = NULL;
77 /* Helper functions */
78 uint32_t * AddrFromIndex(unsigned int i) {
79 return mem.SRAM_entry + ( i * mem.block_size );
83 unsigned int IndexFromAddr(const uint32_t * p) {
84 return (((unsigned int)(p - mem.SRAM_entry)) / mem.block_size);
90 if (mem.blocks_alloc < mem.blocks ) {
91 unsigned int * p = (unsigned int *)AddrFromIndex( mem.blocks_alloc );
92 *p = mem.blocks_alloc + 1;
97 if ( mem.free_blocks > 0 ) {
98 ret = (void*)mem.m_next;
100 if (mem.free_blocks!=0) {
101 mem.m_next = AddrFromIndex( *((unsigned int*)mem.m_next) );
112 if (mem.m_next != NULL) {
113 (*(unsigned int *)p) = IndexFromAddr( mem.m_next );
114 mem.m_next = (uint32_t *)p;
117 *((unsigned int*)p) = mem.blocks;
118 mem.m_next = (uint32_t *) p;