From: Robin Krens Date: Sat, 19 Oct 2019 21:04:22 +0000 (+0200) Subject: basic heap implementation X-Git-Url: https://robinkrens.nl/gitweb/?a=commitdiff_plain;h=8f723dacce04fc3f4931eed86ccc861ceb83d49d;p=cortex-from-scratch basic heap implementation --- diff --git a/include/lib/pool.h b/include/lib/pool.h index 9204d36..e68ef38 100644 --- a/include/lib/pool.h +++ b/include/lib/pool.h @@ -1,4 +1,17 @@ /* Fixed size memory pool allocation */ -extern void pool_init(size_t size_arg, unsigned int blocks_arg, uint32_t * entry_SRAM); -extern void * alloc(); -extern void free(void* p); +typedef struct mem_pool mem_pool_t; + +struct mem_pool { + + unsigned short blocks; + unsigned short block_size; + unsigned short free_blocks; + unsigned short blocks_init; + uint32_t * SRAM_entry; + uint32_t * m_next; + +}; + +extern void kpool_init(mem_pool_t *, size_t size_arg, unsigned int blocks_arg, uint32_t * entry_SRAM); +extern void * kalloc(mem_pool_t * ); +extern void kfree(mem_pool_t *, void* p); diff --git a/include/sys/mmap.h b/include/sys/mmap.h index 091d242..4ed901d 100644 --- a/include/sys/mmap.h +++ b/include/sys/mmap.h @@ -26,6 +26,8 @@ #define SRAM_SIZE 0x00010000 #define SRAM_OFFSET 0x20000000 +/* TODO #define KHEAP_SIZE 0x100 */ + /* Safety macro's to get the address or value */ #define MEM_VALUE(addr) *((volatile uint32_t(*) (addr)) #define MEM_ADDR(addr) ((volatile uint32_t *) (addr)) diff --git a/include/sys/robsys.h b/include/sys/robsys.h index bac6703..369544f 100644 --- a/include/sys/robsys.h +++ b/include/sys/robsys.h @@ -30,9 +30,9 @@ extern void systick_init(); extern void sysinfo(); /* POOL.c */ -extern void pool_init(size_t, unsigned int, uint32_t *); -extern void * alloc(); -extern void free(); +//extern void pool_init(size_t, unsigned int, uint32_t *); +//extern void * alloc(); +//extern void free(); /* TERM.C */ extern void terminal(); diff --git a/lib/pool.c b/lib/pool.c index 63cf3fd..c9ce23b 100644 --- a/lib/pool.c +++ b/lib/pool.c @@ -32,77 +32,65 @@ #include #include -struct MEMPOOL { - - unsigned short blocks; - unsigned short block_size; - unsigned short free_blocks; - unsigned short blocks_alloc; - uint32_t * SRAM_entry; - uint32_t * m_next; - -}; - -struct MEMPOOL mem; - -void pool_init(size_t size_arg, unsigned int blocks_arg, uint32_t * entry_SRAM) { +void kpool_init(mem_pool_t * pool, size_t size_arg, unsigned int blocks_arg, uint32_t * entry_SRAM) { - mem.blocks = blocks_arg; - mem.block_size = size_arg; - mem.SRAM_entry = entry_SRAM; + pool->blocks = blocks_arg; + pool->block_size = size_arg; + pool->blocks_init = 0; + pool->SRAM_entry = entry_SRAM; memset(entry_SRAM, 0x00, (sizeof(char) * (size_arg * blocks_arg))); - mem.free_blocks = blocks_arg; - mem.m_next = mem.SRAM_entry; + pool->free_blocks = blocks_arg; + pool->m_next = pool->SRAM_entry; } /* void deletepool() { - mem.SRAM_entry = NULL; + pool->SRAM_entry = NULL; } */ /* Helper functions */ -uint32_t * AddrFromIndex(unsigned int i) { - return mem.SRAM_entry + ( i * mem.block_size ); +uint32_t * AddrFromIndex(mem_pool_t * pool, unsigned int i) { + return pool->SRAM_entry + ( i * pool->block_size ); } -unsigned int IndexFromAddr(const uint32_t * p) { - return (((unsigned int)(p - mem.SRAM_entry)) / mem.block_size); +unsigned int IndexFromAddr(mem_pool_t * pool, const uint32_t * p) { + return (((unsigned int)(p - pool->SRAM_entry)) / pool->block_size); } /* alloc and free */ -void * alloc() { - if (mem.blocks_alloc < mem.blocks ) { - unsigned int * p = (unsigned int *)AddrFromIndex( mem.blocks_alloc ); - *p = mem.blocks_alloc + 1; - mem.blocks_alloc++; +void * kalloc(mem_pool_t * pool) { + if (pool->blocks_init < pool->blocks ) { + unsigned int * p = (unsigned int *)AddrFromIndex(pool, pool->blocks_init ); + *p = pool->blocks_init + 1; + pool->blocks_init++; } void* ret = NULL; - if ( mem.free_blocks > 0 ) { - ret = (void*)mem.m_next; - --mem.free_blocks; - if (mem.free_blocks!=0) { - mem.m_next = AddrFromIndex( *((unsigned int*)mem.m_next) ); + if ( pool->free_blocks > 0 ) { + ret = (void*)pool->m_next; + --pool->free_blocks; + if (pool->free_blocks!=0) { + pool->m_next = AddrFromIndex(pool, *((unsigned int*)pool->m_next) ); } else { - mem.m_next = NULL; + pool->m_next = NULL; } } return ret; } -void free(void* p) { - if (mem.m_next != NULL) { - (*(unsigned int *)p) = IndexFromAddr( mem.m_next ); - mem.m_next = (uint32_t *)p; +void kfree(mem_pool_t * pool, void* p) { + if (pool->m_next != NULL) { + (*(unsigned int *)p) = IndexFromAddr(pool, pool->m_next ); + pool->m_next = (uint32_t *)p; } else { - *((unsigned int*)p) = mem.blocks; - mem.m_next = (uint32_t *) p; + *((unsigned int*)p) = pool->blocks; + pool->m_next = (uint32_t *) p; } - ++mem.free_blocks; + ++pool->free_blocks; } diff --git a/lib/stdio.c b/lib/stdio.c index 76e1f28..0149eba 100644 --- a/lib/stdio.c +++ b/lib/stdio.c @@ -23,7 +23,7 @@ #include #define SERIAL 1 -#define TFT 1 +#define TFT 0 #define BUFSIZE 256 static char stdbuf[BUFSIZE]; diff --git a/link.ld b/link.ld index d8361dc..bf5b6af 100644 --- a/link.ld +++ b/link.ld @@ -21,6 +21,8 @@ * * */ +HEAP_SIZE = 0x100; + MEMORY { FLASH (xr) : ORIGIN = 0x08000000, LENGTH = 512K @@ -48,4 +50,8 @@ SECTIONS *(.bss) } _endofbss = .; + _beginofheap = .; + . = . + HEAP_SIZE; + . = ALIGN(8); + _endofheap = .; } diff --git a/main.c b/main.c index 4712f7a..b2ff855 100644 --- a/main.c +++ b/main.c @@ -18,6 +18,7 @@ #include #include +#include #include #include @@ -29,6 +30,8 @@ //#include #include +mem_pool_t kheap_pool; + void main() { @@ -42,14 +45,13 @@ void main() uart_init(); /* TFT screen */ - tft_init(); + // tft_init(); /* Cortex M* integrated systick, can be replaced * by the more accurate RTC. systick_init(); */ - /* Set up a very small libc library */ init_printf(NULL, putc); @@ -61,7 +63,35 @@ void main() /* Real time clock */ rtc_init(); + + extern uint32_t * _beginofheap; + //printf("%p", &_beginofheap); + kpool_init(&kheap_pool, 512, 10, (uint32_t *) &_beginofheap); + +// printf("%p\n", &kheap_pool); + + char * string = (char *) kalloc(&kheap_pool); + char * string2 = (char *) kalloc(&kheap_pool); + char * string3 = (char *) kalloc(&kheap_pool); + + + printf("%p\n", string); + printf("%p\n", string2); + printf("%p\n", string3); + + kfree(&kheap_pool, string); + + char * string6 = (char *) kalloc(&kheap_pool); + char * string7 = (char *) kalloc(&kheap_pool); + printf("%p\n", string6); + printf("%p\n", string7); + + //free(string); + + //char * string2 = (char *) alloc(); + //string2 = "taalb"; + /* Eeprom Driver eeprom_at24c_init(); eeprom_test(); diff --git a/sysinfo.c b/sysinfo.c index d66aaf0..3f632a3 100644 --- a/sysinfo.c +++ b/sysinfo.c @@ -33,7 +33,7 @@ void sysinfo() { printf("UNKNOWN\n"); } - tmp = (tmp >> 16); + /* tmp = (tmp >> 16); printf("REV: "); switch (tmp) { case 0x1000: @@ -47,7 +47,7 @@ void sysinfo() { break; default: printf("UNKNOWN\n"); - } + } */ extern char _endofbss; @@ -56,9 +56,12 @@ void sysinfo() { uint32_t data_bss = (uint32_t) &_endofbss - SRAM_OFFSET; uint32_t mem_free = SRAM_SIZE - stack_usage - data_bss; + extern uint32_t HEAP_SIZE; + printf("TOTAL MEM: %#x\n", SRAM_SIZE); printf("FREE MEM: %#x\n", mem_free); - printf("STACK USE: %#x\n\n", stack_usage); + printf("STACK USE: %#x\n", stack_usage); + printf("HEAP_SIZE: %#x\n\n", &HEAP_SIZE); } diff --git a/term.c b/term.c index d1a6431..92017e6 100644 --- a/term.c +++ b/term.c @@ -21,6 +21,8 @@ #include #include +#include + #include #define SERIAL 1 @@ -41,7 +43,6 @@ static char buf[BUFSIZE]; - struct cmd { char * name; int (*function)(int argc, char ** argsv); @@ -83,6 +84,12 @@ int showmem(int argc, char ** argsv) { } + extern mem_pool_t kheap_pool; + int * a = kalloc(&kheap_pool); + *a = argc; + printf("%d\n", *a); + printf("%p\n", a); + return 0; }