Skip to content

Commit 40e146b

Browse files
authored
Memory Allocation + Deallocation implementation (#19)
* update README * [FIX] fix param name in gettime * [UPDATE] updated printf param to const * [ADD] kernel halt and panic for unexpected behaviors * refactor + added discard commands + reserve spaces for page table * [REFACTOR] consolidate irq files to interrupt file * [refactor] updated the way we handled irq testing * update function documentation * [ADD] custom division operation * added more macros + moved the irq handlers from irq file to interrupt file * added mapping file for linker instruction address mapping * fix documentation mismatch + add .data init * move the attribute noreturn order * updated panic to use more modern C standard (C23) * Updated stack start and end + added proper heap start and end * enforce std=C23 * update mapping with no debu mode * updated doc with updated stack name * Updated header documentation for Doxygen auto generated docs * Updated implementation documentation for Doxygen auto generated documentation. * Added config file for Doxygen. * remove MAN generated documentation from Doxygen config * Added Doxygen documentation generator to makefile * added README as main page * update formatting * init implementation of kernel malloc * added init of kernel malloc to main kernel * added docs to gitignore * refactor codebase structure to move kernel/ and user/ directories to src/ directory * Switch ifndef to pragam once * added easy open documentation * updated variables * Added testing for memory * Refactor printf file by simplifiying printf parameters handling * Added documentation for doxygen + FSM for printf function * Added testing for printf * mapp update * remove map_file.map do VC * Added debugging make command to makefile * Added todo for printf file to refactor the file * added more testing for malloc on kernel.c * added structure definition for linked list kmalloc * Move from linear kmalloc to dynamic kmalloc using linked list. Also added kfree first implementation with some testing * Added doxygen docs deployement badge to readme * refatored kmalloc to separate the split block + added doxygem * added todo comment for memory file testing section
1 parent 543f847 commit 40e146b

File tree

7 files changed

+448
-29
lines changed

7 files changed

+448
-29
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/build
22
.DS_Store
3-
/docs
3+
/docs
4+
map_file.map

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,7 @@ docs:
6868
doc:
6969
open "docs/html/index.html"
7070

71+
debug:
72+
make KFLAGS="-DUSE_KTESTS"
73+
7174
.PHONY: all clean qemu docker docs

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# AstraKernel ~ A minimal ARM kernel for QEMU
22

33
[![GitHub release (including pre-releases)](https://img.shields.io/github/v/release/sandbox-science/AstraKernel?include_prereleases)](https://github.com/sandbox-science/AstraKernel/releases)
4+
[![Doxygen Docs](https://github.com/sandbox-science/AstraKernel/actions/workflows/static.yml/badge.svg?branch=main)](https://github.com/sandbox-science/AstraKernel/actions/workflows/static.yml)
45

56
AstraKernel is a minimal experimental kernel written in modern C and ARM assembly, designed to run on
67
**QEMU’s Versatile AB/PB board with a Cortex‑A8 CPU override (-cpu cortex-a8)**. This setup keeps the

include/memory.h

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,37 @@
77
extern "C"
88
{
99
#endif
10+
/**
11+
* @enum block_state
12+
* @brief Enumeration of block states in the memory allocator.
13+
*
14+
* This enum defines the possible states of a memory block in the
15+
* kernel memory allocator.
16+
*/
17+
enum block_state {
18+
BLOCK_FREE = 0, /**< Block is free and available for allocation. */
19+
BLOCK_USED /**< Block is currently allocated and in use. */
20+
};
1021

11-
void kmalloc_init(void *start, void *limit);
12-
void* kmalloc(size_t size);
13-
size_t kmalloc_remaining(void);
22+
/**
23+
* @brief Header structure for memory blocks in the allocator.
24+
*
25+
* Each allocated or free block in the kernel memory allocator is
26+
* preceded by a header that contains metadata about the block.
27+
*/
28+
struct header {
29+
size_t size; /**< Size of the memory block (excluding header). */
30+
enum block_state state; /**< State of the block (free or used). */
31+
struct header *next; /**< Pointer to the next block in the linked list. */
32+
struct header *prev; /**< Pointer to the previous block in the linked list. */
33+
};
1434

35+
struct header *kmalloc_get_head(void);
36+
37+
void kmalloc_init(void *start, void *limit);
38+
void* kmalloc(size_t size);
39+
void kfree(void *block);
40+
int kmalloc_test(void);
1541
#ifdef __cplusplus
1642
}
1743
#endif

src/kernel/kernel.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ void not_main(void)
7171
void *p2 = kmalloc(48);
7272
printf("kmalloc(10) addr: %p\n", p);
7373
printf("kmalloc(48) addr: %p\n", p2);
74+
struct header *h = (struct header *)p - 1;
75+
struct header *h2 = (struct header *)p2 - 1;
76+
printf("size of kmalloc(10) %d\nsize of kmalloc(48) %d\n", h->size, h2->size);
77+
kfree(p);
78+
kfree(p2);
7479

7580
char *buf = kmalloc(1);
7681
if (buf)
@@ -114,20 +119,21 @@ void not_main(void)
114119
#define TIMER_TICK_TEST not_main()
115120
#define SANITY_CHECK irq_sanity_check()
116121
#define CALL_SVC_0 __asm__ volatile ("svc #0")
122+
#define KMALLOC_TEST kmalloc_test()
117123

118124
// Entry point for the kernel
119125
void kernel_main(void)
120126
{
121127
clear();
122128
kmalloc_init(&__heap_start__, &__heap_end__);
123-
kmalloc_remaining();
124129

125130
/* TESTS */
126-
#ifdef USE_KTESTS
127-
SANITY_CHECK;
128-
CALL_SVC_0;
129-
TIMER_TICK_TEST;
130-
#endif
131+
#ifdef USE_KTESTS
132+
SANITY_CHECK;
133+
CALL_SVC_0;
134+
KMALLOC_TEST;
135+
TIMER_TICK_TEST;
136+
#endif
131137

132138
/* Back to normal operations */
133139
init_message();

0 commit comments

Comments
 (0)