7 /* TOTAL SRAM MEMORY: 64kB
9 * 128 chunks of 512 bytes
10 * SIMPLE BITMAP IMPLEMENTATION
17 #define BASE 0x20000400
19 #define SET_SIZE(s) (s << 8)
20 #define SET_FLAGS(f) (f << 24)
21 #define IS_ALLOC(c) ((c >> 24) & 0x0F )
22 #define PADDR(i) (0x20000400 + (i * 0x200))
23 #define I_OFFSET(p) ((p - 0x20000400))
25 uint32_t chunk[CHUNKS];
28 * | FLAGS | SIZE | RESERVED |
29 * | 0x00 | 0x0000 | 0x00 |
37 chunk[0] = SET_SIZE(96) | SET_FLAGS(ALLOC);
40 /* uart_puts("ALLOC:\n");
41 int * p = mm_alloc(100);
49 int * p2 = mm_alloc(100);
58 void test_memory(uint32_t * ptr) {
61 for (int i = 0; i < 100; i++) {
69 void * mm_alloc(size_t size) { // in bytes
72 uart_puts("SYSERROR: WE CAN'T ALLOCATE THAT MUCH!\n");
76 /* check which chunk is free */
77 for (int i = 1; i < CHUNKS; i++) {
78 if (!IS_ALLOC(chunk[i])) {
79 chunk[i] = SET_SIZE(size) | SET_FLAGS(ALLOC);
80 return (void *) PADDR(i);
84 uart_puts("SYSERROR: OUT OF MEMORY\n");
88 void free(void *ptr) {
90 uint32_t index = (uint32_t) I_OFFSET(ptr) / 0x200;
92 uint32_t tmp = chunk[index];
94 uart_puts("SYSERROR: ALREADY FREED!\n");
96 else if(index < CHUNKS) {
97 chunk[index] = SET_FLAGS(FREE) | SET_SIZE(0);