Support "typedef"

This commit is contained in:
Christoffer Lerno
2023-02-14 16:36:48 +01:00
committed by Christoffer Lerno
parent b7e19b75d0
commit df77b692d6
99 changed files with 468 additions and 441 deletions

View File

@@ -1,6 +1,6 @@
module std::core::mem::allocator;
struct DynamicArenaPage @private
struct DynamicArenaPage @local
{
void* memory;
void* prev_arena;
@@ -9,7 +9,7 @@ struct DynamicArenaPage @private
void* last_ptr;
}
struct DynamicArenaChunk @private
struct DynamicArenaChunk @local
{
usz size;
}
@@ -32,7 +32,7 @@ fn void DynamicArenaAllocator.free(DynamicArenaAllocator* this, void* ptr) @priv
* @require old_pointer && size > 0
* @require this.page `tried to realloc pointer on invalid allocator`
*/
fn void*! DynamicArenaAllocator._realloc(DynamicArenaAllocator* this, void* old_pointer, usz size, usz alignment, usz offset) @private
fn void*! DynamicArenaAllocator._realloc(DynamicArenaAllocator* this, void* old_pointer, usz size, usz alignment, usz offset) @local
{
DynamicArenaPage* current_page = this.page;
alignment = alignment_for_allocation(alignment);
@@ -82,7 +82,7 @@ fn void DynamicArenaAllocator.reset(DynamicArenaAllocator* this) @private
* @require math::is_power_of_2(alignment)
* @require size > 0
*/
fn void*! DynamicArenaAllocator._alloc_new(DynamicArenaAllocator* this, usz size, usz alignment, usz offset) @private
fn void*! DynamicArenaAllocator._alloc_new(DynamicArenaAllocator* this, usz size, usz alignment, usz offset) @local
{
// First, make sure that we can align it, extending the page size if needed.
usz page_size = max(this.page_size, mem::aligned_offset(size + DynamicArenaChunk.sizeof + offset, alignment) - offset);
@@ -113,7 +113,7 @@ fn void*! DynamicArenaAllocator._alloc_new(DynamicArenaAllocator* this, usz size
* @require size > 0
* @require this
*/
fn void*! DynamicArenaAllocator._alloc(DynamicArenaAllocator* this, usz size, usz alignment, usz offset) @private
fn void*! DynamicArenaAllocator._alloc(DynamicArenaAllocator* this, usz size, usz alignment, usz offset) @local
{
alignment = alignment_for_allocation(alignment);
DynamicArenaPage* page = this.page;

View File

@@ -1,6 +1,6 @@
module std::core::mem::allocator;
define MemoryAllocFn = fn char[]!(usz);
typedef MemoryAllocFn = fn char[]!(usz);
struct SimpleHeapAllocator
{

View File

@@ -1,7 +1,7 @@
module std::core::mem::allocator;
import std::io;
struct TempAllocatorChunk @private
struct TempAllocatorChunk @local
{
usz size;
char[*] data;
@@ -31,8 +31,8 @@ struct TempAllocatorPage
char[*] data;
}
macro usz TempAllocatorPage.pagesize(TempAllocatorPage* page) { return page.size & ~PAGE_IS_ALIGNED; }
macro bool TempAllocatorPage.is_aligned(TempAllocatorPage* page) { return page.size & PAGE_IS_ALIGNED == PAGE_IS_ALIGNED; }
macro usz TempAllocatorPage.pagesize(TempAllocatorPage* page) => page.size & ~PAGE_IS_ALIGNED;
macro bool TempAllocatorPage.is_aligned(TempAllocatorPage* page) => page.size & PAGE_IS_ALIGNED == PAGE_IS_ALIGNED;
/**
* @require size >= 16
@@ -86,7 +86,7 @@ fn void*! temp_allocator_function(Allocator* data, usz size, usz alignment, usz
unreachable();
}
fn void! TempAllocator._free(TempAllocator* this, void* old_pointer) @private
fn void! TempAllocator._free(TempAllocator* this, void* old_pointer) @local
{
// TODO fix free
assert((uptr)old_pointer >= (uptr)&this.data, "Pointer originates from a different allocator.");
@@ -96,7 +96,7 @@ fn void! TempAllocator._free(TempAllocator* this, void* old_pointer) @private
this.used -= old_size;
}
}
fn void! TempAllocator._reset(TempAllocator* this, usz mark) @private
fn void! TempAllocator._reset(TempAllocator* this, usz mark) @local
{
TempAllocatorPage *last_page = this.last_page;
while (last_page && last_page.mark > mark)
@@ -109,14 +109,14 @@ fn void! TempAllocator._reset(TempAllocator* this, usz mark) @private
this.used = mark;
}
fn void! TempAllocator._free_page(TempAllocator* this, TempAllocatorPage* page) @inline @private
fn void! TempAllocator._free_page(TempAllocator* this, TempAllocatorPage* page) @inline @local
{
void* mem = page.start;
if (page.is_aligned()) return this.backing_allocator.free_aligned(mem);
return this.backing_allocator.free(mem);
}
fn void*! TempAllocator._realloc_page(TempAllocator* this, TempAllocatorPage* page, usz size, usz alignment, usz offset) @inline @private
fn void*! TempAllocator._realloc_page(TempAllocator* this, TempAllocatorPage* page, usz size, usz alignment, usz offset) @inline @local
{
// Then the actual start pointer:
void* real_pointer = page.start;
@@ -144,7 +144,7 @@ fn void*! TempAllocator._realloc_page(TempAllocator* this, TempAllocatorPage* pa
return data;
}
fn void*! TempAllocator._realloc(TempAllocator* this, void* pointer, usz size, usz alignment, usz offset) @inline @private
fn void*! TempAllocator._realloc(TempAllocator* this, void* pointer, usz size, usz alignment, usz offset) @inline @local
{
TempAllocatorChunk *chunk = pointer - TempAllocatorChunk.sizeof;
if (chunk.size == (usz)-1)
@@ -170,7 +170,7 @@ fn void*! TempAllocator._realloc(TempAllocator* this, void* pointer, usz size, u
* @require alignment <= MAX_MEMORY_ALIGNMENT `alignment too big`
* @require this != null
**/
fn void*! TempAllocator._alloc(TempAllocator* this, usz size, usz alignment, usz offset, bool clear) @private
fn void*! TempAllocator._alloc(TempAllocator* this, usz size, usz alignment, usz offset, bool clear) @local
{
void* start_mem = &this.data;
void* starting_ptr = start_mem + this.used;

View File

@@ -88,7 +88,7 @@ fn void default_panic(String message, String file, String function, uint line)
$$trap();
}
define PanicFn = fn void(String message, String file, String function, uint line);
typedef PanicFn = fn void(String message, String file, String function, uint line);
PanicFn panic = &default_panic;

View File

@@ -18,70 +18,70 @@ $assert (C_LONG_SIZE <= C_LONG_LONG_SIZE);
$switch ($$C_INT_SIZE):
$case 64:
define CInt = long;
define CUInt = ulong;
typedef CInt = long;
typedef CUInt = ulong;
$case 32:
define CInt = int;
define CUInt = uint;
typedef CInt = int;
typedef CUInt = uint;
$case 16:
define CInt = short;
define CUInt = ushort;
typedef CInt = short;
typedef CUInt = ushort;
$default:
$assert(false, "Invalid C int size");
$endswitch;
$switch ($$C_LONG_SIZE):
$case 64:
define CLong = long;
define CULong = ulong;
typedef CLong = long;
typedef CULong = ulong;
$case 32:
define CLong = int;
define CULong = uint;
typedef CLong = int;
typedef CULong = uint;
$case 16:
define CLong = short;
define CULong = ushort;
typedef CLong = short;
typedef CULong = ushort;
$default:
$assert(false, "Invalid C long size");
$endswitch;
$switch ($$C_SHORT_SIZE):
$case 32:
define CShort = int;
define CUShort = uint;
typedef CShort = int;
typedef CUShort = uint;
$case 16:
define CShort = short;
define CUShort = ushort;
typedef CShort = short;
typedef CUShort = ushort;
$case 8:
define CShort = ichar;
define CUShort = char;
typedef CShort = ichar;
typedef CUShort = char;
$default:
$assert(false, "Invalid C short size");
$endswitch;
$switch ($$C_LONG_LONG_SIZE):
$case 128:
define CLongLong = int128;
define CULongLong = uint128;
typedef CLongLong = int128;
typedef CULongLong = uint128;
$case 64:
define CLongLong = long;
define CULongLong = ulong;
typedef CLongLong = long;
typedef CULongLong = ulong;
$case 32:
define CLongLong = int;
define CULongLong = uint;
typedef CLongLong = int;
typedef CULongLong = uint;
$case 16:
define CLongLong = short;
define CULongLong = ushort;
typedef CLongLong = short;
typedef CULongLong = ushort;
$default:
$assert(false, "Invalid C long long size");
$endswitch;
define CSChar = ichar;
define CUChar = char;
typedef CSChar = ichar;
typedef CUChar = char;
$if ($$C_CHAR_IS_SIGNED):
define CChar = ichar;
typedef CChar = ichar;
$else:
define CChar = char;
typedef CChar = char;
$endif;

View File

@@ -8,7 +8,7 @@ const DEFAULT_SIZE_PREFIX_ALIGNMENT = usz.alignof;
const Allocator* NULL_ALLOCATOR = &_NULL_ALLOCATOR;
const Allocator* LIBC_ALLOCATOR = &_SYSTEM_ALLOCATOR;
define AllocatorFunction = fn void*!(Allocator* allocator, usz new_size, usz alignment, usz offset, void* old_pointer, AllocationKind kind);
typedef AllocatorFunction = fn void*!(Allocator* allocator, usz new_size, usz alignment, usz offset, void* old_pointer, AllocationKind kind);
struct Allocator
{

View File

@@ -28,7 +28,7 @@ struct VarArrayHeader
void *allocator;
}
define TestFn = fn void!();
typedef TestFn = fn void!();
struct TestRunner
{

View File

@@ -1,9 +1,9 @@
module std::core::str;
define ZString = distinct char*;
define String = char[];
define Char32 = uint;
define Char16 = ushort;
typedef ZString = distinct char*;
typedef String = char[];
typedef Char32 = uint;
typedef Char16 = ushort;
const uint SURROGATE_OFFSET @private = 0x10000;
const uint SURROGATE_GENERIC_MASK @private = 0xF800;

View File

@@ -1,11 +1,11 @@
module std::core::string;
import libc;
define VarString = distinct void*;
define DynStr = VarString;
define DynString = VarString;
define DString = VarString;
define VString = VarString;
typedef VarString = distinct void*;
typedef DynStr = VarString;
typedef DynString = VarString;
typedef DString = VarString;
typedef VString = VarString;
const usz MIN_CAPACITY = 16;