fix: set_free.

This commit is contained in:
2025-06-26 09:33:25 +02:00
parent ed41c04209
commit 5c08d33f22
3 changed files with 24 additions and 5 deletions

View File

@@ -1,5 +1,6 @@
#include "list.h" #include "list.h"
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
void set_ll_append(struct SETBlockMeta *head, struct SETBlockMeta *next) void set_ll_append(struct SETBlockMeta *head, struct SETBlockMeta *next)
@@ -15,28 +16,38 @@ void set_ll_free_all(struct SETBlockMeta *head)
{ {
while (head) while (head)
{ {
printf("Some free.\n");
void *block_start = head; void *block_start = head;
head = head->next; head = head->next;
free(block_start); free(block_start);
} }
printf("Some free.\n");
} }
struct SETBlockMeta *set_ll_free_one(struct SETBlockMeta *head, void *address) struct SETBlockMeta *set_ll_free_one(struct SETBlockMeta *head, void *address)
{ {
struct SETBlockMeta *meta = address - (sizeof(struct SETBlockMeta)); struct SETBlockMeta *meta = address - (sizeof(struct SETBlockMeta));
if (meta == head) if (meta->prev == NULL)
{ {
struct SETBlockMeta *ret = meta->next; struct SETBlockMeta *ret = meta->next;
ret->prev = NULL; if (ret)
ret->end = meta->end; {
ret->prev = NULL;
ret->end = meta->end;
}
free(meta); free(meta);
return ret; return ret;
} }
meta->prev->next = meta->next; meta->prev->next = meta->next;
if (meta->next)
if (meta->next != NULL)
meta->next->prev = meta->prev; meta->next->prev = meta->prev;
else
head->end = meta->prev;
free(meta); free(meta);

View File

@@ -15,7 +15,7 @@ char *format_string(const char *fmt, ...)
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
size_t size = vsnprintf(NULL, 0, fmt, args); size_t size = vsnprintf(NULL, 0, fmt, args);
char *out = (char *)malloc(size + 1); char *out = (char *)set_malloc(size + 1);
if (!out) if (!out)
{ {
@@ -50,6 +50,9 @@ void *set_malloc(size_t n)
void *blocks = malloc(n + sizeof(struct SETBlockMeta)); void *blocks = malloc(n + sizeof(struct SETBlockMeta));
struct SETBlockMeta *meta = blocks; struct SETBlockMeta *meta = blocks;
meta->next = NULL; meta->next = NULL;
meta->prev = NULL;
fprintf(stdout, "%s\n", "Some malloc");
if (block_meta_head == NULL) if (block_meta_head == NULL)
{ {

View File

@@ -61,6 +61,7 @@ TEST(Other_Basic)
TEST(Other_With_Malloc) TEST(Other_With_Malloc)
{ {
int *some_array = set_malloc(20 * sizeof(int)); int *some_array = set_malloc(20 * sizeof(int));
int *array = set_malloc(20);
for (int i = 0; i < 20; i++) for (int i = 0; i < 20; i++)
{ {
@@ -73,6 +74,10 @@ TEST(Other_With_Malloc)
} }
fprintf(stdout, ".\n"); fprintf(stdout, ".\n");
set_free(array);
set_free(some_array);
void *some_other = set_malloc(15);
ASSERT_TRUE(false); ASSERT_TRUE(false);
} }