115 if (newBlock ==
NULL)
137 list_append(&last->listEntry, &newBlock->listEntry);
145 if (block ==
NULL || newSize == 0)
161 block->size = newSize;
236 if (freeBinIndex != freeBitmap.length)
245 if (suitableBlock ==
NULL)
248 if (suitableBlock ==
NULL)
260 return suitableBlock;
279 block->flags &= ~_HEAP_ALLOCATED;
290 block->size = newSize;
303 prev->size = newSize;
#define assert(expression)
void * vmm_alloc(space_t *space, void *virtAddr, size_t length, size_t alignment, pml_flags_t pmlFlags, vmm_alloc_flags_t allocFlags)
Allocates and maps virtual memory in a given address space.
void * vmm_unmap(space_t *space, void *virtAddr, size_t length)
Unmaps virtual memory from a given address space.
@ VMM_ALLOC_OVERWRITE
If any page is already mapped, overwrite the mapping.
static void lock_init(lock_t *lock)
Initializes a lock.
#define _HEAP_LARGE_ALLOC_THRESHOLD
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
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.
#define ENOMEM
Out of memory.
#define errno
Error number variable.
#define BITMAP_CREATE_ZERO(name, bits)
Define and create a zero-initialized bitmap and its buffer.
static uint64_t bitmap_find_first_set(bitmap_t *map, uint64_t startIdx, uint64_t endIdx)
Find the first set bit in the bitmap.
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.
fd_t open(const char *path)
System call for opening files.
static void list_remove(list_entry_t *entry)
Removes a list entry from its current list.
static list_entry_t * list_last(list_t *list)
Gets the last entry in the list without removing it.
static void list_push_back(list_t *list, list_entry_t *entry)
Pushes an entry to the end of the list.
#define LIST_CREATE(name)
Creates a list initializer.
static void list_append(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 list_entry_t * list_pop_front(list_t *list)
Pops the first entry from the list.
static void list_init(list_t *list)
Initializes a list.
#define ROUND_UP(number, multiple)
void * mmap(fd_t fd, void *address, size_t length, prot_t prot)
System call to map memory from a file.
#define BYTES_TO_PAGES(amount)
Convert a size in bytes to pages.
void * munmap(void *address, size_t length)
System call to unmap mapped memory.
@ PROT_READ
Readable memory.
@ PROT_WRITE
Writable memory.
#define NULL
Pointer error value.
#define ERR
Integer error value.
#define PAGE_SIZE
The size of a memory page in bytes.
#define CONTAINER_OF(ptr, type, member)
Container of macro.
__UINT64_TYPE__ fd_t
File descriptor type.
#define CONTAINER_OF_SAFE(ptr, type, member)
Safe container of macro.
static list_t freeLists[_HEAP_NUM_BINS]
double remainder(double x, double y)
__UINTPTR_TYPE__ uintptr_t
_PUBLIC void * memset(void *s, int c, size_t n)
A simple ticket lock implementation.
_PUBLIC int mtx_init(mtx_t *mtx, int type)