24
|
1 #include "kernel.h"
|
|
2
|
28
|
3 // ================= Placement malloc:
|
29
|
4 // Assume here that nothing gets ever kfree'd. This makes it simple!
|
|
5
|
|
6 uint64_t placement_address = (uint64_t)&kernel_end;
|
28
|
7
|
|
8 void* kmalloc_int(uint64_t size)
|
|
9 {
|
|
10 uint64_t tmp = placement_address;
|
|
11 placement_address += size;
|
|
12 return (void*)tmp;
|
|
13 }
|
|
14
|
|
15 void* kmalloc_aligned_int(uint64_t size)
|
|
16 {
|
|
17 if ( (placement_address | 0xFFF) != 0 )
|
|
18 {
|
|
19 placement_address &= ~(0xFFF);
|
|
20 placement_address += 0x1000;
|
|
21 }
|
|
22 uint64_t tmp = placement_address;
|
|
23 placement_address += size;
|
|
24 return (void*)tmp;
|
|
25 }
|
|
26
|
|
27
|
|
28 // ================= Other malloc
|
|
29 // TODO: move this to user space?
|
|
30
|
24
|
31 #define HEAP_MAGIC 0xc0ffee
|
|
32 #define HEAP_START 0x400000
|
|
33 #define HEAP_SIZE 0x200000
|
|
34 #define HEAP_INUSE 1
|
|
35 #define HEAP_FREE 0
|
|
36
|
|
37 typedef struct {
|
|
38 uint64_t magic;
|
|
39 uint64_t state;
|
|
40 uint64_t size;
|
|
41 } heap_t;
|
|
42
|
|
43 /*
|
|
44 malloc and free divide the chunks of memory present at the heap
|
|
45 of the kernel into smaller parts.
|
|
46 The heap is located at: 0x
|
|
47 */
|
28
|
48
|
24
|
49 static heap_t* kernel_heap = (heap_t*) 0x400000; // 4 MB - 6 MB is heap
|
|
50 /* Allocates 'size' bytes and returns the pointer if succesfull.
|
|
51 Kernelpanic in case of failure..
|
|
52 */
|
|
53
|
26
|
54 void* kmalloc(uint64_t size)
|
|
55 {
|
29
|
56 return kmalloc_int(size);
|
|
57
|
24
|
58 // Start at the beginning of our heap and search a free block:
|
|
59 heap_t *current = kernel_heap;
|
|
60 while (current->magic == HEAP_MAGIC)
|
|
61 {
|
|
62 if ((current->state == HEAP_FREE) && (current->size >= size))
|
|
63 {
|
|
64 // Mark block as used:
|
|
65 current->state = HEAP_INUSE;
|
|
66
|
|
67 // Insert a heap header if required:
|
|
68 if (current->size > size + sizeof(heap_t) + 1)
|
|
69 {
|
|
70 // Calculate location of the inserted header:
|
|
71 heap_t *newheader = (heap_t*) (((char*)current)+size+sizeof(heap_t));
|
|
72
|
|
73 // Set the new header fields:
|
|
74 newheader->size = current->size - size - sizeof(heap_t);
|
|
75 newheader->state = HEAP_FREE;
|
|
76 newheader->magic = HEAP_MAGIC;
|
|
77
|
|
78 // Set the size of this block
|
|
79 current->size = size;
|
|
80 }
|
|
81 else
|
|
82 {
|
|
83 // We allocate this whole block
|
|
84 }
|
|
85 // Calculate the size of the block:
|
|
86 char *address = ((char*)current)+sizeof(heap_t);
|
|
87 return address;
|
|
88
|
|
89 }
|
|
90 // Goto next heap block:
|
|
91 current = (heap_t*)(((char*) current) + current->size + sizeof(heap_t));
|
|
92 }
|
|
93 return 0x0;
|
|
94 }
|
|
95
|
26
|
96 void kfree(void* ptr)
|
|
97 {
|
24
|
98 printf("Free address %x\n", ptr);
|
|
99 }
|
|
100
|
|
101 void init_heap(void)
|
|
102 {
|
|
103 // Initialize the kernel heap:
|
|
104 kernel_heap->magic = HEAP_MAGIC;
|
|
105 kernel_heap->state = HEAP_FREE;
|
|
106 kernel_heap->size = HEAP_SIZE - sizeof(heap_t);
|
|
107 }
|
25
|
108
|