Skip to content

Commit ca8b2e2

Browse files
coderabbitai[bot]Matt-DESTROYER
authored andcommitted
📝 Add docstrings to kernel
Docstrings generation was requested by @Matt-DESTROYER. * #32 (comment) The following files were modified: * `kernel/src/drivers/allocator.c`
1 parent d2e3078 commit ca8b2e2

File tree

1 file changed

+56
-22
lines changed

1 file changed

+56
-22
lines changed

kernel/src/drivers/allocator.c

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,27 @@ static void* _get_buffer_start(MemoryHeader_t* header) {
3535
return (void*)(header + 1);
3636
}
3737

38+
/**
39+
* Compute the address immediately after the last block's usable data in the managed heap.
40+
*
41+
* @returns The byte address (as `uintptr_t`) one past the last block's data region; `0` if the allocator is uninitialized (`_heap_last` is `NULL`).
42+
*/
3843
static uintptr_t _current_heap_end() {
3944
if (_heap_last == NULL) return 0;
4045

4146
return (uintptr_t)_heap_last + sizeof(MemoryHeader_t) + _heap_last->size;
4247
}
4348

49+
/**
50+
* Merge consecutive free blocks after `header` into `header`, enlarging its usable size.
51+
*
52+
* If the merged region reaches the end of the managed heap, `header->next` is set to NULL
53+
* and the global `_heap_last` is updated to point to `header`. If a heap corruption is
54+
* detected (invalid pointer ordering), a diagnostic is emitted and the process aborts in
55+
* debug builds.
56+
*
57+
* @param header Header whose following free blocks should be absorbed; no action is taken if `header` is NULL or its immediate successor is not free.
58+
*/
4459
static void _extend_block(MemoryHeader_t* header) {
4560
if (header == NULL) return;
4661
if (header->next == NULL || !_is_free(header->next)) return;
@@ -81,6 +96,15 @@ static void _extend_block(MemoryHeader_t* header) {
8196
return;
8297
}
8398

99+
/**
100+
* Ensure a free block is coalesced with adjacent free blocks and that the heap end sentinel is correct.
101+
*
102+
* If `header` refers to a free block, this will merge it with any following free blocks and update
103+
* the allocator's end-of-heap marker when `header` becomes the last block. If `header` is NULL or
104+
* not free, the function does nothing.
105+
*
106+
* @param header Pointer to the block header to defragment (must belong to this allocator).
107+
*/
84108
static void _defragment_block(MemoryHeader_t* header) {
85109
if (header == NULL || !_is_free(header)) return;
86110
if (header->next == NULL) {
@@ -90,6 +114,14 @@ static void _defragment_block(MemoryHeader_t* header) {
90114
_extend_block(header);
91115
}
92116

117+
/**
118+
* Split a free memory block into a leading block of the requested size and a trailing free fragment when there is sufficient leftover space.
119+
*
120+
* If the header's size minus `size` is smaller than the space required for a new MemoryHeader_t plus MINIMUM_BLOCK_SIZE, no action is taken.
121+
*
122+
* @param header Pointer to the free block's header to be potentially split.
123+
* @param size Requested size (in bytes) for the leading block; remaining bytes (if large enough) form a new free fragment.
124+
*/
93125
static void _fragment_block(MemoryHeader_t* header, uintptr_t size) {
94126
if (size > header->size) return;
95127
uintptr_t remaining_space = header->size - size;
@@ -157,11 +189,10 @@ void alloc_free() {
157189
/**
158190
* Allocate a contiguous block of memory from the custom heap.
159191
*
160-
* Allocates a block of at least `bytes` bytes from the allocator's managed
161-
* heap and returns a pointer to the start of the usable memory region.
162-
* @param bytes Number of bytes requested.
163-
* @return Pointer to the allocated memory region, or NULL if the heap is
164-
* uninitialized or no suitable block is available.
192+
* Requests a block of at least `bytes` bytes from the allocator and returns
193+
* a pointer to the start of the usable data region.
194+
* @param bytes Number of bytes requested; value is rounded up to the allocator's alignment (ALIGN). A request of 0 returns NULL.
195+
* @return Pointer to the start of the allocated usable memory, or NULL if the heap is uninitialized, `bytes` is zero, or no suitable block is available.
165196
*/
166197
void* malloc(uintptr_t bytes) {
167198
if (_heap_start == NULL || bytes == 0) return NULL;
@@ -251,14 +282,12 @@ void* malloc(uintptr_t bytes) {
251282
}
252283

253284
/**
254-
* Resize an allocated memory block to a new size, preserving existing data up
255-
* to the smaller of the old and new sizes.
256-
* @param ptr Pointer to a previously allocated memory block (must be non-NULL
257-
* and returned by this allocator).
258-
* @param new_size New size in bytes for the allocation.
259-
* @returns Pointer to the newly allocated memory containing the copied data,
260-
* or `NULL` if allocation failed.
261-
*/
285+
* Resize an allocated memory block to hold at least `new_size` bytes, preserving existing data up to the smaller of the old and new sizes.
286+
*
287+
* If `ptr` is `NULL`, the call is equivalent to `malloc(new_size)`. If `new_size` is 0, the allocation is freed and `NULL` is returned. The requested size is rounded up to the allocator's alignment before allocation.
288+
* @param ptr Pointer to a previously allocated memory block returned by this allocator, or `NULL`.
289+
* @param new_size Desired size in bytes for the allocation.
290+
* @returns Pointer to a memory region containing the original data (possibly relocated), or `NULL` if allocation failed or `new_size` was 0.
262291
void* realloc(void* ptr, uintptr_t new_size) {
263292
if (ptr == NULL) return malloc(new_size);
264293
if (new_size == 0) {
@@ -323,11 +352,17 @@ void* calloc(uintptr_t num, uintptr_t size) {
323352
}
324353

325354
/**
326-
* Mark the memory block referenced by `ptr` as free.
355+
* Release a previously allocated memory block back to the allocator.
356+
*
357+
* If the allocator is uninitialized or `ptr` is `NULL`, the call is a no-op.
358+
* If the block has already been freed, a double-free is detected: in debug
359+
* builds this prints an error and aborts; otherwise the call returns without
360+
* modifying state. Otherwise the block's free counter is incremented (capped
361+
* at `UINT8_MAX`) and the allocator attempts to coalesce adjacent free blocks.
327362
*
328363
* @param ptr Pointer to a data region previously returned by `malloc`,
329-
* `calloc`, or `realloc`. Behavior is undefined if `ptr` is `NULL` or was not
330-
* allocated by this allocator.
364+
* `calloc`, or `realloc`. Behavior is undefined if `ptr` was not
365+
* allocated by this allocator.
331366
*/
332367
void free(void* ptr) {
333368
if (_heap_start == NULL || ptr == NULL) return;
@@ -349,12 +384,11 @@ void free(void* ptr) {
349384
}
350385

351386
/**
352-
* Coalesces adjacent free blocks in the allocator's heap to reduce
353-
* fragmentation.
387+
* Coalesces adjacent free blocks in the allocator's heap to reduce fragmentation.
354388
*
355-
* Traverses the allocator's header list starting at _heap_first and merges
356-
* consecutive blocks that are not in use by increasing the earlier block's
357-
* size and updating next pointers; modifies the allocator's heap metadata.
389+
* Iterates headers from the first managed block and merges consecutive freed
390+
* blocks by increasing the earlier block's size and updating next pointers,
391+
* keeping the allocator's heap metadata consistent.
358392
*/
359393
static void _defragment_all() {
360394
if (_heap_first == NULL) return;
@@ -375,4 +409,4 @@ void* memcpy(void* restrict dest, const void* restrict src, uintptr_t n) {
375409
}
376410

377411
return dest;
378-
}
412+
}

0 commit comments

Comments
 (0)