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)
33 #include <lib/tinyprintf.h>
34 #include <lib/string.h>
37 void kpool_init(mem_pool_t * pool, size_t size_arg, unsigned int blocks_arg, unsigned char* entry_SRAM) {
39 pool->blocks = blocks_arg;
40 pool->block_size = size_arg;
41 pool->blocks_init = 0;
42 pool->SRAM_entry = entry_SRAM;
43 memset(entry_SRAM, 0x00, (sizeof(char) * (size_arg * blocks_arg)));
44 pool->free_blocks = blocks_arg;
45 pool->m_next = pool->SRAM_entry;
48 /* void deletepool() {
49 pool->SRAM_entry = NULL;
52 /* Helper functions */
53 unsigned char* AddrFromIndex(mem_pool_t * pool, unsigned int i) {
54 return pool->SRAM_entry + ( i * pool->block_size );
58 unsigned int IndexFromAddr(mem_pool_t * pool, const unsigned char* p) {
59 return (((unsigned int)(p - pool->SRAM_entry)) / pool->block_size);
64 void * kalloc(mem_pool_t * pool) {
65 if (pool->blocks_init < pool->blocks ) {
66 unsigned int * p = (unsigned int *)AddrFromIndex(pool, pool->blocks_init );
67 *p = pool->blocks_init + 1;
72 if ( pool->free_blocks > 0 ) {
73 ret = (void*)pool->m_next;
75 if (pool->free_blocks!=0) {
76 pool->m_next = AddrFromIndex(pool, *((unsigned int*)pool->m_next) );
86 void kfree(mem_pool_t * pool, void* p) {
87 if (pool->m_next != NULL) {
88 (*(unsigned int *)p) = IndexFromAddr(pool, pool->m_next );
89 pool->m_next = (unsigned char*)p;
92 *((unsigned int*)p) = pool->blocks;
93 pool->m_next = (unsigned char*) p;
99 /* Heap info helper functions */
101 void kheap_info(mem_pool_t * pool) {
104 printf("HEAP INFO:\n");
105 printf("BLOCKS FREE: %d\n", pool->free_blocks);
107 unsigned char* curr_addr = pool->SRAM_entry;
109 // Loop over heap blocks
110 for (int i = 0; i < pool->blocks; i++) {
111 printf("\nBLOCK %d", i+1);
112 for (int j = 0; j < (pool->block_size-1); j++) {
115 printf("\n%x ", curr_addr);
116 printf("%#x ", *curr_addr);