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
--- /dev/null
+/* (CC-BY-NC-SA) ROBIN KRENS - ROBIN @ ROBINKRENS.NL
+ *
+ * $LOG$
+ * 2019/10/20 - ROBIN KRENS
+ * Initial version
+ *
+ * $DESCRIPTION$
+ *
+ * kalloc();
+ * kfree();
+ *
+ */
+
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+
+#include <lib/pool.h>
+
+#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;
+}
+
+
+
};
-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);
//#define CRYSTAL_MHZ 8
#define CLKSPEED_MHZ 8
extern void clock_init();
-// extern int clock_test();
// extern void clock_reset();
/* RTC.C */
/* 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();
* 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.
*
*
* * */
#include <lib/string.h>
-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;
}
/* 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;
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);
*
* */
-HEAP_SIZE = 0x100;
+KHEAP_SIZE = 0x100;
MEMORY
{
}
_endofbss = .;
_beginofheap = .;
- . = . + HEAP_SIZE;
+ . = . + KHEAP_SIZE;
. = ALIGN(8);
_endofheap = .;
}
//#include <drivers/mk450_joystick.h>
#include <drivers/st7735s.h>
-mem_pool_t kheap_pool;
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();
/* 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();
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);
}
#include <lib/regfunc.h>
#include <lib/tinyprintf.h>
-#include <lib/pool.h>
-
#include <drivers/led.h>
#define SERIAL 1
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;
}