axle OS
x86_32 UNIX-like hobby OS
kheap.h
1 #ifndef STD_KHEAP_H
2 #define STD_KHEAP_H
3 
4 #include "std_base.h"
5 #include "array_o.h"
6 #include <stdint.h>
7 #include <stdbool.h>
8 #include <std/klog.h>
9 
10 __BEGIN_DECLS
11 
12 //page aligned
13 STDAPI void* kmalloc_a(uint32_t sz);
14 
15 //returns physical address
16 STDAPI void* kmalloc_p(uint32_t sz, uint32_t* phys);
17 
18 //page aligned and returns physical address
19 STDAPI void* kmalloc_ap(uint32_t sz, uint32_t* phys);
20 
21 //private functions/macros required for kmalloc macro
22 //TODO figure out how to hide these while keeping kmalloc public
23 void* kmalloc_real(uint32_t sz);
24 void kmalloc_track_int(char* file, int line, uint32_t size);
25 #define kmalloc_track(bytes) ({ kmalloc_track_int(__FILE__, __LINE__, bytes); kmalloc_real(bytes); })
26 #define kmalloc(bytes) kmalloc_track(bytes)
27 
28 #define KHEAP_START 0xC0000000
29 #define KHEAP_INITIAL_SIZE 0x6000000
30 #define KHEAP_MAX_ADDRESS 0xDFFFF000
31 //#define KHEAP_MAX_ADDRESS 0xCFFFF000
32 
33 #define HEAP_MAGIC 0xCAFEBABE
34 #define HEAP_MIN_SIZE 0x70000
35 #define MIN_BLOCK_SIZE 0x10
36 
37 //size information for hole/block
38 typedef struct alloc_block_t {
39  uint32_t magic; //magic number
40  struct alloc_block_t* next;
41  struct alloc_block_t* prev;
42  bool free; //is this block in use?
43  uint32_t size; //usable size
45 
46 typedef struct {
47  uint32_t start_address; //start of allocated space
48  uint32_t end_address; //end of allocated space (can be expanded up to max_address)
49  uint32_t max_address; //maximum address heap can be expanded to
50  uint8_t supervisor; //should new pages mapped be marked as kernel mode?
51  uint8_t readonly; //should new pages mapped be marked as read-only?
52 } heap_t;
53 
54 //create new heap
55 STDAPI heap_t* create_heap(uint32_t start, uint32_t end, uint32_t max, uint8_t supervisor, uint8_t readonly);
56 
57 //allocates contiguous region of memory of size 'size'. If aligned, creates block starting on page boundary
58 STDAPI void* alloc(uint32_t size, uint8_t page_align, heap_t* heap);
59 
60 //releases block allocated with alloc
61 STDAPI void free(void* p, heap_t* heap);
62 
63 //releases block allocated with alloc using current heap
64 STDAPI void kfree(void* p);
65 
66 //enlarges heap to new_size
67 void expand(uint32_t new_size, heap_t* heap);
68 
69 //returns number of bytes currently in use by heap
70 uint32_t used_mem();
71 
72 //debug function to dump last 'count' kernel heap allocs
73 //if 'count' is larger than total heap allocations, or
74 //count is -1, prints all heap allocations
75 //outputs to syslog
76 void heap_print(int count);
77 
78 //debug function to dump amounts of memory in use by axle source files
79 //outputs to syslog
80 void memdebug();
81 
82 //internal function to traverse heap and verify that
83 //no heap data has been corrupted
84 //on failure, kills current process
85 void heap_verify_integrity();
86 
87 __END_DECLS
88 
89 #endif // STD_KHEAP_H
Definition: size.h:4
Definition: shapes.h:6
Definition: kheap.h:46
Definition: kheap.h:38