PatchworkOS
Loading...
Searching...
No Matches
heap.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdbool.h>
4#include <stdint.h>
5#include <sys/list.h>
6#include <sys/proc.h>
7
31#define _HEAP_ALIGNMENT 64
32
36#define _HEAP_HEADER_MAGIC 0xDEADBEEF
37
41#define _HEAP_LARGE_ALLOC_THRESHOLD (PAGE_SIZE * 4)
42
46#define _HEAP_NUM_BINS (_HEAP_LARGE_ALLOC_THRESHOLD / _HEAP_ALIGNMENT)
47
51typedef enum
52{
53 _HEAP_ALLOCATED = 1 << 0,
54 _HEAP_MAPPED = 1 << 1,
55 _HEAP_ZEROED = 1 << 2,
57
75
76static_assert(sizeof(_heap_header_t) % _HEAP_ALIGNMENT == 0, "_heap_header_t size must be multiple of 64");
77
81extern list_t _heapList;
82
86void _heap_init(void);
87
93void _heap_acquire(void);
94
98void _heap_release(void);
99
107
117
126void _heap_block_split(_heap_header_t* block, uint64_t size);
127
140
153
165
175void _heap_free(_heap_header_t* block);
176
185void* _heap_map_memory(uint64_t size);
186
195void _heap_unmap_memory(void* addr, uint64_t size);
196
static fd_t data
Definition dwm.c:21
_heap_flags_t
Flags for heap blocks.
Definition heap.h:52
void _heap_acquire(void)
Acquire the heap lock.
Definition heap.c:81
void _heap_add_to_free_list(_heap_header_t *block)
Adds a block to the appropriate free list.
Definition heap.c:174
_heap_header_t * _heap_block_new(uint64_t minSize)
Directly maps a new heap block of at least minSize bytes.
Definition heap.c:114
void _heap_unmap_memory(void *addr, uint64_t size)
Unmaps previously mapped memory.
Definition heap.c:50
void _heap_init(void)
Initialize the heap.
Definition heap.c:62
void _heap_free(_heap_header_t *block)
Frees a previously allocated heap block.
Definition heap.c:259
void _heap_block_split(_heap_header_t *block, uint64_t size)
Splits a heap block into two blocks, the first of size bytes and the second with the remaining bytes.
Definition heap.c:154
void * _heap_map_memory(uint64_t size)
Directly maps memory of the given size.
Definition heap.c:39
_heap_header_t * _heap_alloc(uint64_t size)
Allocates a block of memory of given size.
Definition heap.c:199
list_t _heapList
A list of all blocks sorted by address.
Definition heap.c:60
void _heap_remove_from_free_list(_heap_header_t *block)
Removes a block from its free list.
Definition heap.c:185
void _heap_release(void)
Release the heap lock.
Definition heap.c:90
#define _HEAP_ALIGNMENT
Definition heap.h:31
uint64_t _heap_get_bin_index(uint64_t size)
Get the bin index for a given size.
Definition heap.c:99
@ _HEAP_ALLOCATED
Block is allocated.
Definition heap.h:53
@ _HEAP_MAPPED
Block is not on the heap, but mapped directly, used for large allocations.
Definition heap.h:54
@ _HEAP_ZEROED
Block is zeroed.
Definition heap.h:55
__UINT32_TYPE__ uint32_t
Definition stdint.h:15
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
list_entry_t freeEntry
Definition heap.h:71
uint64_t size
Definition heap.h:70
list_entry_t listEntry
Definition heap.h:72
_heap_flags_t flags
Definition heap.h:69
uint32_t magic
Definition heap.h:68
Header for each heap block.
A entry in a doubly linked list.
Definition list.h:38
A doubly linked list.
Definition list.h:51