fixed-sized memory pool allocator
[cortex-from-scratch] / mm.c
1 /* (CC-BY-NC-SA) ROBIN KRENS - ROBIN @ ROBINKRENS.NL
2  * 
3  * $LOG$
4  * 2019/7/20 - ROBIN KRENS      
5  * Initial version 
6  * 
7  * $DESCRIPTION$
8  * Simple bitmap on bitband memory implementation for kernel
9  * heap. Sensitive to fragmentation over time. Bitband 
10  * memory makes it possible to access each bit of memory
11  * atomically. 
12  * 
13  * DEPRECATED
14  *
15  * */
16
17
18 #include <stdbool.h>
19 #include <stddef.h>
20 #include <stdint.h>
21 #include <stm32.h>
22 #include <mmap.h>
23
24
25 #define CHUNKS 256
26
27 #define MEM_VALUE(addr) *((volatile uint32_t *) (addr))
28 #define MEM_ADDR(addr) ((uint32_t *) (addr))
29 #define BITBAND(a, b) ((a & 0xF0000000) + 0x02000000 + ((a &0xFFFFF)<<5) + (b<<2))
30 #define INDEXTOADDR(a) (((a & 0xFFFFF) >> 5) + ((a & 0xFF000000) - 0x02000000))
31
32
33 /* Total SRAM: ~ 64kB 
34  * Divided into chunks (256 bytes)
35  * Each bit will index a chunk
36  * Bits needed: 0x100 (= 4 uint32_t)
37  * */
38
39 //uint32_t chunk_index[4];
40
41 void mm_init() {
42
43
44 //      memset(&chunk_index, 0, sizeof(uint32_t) * 4);
45
46
47 //      uint32_t *p = MEM_ADDR(0x20000278);
48         
49 //      extern stub();
50 //      stub();
51         // __asm__ __volatile__ ("udiv r1, r3 ,%0" :: "r"(0)); 
52 //
53 //      for(;;);
54 //      *p = (uint32_t volatile) 0x12345678;
55 //      *x = (uint32_t volatile) 0x12345679;
56 //      addrtohex(p);
57 //      addrtohex(*p);
58 //      MEM_ADDR(x) = 0x1;
59 //
60         
61
62 //      char * new = malloc(10);
63 //      addrtohex(new);
64 //      char * new2 = malloc(10);
65 //      addrtohex(new2);
66
67
68         //uint32_t * test = MEM_ADDR(0x20000000);
69         //uint32_t random_location = MEM_VALUE(0x20000000);
70
71         //uint32_t random_location = 0x20000900;
72
73         //MEM_VALUE(random_location);
74         //MEM_VALUE(BITBAND(random_location, 0)) = 0x1;
75
76         //addrtohex(MEM_VALUE(random_location));
77
78
79         
80 }
81
82  void test_memory(uint32_t * ptr) {
83
84         *ptr = 0xEEEEEEEE;
85         for (int i = 0; i < 100; i++) {
86                 ptr++;
87                 *ptr = 0xEEEEEEEE;
88         }
89
90
91
92 /* BIT BAND SCAN */
93
94  /* uint32_t fits(uint32_t * current, size_t size) {
95  
96         uint32_t addr_start = current;
97  
98         for (int i = 1; i < size; i++) {
99                 current + 4; // next bit offset is 0x4
100                 if ((MEM_VALUE(current)) == 0x1) 
101                         return 0x0;
102         }
103         return addr_start;
104  } */
105
106
107 /* void * malloc(size_t size) {
108
109         if (size < 256) {
110
111                 extern char * _endofbss;
112
113                 uint32_t start = (uint32_t) &chunk_index[0];    
114                 uint32_t current;
115                 int offset = 0x100;
116
117                 uint32_t * index = MEM_ADDR(BITBAND(start, 0));
118                 for(int i = 0; i < CHUNKS; i++) {
119                         if (*index == 0x0) {
120                                 addrtohex(*index);
121                                 *index = 0x1;
122                                 return INDEXTOADDR(((uint32_t)index)) + (i * offset);
123                         }
124                         index += 0x04;
125                 }
126                 return NULL;
127         }
128 } */
129  
130
131