The manual
ginnOS
The reference: libc, the libraries and the kernel
Toggle main menu visibility
Loading...
Searching...
No Matches
heap.h
Go to the documentation of this file.
1
#pragma once
2
3
#include "
common/stdint.h
"
4
5
/**
6
* initial number of pages allocated for the kernel heap.
7
* 16 pages = 64 KiB.
8
*/
9
#define HEAP_INITIAL_PAGES 16
10
11
/**
12
* minimum data size for a split block.
13
* prevents creating uselessly small free blocks during splitting.
14
*/
15
#define HEAP_MIN_BLOCK_DATA 16
16
17
/**
18
* minimum number of pages to add when the heap needs to grow.
19
* 16 pages = 64 KiB per expansion, so the segment table fills slowly.
20
*/
21
#define HEAP_EXPAND_PAGES 16
22
23
/**
24
* maximum number of separate physical regions (segments) the heap can span.
25
* the heap grows in place when the pages after its newest segment are free,
26
* otherwise it takes a new contiguous run of pages from anywhere in RAM.
27
* that works because all RAM is identity-mapped; a later virtual-memory heap
28
* (kernel plan K3) removes the limit.
29
*/
30
#define HEAP_MAX_SEGMENTS 64
31
32
#ifdef HEAP_DEBUG
33
// redzone canary value placed before and after each allocation.
34
#define HEAP_CANARY 0xCAFEBABEu
35
36
// poison value written over freed memory to detect use-after-free.
37
#define HEAP_POISON 0xDEADBEEFu
38
39
// size of the redzone (in bytes) placed at each end of an allocation.
40
#define HEAP_REDZONE_SIZE 4
41
#endif
42
43
/**
44
* block header placed before each allocation.
45
* the usable memory returned to the caller begins immediately after this header.
46
* all allocations are 4-byte aligned.
47
*/
48
typedef
struct
heap_block
49
{
50
size_t
size
;
// size of the data area in bytes (excludes header)
51
bool
free
;
// true if this block is available for allocation
52
struct
heap_block
*
next
;
// next block in the linked list (NULL if last)
53
}
heap_block_t
;
54
55
/**
56
* heap allocation statistics.
57
*/
58
typedef
struct
59
{
60
uint32_t
total_allocs
;
// total number of kmalloc/kcalloc/krealloc calls that succeeded
61
uint32_t
total_frees
;
// total number of kfree calls (excluding NULL)
62
size_t
current_usage
;
// bytes currently allocated (data only, excludes headers)
63
size_t
peak_usage
;
// highest current_usage value ever reached
64
}
heap_stats_t
;
65
66
/**
67
* initialize the kernel heap.
68
* allocates HEAP_INITIAL_PAGES pages from the PMM and sets up the free list.
69
* must be called after pmm_init().
70
*/
71
void
heap_init
(
void
);
72
73
/**
74
* allocate size bytes from the kernel heap.
75
* the returned pointer is 4-byte aligned.
76
* @param size number of bytes to allocate.
77
* @return pointer to the allocated memory, or NULL on failure.
78
*/
79
void
*
kmalloc
(
size_t
size
);
80
81
/**
82
* allocate zeroed memory for an array.
83
* @param count number of elements.
84
* @param size size of each element in bytes.
85
* @return pointer to zeroed memory, or NULL on failure.
86
*/
87
void
*
kcalloc
(
size_t
count,
size_t
size
);
88
89
/**
90
* resize a previously allocated block.
91
* if possible, expands in place. otherwise allocates a new block,
92
* copies the old data, and frees the original.
93
* @param ptr pointer previously returned by kmalloc/kcalloc/krealloc, or NULL.
94
* @param new_size new size in bytes. if 0, equivalent to kfree(ptr).
95
* @return pointer to the resized memory, or NULL on failure (original unchanged).
96
*/
97
void
*
krealloc
(
void
*ptr,
size_t
new_size);
98
99
/**
100
* free a previously allocated block.
101
* coalesces adjacent free blocks to reduce fragmentation.
102
* @param ptr pointer previously returned by kmalloc(). NULL is a no-op.
103
*/
104
void
kfree
(
void
*ptr);
105
106
/**
107
* return the total heap size in bytes, summed over all segments.
108
*/
109
size_t
heap_total_size
(
void
);
110
111
/**
112
* return the number of physical segments the heap currently spans.
113
*/
114
uint32_t
heap_segment_count
(
void
);
115
116
/**
117
* return the number of free bytes available in the heap.
118
*/
119
size_t
heap_free_size
(
void
);
120
121
/**
122
* return the total number of blocks (free + used) in the heap.
123
*/
124
uint32_t
heap_block_count
(
void
);
125
126
/**
127
* return a snapshot of the heap allocation statistics.
128
*/
129
heap_stats_t
heap_get_stats
(
void
);
130
131
/**
132
* walk the block list and check its invariants: every block lies entirely
133
* inside one segment, blocks inside a segment are back to back and cover it
134
* exactly, and no two adjacent blocks are both free (they would have been
135
* merged). intended for tests and debugging.
136
* @return true if the heap is consistent.
137
*/
138
bool
heap_check
(
void
);
stdint.h
kfree
void kfree(void *ptr)
free a previously allocated block.
Definition
heap.c:555
krealloc
void * krealloc(void *ptr, size_t new_size)
resize a previously allocated block.
Definition
heap.c:547
heap_get_stats
heap_stats_t heap_get_stats(void)
return a snapshot of the heap allocation statistics.
Definition
heap.c:612
heap_segment_count
uint32_t heap_segment_count(void)
return the number of physical segments the heap currently spans.
Definition
heap.c:576
kcalloc
void * kcalloc(size_t count, size_t size)
allocate zeroed memory for an array.
Definition
heap.c:367
heap_init
void heap_init(void)
initialize the kernel heap.
Definition
heap.c:162
kmalloc
void * kmalloc(size_t size)
allocate size bytes from the kernel heap.
Definition
heap.c:539
heap_check
bool heap_check(void)
walk the block list and check its invariants: every block lies entirely inside one segment,...
Definition
heap.c:660
heap_block_count
uint32_t heap_block_count(void)
return the total number of blocks (free + used) in the heap.
Definition
heap.c:598
heap_total_size
size_t heap_total_size(void)
return the total heap size in bytes, summed over all segments.
Definition
heap.c:562
heap_free_size
size_t heap_free_size(void)
return the number of free bytes available in the heap.
Definition
heap.c:581
heap_block_t
struct heap_block heap_block_t
block header placed before each allocation.
uint32_t
unsigned int uint32_t
Definition
fb.h:17
heap_block
block header placed before each allocation.
Definition
heap.h:49
heap_block::free
bool free
Definition
heap.h:51
heap_block::next
struct heap_block * next
Definition
heap.h:52
heap_block::size
size_t size
Definition
heap.h:50
heap_stats_t
heap allocation statistics.
Definition
heap.h:59
heap_stats_t::current_usage
size_t current_usage
Definition
heap.h:62
heap_stats_t::total_frees
uint32_t total_frees
Definition
heap.h:61
heap_stats_t::peak_usage
size_t peak_usage
Definition
heap.h:63
heap_stats_t::total_allocs
uint32_t total_allocs
Definition
heap.h:60
src
kernel
memory
heap.h
Generated by
1.18.0