Fix on rethrow + macros.

This commit is contained in:
Christoffer Lerno
2023-06-27 13:19:28 +02:00
parent fee80682b1
commit f651a59294
14 changed files with 566 additions and 498 deletions

View File

@@ -33,71 +33,6 @@ macro bool is_valid_aligned_allocator($Type)
);
}
/**
* @require is_allocator($AllocatorType) : "Type does not implement all allocator methods"
* @require is_valid_aligned_allocator($AllocatorType) : "Type does not implement all aligned methods"
**/
macro AllocatorFunction allocator_fn($AllocatorType)
{
return fn void*!(Allocator* data, usz size, usz alignment, usz offset, void* old_pointer, AllocationKind kind)
{
$AllocatorType* allocator = ($AllocatorType*)data;
bool $supports_aligned = $defined(allocator.alloc_aligned);
switch (kind)
{
case CALLOC:
return allocator.calloc(size) @inline;
case ALIGNED_CALLOC:
$if $supports_aligned:
return allocator.calloc_aligned(size, alignment, offset) @inline;
$else
return @aligned_calloc(allocator.calloc, size, alignment, offset);
$endif
case ALLOC:
return allocator.alloc(size);
case ALIGNED_ALLOC:
$if $supports_aligned:
return allocator.alloc_aligned(size, alignment, offset) @inline;
$else
return @aligned_alloc(allocator.alloc, size, alignment, offset);
$endif
case REALLOC:
return allocator.realloc(old_pointer, size) @inline;
case ALIGNED_REALLOC:
$if $supports_aligned:
return allocator.realloc_aligned(old_pointer, size, alignment, offset) @inline;
$else
return @aligned_realloc(allocator.alloc, allocator.free, old_pointer, size, alignment, offset);
$endif
case ALIGNED_FREE:
$if $supports_aligned:
allocator.free_aligned(old_pointer, alignment, offset) @inline!;
return null;
$else
@aligned_free(allocator.free, old_pointer) @inline!;
return null;
$endif
case FREE:
allocator.free(old_pointer)!;
return null;
case MARK:
$if $defined(allocator.mark):
allocator.mark() @inline;
return null;
$else
return AllocationFailure.UNSUPPORTED_OPERATION?;
$endif
case RESET:
$if $defined(allocator.reset):
allocator.reset() @inline;
return null;
$else
return AllocationFailure.UNSUPPORTED_OPERATION?;
$endif
}
unreachable();
};
}
struct Allocator
{
@@ -121,13 +56,12 @@ enum AllocationKind
fault AllocationFailure
{
OUT_OF_MEMORY,
UNSUPPORTED_OPERATION,
CHUNK_TOO_LARGE,
}
fn void*! Allocator.alloc(Allocator* allocator, usz size) @inline
macro void*! Allocator.alloc(Allocator* allocator, usz size)
{
return allocator.function(allocator, size, 0, 0, null, ALLOC);
}
@@ -135,12 +69,12 @@ fn void*! Allocator.alloc(Allocator* allocator, usz size) @inline
/**
* @require alignment && math::is_power_of_2(alignment)
*/
fn void*! Allocator.alloc_aligned(Allocator* allocator, usz size, usz alignment, usz offset = 0) @inline
macro void*! Allocator.alloc_aligned(Allocator* allocator, usz size, usz alignment, usz offset = 0)
{
return allocator.function(allocator, size, alignment, offset, null, ALIGNED_ALLOC);
}
fn void*! Allocator.realloc(Allocator* allocator, void* old_pointer, usz size) @inline
macro void*! Allocator.realloc(Allocator* allocator, void* old_pointer, usz size)
{
return allocator.function(allocator, size, 0, 0, old_pointer, REALLOC);
}
@@ -148,18 +82,18 @@ fn void*! Allocator.realloc(Allocator* allocator, void* old_pointer, usz size) @
/**
* @require alignment && math::is_power_of_2(alignment)
*/
fn void*! Allocator.realloc_aligned(Allocator* allocator, void* old_pointer, usz size, usz alignment, usz offset = 0) @inline
macro void*! Allocator.realloc_aligned(Allocator* allocator, void* old_pointer, usz size, usz alignment, usz offset = 0)
{
return allocator.function(allocator, size, alignment, offset, old_pointer, ALIGNED_REALLOC);
}
fn usz! Allocator.mark(Allocator* allocator) @inline
macro usz Allocator.mark(Allocator* allocator)
{
return (usz)(uptr)allocator.function(allocator, 0, 0, 0, null, MARK);
return (usz)(uptr)allocator.function(allocator, 0, 0, 0, null, MARK) ?? 0;
}
fn void*! Allocator.calloc(Allocator* allocator, usz size) @inline
macro void*! Allocator.calloc(Allocator* allocator, usz size)
{
return allocator.function(allocator, size, 0, 0, null, CALLOC);
}
@@ -167,24 +101,24 @@ fn void*! Allocator.calloc(Allocator* allocator, usz size) @inline
/**
* @require alignment && math::is_power_of_2(alignment)
*/
fn void*! Allocator.calloc_aligned(Allocator* allocator, usz size, usz alignment, usz offset = 0) @inline
macro void*! Allocator.calloc_aligned(Allocator* allocator, usz size, usz alignment, usz offset = 0)
{
return allocator.function(allocator, size, alignment, offset, null, ALIGNED_CALLOC);
}
fn void! Allocator.free(Allocator* allocator, void* old_pointer) @inline
macro void! Allocator.free(Allocator* allocator, void* old_pointer)
{
allocator.function(allocator, 0, 0, 0, old_pointer, FREE)!;
}
fn void! Allocator.free_aligned(Allocator* allocator, void* old_pointer) @inline
macro void! Allocator.free_aligned(Allocator* allocator, void* old_pointer)
{
allocator.function(allocator, 0, 0, 0, old_pointer, ALIGNED_FREE)!;
}
fn void Allocator.reset(Allocator* allocator, usz mark = 0)
macro void Allocator.reset(Allocator* allocator, usz mark = 0)
{
allocator.function(allocator, mark, 0, 0, null, RESET)!!;
(void)allocator.function(allocator, mark, 0, 0, null, RESET);
}
fn usz alignment_for_allocation(usz alignment) @inline @private