126 if (newBlock ==
NULL)
167 block->
size = newSize;
241 if (suitableBlock ==
NULL)
244 if (suitableBlock ==
NULL)
256 return suitableBlock;
272 block->
flags &= ~_HEAP_ALLOCATED;
283 block->
size = newSize;
296 prev->
size = newSize;
#define assert(expression)
uint64_t vmm_unmap(space_t *space, void *virtAddr, uint64_t length)
Unmaps virtual memory from a given address space.
void * vmm_alloc(space_t *space, void *virtAddr, uint64_t length, pml_flags_t pmlFlags, vmm_alloc_flags_t allocFlags)
Allocates and maps virtual memory in a given address space.
static void lock_init(lock_t *lock)
Initializes a lock.
static void lock_release(lock_t *lock)
Releases a lock.
static void lock_acquire(lock_t *lock)
Acquires a lock, blocking until it is available.
#define _HEAP_LARGE_ALLOC_THRESHOLD
void _heap_acquire(void)
Acquire the heap lock.
void _heap_add_to_free_list(_heap_header_t *block)
Adds a block to the appropriate free list.
_heap_header_t * _heap_block_new(uint64_t minSize)
Directly maps a new heap block of at least minSize bytes.
void _heap_unmap_memory(void *addr, uint64_t size)
Unmaps previously mapped memory.
void _heap_init(void)
Initialize the heap.
void _heap_free(_heap_header_t *block)
Frees a previously allocated heap block.
void _heap_block_split(_heap_header_t *block, uint64_t newSize)
Splits a heap block into two blocks, the first of size bytes and the second with the remaining bytes.
void * _heap_map_memory(uint64_t size)
Directly maps memory of the given size.
_heap_header_t * _heap_alloc(uint64_t size)
Allocates a block of memory of given size.
list_t _heapList
A list of all blocks sorted by address.
void _heap_remove_from_free_list(_heap_header_t *block)
Removes a block from its free list.
#define _HEAP_HEADER_MAGIC
void _heap_release(void)
Release the heap lock.
uint64_t _heap_get_bin_index(uint64_t size)
Get the bin index for a given size.
@ _HEAP_ALLOCATED
Block is allocated.
@ _HEAP_MAPPED
Block is not on the heap, but mapped directly, used for large allocations.
@ _HEAP_ZEROED
Block is zeroed.
static void bitmap_clear(bitmap_t *map, uint64_t index)
Clear a bit in the bitmap.
static void bitmap_set(bitmap_t *map, uint64_t index)
Set a bit in the bitmap.
static void bitmap_init(bitmap_t *map, void *buffer, uint64_t length)
Initialize a bitmap.
#define BITMAP_BITS_TO_QWORDS(bits)
Convert number of bits to number of qwords.
static uint64_t bitmap_find_first_set(bitmap_t *map, uint64_t startIdx)
Find the first set bit in the bitmap.
fd_t open(const char *path)
System call for opening files.
static list_entry_t * list_last(list_t *list)
Gets the last entry in the list without removing it.
#define LIST_CREATE(name)
Creates a list initializer.
static void list_remove(list_t *list, list_entry_t *entry)
Removes a list entry from its current list.
static void list_push(list_t *list, list_entry_t *entry)
Pushes an entry to the end of the list.
static void list_append(list_t *list, list_entry_t *prev, list_entry_t *entry)
Appends an entry to the list.
static bool list_is_empty(list_t *list)
Checks if a list is empty.
static void list_entry_init(list_entry_t *entry)
Initializes a list entry.
static void list_init(list_t *list)
Initializes a list.
static list_entry_t * list_pop(list_t *list)
Pops the first entry from the list.
#define ROUND_UP(number, multiple)
void * mmap(fd_t fd, void *address, uint64_t length, prot_t prot)
System call to map memory from a file.
#define PAGE_SIZE
Memory page size.
uint64_t munmap(void *address, uint64_t length)
System call to unmap mapped memory.
#define BYTES_TO_PAGES(amount)
Convert bytes to pages.
@ PROT_READ
Memory can be read from.
@ PROT_WRITE
Memory can be written to.
#define NULL
Pointer error value.
#define ERR
Integer error value.
#define CONTAINER_OF(ptr, type, member)
Container of macro.
__UINT64_TYPE__ fd_t
A file descriptor.
#define CONTAINER_OF_SAFE(ptr, type, member)
Safe container of macro.
static list_t freeLists[_HEAP_NUM_BINS]
static bitmap_t freeBitmap
static uint64_t freeBitmapBuffer[BITMAP_BITS_TO_QWORDS(_HEAP_NUM_BINS)]
double remainder(double x, double y)
static uint64_t pageAmount
__UINTPTR_TYPE__ uintptr_t
_PUBLIC _NORETURN void abort(void)
_PUBLIC void * memset(void *s, int c, size_t n)
struct list_entry * next
The next entry in the list.
struct list_entry * prev
The previous entry in the list.
A simple ticket lock implementation.
_PUBLIC int mtx_lock(mtx_t *mtx)
_PUBLIC int mtx_init(mtx_t *mtx, int type)
_PUBLIC int mtx_unlock(mtx_t *mtx)