MushOS  0.1
A UNIX-like OS prototype, written from scratch
Loading...
Searching...
No Matches
heap.h
Go to the documentation of this file.
1/**
2 * @file
3 *
4 * @brief This is a standard MushLib heap header. See standard implementation in @ref lib/base/heap.c
5 *
6 * This heap solves a strange C stdlib issue - each pointer in heap has a @link heap_block_header header @endlink
7 * that contains allocated data size.
8 * One can determine allocated space (in pieces, e.g. array length) using @ref size_of macro.
9 *
10 * It's also possible to determine whether a structure belongs to heap or not (using @ref is_in_heap function).
11 */
12
13#ifndef MUSHLIB_HEAP_H
14#define MUSHLIB_HEAP_H
15
16#include "generic.h"
17
18
19/**
20 * Allocation exception ID.
21 * Indicates that a program asked for more space than heap can provide.
22 * Will be thrown by default allocation exception handler.
23 * Should be thrown if a custom program is terminated because of allocation exception.
24 */
25#define allocation_exception_id 0x1
26/**
27 * Allocation exception type.
28 * May be used by custom allocation exception handlers.
29 */
30#define allocation_exception_type "Allocation Exception"
31
32/**
33 * Heap exception ID.
34 * Indicates that there was an attempt to calculate size or free a structure that is located outside of the heap.
35 * Will be thrown by default heap exception handler.
36 * Should be thrown if a custom program is terminated because of heap exception.
37 */
38#define heap_exception_id 0x2
39/**
40 * Heap exception type.
41 * May be used by custom heap exception handlers.
42 */
43#define heap_exception_type "Heap Exception"
44
45
46/**
47 * Macro for for structure located in heap size calculation (in units).
48 * For example, can be used to calculate array of structures length.
49 * Throws heap exception if the given pointer is outside of the current heap.
50 * @param structure a pointer to the structure.
51 * @return structure size (in units).
52 */
53#define size_of(structure) (size(structure) / sizeof(typeof(*structure)))
54
55/**
56 * Function for heap occupation calculation.
57 * @return percent of allocated space in this heap.
58 */
59precise occupation();
60
61/**
62 * Function for heap initialization, sets up singleton heap header.
63 * This implementation of heap has fixed size, and so doesn't rely on paging.
64 * That's enough for kernel and basic app needs, TODO: provide another heap implementation for extended MushLib.
65 * @param start_address pointer to the start of the heap.
66 * @param initial_size initial size of the heap.
67 */
68void initialize_heap(void* start_address, u_dword initial_size);
69
70/**
71 * Function for allocation of a raw heap block.
72 * Searches for a place of suitable size to fit a structure of requested size in.
73 * Throws allocation exception if no free space is available.
74 * @param size requested block size.
75 * @return pointer to the neewly allocated block.
76 */
77void* ralloc(u_dword size);
78
79/**
80 * Function for allocation of a heap block and clearing it.
81 * Does the same as `ralloc`, but sets every allocated byte to zero.
82 * Throws allocation exception if no free space is available.
83 * @param size requested block size.
84 * @return pointer to the neewly allocated block.
85 */
86void* zalloc(u_dword size);
87
88/**
89 * Function for heap block size alteration.
90 * Attempts to alter size in-place and copys the block only if in-place growth is not available.
91 * Throws heap exception if the structure is located outside of the heap.
92 * @param structure pointer to the structure for allocation size changing.
93 * @param new_size new requested size of the structure.
94 * @return pointer to the new structure.
95 */
96void* challoc(void* structure, u_dword new_size);
97
98/**
99 * Function for heap block unallocation.
100 * Throws heap exception if the structure is located outside of the heap.
101 * @param structure pointer to the structure for unallocation.
102 */
103void unalloc(void* structure);
104
105/**
106 * Function for determining whether the structure is inside the heap or outside.
107 * @param structure a pointer to the structure.
108 * @return true is structure is in heap, false otherwise.
109 */
110boolean is_in_heap(void* structure);
111
112/**
113 * Function for structure located in heap size calculation (in bytes).
114 * Throws heap exception if the given pointer is outside of the current heap.
115 * @param structure a pointer to the structure.
116 * @return structure size (in bytes).
117 */
118u_dword size(void* structure);
119
120#endif // MUSHLIB_HEAP_H
void * ralloc(u_dword size)
void unalloc(void *structure)
precise occupation()
Definition: lib/base/heap.c:69
u_dword size(void *structure)
Definition: lib/base/heap.c:65
void * zalloc(u_dword size)
void * challoc(void *structure, u_dword new_size)
void initialize_heap(void *start_address, u_dword initial_size)
boolean is_in_heap(void *structure)
Definition: lib/base/heap.c:49