From: Robin Krens Date: Sun, 20 Oct 2019 00:14:29 +0000 (+0200) Subject: kheap_info and trace X-Git-Url: https://robinkrens.nl/gitweb/?a=commitdiff_plain;h=40ccfa73408769ca3fbc3b73ff6531db241d1d60;p=cortex-from-scratch kheap_info and trace --- diff --git a/include/lib/pool.h b/include/lib/pool.h index e68ef38..b39f931 100644 --- a/include/lib/pool.h +++ b/include/lib/pool.h @@ -7,11 +7,12 @@ struct mem_pool { unsigned short block_size; unsigned short free_blocks; unsigned short blocks_init; - uint32_t * SRAM_entry; - uint32_t * m_next; + unsigned char * SRAM_entry; + unsigned char * m_next; }; -extern void kpool_init(mem_pool_t *, size_t size_arg, unsigned int blocks_arg, uint32_t * entry_SRAM); +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 *); diff --git a/lib/pool.c b/lib/pool.c index c9ce23b..83b2df2 100644 --- a/lib/pool.c +++ b/lib/pool.c @@ -30,10 +30,11 @@ #include #include +#include #include -void kpool_init(mem_pool_t * pool, 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, unsigned char* entry_SRAM) { pool->blocks = blocks_arg; pool->block_size = size_arg; @@ -49,12 +50,12 @@ void kpool_init(mem_pool_t * pool, size_t size_arg, unsigned int blocks_arg, uin } */ /* Helper functions */ -uint32_t * AddrFromIndex(mem_pool_t * pool, unsigned int i) { +unsigned char* AddrFromIndex(mem_pool_t * pool, unsigned int i) { return pool->SRAM_entry + ( i * pool->block_size ); } -unsigned int IndexFromAddr(mem_pool_t * pool, const uint32_t * p) { +unsigned int IndexFromAddr(mem_pool_t * pool, const unsigned char* p) { return (((unsigned int)(p - pool->SRAM_entry)) / pool->block_size); } @@ -85,12 +86,43 @@ void * kalloc(mem_pool_t * pool) { 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; + pool->m_next = (unsigned char*)p; } else { *((unsigned int*)p) = pool->blocks; - pool->m_next = (uint32_t *) p; + pool->m_next = (unsigned char*) p; } ++pool->free_blocks; - } +} + +/* Heap info helper functions */ + +void kheap_info(mem_pool_t * pool) { + + + printf("HEAP INFO:\n"); + printf("BLOCKS FREE: %d\n", pool->free_blocks); + + unsigned char* curr_addr = pool->SRAM_entry; + + // Loop over heap blocks + for (int i = 0; i < pool->blocks; i++) { + printf("\nBLOCK %d", i+1); + for (int j = 0; j < (pool->block_size-1); j++) { + + if (j % 10 == 0) + printf("\n%x ", curr_addr); + printf("%#x ", *curr_addr); + curr_addr++; + } + printf("\n"); + curr_addr++; + + } + printf("\n"); + +} + + + diff --git a/main.c b/main.c index b2ff855..4104960 100644 --- a/main.c +++ b/main.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -66,7 +67,7 @@ void main() extern uint32_t * _beginofheap; //printf("%p", &_beginofheap); - kpool_init(&kheap_pool, 512, 10, (uint32_t *) &_beginofheap); + kpool_init(&kheap_pool, 0x10, 10, (uint32_t *) &_beginofheap); // printf("%p\n", &kheap_pool); @@ -75,6 +76,10 @@ void main() 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); @@ -82,12 +87,29 @@ void main() kfree(&kheap_pool, string); char * string6 = (char *) kalloc(&kheap_pool); - char * string7 = (char *) kalloc(&kheap_pool); + + + memset(string6, 0xCC, 0x10); +// char * string7 = (char *) kalloc(&kheap_pool); printf("%p\n", string6); - printf("%p\n", string7); +// 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"; diff --git a/term.c b/term.c index 92017e6..4a2a88b 100644 --- a/term.c +++ b/term.c @@ -51,7 +51,8 @@ struct cmd { struct cmd builtincmds[BUILTINCMDS]; int info(int argc, char ** argsv) { - sysinfo(); + extern mem_pool_t kheap_pool; + kheap_info(&kheap_pool); return 0; } @@ -84,12 +85,6 @@ 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; }