diff --git a/src/list.c b/src/list.c index e182b59..d422267 100644 --- a/src/list.c +++ b/src/list.c @@ -1,5 +1,6 @@ #include "list.h" +#include #include void set_ll_append(struct SETBlockMeta *head, struct SETBlockMeta *next) @@ -15,28 +16,38 @@ void set_ll_free_all(struct SETBlockMeta *head) { while (head) { + printf("Some free.\n"); void *block_start = head; head = head->next; free(block_start); } + + printf("Some free.\n"); } struct SETBlockMeta *set_ll_free_one(struct SETBlockMeta *head, void *address) { + struct SETBlockMeta *meta = address - (sizeof(struct SETBlockMeta)); - if (meta == head) + if (meta->prev == NULL) { struct SETBlockMeta *ret = meta->next; - ret->prev = NULL; - ret->end = meta->end; + if (ret) + { + ret->prev = NULL; + ret->end = meta->end; + } free(meta); return ret; } meta->prev->next = meta->next; - if (meta->next) + + if (meta->next != NULL) meta->next->prev = meta->prev; + else + head->end = meta->prev; free(meta); diff --git a/src/utils.c b/src/utils.c index 1a693a5..8c13926 100644 --- a/src/utils.c +++ b/src/utils.c @@ -15,7 +15,7 @@ char *format_string(const char *fmt, ...) va_list args; va_start(args, fmt); size_t size = vsnprintf(NULL, 0, fmt, args); - char *out = (char *)malloc(size + 1); + char *out = (char *)set_malloc(size + 1); if (!out) { @@ -50,6 +50,9 @@ void *set_malloc(size_t n) void *blocks = malloc(n + sizeof(struct SETBlockMeta)); struct SETBlockMeta *meta = blocks; meta->next = NULL; + meta->prev = NULL; + + fprintf(stdout, "%s\n", "Some malloc"); if (block_meta_head == NULL) { diff --git a/testtest.c b/testtest.c index 3d875c8..b1529f2 100644 --- a/testtest.c +++ b/testtest.c @@ -61,6 +61,7 @@ TEST(Other_Basic) TEST(Other_With_Malloc) { int *some_array = set_malloc(20 * sizeof(int)); + int *array = set_malloc(20); for (int i = 0; i < 20; i++) { @@ -73,6 +74,10 @@ TEST(Other_With_Malloc) } fprintf(stdout, ".\n"); + set_free(array); + set_free(some_array); + + void *some_other = set_malloc(15); ASSERT_TRUE(false); }