A simple heap memory manager. More...
#include <core.h>
#include <libc/string.h>
#include <kernel/mem/heap.h>
#include <kernel/mem/paging.h>
Go to the source code of this file.
Data Structures | |
struct | heap_t |
struct | block_header_t |
struct | block_footer_t |
struct | fblock_header_t |
Macros | |
#define | ALLOC_PAGES 16 |
#define | FLAG_ALLOCATED (1 << 0) |
#define | round16(n, r) (((((n) - (r) + 31) >> 4) << 4) + (r) - 16) |
#define | total_bytes(b) ((b)->size + sizeof(block_header_t) + sizeof(block_footer_t)) |
Functions | |
heap_t * | heap_create (pagetable_t *pt, void *vaddr, uint64_t maxpages) |
Create a new heap from which to allocate virtual memory. More... | |
void | heap_destroy (heap_t *heap) |
Destroy a heap, returning its memory to page table. More... | |
static fblock_header_t * | next_fblock_adj (heap_t *heap, block_header_t *bh) |
Check the next block adjacent to 'b'. More... | |
static fblock_header_t * | prev_fblock_adj (heap_t *heap, block_header_t *bh) |
Check the preceding block adjacent to 'b'. More... | |
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. More... | |
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. More... | |
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 headers). More... | |
static fblock_header_t * | find_fblock (heap_t *heap, uint64_t size) |
void * | heap_alloc (heap_t *heap, uint64_t size) |
Allocate memory from a heap. More... | |
void | heap_free (heap_t *heap, void *ptr) |
Free memory previously allocated with heap_alloc. More... | |
A simple heap memory manager.
Definition in file heap.c.
struct heap |
Data Fields | ||
---|---|---|
pagetable_t * | pt | |
void * | vaddr | |
uint64_t | pages | |
uint64_t | maxpages | |
struct fblock_header * | first_fblock | |
uint64_t | reserved |
struct block_header_t |
struct fblock_header_t |
#define ALLOC_PAGES 16 |
Definition at line 17 of file heap.c.
Referenced by grow_heap(), and heap_create().
#define FLAG_ALLOCATED (1 << 0) |
Definition at line 20 of file heap.c.
Referenced by grow_heap(), heap_alloc(), next_fblock(), next_fblock_adj(), prev_fblock(), and prev_fblock_adj().
#define round16 | ( | n, | |
r | |||
) | (((((n) - (r) + 31) >> 4) << 4) + (r) - 16) |
Definition at line 24 of file heap.c.
Referenced by heap_alloc().
#define total_bytes | ( | b | ) | ((b)->size + sizeof(block_header_t) + sizeof(block_footer_t)) |
Definition at line 29 of file heap.c.
Referenced by heap_free(), next_fblock(), next_fblock_adj(), prev_fblock(), and prev_fblock_adj().
heap_t* heap_create | ( | pagetable_t * | pt, |
void * | vaddr, | ||
uint64_t | maxpages | ||
) |
Create a new heap from which to allocate virtual memory.
[in] | pt | The page table from which virtual memory is to be allocated. |
[in] | vaddr | The virtual address of the first byte to use for the heap. |
[in] | maxpages | The maximum number of pages that the heap will grow to fill. |
Definition at line 61 of file heap.c.
Referenced by cmd_test_heap().
void heap_destroy | ( | heap_t * | heap | ) |
Destroy a heap, returning its memory to page table.
[in] | heap | The heap to be destroyed. |
Definition at line 90 of file heap.c.
Referenced by cmd_test_heap().
|
static |
Check the next block adjacent to 'b'.
If it's free, return a pointer to it.
Definition at line 99 of file heap.c.
Referenced by heap_free().
|
static |
Check the preceding block adjacent to 'b'.
If it's free, return a pointer to it.
Definition at line 114 of file heap.c.
Referenced by heap_free().
|
static |
Scan all blocks after 'b' until a free block is encountered and return it.
If no free blocks are found, return NULL.
Definition at line 131 of file heap.c.
Referenced by heap_free().
|
static |
Scan all blocks preceding 'b' until a free block is encountered and return it.
If no free blocks are found, return NULL.
Definition at line 148 of file heap.c.
Referenced by grow_heap(), and heap_free().
|
static |
Grow the heap so that it's big enough to hold at least minsize newly allocated bytes (not including headers).
Return a pointer to the first new free block if successful. Otherwise return NULL.
Definition at line 168 of file heap.c.
Referenced by find_fblock().
|
static |
Definition at line 225 of file heap.c.
Referenced by heap_alloc().
void* heap_alloc | ( | heap_t * | heap, |
uint64_t | size | ||
) |
Allocate memory from a heap.
[in] | heap | The heap from which to allocate the memory. |
[in] | size | The size, in bytes, of the allocation. |
Definition at line 239 of file heap.c.
Referenced by cmd_test_heap().
void heap_free | ( | heap_t * | heap, |
void * | ptr | ||
) |
Free memory previously allocated with heap_alloc.
[in] | heap | The heap from which the memory was allocated. |
[in] | ptr | A pointer to the memory that was allocated. |
Definition at line 319 of file heap.c.
Referenced by cmd_test_heap().