MushOS
0.1
A UNIX-like OS prototype, written from scratch
|
This is a standard MushLib heap header. See standard implementation in lib/base/heap.c. More...
#include "generic.h"
Go to the source code of this file.
Macros | |
#define | allocation_exception_id 0x1 |
#define | allocation_exception_type "Allocation Exception" |
#define | heap_exception_id 0x2 |
#define | heap_exception_type "Heap Exception" |
#define | size_of(structure) (size(structure) / sizeof(typeof(*structure))) |
Functions | |
precise | occupation () |
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) |
boolean | is_in_heap (void *structure) |
u_dword | size (void *structure) |
This is a standard MushLib heap header. See standard implementation in lib/base/heap.c.
This heap solves a strange C stdlib issue - each pointer in heap has a header that contains allocated data size. One can determine allocated space (in pieces, e.g. array length) using size_of macro.
It's also possible to determine whether a structure belongs to heap or not (using is_in_heap function).
Definition in file heap.h.
#define allocation_exception_id 0x1 |
#define allocation_exception_type "Allocation Exception" |
#define heap_exception_id 0x2 |
#define heap_exception_type "Heap Exception" |
#define size_of | ( | structure | ) | (size(structure) / sizeof(typeof(*structure))) |
Macro for for structure located in heap size calculation (in units). For example, can be used to calculate array of structures length. Throws heap exception if the given pointer is outside of the current heap.
structure | a pointer to the structure. |
precise occupation | ( | ) |
Function for heap occupation calculation.
Definition at line 69 of file lib/base/heap.c.
References get_header(), and header.
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.
start_address | pointer to the start of the heap. |
initial_size | initial 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().
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.
size | requested block size. |
Definition at line 125 of file lib/base/heap.c.
References allocate_space(), allocation_exception_id, allocation_exception_type, get_header(), header, and size().
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.
size | requested block size. |
Definition at line 171 of file lib/base/heap.c.
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.
structure | pointer to the structure for allocation size changing. |
new_size | new requested size of the structure. |
Definition at line 177 of file lib/base/heap.c.
References get_header(), ralloc(), size(), and unalloc().
void unalloc | ( | void * | structure | ) |
Function for heap block unallocation. Throws heap exception if the structure is located outside of the heap.
structure | pointer to the structure for unallocation. |
Definition at line 193 of file lib/base/heap.c.
References get_header(), and header.
Referenced by challoc().
boolean is_in_heap | ( | void * | structure | ) |
Function for determining whether the structure is inside the heap or outside.
structure | a pointer to the structure. |
Definition at line 49 of file lib/base/heap.c.
References header.
Referenced by get_header().
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.
structure | a pointer to the structure. |
Definition at line 65 of file lib/base/heap.c.
References get_header().
Referenced by allocate_space(), challoc(), ralloc(), and zalloc().