comparison cos/kernel/kernel.c @ 23:5dd47d6eebac

Added ubersimple malloc algorithm
author windel
date Thu, 01 Dec 2011 21:42:59 +0100
parents 69bc6d477b38
children d8627924d40d
comparison
equal deleted inserted replaced
22:69bc6d477b38 23:5dd47d6eebac
124 i++; 124 i++;
125 } 125 }
126 buffer[i] = 0; 126 buffer[i] = 0;
127 } 127 }
128 128
129 #define HEAP_MAGIC 0xc0ffee
130 #define HEAP_START 0x400000
131 #define HEAP_SIZE 0x200000
132 #define HEAP_INUSE 1
133 #define HEAP_FREE 0
134
135 typedef struct {
136 uint64_t magic;
137 uint64_t state;
138 uint64_t size;
139 } heap_t;
140
129 /* 141 /*
130 malloc and free divide the chunks of memory present at the heap 142 malloc and free divide the chunks of memory present at the heap
131 of the kernel into smaller parts. 143 of the kernel into smaller parts.
132 The heap is located at: 0x 144 The heap is located at: 0x
133 */ 145 */
134 static void* kernel_heap = (void*) 0x400000; // 4 MB - 6 MB is heap 146 static heap_t* kernel_heap = (heap_t*) 0x400000; // 4 MB - 6 MB is heap
135 /* Allocates 'size' bytes and returns the pointer if succesfull. 147 /* Allocates 'size' bytes and returns the pointer if succesfull.
136 Kernelpanic in case of failure.. 148 Kernelpanic in case of failure..
137 */ 149 */
150
138 void* kmalloc(uint64_t size) { 151 void* kmalloc(uint64_t size) {
139 printf("Malloc %d bytes\n", size); 152 // printf("Malloc %d bytes\n", size);
140 return kernel_heap; 153
154 // Start at the beginning of our heap and search a free block:
155 heap_t *current = kernel_heap;
156 while (current->magic == HEAP_MAGIC)
157 {
158 if ((current->state == HEAP_FREE) && (current->size >= size))
159 {
160 // Mark block as used:
161 current->state = HEAP_INUSE;
162
163 // Insert a heap header if required:
164 if (current->size > size + sizeof(heap_t) + 1)
165 {
166 // Calculate location of the inserted header:
167 heap_t *newheader = (heap_t*) (((char*)current)+size+sizeof(heap_t));
168
169 // Set the new header fields:
170 newheader->size = current->size - size - sizeof(heap_t);
171 newheader->state = HEAP_FREE;
172 newheader->magic = HEAP_MAGIC;
173
174 // Set the size of this block
175 current->size = size;
176 }
177 else
178 {
179 // We allocate this whole block
180 }
181 // Calculate the size of the block:
182 char *address = ((char*)current)+sizeof(heap_t);
183 return address;
184
185 }
186 // Goto next heap block:
187 current = (heap_t*)(((char*) current) + current->size + sizeof(heap_t));
188 }
189 return 0x0;
141 } 190 }
142 191
143 void kfree(void* ptr) { 192 void kfree(void* ptr) {
144 printf("Free address %x\n", ptr); 193 printf("Free address %x\n", ptr);
145 } 194 }
146 195
196 void init_heap(void)
197 {
198 // Initialize the kernel heap:
199 kernel_heap->magic = HEAP_MAGIC;
200 kernel_heap->state = HEAP_FREE;
201 kernel_heap->size = HEAP_SIZE - sizeof(heap_t);
202 }
203
147 void startPython() 204 void startPython()
148 { 205 {
149 // TODO: connect to Py_Main 206 // TODO: connect to Py_Main
150 PyRun_SimpleString("print('hello world')"); 207 //PyRun_SimpleString("print('hello world')");
151 208
152 } 209 }
153 210
154 void testMalloc() 211 void testMalloc()
155 { 212 {
156 char *a, *b; 213 char *a, *b;
214
215 printf("Testing malloc\n");
157 a = kmalloc(100); 216 a = kmalloc(100);
158 printf("Got a at %x\n", a); 217 printf("Got a at %x\n", a);
159 a[0] = 'A'; 218 a[0] = 'A';
160 b = kmalloc(22); 219 b = kmalloc(22);
161 printf("Got b at %x\n", b); 220 printf("Got b at %x\n", b);
162 b[0] = 'B'; 221 b[0] = 'B';
163 kfree(a); 222 kfree(a);
164
165 } 223 }
166 224
167 void kmain() 225 void kmain()
168 { 226 {
169 init_screen(); 227 init_screen();
170 setupIDT(); 228 setupIDT();
171 229 init_heap();
172 printf("Welcome! .. "); 230
173 printf("Testing malloc"); 231 printf("Welcome!\n");
174 testMalloc();
175 printf("Entering mainloop!\n");
176 232
177 while (1==1) 233 while (1==1)
178 { 234 {
179 char buffer[70]; 235 char buffer[70];
180 printf(">>>"); 236 printf(">>>");
184 printf("Got line: '%s'\n", buffer); 240 printf("Got line: '%s'\n", buffer);
185 if (buffer[0] == 'x') 241 if (buffer[0] == 'x')
186 { 242 {
187 printf("System time in ms: %d\n", getTimeMS()); 243 printf("System time in ms: %d\n", getTimeMS());
188 } 244 }
245 if (buffer[0] == 't')
246 {
247 testMalloc();
248 }
189 if ( strncmp(buffer, "help", 4)) 249 if ( strncmp(buffer, "help", 4))
190 { 250 {
191 printf("Help\n Try one of these commands:\n"); 251 printf("Help\n Try one of these commands:\n");
192 printf(" x: print system time in ms\n"); 252 printf(" x: print system time in ms\n");
193 } 253 }