MonkOS  v0.1
A simple 64-bit operating system (x86_64)
heap.c File Reference

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  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_tnext_fblock_adj (heap_t *heap, block_header_t *bh)
 Check the next block adjacent to 'b'. More...
 
static fblock_header_tprev_fblock_adj (heap_t *heap, block_header_t *bh)
 Check the preceding block adjacent to 'b'. More...
 
static fblock_header_tnext_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_tprev_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_tgrow_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_tfind_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...
 

Detailed Description

A simple heap memory manager.

Definition in file heap.c.


Data Structure Documentation

struct heap

Definition at line 32 of file heap.c.

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

Definition at line 42 of file heap.c.

Data Fields
uint64_t size
uint64_t flags
struct block_footer_t

Definition at line 48 of file heap.c.

Data Fields
uint64_t size
struct fblock_header_t

Definition at line 53 of file heap.c.

Data Fields
struct block_header block
struct fblock_header * next_fblock
struct fblock_header * prev_fblock

Macro Definition Documentation

#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,
 
)    (((((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().

Function Documentation

heap_t* heap_create ( pagetable_t pt,
void *  vaddr,
uint64_t  maxpages 
)

Create a new heap from which to allocate virtual memory.

Parameters
[in]ptThe page table from which virtual memory is to be allocated.
[in]vaddrThe virtual address of the first byte to use for the heap.
[in]maxpagesThe maximum number of pages that the heap will grow to fill.
Returns
A pointer to a the created heap structure.

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.

Parameters
[in]heapThe heap to be destroyed.

Definition at line 90 of file heap.c.

Referenced by cmd_test_heap().

static fblock_header_t* next_fblock_adj ( heap_t *  heap,
block_header_t bh 
)
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 fblock_header_t* prev_fblock_adj ( heap_t *  heap,
block_header_t bh 
)
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 fblock_header_t* next_fblock ( heap_t *  heap,
block_header_t bh 
)
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 fblock_header_t* prev_fblock ( heap_t *  heap,
block_header_t bh 
)
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 fblock_header_t* grow_heap ( heap_t *  heap,
uint64_t  minsize 
)
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 fblock_header_t* find_fblock ( heap_t *  heap,
uint64_t  size 
)
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.

Parameters
[in]heapThe heap from which to allocate the memory.
[in]sizeThe size, in bytes, of the allocation.
Returns
A pointer to a the allocated memory.

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.

Parameters
[in]heapThe heap from which the memory was allocated.
[in]ptrA pointer to the memory that was allocated.

Definition at line 319 of file heap.c.

Referenced by cmd_test_heap().