separate heap locations for mapping arr and items arr

This commit is contained in:
2026-04-25 01:08:03 +02:00
parent a0e3fa8503
commit 6289039ed4
+29 -24
View File
@@ -1,5 +1,6 @@
#include "cig.h" #include "cig.h"
#include <assert.h> #include <assert.h>
#include <stddef.h>
#include <stdio.h> #include <stdio.h>
#define FLAG_FREE (-1) #define FLAG_FREE (-1)
@@ -46,21 +47,19 @@ static inline unsigned int mapping_cap(unsigned int capacity) {
} }
typedef struct internal_map_sizes { typedef struct internal_map_sizes {
int bytes; size_t header_bytes;
int real_cap_of_items_arr; size_t mapping_arr_bytes;
} internal_map_sizes_t; } internal_map_sizes_t;
internal_map_sizes_t internal_map_sizes(unsigned int capacity, unsigned int item_size) { internal_map_sizes_t internal_map_sizes(size_t capacity, size_t item_size) {
const size_t cap_of_index_arr = const size_t header_bytes = sizeof(map_header_t) + item_size * capacity;
sizeof(int) * mapping_cap(capacity); map_header_t foo;
const size_t INC = ALIGN_OF(int); // TODO: this is shit. I am defining that the mapping arr is 2x the size of
const size_t SIZE = item_size * capacity; // the items arr in multiple places. Figure this out at some point.
const size_t cap_of_items_arr = (SIZE + INC - 1) / INC; const size_t mapping_arr_bytes = sizeof(*foo.mapping_arr) * capacity * 2;
const size_t bytes =
sizeof(map_header_t) + cap_of_items_arr + cap_of_index_arr;
return (internal_map_sizes_t){ return (internal_map_sizes_t){
.bytes=bytes, .header_bytes=header_bytes,
.real_cap_of_items_arr=cap_of_items_arr, .mapping_arr_bytes=mapping_arr_bytes,
}; };
} }
@@ -71,13 +70,20 @@ void *map_create_func(
args.initial_capacity, args.initial_capacity,
args.item_size args.item_size
); );
map_header_t *header = map_header_t *header = allocator_alloc_func(
allocator_alloc_func( args.allocator,
args.allocator, sizes.header_bytes,
sizes.bytes, args.file,
args.file, args.line
args.line );
);
header->mapping_arr = allocator_alloc_func(
args.allocator,
sizes.mapping_arr_bytes,
args.file,
args.line
);
header->mapping_capacity = args.initial_capacity * 2;
header->n_items = 0; header->n_items = 0;
header->capacity = args.initial_capacity; header->capacity = args.initial_capacity;
@@ -86,8 +92,6 @@ void *map_create_func(
header->allocator = args.allocator; header->allocator = args.allocator;
header->hash = args.hash; header->hash = args.hash;
header->equals = args.equals; header->equals = args.equals;
header->mapping_arr = (int*) &header->bytes[sizes.real_cap_of_items_arr];
header->mapping_capacity = mapping_cap(args.initial_capacity);
for ( unsigned int i = 0; i < header->mapping_capacity; i++ ) { for ( unsigned int i = 0; i < header->mapping_capacity; i++ ) {
header->mapping_arr[i] = FLAG_FREE; header->mapping_arr[i] = FLAG_FREE;
@@ -215,9 +219,10 @@ void map_assure_growable_by_1(void **this, const char *file, int line) {
new_capacity, new_capacity,
header->item_size header->item_size
); );
header = allocator_resize_func(allocator, header, new_size.bytes, file, line);
header->mapping_arr = (int*) &header->bytes[new_size.real_cap_of_items_arr]; header = allocator_resize_func(allocator, header, new_size.header_bytes, file, line);
header->mapping_capacity = mapping_cap(new_capacity); header->mapping_arr = (int*) allocator_resize_func(allocator, header->mapping_arr, new_size.mapping_arr_bytes, file, line);
header->mapping_capacity = new_capacity * 2;
header->capacity=new_capacity; header->capacity=new_capacity;
// first we have to clear the entire mapping array // first we have to clear the entire mapping array