MushOS  0.1
A UNIX-like OS prototype, written from scratch
Loading...
Searching...
No Matches
lib/base/heap.c File Reference

This is a standard implementation of heap for MushLib. More...

#include "heap.h"
#include "memory.h"
#include "exceptions.h"
Include dependency graph for lib/base/heap.c:

Go to the source code of this file.

Data Structures

struct  heap_header
 
struct  heap_block_header
 

Functions

boolean is_in_heap (void *structure)
 
static heap_block_headerget_header (void *structure)
 
u_dword size (void *structure)
 
precise occupation ()
 
static void * allocate_space (void *free_pointer, u_dword size, heap_block_header *previous, heap_block_header *next)
 
static void handle_allocation_exception ()
 
void initialize_heap (void *start_address, u_dword initial_size)
 
void * ralloc (u_dword size)
 
void * zalloc (u_dword size)
 
void * challoc (void *structure, u_dword new_size)
 
void unalloc (void *structure)
 

Variables

heap_headerheader
 

Detailed Description

This is a standard implementation of heap for MushLib.

The heap doesn't use paging, so it has a limited size. This allows kernel to use it right after launch, but there's also a significant drawback: when kernel heap gets overflown, no standard exception can be shown (because it requires heap space allocation). The drawback is handled with kernel allocation exception handler . Any non-kernel apps may set up their own kernel exception handler (using allocation_exception_id) or just do nothing - default allocation exception handler will allow kernel to terminate safely any app with overflown heap.

Definition in file lib/base/heap.c.

Function Documentation

◆ is_in_heap()

boolean is_in_heap ( void *  structure)

Function for determining whether the structure is inside the heap or outside.

Parameters
structurea pointer to the structure.
Returns
true is structure is in heap, false otherwise.

Definition at line 49 of file lib/base/heap.c.

References header.

Referenced by get_header().

◆ get_header()

static heap_block_header * get_header ( void *  structure)
static

Internal function for getting heap header of any structure located in heap. It just substracts heap block header size from pointer and casts it to heap block header. Throws heap exception if the given pointer is outside of the current heap.

Parameters
structurea pointer to the structure.
Returns
heap_block_header pointer.

Definition at line 60 of file lib/base/heap.c.

References heap_exception_id, heap_exception_type, and is_in_heap().

Referenced by allocate_space(), challoc(), occupation(), ralloc(), size(), and unalloc().

◆ size()

u_dword size ( void *  structure)

Function for structure located in heap size calculation (in bytes). Throws heap exception if the given pointer is outside of the current heap.

Parameters
structurea pointer to the structure.
Returns
structure size (in bytes).

Definition at line 65 of file lib/base/heap.c.

References get_header().

Referenced by allocate_space(), challoc(), ralloc(), and zalloc().

◆ occupation()

precise occupation ( )

Function for heap occupation calculation.

Returns
percent of allocated space in this heap.

Definition at line 69 of file lib/base/heap.c.

References get_header(), and header.

◆ allocate_space()

static void * allocate_space ( void *  free_pointer,
u_dword  size,
heap_block_header previous,
heap_block_header next 
)
static

Internal function for space allocation in heap. Sets up and fills heap block header, optionally alters headers of the next and previous heap blocks.

Parameters
free_pointerpointer to free space (heap block header size + required size).
sizerequired size of heap block.
previousprevious heap block.
nextnext heap block.
Returns
pointer to the start of the block.

Definition at line 95 of file lib/base/heap.c.

References get_header(), and size().

Referenced by ralloc().

◆ handle_allocation_exception()

static void handle_allocation_exception ( )
static

Allocation exception can't be handled regularily: handling requires heap. So, heap exception terminate app without any on-screen explanation.

Definition at line 111 of file lib/base/heap.c.

References heap_exception_id.

Referenced by initialize_heap().

◆ initialize_heap()

void initialize_heap ( void *  start_address,
u_dword  initial_size 
)

Function for heap initialization, sets up singleton heap header. This implementation of heap has fixed size, and so doesn't rely on paging. That's enough for kernel and basic app needs, TODO: provide another heap implementation for extended MushLib.

Parameters
start_addresspointer to the start of the heap.
initial_sizeinitial size of the heap.

Definition at line 116 of file lib/base/heap.c.

References allocation_exception_id, handle_allocation_exception(), and header.

Referenced by _start().

◆ ralloc()

void * ralloc ( u_dword  size)

Function for allocation of a raw heap block. Searches for a place of suitable size to fit a structure of requested size in. Throws allocation exception if no free space is available.

Parameters
sizerequested block size.
Returns
pointer to the neewly allocated block.

Definition at line 125 of file lib/base/heap.c.

References allocate_space(), allocation_exception_id, allocation_exception_type, get_header(), header, and size().

Referenced by challoc(), and zalloc().

◆ zalloc()

void * zalloc ( u_dword  size)

Function for allocation of a heap block and clearing it. Does the same as ralloc, but sets every allocated byte to zero. Throws allocation exception if no free space is available.

Parameters
sizerequested block size.
Returns
pointer to the neewly allocated block.

Definition at line 171 of file lib/base/heap.c.

References ralloc(), and size().

◆ challoc()

void * challoc ( void *  structure,
u_dword  new_size 
)

Function for heap block size alteration. Attempts to alter size in-place and copys the block only if in-place growth is not available. Throws heap exception if the structure is located outside of the heap.

Parameters
structurepointer to the structure for allocation size changing.
new_sizenew requested size of the structure.
Returns
pointer to the new structure.

Definition at line 177 of file lib/base/heap.c.

References get_header(), ralloc(), size(), and unalloc().

◆ unalloc()

void unalloc ( void *  structure)

Function for heap block unallocation. Throws heap exception if the structure is located outside of the heap.

Parameters
structurepointer to the structure for unallocation.

Definition at line 193 of file lib/base/heap.c.

References get_header(), and header.

Referenced by challoc().

Variable Documentation

◆ header

heap_header* header

Header of the current app heap. Every app in MushOS has its own heap. This implementation of heap is a singleton.

Definition at line 46 of file lib/base/heap.c.

Referenced by initialize_heap(), is_in_heap(), occupation(), ralloc(), and unalloc().