From 967be9e750221ab2ab783f95df79bb26d290a45e Mon Sep 17 00:00:00 2001 From: Martial Simon Date: Mon, 15 Sep 2025 01:07:58 +0200 Subject: add: added projects --- malloc/block_allocator/main.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 malloc/block_allocator/main.c (limited to 'malloc/block_allocator/main.c') diff --git a/malloc/block_allocator/main.c b/malloc/block_allocator/main.c new file mode 100644 index 0000000..161185a --- /dev/null +++ b/malloc/block_allocator/main.c @@ -0,0 +1,39 @@ +#include + +#include "allocator.h" +#include "utils.h" + +int main(void) +{ + struct blk_allocator *ba = blka_new(); + + struct blk_meta *metas[6] = { NULL }; + char *messages[] = { "Hello world!\n", "You are doing ", "a great job ", + "if you see ", "this message ", "correctly.\n", + "But do not forget ", "to release ", "memory.\n" }; + + // Allocate metadatas + for (size_t i = 0; i < 3; ++i) + metas[i] = blka_alloc(ba, 2048); + for (size_t i = 3; i < 6; ++i) + metas[i] = blka_alloc(ba, 4096); + + // Write and read messages to metadatas + for (size_t i = 0; i < 6; ++i) + write_data(metas[i], messages[i], strlen(messages[i])); + for (size_t i = 0; i < 6; ++i) + read_data(metas[i]); + + // Overwrite metadatas, pop and read them + for (size_t i = 6; i < 9; ++i) + write_data(metas[i - 6], messages[i], strlen(messages[i])); + for (size_t i = 0; i < 3; ++i) + blka_pop(ba); + for (size_t i = 0; i < 3; ++i) + read_data(metas[i]); + + // Release memory + blka_delete(ba); + + return 0; +} -- cgit v1.2.3