7 /* TOTAL SRAM MEMORY: 64kB
9 * 128 chunks of 512 bytes
10 * SIMPLE BITMAP IMPLEMENTATION
18 #define BASE 0x20000400
20 #define SET_SIZE(s) (s << 8)
21 #define SET_FLAGS(f) (f << 24)
22 #define IS_ALLOC(c) ((c >> 24) & 0x0F )
23 #define PADDR(i) (0x20000400 + (i * 0x200))
24 #define I_OFFSET(p) ((p - 0x20000400))
26 uint32_t chunk[CHUNKS];
29 * | FLAGS | SIZE | RESERVED |
30 * | 0x00 | 0x0000 | 0x00 |
38 chunk[0] = SET_SIZE(96) | SET_FLAGS(ALLOC);
41 /* uart_puts("ALLOC:\n");
42 int * p = mm_alloc(100);
50 int * p2 = mm_alloc(100);
59 void test_memory(uint32_t * ptr) {
62 for (int i = 0; i < 100; i++) {
70 void * mm_alloc(size_t size) { // in bytes
73 uart_puts("SYSERROR: WE CAN'T ALLOCATE THAT MUCH!\n");
77 /* check which chunk is free */
78 for (int i = 1; i < CHUNKS; i++) {
79 if (!IS_ALLOC(chunk[i])) {
80 chunk[i] = SET_SIZE(size) | SET_FLAGS(ALLOC);
81 return (void *) PADDR(i);
85 uart_puts("SYSERROR: OUT OF MEMORY\n");
89 void free(void *ptr) {
91 uint32_t index = (uint32_t) I_OFFSET(ptr) / 0x200;
93 uint32_t tmp = chunk[index];
95 uart_puts("SYSERROR: ALREADY FREED!\n");
97 else if(index < CHUNKS) {
98 chunk[index] = SET_FLAGS(FREE) | SET_SIZE(0);