annotate cos/kernel/malloc.c @ 31:88590c42320f

Changed interrupt handler
author windel
date Tue, 10 Jan 2012 20:40:35 +0100
parents 7e3bdcb391dc
children 24ce177e01e8
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
28
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
3 // ================= Placement malloc:
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
4 // Assume here that nothing gets ever kfree'd. This makes it simple!
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
5
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
6 uint64_t placement_address = (uint64_t)&kernel_end;
28
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
7
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
8 void* kmalloc_int(uint64_t size)
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
9 {
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
10 uint64_t tmp = placement_address;
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
11 placement_address += size;
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
12 return (void*)tmp;
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
13 }
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
14
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
15 void* kmalloc_aligned_int(uint64_t size)
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
16 {
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
17 if ( (placement_address | 0xFFF) != 0 )
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
18 {
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
19 placement_address &= ~(0xFFF);
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
20 placement_address += 0x1000;
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
21 }
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
22 uint64_t tmp = placement_address;
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
23 placement_address += size;
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
24 return (void*)tmp;
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
25 }
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
26
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
27
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
28 // ================= Other malloc
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
29 // TODO: move this to user space?
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
30
24
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
31 #define HEAP_MAGIC 0xc0ffee
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
32 #define HEAP_START 0x400000
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
33 #define HEAP_SIZE 0x200000
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
34 #define HEAP_INUSE 1
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
35 #define HEAP_FREE 0
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 typedef struct {
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
38 uint64_t magic;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
39 uint64_t state;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
40 uint64_t size;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
41 } 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 /*
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
44 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
45 of the kernel into smaller parts.
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
46 The heap is located at: 0x
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
47 */
28
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
48
24
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
49 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
50 /* Allocates 'size' bytes and returns the pointer if succesfull.
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
51 Kernelpanic in case of failure..
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
26
dcce92b1efbc Added mm.c
windel
parents: 25
diff changeset
54 void* kmalloc(uint64_t size)
dcce92b1efbc Added mm.c
windel
parents: 25
diff changeset
55 {
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
56 return kmalloc_int(size);
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
57
24
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
58 // 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
59 heap_t *current = kernel_heap;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
60 while (current->magic == HEAP_MAGIC)
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
61 {
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
62 if ((current->state == HEAP_FREE) && (current->size >= size))
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
63 {
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
64 // Mark block as used:
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
65 current->state = HEAP_INUSE;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
66
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
67 // Insert a heap header if required:
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
68 if (current->size > size + sizeof(heap_t) + 1)
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 // Calculate location of the inserted header:
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
71 heap_t *newheader = (heap_t*) (((char*)current)+size+sizeof(heap_t));
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
72
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
73 // Set the new header fields:
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
74 newheader->size = current->size - size - sizeof(heap_t);
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
75 newheader->state = HEAP_FREE;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
76 newheader->magic = HEAP_MAGIC;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
77
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
78 // Set the size of this block
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
79 current->size = size;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
80 }
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
81 else
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
82 {
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
83 // We allocate this whole block
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
84 }
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
85 // Calculate the size of the block:
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
86 char *address = ((char*)current)+sizeof(heap_t);
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
87 return address;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
88
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
89 }
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
90 // Goto next heap block:
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
91 current = (heap_t*)(((char*) current) + current->size + sizeof(heap_t));
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
92 }
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
93 return 0x0;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
94 }
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
95
26
dcce92b1efbc Added mm.c
windel
parents: 25
diff changeset
96 void kfree(void* ptr)
dcce92b1efbc Added mm.c
windel
parents: 25
diff changeset
97 {
24
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
98 printf("Free address %x\n", ptr);
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
99 }
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
100
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
101 void init_heap(void)
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
102 {
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
103 // Initialize the kernel heap:
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
104 kernel_heap->magic = HEAP_MAGIC;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
105 kernel_heap->state = HEAP_FREE;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
106 kernel_heap->size = HEAP_SIZE - sizeof(heap_t);
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
107 }
25
d3c4bf3720a3 Beginning of multitasking
windel
parents: 24
diff changeset
108