From f88198cb987ab006f3406698fec410ff1de05534 Mon Sep 17 00:00:00 2001 From: "Douglas B. Rumbaugh" Date: Sun, 2 Nov 2025 17:57:53 -0500 Subject: Ran clangtidy --- include/alloc.h | 2 +- include/alloc_header.h | 4 +-- include/constants.h | 2 +- include/free_list.h | 4 +-- src/alloc.c | 8 ++--- src/free_list.c | 18 +++++----- tests/liballoc_tests.c | 97 +++++++++++++++++++++++--------------------------- 7 files changed, 63 insertions(+), 72 deletions(-) diff --git a/include/alloc.h b/include/alloc.h index 82a4b7b..6374b3a 100644 --- a/include/alloc.h +++ b/include/alloc.h @@ -5,7 +5,7 @@ * CISC 301 -- Operating Systems, Project 3 * * Copyright (C) 2025 Douglas B. Rumbaugh - * + * * Distributed under the Modified BSD License * */ diff --git a/include/alloc_header.h b/include/alloc_header.h index bf4716c..381f41b 100644 --- a/include/alloc_header.h +++ b/include/alloc_header.h @@ -5,15 +5,15 @@ * CISC 301 -- Operating Systems, Project 3 * * Copyright (C) 2025 Douglas B. Rumbaugh - * + * * Distributed under the Modified BSD License * */ #ifndef H_LIBALLOC_HEADER #define H_LIBALLOC_HEADER -#include #include +#include #include "constants.h" diff --git a/include/constants.h b/include/constants.h index 8cb2db7..5f0cb6a 100644 --- a/include/constants.h +++ b/include/constants.h @@ -5,7 +5,7 @@ * CISC 301 -- Operating Systems, Project 3 * * Copyright (C) 2025 Douglas B. Rumbaugh - * + * * Distributed under the Modified BSD License * */ diff --git a/include/free_list.h b/include/free_list.h index 1993aa7..8eab36d 100644 --- a/include/free_list.h +++ b/include/free_list.h @@ -5,7 +5,7 @@ * CISC 301 -- Operating Systems, Project 3 * * Copyright (C) 2025 Douglas B. Rumbaugh - * + * * Distributed under the Modified BSD License * */ @@ -14,8 +14,8 @@ #include -#include "constants.h" #include "alloc_header.h" +#include "constants.h" typedef struct free_nd { size_t size; diff --git a/src/alloc.c b/src/alloc.c index 3f805db..39cbf88 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -5,14 +5,14 @@ * CISC 301 -- Operating Systems, Project 3 * * Copyright (C) 2025 Douglas B. Rumbaugh - * + * * Distributed under the Modified BSD License * */ #include "alloc.h" +#include "alloc_header.h" #include "constants.h" #include "free_list.h" -#include "alloc_header.h" static void *heap_start = 0; static void *heap_end = 0; @@ -84,6 +84,4 @@ void release(void *ptr) { fl_coalesce_nodes(free_list); } -free_nd *free_list_head() { - return free_list; -} +free_nd *free_list_head() { return free_list; } diff --git a/src/free_list.c b/src/free_list.c index 6c0ff35..0ecb23f 100644 --- a/src/free_list.c +++ b/src/free_list.c @@ -25,8 +25,8 @@ void *fl_split_node(free_nd **free_list, free_nd *split_nd, size_t size) { ssize_t extra_space = split_nd->size - size - sizeof(header); if (extra_space >= SPLIT_THRESHOLD) { - free_nd *new_nd = (free_nd*)((char*) (split_nd) + size + sizeof(header)); - new_nd->next = split_nd ->next; + free_nd *new_nd = (free_nd *)((char *)(split_nd) + size + sizeof(header)); + new_nd->next = split_nd->next; split_nd->next = new_nd; new_nd->size = extra_space - sizeof(header); @@ -35,7 +35,7 @@ void *fl_split_node(free_nd **free_list, free_nd *split_nd, size_t size) { new_hdr->magic_number = MAGIC_NUMBER; new_hdr->size = extra_space - sizeof(header); - header *old_hdr = (header *)((char *)split_nd- sizeof(header)); + header *old_hdr = (header *)((char *)split_nd - sizeof(header)); old_hdr->size = size; } @@ -45,9 +45,9 @@ void *fl_split_node(free_nd **free_list, free_nd *split_nd, size_t size) { 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 + /* + * split_nd is not null, so we know that nd->next is + * also not null here */ nd->next = nd->next->next; } @@ -77,8 +77,8 @@ void fl_add_node(free_nd **free_list, void *ptr) { } } - new_nd->next = *insert_point; - new_nd->size = ((header*)(ptr - sizeof(header)))->size; + new_nd->next = *insert_point; + new_nd->size = ((header *)(ptr - sizeof(header)))->size; *insert_point = new_nd; - } +} diff --git a/tests/liballoc_tests.c b/tests/liballoc_tests.c index 2d3a439..16d50d6 100644 --- a/tests/liballoc_tests.c +++ b/tests/liballoc_tests.c @@ -1,11 +1,11 @@ /* * tests/liballoc_tests.c * - * Unit tests for liballoc + * Unit tests for liballoc * CISC 301 -- Operating Systems, Project 3 * * Copyright (C) 2025 Douglas B. Rumbaugh - * + * * Distributed under the Modified BSD License * */ @@ -13,17 +13,17 @@ #include "constants.h" #include -#include #include +#include START_TEST(basic_allocate) { void *memory = allocate(ALIGNMENT * 3); ck_assert_ptr_nonnull(memory); /* verify we can write to the memory w/o segfaulting */ - memset(memory, 0, ALIGNMENT*3); + memset(memory, 0, ALIGNMENT * 3); - size_t alignment = (size_t) memory % ALIGNMENT; + size_t alignment = (size_t)memory % ALIGNMENT; ck_assert_int_eq(alignment, 0); /* leak the memory--we aren't testing release */ @@ -31,21 +31,21 @@ START_TEST(basic_allocate) { END_TEST START_TEST(multiple_allocations) { - size_t size = ALIGNMENT*5; + size_t size = ALIGNMENT * 5; void *allocations[100]; - for (size_t i=0; i<100; i++) { + for (size_t i = 0; i < 100; i++) { allocations[i] = allocate(size); ck_assert_ptr_nonnull(allocations[i]); memset(allocations[i], 0, size); - size_t alignment = (size_t) allocations[i]% ALIGNMENT; + size_t alignment = (size_t)allocations[i] % ALIGNMENT; ck_assert_int_eq(alignment, 0); } /* verify the headers */ - for (size_t i=0; i<100; i++) { + for (size_t i = 0; i < 100; i++) { header *hdr = allocations[i] - sizeof(header); ck_assert_int_eq(hdr->size, size); ck_assert_int_eq(hdr->magic_number, MAGIC_NUMBER); @@ -63,7 +63,7 @@ START_TEST(basic_release) { free_nd *list_head = free_list_head(); ck_assert_ptr_nonnull(list_head); ck_assert_ptr_eq(list_head, memory); - ck_assert_int_eq(list_head->size, ALIGNMENT*3); + ck_assert_int_eq(list_head->size, ALIGNMENT * 3); ck_assert_ptr_null(list_head->next); } END_TEST @@ -77,10 +77,10 @@ START_TEST(free_list_emptying) { free_nd *list_head = free_list_head(); ck_assert_ptr_nonnull(list_head); ck_assert_ptr_eq(list_head, memory); - ck_assert_int_eq(list_head->size, ALIGNMENT*3); + ck_assert_int_eq(list_head->size, ALIGNMENT * 3); ck_assert_ptr_null(list_head->next); - void *memory2 = allocate(ALIGNMENT *3); + void *memory2 = allocate(ALIGNMENT * 3); ck_assert_ptr_eq(memory, memory2); ck_assert_ptr_null(free_list_head()); } @@ -99,15 +99,15 @@ START_TEST(unaligned_allocation) { void *allocations[100]; /* ensure first allocation is aligned */ allocations[0] = allocate(unaligned_size); - size_t first_alignment = (size_t) allocations[0] % ALIGNMENT; + size_t first_alignment = (size_t)allocations[0] % ALIGNMENT; ck_assert_int_eq(first_alignment, 0); memset(allocations[0], 0, unaligned_size); /* now allocate several more times--each allocation should be aligned */ - for (size_t i=1; i<100; i++) { + for (size_t i = 1; i < 100; i++) { allocations[i] = allocate(unaligned_size); - size_t alignment = (size_t) allocations[i] % ALIGNMENT; + size_t alignment = (size_t)allocations[i] % ALIGNMENT; ck_assert_int_eq(alignment, 0); /* verify we can write the the memory */ @@ -115,23 +115,21 @@ START_TEST(unaligned_allocation) { /* just leak the memory--we aren't testing release here */ } - + /* verify the headers */ - for (size_t i=0; i<100; i++) { + for (size_t i = 0; i < 100; i++) { header *hdr = allocations[i] - sizeof(header); ck_assert_int_eq(hdr->magic_number, MAGIC_NUMBER); } - } - START_TEST(free_list_reuse) { - size_t size = ALIGNMENT *3; + size_t size = ALIGNMENT * 3; void *memory = allocate(size); release(memory); - for (size_t i=0; i<10; i++) { + for (size_t i = 0; i < 10; i++) { void *new_memory = allocate(size); ck_assert_ptr_eq(memory, new_memory); ck_assert_ptr_null(free_list_head()); @@ -141,51 +139,50 @@ START_TEST(free_list_reuse) { END_TEST START_TEST(free_list_coalesce_forward) { - size_t size =ALIGNMENT*3; + size_t size = ALIGNMENT * 3; const size_t n = 100; void *ptrs[n]; - for (size_t i=0; inext); - ck_assert_int_eq(fl_head->size, (cnt)*size + (cnt-1)*sizeof(header)); - } + */ + size_t cnt = 0; + for (size_t i = 0; i < n; i++) { + release(ptrs[i]); + cnt++; + free_nd *fl_head = free_list_head(); + ck_assert_ptr_null(fl_head->next); + ck_assert_int_eq(fl_head->size, (cnt)*size + (cnt - 1) * sizeof(header)); + } } END_TEST - START_TEST(free_list_coalesce_backward) { - size_t size =ALIGNMENT*3; + size_t size = ALIGNMENT * 3; const size_t n = 100; void *ptrs[n]; - for (size_t i=0; i=0; i--) { - release(ptrs[i]); - cnt++; - free_nd *fl_head = free_list_head(); - ck_assert_ptr_null(fl_head->next); - ck_assert_int_eq(fl_head->size, (cnt)*size + (cnt-1)*sizeof(header)); - } + */ + size_t cnt = 0; + for (ssize_t i = n - 1; i >= 0; i--) { + release(ptrs[i]); + cnt++; + free_nd *fl_head = free_list_head(); + ck_assert_ptr_null(fl_head->next); + ck_assert_int_eq(fl_head->size, (cnt)*size + (cnt - 1) * sizeof(header)); + } } END_TEST @@ -200,13 +197,12 @@ START_TEST(free_list_split_basic) { ck_assert_ptr_eq(memory, memory2); ck_assert_ptr_nonnull(free_list_head()); - } END_TEST START_TEST(free_list_split_below_threshold) { - size_t initial_size = 5*ALIGNMENT; - size_t second_size = 2*ALIGNMENT; + size_t initial_size = 5 * ALIGNMENT; + size_t second_size = 2 * ALIGNMENT; void *memory = allocate(initial_size); release(memory); @@ -217,8 +213,6 @@ START_TEST(free_list_split_below_threshold) { ck_assert_ptr_null(free_list_head()); } - - Suite *liballoc_suite(void) { Suite *s; TCase *unit; @@ -242,7 +236,6 @@ Suite *liballoc_suite(void) { return s; } - int main(void) { int number_failed; Suite *s; -- cgit v1.2.3