annotate cos/kernel/malloc.c @ 25:d3c4bf3720a3

Beginning of multitasking
author windel
date Tue, 27 Dec 2011 13:31:38 +0100
parents d8627924d40d
children dcce92b1efbc
rev   line source
24
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
1 #include "kernel.h"
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
2
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
3 #define HEAP_MAGIC 0xc0ffee
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
4 #define HEAP_START 0x400000
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
5 #define HEAP_SIZE 0x200000
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
6 #define HEAP_INUSE 1
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
7 #define HEAP_FREE 0
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
8
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
9 typedef struct {
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
10 uint64_t magic;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
11 uint64_t state;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
12 uint64_t size;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
13 } heap_t;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
14
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
15 /*
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
16 malloc and free divide the chunks of memory present at the heap
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
17 of the kernel into smaller parts.
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
18 The heap is located at: 0x
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
19 */
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
20 static heap_t* kernel_heap = (heap_t*) 0x400000; // 4 MB - 6 MB is heap
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
21 /* Allocates 'size' bytes and returns the pointer if succesfull.
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
22 Kernelpanic in case of failure..
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
23 */
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
24
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
25 void* kmalloc(uint64_t size) {
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
26 // printf("Malloc %d bytes\n", size);
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
27
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
28 // Start at the beginning of our heap and search a free block:
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
29 heap_t *current = kernel_heap;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
30 while (current->magic == HEAP_MAGIC)
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
31 {
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
32 if ((current->state == HEAP_FREE) && (current->size >= size))
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
33 {
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
34 // Mark block as used:
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
35 current->state = HEAP_INUSE;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
36
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
37 // Insert a heap header if required:
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
38 if (current->size > size + sizeof(heap_t) + 1)
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
39 {
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
40 // Calculate location of the inserted header:
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
41 heap_t *newheader = (heap_t*) (((char*)current)+size+sizeof(heap_t));
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
42
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
43 // Set the new header fields:
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
44 newheader->size = current->size - size - sizeof(heap_t);
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
45 newheader->state = HEAP_FREE;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
46 newheader->magic = HEAP_MAGIC;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
47
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
48 // Set the size of this block
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
49 current->size = size;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
50 }
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
51 else
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
52 {
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
53 // We allocate this whole block
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
54 }
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
55 // Calculate the size of the block:
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
56 char *address = ((char*)current)+sizeof(heap_t);
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
57 return address;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
58
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
59 }
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
60 // Goto next heap block:
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
61 current = (heap_t*)(((char*) current) + current->size + sizeof(heap_t));
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
62 }
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
63 return 0x0;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
64 }
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
65
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
66 void kfree(void* ptr) {
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
67 printf("Free address %x\n", ptr);
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
68 }
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
69
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
70 void init_heap(void)
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
71 {
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
72 // Initialize the kernel heap:
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
73 kernel_heap->magic = HEAP_MAGIC;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
74 kernel_heap->state = HEAP_FREE;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
75 kernel_heap->size = HEAP_SIZE - sizeof(heap_t);
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
76 }
25
d3c4bf3720a3 Beginning of multitasking
windel
parents: 24
diff changeset
77