diff options
| author | Douglas B. Rumbaugh <doug@douglasrumbaugh.com> | 2025-11-02 15:39:43 -0500 |
|---|---|---|
| committer | Douglas B. Rumbaugh <doug@douglasrumbaugh.com> | 2025-11-02 15:39:43 -0500 |
| commit | 04b57a756402e156953edfa2079d69b41db26e51 (patch) | |
| tree | 8bbfc5301c960a72b67918f37456460e23e496ba | |
| parent | 24d5cd8e806477b0fd2ddb5a232e672707f0da34 (diff) | |
| download | liballoc-04b57a756402e156953edfa2079d69b41db26e51.tar.gz | |
Updated free list interface
I realized my existing interface wouldn't work
if the coalescing or adding of a new node replaced
the head of the list, as there was no way to
communicate that back to the caller. As a result,
I've updated those interfaces to accept a pointer
to the free list head pointer. This will let them
change the free list head in alloc.c if necessary.
| -rw-r--r-- | include/alloc_header.h | 27 | ||||
| -rw-r--r-- | include/free_list.h | 11 | ||||
| -rw-r--r-- | src/alloc.c | 12 |
3 files changed, 39 insertions, 11 deletions
diff --git a/include/alloc_header.h b/include/alloc_header.h new file mode 100644 index 0000000..bf4716c --- /dev/null +++ b/include/alloc_header.h @@ -0,0 +1,27 @@ +/* + * include/alloc_header.h + * + * liballoc memory block header type. + * CISC 301 -- Operating Systems, Project 3 + * + * Copyright (C) 2025 Douglas B. Rumbaugh <dbrumbaugh@harrisburgu.edu> + * + * Distributed under the Modified BSD License + * + */ +#ifndef H_LIBALLOC_HEADER +#define H_LIBALLOC_HEADER + +#include <stdlib.h> +#include <assert.h> + +#include "constants.h" + +typedef struct header { + size_t size; + size_t magic_number; +} header; + +static_assert(sizeof(header) % ALIGNMENT == 0, "Header improperly aligned"); + +#endif diff --git a/include/free_list.h b/include/free_list.h index 887167b..1993aa7 100644 --- a/include/free_list.h +++ b/include/free_list.h @@ -15,6 +15,7 @@ #include <stdlib.h> #include "constants.h" +#include "alloc_header.h" typedef struct free_nd { size_t size; @@ -34,9 +35,12 @@ free_nd *fl_find_first_fit(free_nd *free_list, size_t size); * specified node cannot be split (e.g., it's an exact match for size * within alignment restrictions), simply remove it from the list. * + * If the head of the free list is updated by this operation, the free + * list pointer passed as an argument will be updated to reflect this + * * Returns a pointer to the region of memory removed from the list. */ -void *fl_split_node(free_nd *free_list, free_nd *nd, size_t size); +void *fl_split_node(free_nd **free_list, free_nd *nd, size_t size); /* * Scans the free list for adjacent nodes and merges them together @@ -46,7 +50,10 @@ void fl_coalesce_nodes(free_nd *free_list); /* * Scans the free list for the correct location for the specified * pointer, and link it into the list at that point. + * + * If the head of the free list is updated by this operation, the free + * list pointer passed as an argument will be updated to reflect this */ -void fl_add_node(free_nd *free_list, void *ptr); +void fl_add_node(free_nd **free_list, void *ptr); #endif diff --git a/src/alloc.c b/src/alloc.c index c13b1e8..830e49f 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -12,13 +12,7 @@ #include "alloc.h" #include "constants.h" #include "free_list.h" - -typedef struct header { - size_t size; - size_t magic_number; -} header; - -static_assert(sizeof(header) % ALIGNMENT == 0, "Header improperly aligned"); +#include "alloc_header.h" static void *heap_start = 0; static void *heap_end = 0; @@ -59,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); + void *return_region = fl_split_node(&free_list, nd, size); } /* @@ -82,6 +76,6 @@ void *allocate(size_t size) { } void release(void *ptr) { - fl_add_node(free_list, ptr); + fl_add_node(&free_list, ptr); fl_coalesce_nodes(free_list); } |