|
12 | 12 |
|
13 | 13 | #include <stddef.h> |
14 | 14 |
|
15 | | -/* Initialize the heap system */ |
16 | | -void init_heap(); |
17 | | -void print_heapinfo(); |
| 15 | +/** |
| 16 | + * @brief Initialise the kernel heap. |
| 17 | + * |
| 18 | + * Sets up internal data structures required for dynamic memory allocation. |
| 19 | + * Should be called early during kernel initialisation. |
| 20 | + */ |
| 21 | +void init_heap(void); |
| 22 | + |
| 23 | +/** |
| 24 | + * @brief Print diagnostic heap information to the terminal. |
| 25 | + * |
| 26 | + * Outputs allocator statistics such as used, free, and total heap space. |
| 27 | + * Primarily for debugging and diagnostics. |
| 28 | + */ |
| 29 | +void print_heapinfo(void); |
18 | 30 |
|
19 | | -/* Core alloc/free */ |
| 31 | +/** |
| 32 | + * @brief Allocate memory from the kernel heap. |
| 33 | + * |
| 34 | + * @param size Number of bytes to allocate. |
| 35 | + * @return Pointer to allocated memory, or NULL if allocation fails. |
| 36 | + */ |
20 | 37 | void* kmalloc(uint64_t size); |
| 38 | + |
| 39 | +/** |
| 40 | + * @brief Free previously allocated memory. |
| 41 | + * |
| 42 | + * @param addr Pointer previously returned by kmalloc/kcalloc/krealloc. |
| 43 | + */ |
21 | 44 | void kfree(const void* addr); |
22 | 45 |
|
23 | | -/* calloc/realloc helpers */ |
| 46 | +/** |
| 47 | + * @brief Allocate zero-initialised memory. |
| 48 | + * |
| 49 | + * @param num Number of elements. |
| 50 | + * @param size Size of each element in bytes. |
| 51 | + * @return Pointer to allocated memory, or NULL if allocation fails. |
| 52 | + */ |
24 | 53 | void* kcalloc(size_t num, size_t size); |
| 54 | + |
| 55 | +/** |
| 56 | + * @brief Resize previously allocated memory block. |
| 57 | + * |
| 58 | + * Preserves contents up to the lesser of old and new sizes. |
| 59 | + * |
| 60 | + * @param ptr Pointer to memory allocated by kmalloc/kcalloc/krealloc. |
| 61 | + * @param new_size New size in bytes. |
| 62 | + * @return Pointer to reallocated memory, or NULL if resizing fails. |
| 63 | + */ |
25 | 64 | void* krealloc(void* ptr, size_t new_size); |
26 | 65 |
|
27 | | -/* Low-memory special allocator: for <4GB identity mapped area */ |
| 66 | +/** |
| 67 | + * @brief Allocate memory from low (identity-mapped) memory under 4GB. |
| 68 | + * |
| 69 | + * Intended for DMA buffers or page tables that require low physical addresses. |
| 70 | + * |
| 71 | + * @param size Number of bytes to allocate. |
| 72 | + * @return Physical address of the allocated block (below 4GB). |
| 73 | + */ |
28 | 74 | uint32_t kmalloc_low(uint32_t size); |
29 | | -void kfree_low(uint32_t addr); /* New: explicitly free low memory if needed */ |
30 | 75 |
|
31 | | -/* Debug/statistics */ |
32 | | -uint64_t get_free_memory(); |
33 | | -uint64_t get_used_memory(); |
34 | | -uint64_t get_total_memory(); |
| 76 | +/** |
| 77 | + * @brief Free a low-memory allocation made with kmalloc_low(). |
| 78 | + * |
| 79 | + * Only needed if low memory is explicitly managed and reused. |
| 80 | + * |
| 81 | + * @param addr Physical address of the block to free. |
| 82 | + */ |
| 83 | +void kfree_low(uint32_t addr); |
| 84 | + |
| 85 | +/** |
| 86 | + * @brief Get total amount of free memory. |
| 87 | + * |
| 88 | + * @return Free heap space in bytes. |
| 89 | + */ |
| 90 | +uint64_t get_free_memory(void); |
| 91 | + |
| 92 | +/** |
| 93 | + * @brief Get total amount of used memory. |
| 94 | + * |
| 95 | + * @return Used heap space in bytes. |
| 96 | + */ |
| 97 | +uint64_t get_used_memory(void); |
| 98 | + |
| 99 | +/** |
| 100 | + * @brief Get total available memory managed by the kernel heap. |
| 101 | + * |
| 102 | + * @return Total heap size in bytes. |
| 103 | + */ |
| 104 | +uint64_t get_total_memory(void); |
| 105 | + |
| 106 | +/** |
| 107 | + * @brief Halt the system with a preboot failure message. |
| 108 | + * |
| 109 | + * Used when a critical failure occurs before full initialisation. |
| 110 | + * |
| 111 | + * @param msg Null-terminated string describing the failure. |
| 112 | + */ |
35 | 113 | void preboot_fail(const char* msg); |
36 | 114 |
|
| 115 | +/** |
| 116 | + * @brief Free memory and set the pointer to NULL. |
| 117 | + * |
| 118 | + * This macro safely frees a dynamically allocated pointer and sets it to NULL |
| 119 | + * to avoid dangling references. It must be passed the address of the pointer |
| 120 | + * (i.e., a pointer to the pointer). |
| 121 | + * |
| 122 | + * Example usage: |
| 123 | + * @code |
| 124 | + * void* ptr = kmalloc(128); |
| 125 | + * kfree_null(&ptr); // ptr is now NULL |
| 126 | + * @endcode |
| 127 | + * |
| 128 | + * @param p A pointer to the pointer to be freed. Must not be NULL. |
| 129 | + */ |
| 130 | +#define kfree_null(p) do { kfree(*(p)); *(p) = NULL; } while (0) |
| 131 | + |
0 commit comments