This repository has been archived on 2025-11-10. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
avm/lib/heap.h
Aryadev Chavali a60108ae1d Made heap a vector of pages
Instead of using a linked list, which is incredibly fragmented, a
vector keeps all pointers together.  Keeps all our stuff together and
in theory we should have less cache misses when deleting pages.

It does introduce the issue of fragmenting, where if we allocate and
then delete many times a lot of the heap vector will be empty so
traversal will be over a ton of useless stuff.
2024-06-17 23:11:20 +01:00

124 lines
2.9 KiB
C

/* Copyright (C) 2023 Aryadev Chavali
* You may distribute and modify this code under the terms of the
* GPLv2 license. You should have received a copy of the GPLv2
* license with this file. If not, please write to:
* aryadev@aryadevchavali.com.
* Created: 2023-11-01
* Author: Aryadev Chavali
* Description: Arena allocator
*/
#ifndef HEAP_H
#define HEAP_H
#include "./base.h"
#include "./darr.h"
#include <stdbool.h>
#define PAGE_DEFAULT_SIZE 256
/**
@brief Some fixed portion of bytes allocated on the heap.
@details A fixed allocation of bytes. Cannot be resized nor can it
be stack allocated (the usual way) due to flexible array attached.
@prop[next] Next page in the linked list
@prop[available] Available number of bytes in page
@prop[data] Buffer of bytes attached to page
*/
typedef struct Page
{
size_t available;
byte_t data[];
} page_t;
/**
@brief Allocate a new page on the heap with the given properties.
@details Allocates a new page using malloc with the given size and
pointer to next page. NOTE: all memory is 0 initialised by
default.
@param[max] Maximum available memory in page
*/
page_t *page_create(size_t max);
/**
@brief Delete a page, freeing its memory
@details Free's the memory associated with the page via free().
NOTE: any pointers to the page's memory are considered invalid once
this is called.
@param[page] Page to delete
*/
void page_delete(page_t *page);
/**
@brief A collection of pages through which generic allocations can
occur.
@details Collection of pages maintained through a vector of
pointers to pages.
@prop[page_vec] Vector of pages
*/
typedef struct
{
darr_t page_vec;
} heap_t;
/**
@brief Instantiate a new heap structure
@details Initialises the heap structure given. No heap allocation
occurs here until a new page is created, so this may be called
safely.
@param[heap] Pointer to heap to initialise
*/
void heap_create(heap_t *heap);
/**
@brief Allocate a new page on the heap
@details Creates and joins a new page onto the linked list
maintained by the heap. heap.end is set to this new page.
@param[heap] Heap to create a new page on
@param[size] Size of page to allocate
@return The newly allocated page
*/
page_t *heap_allocate(heap_t *heap, size_t size);
/**
@brief Free a page of memory from the heap
@details The page given is removed from the linked list of pages
then freed from the heap via page_delete(). If the page does not
belong to this heap (O(heap.pages) time) then false is returned,
otherwise true.
@param[heap] Heap to free page from
@param[page] Page to delete
@return Success of deletion
*/
bool heap_free(heap_t *heap, page_t *page);
/**
@brief Stop the heap, freeing all associated memory
@details Traverses the linked list of pages, deleting each one.
@param[heap] Heap to stop
*/
void heap_stop(heap_t *);
#endif