17 #define ALLOC_PAGES 16 20 #define FLAG_ALLOCATED (1 << 0) 24 #define round16(n, r) (((((n) - (r) + 31) >> 4) << 4) + (r) - 16) 29 #define total_bytes(b) \ 30 ((b)->size + sizeof(block_header_t) + sizeof(block_footer_t)) 42 typedef struct block_header
48 typedef struct block_footer
53 typedef struct fblock_header
55 struct block_header block;
71 uint64_t block_size = heap->pages *
PAGE_SIZE -
77 fh->
block.size = block_size;
84 footer->
size = block_size;
92 page_free(heap->pt, heap->vaddr, heap->pages);
175 if (heap->pages + pages > heap->maxpages) {
176 pages = heap->maxpages - heap->pages;
185 heap->pages +=
pages;
204 heap->first_fblock = fh;
229 if (fh->
block.size >= size)
252 uint64_t fsize = fh->
block.size;
272 heap->first_fblock = next;
309 heap->first_fblock = fh;
344 else if (fhp && !fhn) {
353 else if (fhn && !fhp) {
360 heap->first_fblock = fh;
388 heap->first_fblock = fh;
String and memory operations.
static fblock_header_t * grow_heap(heap_t *heap, uint64_t minsize)
Grow the heap so that it's big enough to hold at least minsize newly allocated bytes (not including h...
static fblock_header_t * find_fblock(heap_t *heap, uint64_t size)
void heap_free(heap_t *heap, void *ptr)
Free memory previously allocated with heap_alloc.
void * page_alloc(pagetable_t *pt, void *vaddr_in, int count)
Allocate one or more pages contiguous in virtual memory.
struct fblock_header * prev_fblock
A simple heap memory manager.
static fblock_header_t * prev_fblock_adj(heap_t *heap, block_header_t *bh)
Check the preceding block adjacent to 'b'.
struct block_header block
static fblock_header_t * prev_fblock(heap_t *heap, block_header_t *bh)
Scan all blocks preceding 'b' until a free block is encountered and return it.
struct fblock_header * next_fblock
struct fblock_header * first_fblock
void * heap_alloc(heap_t *heap, uint64_t size)
Allocate memory from a heap.
heap_t * heap_create(pagetable_t *pt, void *vaddr, uint64_t maxpages)
Create a new heap from which to allocate virtual memory.
static fblock_header_t * next_fblock_adj(heap_t *heap, block_header_t *bh)
Check the next block adjacent to 'b'.
void heap_destroy(heap_t *heap)
Destroy a heap, returning its memory to page table.
static fblock_header_t * next_fblock(heap_t *heap, block_header_t *bh)
Scan all blocks after 'b' until a free block is encountered and return it.
void page_free(pagetable_t *pt, void *vaddr_in, int count)
Free one or more contiguous pages from virtual memory.