annotate cos/kernel/malloc.c @ 26:dcce92b1efbc

Added mm.c
author windel
date Tue, 27 Dec 2011 17:36:52 +0100
parents d3c4bf3720a3
children 47b7df514243
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
26
dcce92b1efbc Added mm.c
windel
parents: 25
diff changeset
25 void* kmalloc(uint64_t size)
dcce92b1efbc Added mm.c
windel
parents: 25
diff changeset
26 {
24
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
27 // 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
28 heap_t *current = kernel_heap;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
29 while (current->magic == HEAP_MAGIC)
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
30 {
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
31 if ((current->state == HEAP_FREE) && (current->size >= size))
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
32 {
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
33 // Mark block as used:
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
34 current->state = HEAP_INUSE;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
35
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
36 // Insert a heap header if required:
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
37 if (current->size > size + sizeof(heap_t) + 1)
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
38 {
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
39 // Calculate location of the inserted header:
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
40 heap_t *newheader = (heap_t*) (((char*)current)+size+sizeof(heap_t));
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
41
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
42 // Set the new header fields:
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
43 newheader->size = current->size - size - sizeof(heap_t);
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
44 newheader->state = HEAP_FREE;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
45 newheader->magic = HEAP_MAGIC;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
46
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
47 // Set the size of this block
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
48 current->size = size;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
49 }
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
50 else
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
51 {
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
52 // We allocate this whole block
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
53 }
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
54 // Calculate the size of the block:
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
55 char *address = ((char*)current)+sizeof(heap_t);
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
56 return address;
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
57
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 // Goto next heap block:
d8627924d40d Split up in more files and reboot command
windel
parents:
diff changeset
60 current = (heap_t*)(((char*) current) + current->size + sizeof(heap_t));
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 return 0x0;
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
26
dcce92b1efbc Added mm.c
windel
parents: 25
diff changeset
65 void kfree(void* ptr)
dcce92b1efbc Added mm.c
windel
parents: 25
diff changeset
66 {
24
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