96 lines
2.4 KiB
C
96 lines
2.4 KiB
C
#include <stdio.h>
|
|
#include "base.h"
|
|
#include "memory_arena.h"
|
|
#include <assert.h>
|
|
|
|
int main(void) {
|
|
assert(mem_arena_init(4));
|
|
printf("Initial\n");
|
|
mem_arena_print();
|
|
|
|
{
|
|
printf("Single map\n");
|
|
cc_map( int, int ) int_map;
|
|
cc_init(&int_map);
|
|
cc_reserve(&int_map, 2048);
|
|
|
|
cc_insert(&int_map, 1, 4);
|
|
cc_insert(&int_map, 2, 7);
|
|
cc_insert(&int_map, 7, 1);
|
|
cc_insert(&int_map, 4, 8);
|
|
|
|
printf("Insertion\n");
|
|
mem_arena_print();
|
|
|
|
cc_cleanup(&int_map);
|
|
|
|
printf("Clean up\n");
|
|
mem_arena_print();
|
|
}
|
|
|
|
{
|
|
cc_map(char*, int) str_map;
|
|
cc_init(&str_map);
|
|
cc_reserve(&str_map, 4);
|
|
mem_arena_print();
|
|
{
|
|
// For char*, you have to allocated memory (static or dynamic)
|
|
char* sample = mem_arena_malloc(4);
|
|
memcpy(sample, "lol", 4);
|
|
cc_insert(&str_map, sample, 15);
|
|
}
|
|
int* val = cc_get(&str_map, "lol");
|
|
printf("str key get: %p\n", val);
|
|
mem_arena_print();
|
|
// Need to retrieve char* to free, if it is dynamic
|
|
// On retrive, dereference to get point and free
|
|
// Safer to NULL check first
|
|
// but we know it exists
|
|
mem_arena_free((void*)*cc_key_for(&str_map, val));
|
|
cc_cleanup(&str_map);
|
|
mem_arena_print();
|
|
}
|
|
|
|
{
|
|
printf("Map of maps\n");
|
|
cc_map(int, cc_map(int,int)) map_of_map;
|
|
cc_init(&map_of_map);
|
|
cc_reserve(&map_of_map, 32);
|
|
for (size_t i = 0; i < 10; i += 2) {
|
|
cc_map(int, int) base_map;
|
|
cc_init(&base_map);
|
|
cc_reserve(&base_map, 2048);
|
|
cc_insert(&map_of_map, i, base_map);
|
|
}
|
|
|
|
cc_map(int, int)* s = cc_get(&map_of_map, 3);
|
|
printf("get: %p\n", s);
|
|
s = cc_get(&map_of_map, 2);
|
|
printf("get: %p\n", s);
|
|
cc_insert(s, 3, 5);
|
|
cc_insert(s, 4, 5);
|
|
cc_insert(s, 2, 5);
|
|
cc_insert(s, 1, 5);
|
|
cc_insert(s, 8, 5);
|
|
|
|
s = cc_get(&map_of_map, 2);
|
|
|
|
int* v = cc_get(s, 2);
|
|
printf("get: %i\n", *v);
|
|
mem_arena_print();
|
|
|
|
for (size_t i = 0; i < 10; i += 2) {
|
|
cc_map(int, int)* base_map = cc_get(&map_of_map, i);;
|
|
cc_cleanup(base_map);
|
|
}
|
|
cc_cleanup(&map_of_map);
|
|
|
|
printf("Clean up\n");
|
|
mem_arena_print();
|
|
}
|
|
|
|
mem_arena_deinit();
|
|
return 0;
|
|
}
|
|
|