/* 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);
#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))
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();
#include <lib/pool.h>
#include <lib/string.h>
-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;
}
#include <drivers/st7735s.h>
#define SERIAL 1
-#define TFT 1
+#define TFT 0
#define BUFSIZE 256
static char stdbuf[BUFSIZE];
*
* */
+HEAP_SIZE = 0x100;
+
MEMORY
{
FLASH (xr) : ORIGIN = 0x08000000, LENGTH = 512K
*(.bss)
}
_endofbss = .;
+ _beginofheap = .;
+ . = . + HEAP_SIZE;
+ . = ALIGN(8);
+ _endofheap = .;
}
#include <sys/mmap.h>
#include <lib/regfunc.h>
+#include <lib/pool.h>
#include <lib/stdio.h>
#include <lib/tinyprintf.h>
//#include <drivers/mk450_joystick.h>
#include <drivers/st7735s.h>
+mem_pool_t kheap_pool;
+
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);
/* 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();
printf("UNKNOWN\n");
}
- tmp = (tmp >> 16);
+ /* tmp = (tmp >> 16);
printf("REV: ");
switch (tmp) {
case 0x1000:
break;
default:
printf("UNKNOWN\n");
- }
+ } */
extern char _endofbss;
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);
}
#include <lib/regfunc.h>
#include <lib/tinyprintf.h>
+#include <lib/pool.h>
+
#include <drivers/led.h>
#define SERIAL 1
static char buf[BUFSIZE];
-
struct cmd {
char * name;
int (*function)(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;
}