diff options
| author | Douglas B. Rumbaugh <doug@douglasrumbaugh.com> | 2025-11-02 16:08:52 -0500 |
|---|---|---|
| committer | Douglas B. Rumbaugh <doug@douglasrumbaugh.com> | 2025-11-02 16:08:52 -0500 |
| commit | 2347c038520bc3dc6f3990a6b088d4aa8fd17c7a (patch) | |
| tree | 59e894486861ac13197dd26fdbcfa4ac8a337876 | |
| parent | 5d8131a4240e4dab0e519bd3e29e3cabf5f09672 (diff) | |
| download | liballoc-2347c038520bc3dc6f3990a6b088d4aa8fd17c7a.tar.gz | |
free_list: partial split implementation
Implemented enough of split to get basic free
list reuse working. Also updated the allocate()
function to return the output of split when it is
called.
| -rw-r--r-- | src/alloc.c | 2 | ||||
| -rw-r--r-- | src/free_list.c | 20 |
2 files changed, 19 insertions, 3 deletions
diff --git a/src/alloc.c b/src/alloc.c index dd1c8aa..3f805db 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -53,7 +53,7 @@ void *allocate(size_t size) { /* first check for a suitable memory block on the free list */ free_nd *nd = fl_find_first_fit(free_list, size); if (nd) { - void *return_region = fl_split_node(&free_list, nd, size); + return fl_split_node(&free_list, nd, size); } /* diff --git a/src/free_list.c b/src/free_list.c index 31244fb..ee9e571 100644 --- a/src/free_list.c +++ b/src/free_list.c @@ -21,8 +21,24 @@ free_nd *fl_find_first_fit(free_nd *free_list, size_t size) { return NULL; } -void *fl_split_node(free_nd **free_list, free_nd *nd, size_t size) { - return NULL; +void *fl_split_node(free_nd **free_list, free_nd *split_nd, size_t size) { + /* for now, we'll just remove the node from the free list */ + + if (*free_list == split_nd) { + *free_list = (*free_list)->next; + } + + for (free_nd *nd = *free_list; nd; nd = nd->next) { + if (nd->next == split_nd) { + /* + * split_nd is not null, so we know that nd->next is + * also not null here + */ + nd->next = nd->next->next; + } + } + + return split_nd; } void fl_coalesce_nodes(free_nd *free_list) { |