Updated malloc/calloc/realloc/free deprecation of old helper functions. Add checks to prevent incorrect alignment on types when using malloc. Better errors from $assert. Added @deprecated. Fixed issue using named arguments after varargs.

This commit is contained in:
Christoffer Lerno
2023-02-27 14:51:35 +01:00
committed by Christoffer Lerno
parent 8ad8af861e
commit dd4edfb747
28 changed files with 705 additions and 343 deletions

View File

@@ -45,7 +45,7 @@ macro void LinkedList.@free_node(LinkedList &list, Node* node) @private
macro Node* LinkedList.@alloc_node(LinkedList &list) @private
{
if (!list.allocator) list.allocator = mem::current_allocator();
return list.allocator.alloc(Node.sizeof)!!;
return malloc(Node, .using = list.allocator);
}
fn void LinkedList.link_first(LinkedList* list, Type value) @private
@@ -175,7 +175,7 @@ fn void LinkedList.insert(LinkedList* list, usz index, Type element)
fn void LinkedList.link_before(LinkedList *list, Node *succ, Type value) @private
{
Node* pred = succ.prev;
Node* new_node = mem::alloc(Node);
Node* new_node = malloc(Node);
*new_node = { .prev = pred, .next = succ, .value = value };
succ.prev = new_node;
if (!pred)

View File

@@ -22,7 +22,7 @@ fn void List.init(List* list, usz initial_capacity = 16, Allocator* allocator =
if (initial_capacity > 0)
{
initial_capacity = math::next_power_of_2(initial_capacity);
list.entries = allocator.alloc_aligned(Type.sizeof * initial_capacity, Type[1].alignof)!!;
list.entries = malloc_aligned(Type, initial_capacity, .alignment = Type[1].alignof, .using = allocator)!!;
}
else
{
@@ -133,7 +133,7 @@ fn Type List.get(List* list, usz index)
fn void List.free(List* list)
{
if (!list.allocator) return;
list.allocator.free_aligned(list.entries)!!;
free_aligned(list.entries, .using = list.allocator);
list.capacity = 0;
list.size = 0;
list.entries = null;
@@ -153,7 +153,7 @@ fn void List.reserve(List* list, usz min_capacity)
if (list.capacity >= min_capacity) return;
if (!list.allocator) list.allocator = mem::temp_allocator();
min_capacity = math::next_power_of_2(min_capacity);
list.entries = list.allocator.realloc_aligned(list.entries, Type.sizeof * min_capacity, Type[1].alignof) ?? null;
list.entries = realloc_aligned(list.entries, Type.sizeof * min_capacity, .alignment = Type[1].alignof, .using = list.allocator) ?? null;
list.capacity = min_capacity;
}

View File

@@ -28,7 +28,7 @@ fn void HashMap.init(HashMap* map, uint capacity = DEFAULT_INITIAL_CAPACITY, flo
map.allocator = allocator;
map.load_factor = load_factor;
map.threshold = (uint)(capacity * load_factor);
map.table = array::make(Entry*, capacity, allocator);
map.table = calloc(Entry*, capacity, .using = allocator);
}
/**
@@ -168,7 +168,7 @@ fn Key[] HashMap.key_list(HashMap* map, Allocator* allocator = mem::current_allo
{
if (!map.count) return Key[] {};
Key[] list = array::make(Key, map.count, allocator);
Key[] list = calloc(Key, map.count, .using = allocator);
usz index = 0;
foreach (Entry* entry : map.table)
{
@@ -189,7 +189,7 @@ fn Value[] HashMap.value_tlist(HashMap* map)
fn Value[] HashMap.value_list(HashMap* map, Allocator* allocator = mem::current_allocator())
{
if (!map.count) return Value[] {};
Value[] list = array::make(Value, map.count, allocator);
Value[] list = calloc(Value, map.count, .using = allocator);
usz index = 0;
foreach (Entry* entry : map.table)
{
@@ -222,7 +222,7 @@ $endif;
fn void HashMap.add_entry(HashMap* map, uint hash, Key key, Value value, uint bucket_index) @private
{
Entry* entry = map.allocator.alloc(Entry.sizeof)!!;
Entry* entry = malloc(Entry, .using = map.allocator);
*entry = { .hash = hash, .key = key, .value = value, .next = map.table[bucket_index] };
map.table[bucket_index] = entry;
if (map.count++ >= map.threshold)
@@ -240,7 +240,7 @@ fn void HashMap.resize(HashMap* map, uint new_capacity) @private
map.threshold = uint.max;
return;
}
Entry*[] new_table = array::make(Entry*, new_capacity, map.allocator);
Entry*[] new_table = calloc(Entry*, new_capacity, .using = map.allocator);
map.transfer(new_table);
map.table = new_table;
map.free_internal(old_table.ptr);
@@ -339,7 +339,7 @@ fn bool HashMap.remove_entry_for_key(HashMap* map, Key key) @private
fn void HashMap.create_entry(HashMap* map, uint hash, Key key, Value value, int bucket_index) @private
{
Entry *e = map.table[bucket_index];
Entry* entry = map.allocator.alloc(Entry.sizeof)!!;
Entry* entry = malloc(Entry, .using = map.allocator);
*entry = { .hash = hash, .key = key, .value = value, .next = map.table[bucket_index] };
map.table[bucket_index] = entry;
map.count++;