From: Robin Krens Date: Sun, 20 Oct 2019 20:23:01 +0000 (+0200) Subject: kalloc and kfree interface and abstraction X-Git-Url: https://robinkrens.nl/gitweb/?a=commitdiff_plain;h=4055b7fa53aac93668e3c85b9424c17fb7e10faf;p=cortex-from-scratch kalloc and kfree interface and abstraction new file: heap.c --- diff --git a/Makefile b/Makefile index 630c2be..d6a161a 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ INCLUDE+= -Iinclude BIN = bin ODIR = obj -_OBJ = ivt.o systick.o sysinfo.o term.o main.o clock.o rtc.o +_OBJ = ivt.o systick.o sysinfo.o term.o main.o clock.o rtc.o heap.o OBJ = $(patsubst %, $(ODIR)/%,$(_OBJ)) DDIR = obj/drivers diff --git a/heap.c b/heap.c new file mode 100644 index 0000000..ff9d348 --- /dev/null +++ b/heap.c @@ -0,0 +1,54 @@ +/* (CC-BY-NC-SA) ROBIN KRENS - ROBIN @ ROBINKRENS.NL + * + * $LOG$ + * 2019/10/20 - ROBIN KRENS + * Initial version + * + * $DESCRIPTION$ + * + * kalloc(); + * kfree(); + * + */ + +#include +#include +#include + +#include + +#define BLOCKSIZE 0x10 +#define BLOCKS 10 +/* #define KHEAP_SIZE 0x100 SEE LINK.LD */ + +static mem_pool_t kheap_pool; + +void kheap_init() { + + extern unsigned char * _beginofheap; + pool_init(&kheap_pool, BLOCKSIZE, BLOCKS, (unsigned char *) &_beginofheap); + //kalloc = &alloc; +} + +void * kalloc(void * s) { + + return alloc(s); +} + +void kfree(void * s, void * p) { + + free(s, p); +} + +void kheap_info(void *s) { + + heap_info(s); +} + +void * get_kheap() { + + return (void *) &kheap_pool; +} + + + diff --git a/include/lib/pool.h b/include/lib/pool.h index b39f931..3ce0d1d 100644 --- a/include/lib/pool.h +++ b/include/lib/pool.h @@ -12,7 +12,7 @@ struct mem_pool { }; -extern void kpool_init(mem_pool_t *, size_t size_arg, unsigned int blocks_arg, unsigned char * entry_SRAM); -extern void * kalloc(mem_pool_t * ); -extern void kfree(mem_pool_t *, void* p); -extern void kheap_info(mem_pool_t *); +void pool_init(mem_pool_t *, size_t size_arg, unsigned int blocks_arg, unsigned char * entry_SRAM); +void * alloc(void * s); +void free(void * s, void* p); +void heap_info(void * s); diff --git a/include/sys/robsys.h b/include/sys/robsys.h index 369544f..9d1db41 100644 --- a/include/sys/robsys.h +++ b/include/sys/robsys.h @@ -12,7 +12,6 @@ //#define CRYSTAL_MHZ 8 #define CLKSPEED_MHZ 8 extern void clock_init(); -// extern int clock_test(); // extern void clock_reset(); /* RTC.C */ @@ -29,10 +28,14 @@ extern void systick_init(); /* SYSINFO.C */ extern void sysinfo(); -/* POOL.c */ -//extern void pool_init(size_t, unsigned int, uint32_t *); -//extern void * alloc(); -//extern void free(); +/* HEAP.c */ +/* typedef void * (kalloc)(void * s); + * typedef void (kfree)(void * s, void * p); */ +extern void kheap_init(); +extern void * get_kheap(); +extern void * kalloc(void * s); +extern void kfree(void * s, void * p); +extern void kheap_info(void * s); /* TERM.C */ extern void terminal(); diff --git a/lib/pool.c b/lib/pool.c index 83b2df2..b3f1828 100644 --- a/lib/pool.c +++ b/lib/pool.c @@ -12,15 +12,21 @@ * to protect certain zones. * * This work is based on an article of Ben Kenwright. + * + * Blocks are fixed size. In some situations ideal, in some situation + * may be not. It's fast. * * Preconditions: programmer should make sure the SRAM entry point * + (blocks * blocksize) is free. * * $SAMPLE USAGE$ - * KERNEL: can initialize a big pool for all user tasks + * KERNEL: initialize one (or multiple!) fixed memory-sized + * kernal heaps * * USER TASKS/PROCESS: can use this to dynamically allocate their - * own memory (i.e. heap) + * own memory (i.e. heap). You might argue that a fixed size memory + * pool is not ideal, but you would be amazed that many programs + * use structs of more or less similar size. * * * * */ @@ -34,7 +40,7 @@ #include -void kpool_init(mem_pool_t * pool, size_t size_arg, unsigned int blocks_arg, unsigned char* entry_SRAM) { +void pool_init(mem_pool_t * pool, size_t size_arg, unsigned int blocks_arg, unsigned char* entry_SRAM) { pool->blocks = blocks_arg; pool->block_size = size_arg; @@ -61,7 +67,8 @@ unsigned int IndexFromAddr(mem_pool_t * pool, const unsigned char* p) { } /* alloc and free */ -void * kalloc(mem_pool_t * pool) { +void * alloc(void * s) { + mem_pool_t * pool = (mem_pool_t *) s; if (pool->blocks_init < pool->blocks ) { unsigned int * p = (unsigned int *)AddrFromIndex(pool, pool->blocks_init ); *p = pool->blocks_init + 1; @@ -83,23 +90,26 @@ void * kalloc(mem_pool_t * pool) { return ret; } -void kfree(mem_pool_t * pool, void* p) { - if (pool->m_next != NULL) { - (*(unsigned int *)p) = IndexFromAddr(pool, pool->m_next ); - pool->m_next = (unsigned char*)p; - } - else { +void free(void * s, void* p) { + + mem_pool_t * pool = (mem_pool_t *) s; + if (pool->m_next != NULL) { + (*(unsigned int *)p) = IndexFromAddr(pool, pool->m_next ); + pool->m_next = (unsigned char*)p; + } + else { *((unsigned int*)p) = pool->blocks; pool->m_next = (unsigned char*) p; - } + } - ++pool->free_blocks; + ++pool->free_blocks; } /* Heap info helper functions */ -void kheap_info(mem_pool_t * pool) { +void heap_info(void * s) { + mem_pool_t * pool = (mem_pool_t *) s; printf("HEAP INFO:\n"); printf("BLOCKS FREE: %d\n", pool->free_blocks); diff --git a/link.ld b/link.ld index bf5b6af..5056e38 100644 --- a/link.ld +++ b/link.ld @@ -21,7 +21,7 @@ * * */ -HEAP_SIZE = 0x100; +KHEAP_SIZE = 0x100; MEMORY { @@ -51,7 +51,7 @@ SECTIONS } _endofbss = .; _beginofheap = .; - . = . + HEAP_SIZE; + . = . + KHEAP_SIZE; . = ALIGN(8); _endofheap = .; } diff --git a/main.c b/main.c index 4104960..26a7305 100644 --- a/main.c +++ b/main.c @@ -31,7 +31,6 @@ //#include #include -mem_pool_t kheap_pool; void main() { @@ -56,6 +55,10 @@ void main() /* Set up a very small libc library */ init_printf(NULL, putc); + /* Heap init */ + kheap_init(); + //printf("%p\n", get_kheap()); + /* Display some basic info at startup */ sysinfo(); @@ -65,54 +68,6 @@ void main() /* Real time clock */ rtc_init(); - extern uint32_t * _beginofheap; - //printf("%p", &_beginofheap); - kpool_init(&kheap_pool, 0x10, 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); - - - memset(string, 0xFF, 0x10); - memset(string2, 0xEE, 0x10); - memset(string3, 0xDD, 0x10); - - printf("%p\n", string); - printf("%p\n", string2); - printf("%p\n", string3); - - kfree(&kheap_pool, string); - - char * string6 = (char *) kalloc(&kheap_pool); - - - memset(string6, 0xCC, 0x10); -// char * string7 = (char *) kalloc(&kheap_pool); - printf("%p\n", string6); -// printf("%p\n", string7); - - - kfree(&kheap_pool, string2); - - char * string7 = (char *) kalloc(&kheap_pool); - memset(string7, 0xBB, 0x10); - - - char * string8 = (char *) kalloc(&kheap_pool); - memset(string8, 0xAA, 0x10); - - char * string9 = (char *) kalloc(&kheap_pool); - memset(string9, 0x99, 0x10); - //free(string); - - kfree(&kheap_pool, string3); - - //char * string2 = (char *) alloc(); - - //string2 = "taalb"; /* Eeprom Driver eeprom_at24c_init(); diff --git a/sysinfo.c b/sysinfo.c index 3f632a3..85ca01c 100644 --- a/sysinfo.c +++ b/sysinfo.c @@ -56,12 +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; + extern uint32_t KHEAP_SIZE; printf("TOTAL MEM: %#x\n", SRAM_SIZE); printf("FREE MEM: %#x\n", mem_free); printf("STACK USE: %#x\n", stack_usage); - printf("HEAP_SIZE: %#x\n\n", &HEAP_SIZE); + printf("HEAP_SIZE: %#x\n\n", &KHEAP_SIZE); } diff --git a/term.c b/term.c index 4a2a88b..0b2f0d6 100644 --- a/term.c +++ b/term.c @@ -21,8 +21,6 @@ #include #include -#include - #include #define SERIAL 1 @@ -51,8 +49,9 @@ struct cmd { struct cmd builtincmds[BUILTINCMDS]; int info(int argc, char ** argsv) { - extern mem_pool_t kheap_pool; - kheap_info(&kheap_pool); + /* extern mem_pool_t kheap_pool; + kheap_info(&kheap_pool); */ + kalloc(get_kheap()); return 0; }