Fix bug using $switch. Added mem::equals. Fix to dynamic arena allocator.

This commit is contained in:
Christoffer Lerno
2022-08-05 18:46:38 +02:00
parent f93c20ca34
commit 878bbed929
8 changed files with 102 additions and 8 deletions

View File

@@ -97,12 +97,12 @@ private fn void*! DynamicArenaAllocator._alloc_new(DynamicArenaAllocator* this,
}
page.memory = mem;
void* mem_start = mem::aligned_pointer(mem + offset + DynamicArenaChunk.sizeof, alignment) - offset;
assert(mem_start + DynamicArenaChunk.sizeof + size < mem + page_size);
assert(mem_start + size < mem + page_size);
DynamicArenaChunk* chunk = (DynamicArenaChunk*)mem_start - 1;
chunk.size = size;
page.prev_arena = this.page;
page.total = page_size;
page.used = size + offset;
page.used = mem_start + size - page.memory;
this.page = page;
page.last_ptr = mem_start;
return mem_start;
@@ -144,6 +144,7 @@ private fn void*! DynamicArenaAllocator._alloc(DynamicArenaAllocator* this, usiz
}
page.used = new_used;
assert(start + size == page.memory + page.used);
void* mem = start;
DynamicArenaChunk* chunk = (DynamicArenaChunk*)mem - 1;
chunk.size = size;

View File

@@ -50,6 +50,58 @@ macro void clear(void* dst, usize len, usize $dst_align = 0, bool $is_volatile =
$$memset(dst, (char)0, len, $is_volatile, $dst_align);
}
/**
* @require $typeof(a).kind == TypeKind.SUBARRAY || $typeof(a).kind == TypeKind.POINTER
* @require $typeof(b).kind == TypeKind.SUBARRAY || $typeof(b).kind == TypeKind.POINTER
* @require $typeof(a).kind != TypeKind.SUBARRAY || len == -1
* @require $typeof(a).kind != TypeKind.POINTER || len > -1
* @checked (a = b), (b = a)
**/
macro bool equals(a, b, isize len = -1, usize $alignment = 0)
{
var $align = $alignment;
$if (!$alignment):
$align = $alignof($typeof(a[0]));
$endif;
void* x = void;
void* y = void;
$if ($typeof(a).kind == TypeKind.SUBARRAY):
len = a.len;
if (len != b.len) return false;
x = a.ptr;
y = b.ptr;
$else:
x = a;
y = b;
assert(len >= 0, "A zero or positive length must be given when comparing pointers.");
$endif;
if (!len) return true;
$switch ($align):
$case 1:
var $Type = char;
$case 2:
var $Type = ushort;
$case 4:
var $Type = uint;
$case 8:
$default:
var $Type = ulong;
$endswitch;
var $step = $Type.sizeof;
usize end = len / $step;
for (usize i = 0; i < end; i++)
{
if ((($Type*)x)[i] != (($Type*)y)[i]) return false;
}
usize last = len % $align;
for (usize i = len - last; i < len; i++)
{
if (((char*)x)[i] != ((char*)y)[i]) return false;
}
return true;
}
macro @clone(&value) @builtin
{
$typeof(value)* x = malloc($typeof(value));

View File

@@ -109,6 +109,18 @@ macro bool is_comparable($Type)
$endif;
}
macro bool is_subarray_convertable($Type)
{
$switch ($Type.kind):
$case SUBARRAY:
return true;
$case POINTER:
return $Type.inner.kind == TypeKind.ARRAY;
$default:
return false;
$endswitch;
}
macro bool is_equatable_value(value)
{
$if ($defined(value.less) || $defined(value.compare_to) || $defined(value.equals)):