diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:07:58 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:07:58 +0200 |
| commit | 967be9e750221ab2ab783f95df79bb26d290a45e (patch) | |
| tree | 6802900a5e975f9f68b169f0f503f040056d6952 /malloc/block_allocator/main.c | |
Diffstat (limited to 'malloc/block_allocator/main.c')
| -rw-r--r-- | malloc/block_allocator/main.c | 39 |
1 files changed, 39 insertions, 0 deletions
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 <string.h> + +#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; +} |
