1+ // Based on code from http://www.jamesmolloy.co.uk/tutorial_html/4.-The%20Heap.html
2+ #ifndef _KHEAP_H_
3+ #define _KHEAP_H_
4+
5+ #include "system.h"
6+ #include "printf.h"
7+ #include "string.h"
8+ #include "vga.h"
9+ #include "paging.h"
10+
11+ #define KHEAP_START (void*)0xC0400000
12+ #define KHEAP_INITIAL_SIZE 48 * MB
13+ #define KHEAP_MAX_ADDRESS (void*)0xCFFFFFFF
14+ #define HEAP_MIN_SIZE 4 * MB
15+
16+ #define PAGE_SIZE 4096
17+ #define OVERHEAD (sizeof(struct memory_block) + sizeof(uint32_t))
18+
19+ typedef struct memory_block
20+ {
21+ struct memory_block * next ;
22+ struct memory_block * prev ;
23+ uint32_t size ;
24+ } memory_block_t ;
25+
26+ extern bool Kheap_enabled ;
27+
28+ /*
29+ * if heap is initialized then uss the dynamic memory allocator else use the placement memory allocator
30+ * @param size The size of the memory to be allocated
31+ * @param align Should the memory be aligned?
32+ * @param paddr Should the allocated memory be given a physical address?
33+ */
34+ uint32_t kmalloc_int (uint32_t size , int align , uint32_t * paddr );
35+
36+ /*
37+ * continuous kmalloc which can be used before the heap is initialised
38+ * @param size The size of the memory to be allocated
39+ * @param align Should the memory be aligned?
40+ * @param paddr Should the allocated memory be given a physical address?
41+ */
42+ void * kmalloc_c (uint32_t size , int align , uint32_t * paddr );
43+
44+ /*
45+ * Wrapper for kmalloc_int with align = 1
46+ * @param size The size of the memory to be allocated
47+ */
48+ void * kmalloc_a (uint32_t size );
49+
50+ /*
51+ * Wrapper for kmalloc_int with align = 0 and paddr = paddr
52+ * @param size The size of the memory to be allocated
53+ * @param paddr Should the allocated memory be given a physical address?
54+ */
55+ uint32_t kmalloc_p (uint32_t size , uint32_t * paddr );
56+
57+ /*
58+ * Wrapper for kmalloc_int with align = 1 and paddr = paddr
59+ * @param size The size of the memory to be allocated
60+ * @param paddr Should the allocated memory be given a physical address?
61+ */
62+ uint32_t kmalloc_ap (uint32_t size , uint32_t * paddr );
63+
64+ /*
65+ * Wrapper for kmalloc_int with align = 0 and paddr = 0
66+ * @param size The size of the memory to be allocated
67+ */
68+ void * kmalloc (uint32_t size );
69+
70+ /*
71+ * A function to free a block of memory previously allocated with kmalloc
72+ * @param ptr The pointer to the memory to be freed
73+ */
74+ void kfree (void * ptr );
75+
76+ /*
77+ * A function to reallocate a block of memory previously allocated with kmalloc
78+ * @param ptr The pointer to the memory to be reallocated
79+ * @param size The size of the memory to be reallocated
80+ */
81+ void * krealloc (void * ptr , uint32_t size );
82+
83+ void * kcalloc (uint32_t size , uint32_t num );
84+
85+ void print_db ();
86+
87+ int doesItFit (memory_block_t * block , uint32_t size );
88+
89+ void setFree (uint32_t * size , int x );
90+
91+ void removeFromList (memory_block_t * block );
92+ void addToList (memory_block_t * block );
93+
94+ int isBetter (memory_block_t * block1 , memory_block_t * block2 );
95+ memory_block_t * bestFit (uint32_t size );
96+ memory_block_t * getPrevBlock (memory_block_t * block );
97+ memory_block_t * getNextBlock (memory_block_t * block );
98+
99+ uint32_t getRealSize (uint32_t size );
100+ uint32_t getSbrkSize (uint32_t size );
101+
102+ int isFree (memory_block_t * block );
103+ int isEnd (memory_block_t * block );
104+
105+ void init_kheap (void * start , void * end , void * max );
106+
107+ void * malloc (uint32_t size );
108+ void * realloc (void * ptr , uint32_t size );
109+ void * calloc (uint32_t size , uint32_t num );
110+
111+ void free (void * ptr );
112+ #endif
0 commit comments