mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Add usz and isz.
This commit is contained in:
committed by
Christoffer Lerno
parent
348495b4c8
commit
ab78663f3c
@@ -2,14 +2,14 @@ module std::core::mem::allocator;
|
||||
|
||||
struct ArenaAllocatorHeader
|
||||
{
|
||||
usize size;
|
||||
usz size;
|
||||
char[*] data;
|
||||
}
|
||||
/**
|
||||
* @require !alignment || math::is_power_of_2(alignment)
|
||||
* @require data `unexpectedly missing the allocator`
|
||||
*/
|
||||
private fn void*! arena_allocator_function(Allocator* data, usize size, usize alignment, usize offset, void* old_pointer, AllocationKind kind)
|
||||
private fn void*! arena_allocator_function(Allocator* data, usz size, usz alignment, usz offset, void* old_pointer, AllocationKind kind)
|
||||
{
|
||||
ArenaAllocator* arena = (ArenaAllocator*)data;
|
||||
bool clear = false;
|
||||
@@ -63,14 +63,14 @@ private fn void*! arena_allocator_function(Allocator* data, usize size, usize al
|
||||
* @require mem::aligned_offset(offset, ArenaAllocatorHeader.alignof) == offset
|
||||
* @require this != null
|
||||
**/
|
||||
private fn void*! ArenaAllocator._alloc(ArenaAllocator* this, usize size, usize alignment, usize offset)
|
||||
private fn void*! ArenaAllocator._alloc(ArenaAllocator* this, usz size, usz alignment, usz offset)
|
||||
{
|
||||
usize total_len = this.data.len;
|
||||
usz total_len = this.data.len;
|
||||
if (size > total_len) return AllocationFailure.CHUNK_TOO_LARGE!;
|
||||
void* start_mem = this.data.ptr;
|
||||
void* unaligned_pointer_to_offset = start_mem + this.used + ArenaAllocatorHeader.sizeof + offset;
|
||||
void* aligned_pointer_to_offset = mem::aligned_pointer(unaligned_pointer_to_offset, alignment);
|
||||
usize end = (usize)(aligned_pointer_to_offset - this.data.ptr) + size - offset;
|
||||
usz end = (usz)(aligned_pointer_to_offset - this.data.ptr) + size - offset;
|
||||
if (end > total_len) return AllocationFailure.OUT_OF_MEMORY!;
|
||||
this.used = end;
|
||||
void *mem = aligned_pointer_to_offset - offset;
|
||||
@@ -89,13 +89,13 @@ private fn void*! ArenaAllocator._alloc(ArenaAllocator* this, usize size, usize
|
||||
* @require mem::aligned_offset(offset, ArenaAllocatorHeader.alignof) == offset
|
||||
* @require this != null
|
||||
**/
|
||||
private fn void*! ArenaAllocator._realloc(ArenaAllocator* this, void *old_pointer, usize size, usize alignment, usize offset)
|
||||
private fn void*! ArenaAllocator._realloc(ArenaAllocator* this, void *old_pointer, usz size, usz alignment, usz offset)
|
||||
{
|
||||
assert(old_pointer >= this.data.ptr, "Pointer originates from a different allocator.");
|
||||
usize total_len = this.data.len;
|
||||
usz total_len = this.data.len;
|
||||
if (size > total_len) return AllocationFailure.CHUNK_TOO_LARGE!;
|
||||
ArenaAllocatorHeader* header = old_pointer - ArenaAllocatorHeader.sizeof;
|
||||
usize old_size = header.size;
|
||||
usz old_size = header.size;
|
||||
// Do last allocation and alignment match?
|
||||
if (&this.data[this.used] == old_pointer + old_size && mem::ptr_is_aligned(old_pointer + offset, alignment))
|
||||
{
|
||||
@@ -105,7 +105,7 @@ private fn void*! ArenaAllocator._realloc(ArenaAllocator* this, void *old_pointe
|
||||
}
|
||||
else
|
||||
{
|
||||
usize new_used = this.used + size - old_size;
|
||||
usz new_used = this.used + size - old_size;
|
||||
if (new_used > total_len) return AllocationFailure.OUT_OF_MEMORY!;
|
||||
this.used = new_used;
|
||||
}
|
||||
|
||||
@@ -4,14 +4,14 @@ private struct DynamicArenaPage
|
||||
{
|
||||
void* memory;
|
||||
void* prev_arena;
|
||||
usize total;
|
||||
usize used;
|
||||
usz total;
|
||||
usz used;
|
||||
void* last_ptr;
|
||||
}
|
||||
|
||||
private struct DynamicArenaChunk
|
||||
{
|
||||
usize size;
|
||||
usz size;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -23,7 +23,7 @@ private fn void DynamicArenaAllocator.free(DynamicArenaAllocator* this, void* pt
|
||||
DynamicArenaPage* current_page = this.page;
|
||||
if (ptr == current_page.last_ptr)
|
||||
{
|
||||
current_page.used = (usize)((ptr - DEFAULT_SIZE_PREFIX) - current_page.memory);
|
||||
current_page.used = (usz)((ptr - DEFAULT_SIZE_PREFIX) - current_page.memory);
|
||||
}
|
||||
current_page.last_ptr = null;
|
||||
}
|
||||
@@ -32,26 +32,26 @@ private fn void DynamicArenaAllocator.free(DynamicArenaAllocator* this, void* pt
|
||||
* @require old_pointer && size > 0
|
||||
* @require this.page `tried to realloc pointer on invalid allocator`
|
||||
*/
|
||||
private fn void*! DynamicArenaAllocator._realloc(DynamicArenaAllocator* this, void* old_pointer, usize size, usize alignment, usize offset)
|
||||
private fn void*! DynamicArenaAllocator._realloc(DynamicArenaAllocator* this, void* old_pointer, usz size, usz alignment, usz offset)
|
||||
{
|
||||
DynamicArenaPage* current_page = this.page;
|
||||
alignment = alignment_for_allocation(alignment);
|
||||
usize* old_size_ptr = old_pointer - DEFAULT_SIZE_PREFIX;
|
||||
usize old_size = *old_size_ptr;
|
||||
usz* old_size_ptr = old_pointer - DEFAULT_SIZE_PREFIX;
|
||||
usz old_size = *old_size_ptr;
|
||||
// We have the old pointer and it's correctly aligned.
|
||||
if (old_size >= size && mem::ptr_is_aligned(old_pointer, alignment))
|
||||
{
|
||||
*old_size_ptr = size;
|
||||
if (current_page.last_ptr == old_pointer)
|
||||
{
|
||||
current_page.used = (usize)((old_pointer - DEFAULT_SIZE_PREFIX) - current_page.memory);
|
||||
current_page.used = (usz)((old_pointer - DEFAULT_SIZE_PREFIX) - current_page.memory);
|
||||
}
|
||||
return old_pointer;
|
||||
}
|
||||
if REUSE: (current_page.last_ptr == old_pointer && mem::ptr_is_aligned(old_pointer, alignment))
|
||||
{
|
||||
assert(size > old_size);
|
||||
usize add_size = size - old_size;
|
||||
usz add_size = size - old_size;
|
||||
if (add_size + current_page.used > current_page.total) break REUSE;
|
||||
*old_size_ptr = size;
|
||||
current_page.used += add_size;
|
||||
@@ -82,10 +82,10 @@ private fn void DynamicArenaAllocator.reset(DynamicArenaAllocator* this)
|
||||
* @require math::is_power_of_2(alignment)
|
||||
* @require size > 0
|
||||
*/
|
||||
private fn void*! DynamicArenaAllocator._alloc_new(DynamicArenaAllocator* this, usize size, usize alignment, usize offset)
|
||||
private fn void*! DynamicArenaAllocator._alloc_new(DynamicArenaAllocator* this, usz size, usz alignment, usz offset)
|
||||
{
|
||||
// First, make sure that we can align it, extending the page size if needed.
|
||||
usize page_size = max(this.page_size, mem::aligned_offset(size + DynamicArenaChunk.sizeof + offset, alignment) - offset);
|
||||
usz page_size = max(this.page_size, mem::aligned_offset(size + DynamicArenaChunk.sizeof + offset, alignment) - offset);
|
||||
|
||||
// Grab the page without alignment (we do it ourselves)
|
||||
void* mem = this.backing_allocator.alloc(page_size)?;
|
||||
@@ -113,7 +113,7 @@ private fn void*! DynamicArenaAllocator._alloc_new(DynamicArenaAllocator* this,
|
||||
* @require size > 0
|
||||
* @require this
|
||||
*/
|
||||
private fn void*! DynamicArenaAllocator._alloc(DynamicArenaAllocator* this, usize size, usize alignment, usize offset)
|
||||
private fn void*! DynamicArenaAllocator._alloc(DynamicArenaAllocator* this, usz size, usz alignment, usz offset)
|
||||
{
|
||||
alignment = alignment_for_allocation(alignment);
|
||||
DynamicArenaPage* page = this.page;
|
||||
@@ -125,7 +125,7 @@ private fn void*! DynamicArenaAllocator._alloc(DynamicArenaAllocator* this, usiz
|
||||
}
|
||||
if (!page) return this._alloc_new(size, alignment, offset);
|
||||
void* start = mem::aligned_pointer(page.memory + page.used + DynamicArenaChunk.sizeof + offset, alignment) - offset;
|
||||
usize new_used = start - page.memory + size;
|
||||
usz new_used = start - page.memory + size;
|
||||
if ALLOCATE_NEW: (new_used > page.total)
|
||||
{
|
||||
if ((page = this.unused_page))
|
||||
@@ -155,7 +155,7 @@ private fn void*! DynamicArenaAllocator._alloc(DynamicArenaAllocator* this, usiz
|
||||
* @require !alignment || math::is_power_of_2(alignment)
|
||||
* @require data `unexpectedly missing the allocator`
|
||||
*/
|
||||
private fn void*! dynamic_arena_allocator_function(Allocator* data, usize size, usize alignment, usize offset, void* old_pointer, AllocationKind kind)
|
||||
private fn void*! dynamic_arena_allocator_function(Allocator* data, usz size, usz alignment, usz offset, void* old_pointer, AllocationKind kind)
|
||||
{
|
||||
DynamicArenaAllocator* allocator = (DynamicArenaAllocator*)data;
|
||||
switch (kind)
|
||||
|
||||
@@ -4,7 +4,7 @@ import libc;
|
||||
private const Allocator _NULL_ALLOCATOR = { &null_allocator_fn };
|
||||
private const Allocator _SYSTEM_ALLOCATOR = { &libc_allocator_fn };
|
||||
|
||||
private fn void*! null_allocator_fn(Allocator* this, usize bytes, usize alignment, usize offset, void* old_pointer, AllocationKind kind)
|
||||
private fn void*! null_allocator_fn(Allocator* this, usz bytes, usz alignment, usz offset, void* old_pointer, AllocationKind kind)
|
||||
{
|
||||
switch (kind)
|
||||
{
|
||||
@@ -22,7 +22,7 @@ private fn void*! null_allocator_fn(Allocator* this, usize bytes, usize alignmen
|
||||
|
||||
private struct AlignedBlock
|
||||
{
|
||||
usize len;
|
||||
usz len;
|
||||
void* start;
|
||||
}
|
||||
|
||||
@@ -31,9 +31,9 @@ private struct AlignedBlock
|
||||
* @require bytes > 0
|
||||
* @require alignment > 0
|
||||
**/
|
||||
private fn void* _libc_aligned_alloc(usize bytes, usize alignment, usize offset) @inline
|
||||
private fn void* _libc_aligned_alloc(usz bytes, usz alignment, usz offset) @inline
|
||||
{
|
||||
usize header = mem::aligned_offset(AlignedBlock.sizeof + offset, alignment) - offset;
|
||||
usz header = mem::aligned_offset(AlignedBlock.sizeof + offset, alignment) - offset;
|
||||
void* data = libc::malloc(header + bytes);
|
||||
void* mem = mem::aligned_pointer(data + offset, alignment) - offset;
|
||||
assert(mem > data);
|
||||
@@ -46,9 +46,9 @@ private fn void* _libc_aligned_alloc(usize bytes, usize alignment, usize offset)
|
||||
* @require bytes > 0
|
||||
* @require alignment > 0
|
||||
**/
|
||||
private fn void* _libc_aligned_calloc(usize bytes, usize alignment, usize offset) @inline
|
||||
private fn void* _libc_aligned_calloc(usz bytes, usz alignment, usz offset) @inline
|
||||
{
|
||||
usize header = mem::aligned_offset(AlignedBlock.sizeof + offset, alignment) - offset;
|
||||
usz header = mem::aligned_offset(AlignedBlock.sizeof + offset, alignment) - offset;
|
||||
void* data = libc::calloc(header + bytes, 1);
|
||||
void* mem = mem::aligned_pointer(data + offset, alignment) - offset;
|
||||
AlignedBlock* desc = (AlignedBlock*)mem - 1;
|
||||
@@ -61,7 +61,7 @@ private fn void* _libc_aligned_calloc(usize bytes, usize alignment, usize offset
|
||||
* @require bytes > 0
|
||||
* @require alignment > 0
|
||||
**/
|
||||
private fn void* _libc_aligned_realloc(void* old_pointer, usize bytes, usize alignment, usize offset) @inline
|
||||
private fn void* _libc_aligned_realloc(void* old_pointer, usz bytes, usz alignment, usz offset) @inline
|
||||
{
|
||||
AlignedBlock* desc = (AlignedBlock*)old_pointer - 1;
|
||||
void* data_start = desc.start;
|
||||
@@ -77,7 +77,7 @@ private fn void _libc_aligned_free(void* old_pointer) @inline
|
||||
libc::free(desc.start);
|
||||
}
|
||||
|
||||
fn void*! libc_allocator_fn(Allocator* unused, usize bytes, usize alignment, usize offset, void* old_pointer, AllocationKind kind) @inline
|
||||
fn void*! libc_allocator_fn(Allocator* unused, usz bytes, usz alignment, usz offset, void* old_pointer, AllocationKind kind) @inline
|
||||
{
|
||||
if (!alignment) alignment = DEFAULT_MEM_ALIGNMENT;
|
||||
assert(math::is_power_of_2(alignment), "Alignment was not a power of 2");
|
||||
|
||||
@@ -3,7 +3,7 @@ import std::io;
|
||||
|
||||
private struct TempAllocatorChunk
|
||||
{
|
||||
usize size;
|
||||
usz size;
|
||||
char[*] data;
|
||||
}
|
||||
|
||||
@@ -12,32 +12,32 @@ struct TempAllocator
|
||||
inline Allocator allocator;
|
||||
Allocator* backing_allocator;
|
||||
TempAllocatorPage* last_page;
|
||||
usize used;
|
||||
usize capacity;
|
||||
usz used;
|
||||
usz capacity;
|
||||
char[*] data;
|
||||
}
|
||||
|
||||
|
||||
private const usize PAGE_IS_ALIGNED = (usize)isize.max + 1;
|
||||
private const usz PAGE_IS_ALIGNED = (usz)isz.max + 1;
|
||||
|
||||
|
||||
struct TempAllocatorPage
|
||||
{
|
||||
TempAllocatorPage* prev_page;
|
||||
void* start;
|
||||
usize mark;
|
||||
usize size;
|
||||
usize ident;
|
||||
usz mark;
|
||||
usz size;
|
||||
usz ident;
|
||||
char[*] data;
|
||||
}
|
||||
|
||||
macro usize TempAllocatorPage.pagesize(TempAllocatorPage* page) { return page.size & ~PAGE_IS_ALIGNED; }
|
||||
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; }
|
||||
|
||||
/**
|
||||
* @require size >= 16
|
||||
**/
|
||||
fn TempAllocator*! new_temp(usize size, Allocator* backing_allocator)
|
||||
fn TempAllocator*! new_temp(usz size, Allocator* backing_allocator)
|
||||
{
|
||||
TempAllocator* allocator = backing_allocator.alloc(size + TempAllocator.sizeof)?;
|
||||
allocator.last_page = null;
|
||||
@@ -52,7 +52,7 @@ fn TempAllocator*! new_temp(usize size, Allocator* backing_allocator)
|
||||
* @require !alignment || math::is_power_of_2(alignment)
|
||||
* @require data `unexpectedly missing the allocator`
|
||||
*/
|
||||
private fn void*! temp_allocator_function(Allocator* data, usize size, usize alignment, usize offset, void* old_pointer, AllocationKind kind)
|
||||
private fn void*! temp_allocator_function(Allocator* data, usz size, usz alignment, usz offset, void* old_pointer, AllocationKind kind)
|
||||
{
|
||||
TempAllocator* arena = (TempAllocator*)data;
|
||||
switch (kind)
|
||||
@@ -92,13 +92,13 @@ private fn void! TempAllocator._free(TempAllocator* this, void* old_pointer)
|
||||
|
||||
// TODO fix free
|
||||
assert((uptr)old_pointer >= (uptr)&this.data, "Pointer originates from a different allocator.");
|
||||
usize old_size = *(usize*)(old_pointer - DEFAULT_SIZE_PREFIX);
|
||||
usz old_size = *(usz*)(old_pointer - DEFAULT_SIZE_PREFIX);
|
||||
if (old_pointer + old_size == &this.data[this.used])
|
||||
{
|
||||
this.used -= old_size;
|
||||
}
|
||||
}
|
||||
private fn void! TempAllocator._reset(TempAllocator* this, usize mark)
|
||||
private fn void! TempAllocator._reset(TempAllocator* this, usz mark)
|
||||
{
|
||||
TempAllocatorPage *last_page = this.last_page;
|
||||
while (last_page && last_page.mark > mark)
|
||||
@@ -118,7 +118,7 @@ private fn void! TempAllocator._free_page(TempAllocator* this, TempAllocatorPage
|
||||
return this.backing_allocator.free(mem);
|
||||
}
|
||||
|
||||
private fn void*! TempAllocator._realloc_page(TempAllocator* this, TempAllocatorPage* page, usize size, usize alignment, usize offset) @inline
|
||||
private fn void*! TempAllocator._realloc_page(TempAllocator* this, TempAllocatorPage* page, usz size, usz alignment, usz offset) @inline
|
||||
{
|
||||
// Then the actual start pointer:
|
||||
void* real_pointer = page.start;
|
||||
@@ -131,7 +131,7 @@ private fn void*! TempAllocator._realloc_page(TempAllocator* this, TempAllocator
|
||||
pointer_to_prev = &((*pointer_to_prev).prev_page);
|
||||
}
|
||||
*pointer_to_prev = page.prev_page;
|
||||
usize page_size = page.pagesize();
|
||||
usz page_size = page.pagesize();
|
||||
// Clear on size > original size.
|
||||
void* data = this._alloc(size, alignment, offset, false)?;
|
||||
mem::copy(data, &page.data[0], page_size, DEFAULT_MEM_ALIGNMENT, DEFAULT_MEM_ALIGNMENT);
|
||||
@@ -146,10 +146,10 @@ private fn void*! TempAllocator._realloc_page(TempAllocator* this, TempAllocator
|
||||
return data;
|
||||
}
|
||||
|
||||
private fn void*! TempAllocator._realloc(TempAllocator* this, void* pointer, usize size, usize alignment, usize offset) @inline
|
||||
private fn void*! TempAllocator._realloc(TempAllocator* this, void* pointer, usz size, usz alignment, usz offset) @inline
|
||||
{
|
||||
TempAllocatorChunk *chunk = pointer - TempAllocatorChunk.sizeof;
|
||||
if (chunk.size == (usize)-1)
|
||||
if (chunk.size == (usz)-1)
|
||||
{
|
||||
assert(this.last_page, "Realloc of non temp pointer");
|
||||
// First grab the page
|
||||
@@ -172,7 +172,7 @@ private fn void*! TempAllocator._realloc(TempAllocator* this, void* pointer, usi
|
||||
* @require alignment <= MAX_MEMORY_ALIGNMENT `alignment too big`
|
||||
* @require this != null
|
||||
**/
|
||||
private fn void*! TempAllocator._alloc(TempAllocator* this, usize size, usize alignment, usize offset, bool clear)
|
||||
private fn void*! TempAllocator._alloc(TempAllocator* this, usz size, usz alignment, usz offset, bool clear)
|
||||
{
|
||||
void* start_mem = &this.data;
|
||||
void* starting_ptr = start_mem + this.used;
|
||||
@@ -182,7 +182,7 @@ private fn void*! TempAllocator._alloc(TempAllocator* this, usize size, usize al
|
||||
{
|
||||
mem = mem::aligned_pointer(mem + offset, alignment) - offset;
|
||||
}
|
||||
usize new_usage = (usize)(mem - start_mem) + size;
|
||||
usz new_usage = (usz)(mem - start_mem) + size;
|
||||
|
||||
// Arena alignment, simple!
|
||||
if (new_usage <= this.capacity)
|
||||
@@ -201,7 +201,7 @@ private fn void*! TempAllocator._alloc(TempAllocator* this, usize size, usize al
|
||||
if (alignment > DEFAULT_MEM_ALIGNMENT || offset)
|
||||
{
|
||||
// This is actually simpler, since it will create the offset for us.
|
||||
usize total_alloc_size = TempAllocatorPage.sizeof + size;
|
||||
usz total_alloc_size = TempAllocatorPage.sizeof + size;
|
||||
if (clear)
|
||||
{
|
||||
page = this.backing_allocator.calloc_aligned(total_alloc_size, alignment, TempAllocatorPage.sizeof + offset)?;
|
||||
@@ -216,8 +216,8 @@ private fn void*! TempAllocator._alloc(TempAllocator* this, usize size, usize al
|
||||
else
|
||||
{
|
||||
// Here we might need to pad
|
||||
usize padded_header_size = mem::aligned_offset(TempAllocatorPage.sizeof, DEFAULT_MEM_ALIGNMENT);
|
||||
usize total_alloc_size = padded_header_size + size;
|
||||
usz padded_header_size = mem::aligned_offset(TempAllocatorPage.sizeof, DEFAULT_MEM_ALIGNMENT);
|
||||
usz total_alloc_size = padded_header_size + size;
|
||||
void* alloc = (clear ? this.backing_allocator.calloc(total_alloc_size) : this.backing_allocator.alloc(total_alloc_size))?;
|
||||
|
||||
// Find the page.
|
||||
@@ -229,7 +229,7 @@ private fn void*! TempAllocator._alloc(TempAllocator* this, usize size, usize al
|
||||
}
|
||||
|
||||
// Mark it as a page
|
||||
page.ident = ~(usize)0;
|
||||
page.ident = ~(usz)0;
|
||||
// Store when it was created
|
||||
page.mark = ++this.used;
|
||||
// Hook up the page.
|
||||
|
||||
@@ -95,7 +95,7 @@ macro void unreachable($string = "Unreachable statement reached.") @builtin @nor
|
||||
|
||||
macro bitcast(expr, $Type) @builtin
|
||||
{
|
||||
var $size = (usize)($sizeof(expr));
|
||||
var $size = (usz)($sizeof(expr));
|
||||
$assert($size == $Type.sizeof, "Cannot bitcast between types of different size.");
|
||||
$Type x = void;
|
||||
mem::copy(&x, &expr, $size, $Type.alignof, $alignof(expr));
|
||||
|
||||
@@ -14,7 +14,7 @@ private const uint UTF16_SURROGATE_HIGH_VALUE = 0xD800;
|
||||
* @param [out] output `the resulting buffer`
|
||||
* @param available `the size available`
|
||||
**/
|
||||
fn usize! char32_to_utf8(Char32 c, char* output, usize available)
|
||||
fn usz! char32_to_utf8(Char32 c, char* output, usz available)
|
||||
{
|
||||
if (!available) return UnicodeResult.CONVERSION_FAILED!;
|
||||
switch (true)
|
||||
@@ -71,7 +71,7 @@ fn void char32_to_utf16_unsafe(Char32 c, Char16** output)
|
||||
* @param [inout] available `amount of UTF16 data available.`
|
||||
* @param [inout] output `the resulting utf8 buffer to write to.`
|
||||
**/
|
||||
fn void! char16_to_utf8_unsafe(Char16 *ptr, usize *available, char** output)
|
||||
fn void! char16_to_utf8_unsafe(Char16 *ptr, usz *available, char** output)
|
||||
{
|
||||
Char16 high = *ptr;
|
||||
if (high & UTF16_SURROGATE_GENERIC_MASK != UTF16_SURROGATE_GENERIC_VALUE)
|
||||
@@ -128,9 +128,9 @@ fn void char32_to_utf8_unsafe(Char32 c, char** output)
|
||||
* @param [inout] size `Set to max characters to read, set to characters read`
|
||||
* @return `the parsed 32 bit codepoint`
|
||||
**/
|
||||
fn Char32! utf8_to_char32(char* ptr, usize* size)
|
||||
fn Char32! utf8_to_char32(char* ptr, usz* size)
|
||||
{
|
||||
usize max_size = *size;
|
||||
usz max_size = *size;
|
||||
if (max_size < 1) return UnicodeResult.INVALID_UTF8!;
|
||||
char c = (ptr++)[0];
|
||||
|
||||
@@ -178,9 +178,9 @@ fn Char32! utf8_to_char32(char* ptr, usize* size)
|
||||
* @param utf8 `An UTF-8 encoded slice of bytes`
|
||||
* @return `the number of encoded code points`
|
||||
**/
|
||||
fn usize utf8_codepoints(char[] utf8)
|
||||
fn usz utf8_codepoints(char[] utf8)
|
||||
{
|
||||
usize len = 0;
|
||||
usz len = 0;
|
||||
foreach (char c : utf8)
|
||||
{
|
||||
if (c & 0xC0 != 0x80) len++;
|
||||
@@ -193,9 +193,9 @@ fn usize utf8_codepoints(char[] utf8)
|
||||
* @param [in] utf32 `the utf32 data to calculate from`
|
||||
* @return `the length of the resulting UTF8 array`
|
||||
**/
|
||||
fn usize utf8len_for_utf32(Char32[] utf32)
|
||||
fn usz utf8len_for_utf32(Char32[] utf32)
|
||||
{
|
||||
usize len = 0;
|
||||
usz len = 0;
|
||||
foreach (Char32 uc : utf32)
|
||||
{
|
||||
switch (true)
|
||||
@@ -218,11 +218,11 @@ fn usize utf8len_for_utf32(Char32[] utf32)
|
||||
* @param [in] utf16 `the utf16 data to calculate from`
|
||||
* @return `the length of the resulting UTF8 array`
|
||||
**/
|
||||
fn usize utf8len_for_utf16(Char16[] utf16)
|
||||
fn usz utf8len_for_utf16(Char16[] utf16)
|
||||
{
|
||||
usize len = 0;
|
||||
usize len16 = utf16.len;
|
||||
for (usize i = 0; i < len16; i++)
|
||||
usz len = 0;
|
||||
usz len16 = utf16.len;
|
||||
for (usz i = 0; i < len16; i++)
|
||||
{
|
||||
Char16 c = utf16[i];
|
||||
if (c & UTF16_SURROGATE_GENERIC_MASK != UTF16_SURROGATE_GENERIC_VALUE)
|
||||
@@ -250,11 +250,11 @@ fn usize utf8len_for_utf16(Char16[] utf16)
|
||||
* @param utf8 `the utf8 data to calculate from`
|
||||
* @return `the length of the resulting UTF16 array`
|
||||
**/
|
||||
fn usize utf16len_for_utf8(char[] utf8)
|
||||
fn usz utf16len_for_utf8(char[] utf8)
|
||||
{
|
||||
usize len = utf8.len;
|
||||
usize len16 = 0;
|
||||
for (usize i = 0; i < len; i++)
|
||||
usz len = utf8.len;
|
||||
usz len16 = 0;
|
||||
for (usz i = 0; i < len; i++)
|
||||
{
|
||||
len16++;
|
||||
char c = utf8[i];
|
||||
@@ -273,9 +273,9 @@ fn usize utf16len_for_utf8(char[] utf8)
|
||||
* @param [in] utf32 `the UTF32 array to check the length for`
|
||||
* @return `the required length of an UTF16 array to hold the UTF32 data.`
|
||||
**/
|
||||
fn usize utf16len_for_utf32(Char32[] utf32)
|
||||
fn usz utf16len_for_utf32(Char32[] utf32)
|
||||
{
|
||||
usize len = utf32.len;
|
||||
usz len = utf32.len;
|
||||
foreach (Char32 uc : utf32)
|
||||
{
|
||||
if (uc >= UTF16_SURROGATE_OFFSET) len++;
|
||||
@@ -290,13 +290,13 @@ fn usize utf16len_for_utf32(Char32[] utf32)
|
||||
* @param [out] utf8_buffer
|
||||
* @return `the number of bytes written.`
|
||||
**/
|
||||
fn usize! utf32to8(Char32[] utf32, char[] utf8_buffer)
|
||||
fn usz! utf32to8(Char32[] utf32, char[] utf8_buffer)
|
||||
{
|
||||
usize len = utf8_buffer.len;
|
||||
usz len = utf8_buffer.len;
|
||||
char* ptr = utf8_buffer.ptr;
|
||||
foreach (Char32 uc : utf32)
|
||||
{
|
||||
usize used = char32_to_utf8(uc, ptr, len) @inline?;
|
||||
usz used = char32_to_utf8(uc, ptr, len) @inline?;
|
||||
len -= used;
|
||||
ptr += used;
|
||||
}
|
||||
@@ -310,16 +310,16 @@ fn usize! utf32to8(Char32[] utf32, char[] utf8_buffer)
|
||||
* @param [out] utf32_buffer
|
||||
* @return `the number of Char32s written.`
|
||||
**/
|
||||
fn usize! utf8to32(char[] utf8, Char32[] utf32_buffer)
|
||||
fn usz! utf8to32(char[] utf8, Char32[] utf32_buffer)
|
||||
{
|
||||
usize len = utf8.len;
|
||||
usz len = utf8.len;
|
||||
Char32* ptr = utf32_buffer.ptr;
|
||||
usize len32 = 0;
|
||||
usize buf_len = utf32_buffer.len;
|
||||
for (usize i = 0; i < len;)
|
||||
usz len32 = 0;
|
||||
usz buf_len = utf32_buffer.len;
|
||||
for (usz i = 0; i < len;)
|
||||
{
|
||||
if (len32 == buf_len) return UnicodeResult.CONVERSION_FAILED!;
|
||||
usize width = len - i;
|
||||
usz width = len - i;
|
||||
Char32 uc = utf8_to_char32(&utf8[i], &width) @inline?;
|
||||
i += width;
|
||||
ptr[len32++] = uc;
|
||||
@@ -337,10 +337,10 @@ fn usize! utf8to32(char[] utf8, Char32[] utf32_buffer)
|
||||
**/
|
||||
fn void! utf16to8_unsafe(Char16[] utf16, char* utf8_buffer)
|
||||
{
|
||||
usize len16 = utf16.len;
|
||||
for (usize i = 0; i < len16;)
|
||||
usz len16 = utf16.len;
|
||||
for (usz i = 0; i < len16;)
|
||||
{
|
||||
usize available = len16 - i;
|
||||
usz available = len16 - i;
|
||||
char16_to_utf8_unsafe(&utf16[i], &available, &utf8_buffer) @inline?;
|
||||
i += available;
|
||||
}
|
||||
@@ -356,10 +356,10 @@ fn void! utf16to8_unsafe(Char16[] utf16, char* utf8_buffer)
|
||||
**/
|
||||
fn void! utf8to32_unsafe(char[] utf8, Char32* utf32_buffer)
|
||||
{
|
||||
usize len = utf8.len;
|
||||
for (usize i = 0; i < len;)
|
||||
usz len = utf8.len;
|
||||
for (usz i = 0; i < len;)
|
||||
{
|
||||
usize width = len - i;
|
||||
usz width = len - i;
|
||||
Char32 uc = utf8_to_char32(&utf8[i], &width) @inline?;
|
||||
i += width;
|
||||
(utf32_buffer++)[0] = uc;
|
||||
@@ -376,10 +376,10 @@ fn void! utf8to32_unsafe(char[] utf8, Char32* utf32_buffer)
|
||||
**/
|
||||
fn void! utf8to16_unsafe(char[] utf8, Char16* utf16_buffer)
|
||||
{
|
||||
usize len = utf8.len;
|
||||
for (usize i = 0; i < len;)
|
||||
usz len = utf8.len;
|
||||
for (usz i = 0; i < len;)
|
||||
{
|
||||
usize width = len - i;
|
||||
usz width = len - i;
|
||||
Char32 uc = utf8_to_char32(&utf8[i], &width) @inline?;
|
||||
char32_to_utf16_unsafe(uc, &utf16_buffer) @inline;
|
||||
i += width;
|
||||
|
||||
@@ -59,4 +59,4 @@ const bool I128_SUPPORT = $$PLATFORM_I128_SUPPORTED;
|
||||
const bool F128_SUPPORT = $$PLATFORM_F128_SUPPORTED;
|
||||
const bool F16_SUPPORT = $$PLATFORM_F16_SUPPORTED;
|
||||
const bool COMPILER_SAFE_MODE = $$COMPILER_SAFE_MODE;
|
||||
const usize TEMP_ALLOCATOR_SIZE = 128 * 1024;
|
||||
const usz TEMP_ALLOCATOR_SIZE = 128 * 1024;
|
||||
@@ -16,12 +16,12 @@ macro @volatile_store(&x, y)
|
||||
/**
|
||||
* @require math::is_power_of_2(alignment)
|
||||
**/
|
||||
fn usize aligned_offset(usize offset, usize alignment)
|
||||
fn usz aligned_offset(usz offset, usz alignment)
|
||||
{
|
||||
return alignment * ((offset + alignment - 1) / alignment);
|
||||
}
|
||||
|
||||
macro void* aligned_pointer(void* ptr, usize alignment)
|
||||
macro void* aligned_pointer(void* ptr, usz alignment)
|
||||
{
|
||||
return (void*)(uptr)aligned_offset((uptr)ptr, alignment);
|
||||
}
|
||||
@@ -30,27 +30,27 @@ macro void* aligned_pointer(void* ptr, usize alignment)
|
||||
/**
|
||||
* @require math::is_power_of_2(alignment)
|
||||
**/
|
||||
fn bool ptr_is_aligned(void* ptr, usize alignment) @inline
|
||||
fn bool ptr_is_aligned(void* ptr, usz alignment) @inline
|
||||
{
|
||||
return (uptr)ptr & ((uptr)alignment - 1) == 0;
|
||||
}
|
||||
|
||||
macro void copy(void* dst, void* src, usize len, usize $dst_align = 0, usize $src_align = 0, bool $is_volatile = false)
|
||||
macro void copy(void* dst, void* src, usz len, usz $dst_align = 0, usz $src_align = 0, bool $is_volatile = false)
|
||||
{
|
||||
$$memcpy(dst, src, len, $is_volatile, $dst_align, $src_align);
|
||||
}
|
||||
|
||||
macro void set(void* dst, char val, usize len, usize $dst_align = 0, bool $is_volatile = false)
|
||||
macro void set(void* dst, char val, usz len, usz $dst_align = 0, bool $is_volatile = false)
|
||||
{
|
||||
$$memset(dst, val, len, $is_volatile, $dst_align);
|
||||
}
|
||||
|
||||
macro void move(void* dst, void* src, usize len, usize $dst_align = 0, usize $src_align = 0, bool $is_volatile = false)
|
||||
macro void move(void* dst, void* src, usz len, usz $dst_align = 0, usz $src_align = 0, bool $is_volatile = false)
|
||||
{
|
||||
$$memmove(dst, src, len, $is_volatile, $dst_align, $src_align);
|
||||
}
|
||||
|
||||
macro void clear(void* dst, usize len, usize $dst_align = 0, bool $is_volatile = false)
|
||||
macro void clear(void* dst, usz len, usz $dst_align = 0, bool $is_volatile = false)
|
||||
{
|
||||
$$memset(dst, (char)0, len, $is_volatile, $dst_align);
|
||||
}
|
||||
@@ -62,7 +62,7 @@ macro void clear(void* dst, usize len, usize $dst_align = 0, bool $is_volatile =
|
||||
* @require $typeof(a).kindof != TypeKind.POINTER || len > -1
|
||||
* @checked (a = b), (b = a)
|
||||
**/
|
||||
macro bool equals(a, b, isize len = -1, usize $align = 0)
|
||||
macro bool equals(a, b, isz len = -1, usz $align = 0)
|
||||
{
|
||||
$if (!$align):
|
||||
$align = $typeof(a[0]).alignof;
|
||||
@@ -93,13 +93,13 @@ macro bool equals(a, b, isize len = -1, usize $align = 0)
|
||||
var $Type = ulong;
|
||||
$endswitch;
|
||||
var $step = $Type.sizeof;
|
||||
usize end = len / $step;
|
||||
for (usize i = 0; i < end; i++)
|
||||
usz end = len / $step;
|
||||
for (usz 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++)
|
||||
usz last = len % $align;
|
||||
for (usz i = len - last; i < len; i++)
|
||||
{
|
||||
if (((char*)x)[i] != ((char*)y)[i]) return false;
|
||||
}
|
||||
@@ -120,12 +120,12 @@ macro @tclone(&value) @builtin
|
||||
return x;
|
||||
}
|
||||
|
||||
fn void* malloc(usize size) @builtin @inline
|
||||
fn void* malloc(usz size) @builtin @inline
|
||||
{
|
||||
return thread_allocator.alloc(size)!!;
|
||||
}
|
||||
|
||||
fn void*! malloc_checked(usize size) @builtin @inline
|
||||
fn void*! malloc_checked(usz size) @builtin @inline
|
||||
{
|
||||
return thread_allocator.alloc(size);
|
||||
}
|
||||
@@ -133,12 +133,12 @@ fn void*! malloc_checked(usize size) @builtin @inline
|
||||
/**
|
||||
* @require alignment && math::is_power_of_2(alignment)
|
||||
*/
|
||||
fn void*! malloc_aligned(usize size, usize alignment) @builtin @inline
|
||||
fn void*! malloc_aligned(usz size, usz alignment) @builtin @inline
|
||||
{
|
||||
return thread_allocator.alloc_aligned(size, alignment);
|
||||
}
|
||||
|
||||
fn char[] alloc_bytes(usize bytes) @inline
|
||||
fn char[] alloc_bytes(usz bytes) @inline
|
||||
{
|
||||
return ((char*)thread_allocator.alloc(bytes))[:bytes]!!;
|
||||
}
|
||||
@@ -149,12 +149,12 @@ macro alloc($Type)
|
||||
}
|
||||
|
||||
|
||||
fn void* calloc(usize size) @builtin @inline
|
||||
fn void* calloc(usz size) @builtin @inline
|
||||
{
|
||||
return thread_allocator.calloc(size)!!;
|
||||
}
|
||||
|
||||
fn void*! calloc_checked(usize size) @builtin @inline
|
||||
fn void*! calloc_checked(usz size) @builtin @inline
|
||||
{
|
||||
return thread_allocator.calloc(size);
|
||||
}
|
||||
@@ -162,17 +162,17 @@ fn void*! calloc_checked(usize size) @builtin @inline
|
||||
/**
|
||||
* @require alignment && math::is_power_of_2(alignment)
|
||||
*/
|
||||
fn void*! calloc_aligned(usize size, usize alignment) @builtin @inline
|
||||
fn void*! calloc_aligned(usz size, usz alignment) @builtin @inline
|
||||
{
|
||||
return thread_allocator.calloc_aligned(size, alignment);
|
||||
}
|
||||
|
||||
fn void* realloc(void *ptr, usize new_size) @builtin @inline
|
||||
fn void* realloc(void *ptr, usz new_size) @builtin @inline
|
||||
{
|
||||
return thread_allocator.realloc(ptr, new_size)!!;
|
||||
}
|
||||
|
||||
fn void*! realloc_checked(void *ptr, usize new_size) @builtin @inline
|
||||
fn void*! realloc_checked(void *ptr, usz new_size) @builtin @inline
|
||||
{
|
||||
return thread_allocator.realloc(ptr, new_size);
|
||||
}
|
||||
@@ -180,7 +180,7 @@ fn void*! realloc_checked(void *ptr, usize new_size) @builtin @inline
|
||||
/**
|
||||
* @require alignment && math::is_power_of_2(alignment)
|
||||
*/
|
||||
fn void*! realloc_aligned(void *ptr, usize new_size, usize alignment) @builtin @inline
|
||||
fn void*! realloc_aligned(void *ptr, usz new_size, usz alignment) @builtin @inline
|
||||
{
|
||||
return thread_allocator.realloc_aligned(ptr, new_size, alignment);
|
||||
}
|
||||
@@ -210,7 +210,7 @@ macro void @tscoped(;@body())
|
||||
{
|
||||
Allocator* old_allocator = thread_allocator;
|
||||
TempAllocator* temp = temp_allocator();
|
||||
usize mark = temp.mark()!!;
|
||||
usz mark = temp.mark()!!;
|
||||
thread_allocator = temp;
|
||||
defer temp.reset(mark);
|
||||
defer thread_allocator = old_allocator;
|
||||
@@ -222,17 +222,17 @@ macro talloc($Type) @builtin
|
||||
return temp_allocator().alloc_aligned($Type.sizeof, $Type.alignof)!!;
|
||||
}
|
||||
|
||||
fn void* tmalloc(usize size, usize alignment = allocator::DEFAULT_MEM_ALIGNMENT) @builtin @inline
|
||||
fn void* tmalloc(usz size, usz alignment = allocator::DEFAULT_MEM_ALIGNMENT) @builtin @inline
|
||||
{
|
||||
return temp_allocator().alloc_aligned(size, alignment)!!;
|
||||
}
|
||||
|
||||
fn void* tcalloc(usize size, usize alignment = allocator::DEFAULT_MEM_ALIGNMENT) @builtin @inline
|
||||
fn void* tcalloc(usz size, usz alignment = allocator::DEFAULT_MEM_ALIGNMENT) @builtin @inline
|
||||
{
|
||||
return temp_allocator().calloc_aligned(size, alignment)!!;
|
||||
}
|
||||
|
||||
fn void* trealloc(void* ptr, usize size, usize alignment = allocator::DEFAULT_MEM_ALIGNMENT) @builtin @inline
|
||||
fn void* trealloc(void* ptr, usz size, usz alignment = allocator::DEFAULT_MEM_ALIGNMENT) @builtin @inline
|
||||
{
|
||||
return temp_allocator().realloc_aligned(ptr, size, alignment)!!;
|
||||
}
|
||||
@@ -240,7 +240,7 @@ fn void* trealloc(void* ptr, usize size, usize alignment = allocator::DEFAULT_ME
|
||||
macro void @pool(;@body) @builtin
|
||||
{
|
||||
TempAllocator* temp = temp_allocator();
|
||||
usize mark = temp.used;
|
||||
usz mark = temp.used;
|
||||
defer temp.reset(mark);
|
||||
@body();
|
||||
}
|
||||
|
||||
@@ -2,13 +2,13 @@ module std::core::mem::allocator;
|
||||
|
||||
const MAX_MEMORY_ALIGNMENT = 0x1000_0000;
|
||||
const DEFAULT_MEM_ALIGNMENT = (void*.alignof) * 2;
|
||||
const DEFAULT_SIZE_PREFIX = usize.sizeof;
|
||||
const DEFAULT_SIZE_PREFIX_ALIGNMENT = usize.alignof;
|
||||
const DEFAULT_SIZE_PREFIX = usz.sizeof;
|
||||
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, usize new_size, usize alignment, usize offset, void* old_pointer, AllocationKind kind);
|
||||
define AllocatorFunction = fn void*!(Allocator* allocator, usz new_size, usz alignment, usz offset, void* old_pointer, AllocationKind kind);
|
||||
|
||||
struct Allocator
|
||||
{
|
||||
@@ -38,7 +38,7 @@ fault AllocationFailure
|
||||
|
||||
|
||||
|
||||
fn void*! Allocator.alloc(Allocator* allocator, usize size) @inline
|
||||
fn void*! Allocator.alloc(Allocator* allocator, usz size) @inline
|
||||
{
|
||||
return allocator.function(allocator, size, 0, 0, null, ALLOC);
|
||||
}
|
||||
@@ -46,12 +46,12 @@ fn void*! Allocator.alloc(Allocator* allocator, usize size) @inline
|
||||
/**
|
||||
* @require alignment && math::is_power_of_2(alignment)
|
||||
*/
|
||||
fn void*! Allocator.alloc_aligned(Allocator* allocator, usize size, usize alignment, usize offset = 0) @inline
|
||||
fn void*! Allocator.alloc_aligned(Allocator* allocator, usz size, usz alignment, usz offset = 0) @inline
|
||||
{
|
||||
return allocator.function(allocator, size, alignment, offset, null, ALIGNED_ALLOC);
|
||||
}
|
||||
|
||||
fn void*! Allocator.realloc(Allocator* allocator, void* old_pointer, usize size) @inline
|
||||
fn void*! Allocator.realloc(Allocator* allocator, void* old_pointer, usz size) @inline
|
||||
{
|
||||
return allocator.function(allocator, size, 0, 0, old_pointer, REALLOC);
|
||||
}
|
||||
@@ -59,18 +59,18 @@ fn void*! Allocator.realloc(Allocator* allocator, void* old_pointer, usize size)
|
||||
/**
|
||||
* @require alignment && math::is_power_of_2(alignment)
|
||||
*/
|
||||
fn void*! Allocator.realloc_aligned(Allocator* allocator, void* old_pointer, usize size, usize alignment, usize offset = 0) @inline
|
||||
fn void*! Allocator.realloc_aligned(Allocator* allocator, void* old_pointer, usz size, usz alignment, usz offset = 0) @inline
|
||||
{
|
||||
return allocator.function(allocator, size, alignment, offset, old_pointer, ALIGNED_REALLOC);
|
||||
}
|
||||
|
||||
fn usize! Allocator.mark(Allocator* allocator) @inline
|
||||
fn usz! Allocator.mark(Allocator* allocator) @inline
|
||||
{
|
||||
return (usize)(uptr)allocator.function(allocator, 0, 0, 0, null, MARK);
|
||||
return (usz)(uptr)allocator.function(allocator, 0, 0, 0, null, MARK);
|
||||
}
|
||||
|
||||
|
||||
fn void*! Allocator.calloc(Allocator* allocator, usize size) @inline
|
||||
fn void*! Allocator.calloc(Allocator* allocator, usz size) @inline
|
||||
{
|
||||
return allocator.function(allocator, size, 0, 0, null, CALLOC);
|
||||
}
|
||||
@@ -78,7 +78,7 @@ fn void*! Allocator.calloc(Allocator* allocator, usize size) @inline
|
||||
/**
|
||||
* @require alignment && math::is_power_of_2(alignment)
|
||||
*/
|
||||
fn void*! Allocator.calloc_aligned(Allocator* allocator, usize size, usize alignment, usize offset = 0) @inline
|
||||
fn void*! Allocator.calloc_aligned(Allocator* allocator, usz size, usz alignment, usz offset = 0) @inline
|
||||
{
|
||||
return allocator.function(allocator, size, alignment, offset, null, ALIGNED_CALLOC);
|
||||
}
|
||||
@@ -93,12 +93,12 @@ fn void! Allocator.free_aligned(Allocator* allocator, void* old_pointer) @inline
|
||||
allocator.function(allocator, 0, 0, 0, old_pointer, ALIGNED_FREE)?;
|
||||
}
|
||||
|
||||
fn void Allocator.reset(Allocator* allocator, usize mark = 0)
|
||||
fn void Allocator.reset(Allocator* allocator, usz mark = 0)
|
||||
{
|
||||
allocator.function(allocator, mark, 0, 0, null, RESET)!!;
|
||||
}
|
||||
|
||||
private fn usize alignment_for_allocation(usize alignment) @inline
|
||||
private fn usz alignment_for_allocation(usz alignment) @inline
|
||||
{
|
||||
if (alignment < DEFAULT_MEM_ALIGNMENT)
|
||||
{
|
||||
@@ -113,14 +113,14 @@ struct DynamicArenaAllocator
|
||||
Allocator* backing_allocator;
|
||||
DynamicArenaPage* page;
|
||||
DynamicArenaPage* unused_page;
|
||||
usize page_size;
|
||||
usz page_size;
|
||||
}
|
||||
|
||||
/**
|
||||
* @require page_size >= 128
|
||||
* @require this != null
|
||||
**/
|
||||
fn void DynamicArenaAllocator.init(DynamicArenaAllocator* this, usize page_size, Allocator* backing_allocator = mem::current_allocator())
|
||||
fn void DynamicArenaAllocator.init(DynamicArenaAllocator* this, usz page_size, Allocator* backing_allocator = mem::current_allocator())
|
||||
{
|
||||
this.function = &dynamic_arena_allocator_function;
|
||||
this.page = null;
|
||||
@@ -157,7 +157,7 @@ struct ArenaAllocator
|
||||
{
|
||||
inline Allocator allocator;
|
||||
char[] data;
|
||||
usize used;
|
||||
usz used;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,36 +4,36 @@
|
||||
module std::core::mem::array;
|
||||
|
||||
/**
|
||||
* @require usize.max / elements > $Type.sizeof
|
||||
* @require usz.max / elements > $Type.sizeof
|
||||
**/
|
||||
macro alloc($Type, usize elements)
|
||||
macro alloc($Type, usz elements)
|
||||
{
|
||||
$Type* ptr = malloc($Type.sizeof * elements);
|
||||
return ptr[:elements];
|
||||
}
|
||||
|
||||
/**
|
||||
* @require usize.max / elements > $Type.sizeof
|
||||
* @require usz.max / elements > $Type.sizeof
|
||||
**/
|
||||
macro talloc($Type, usize elements)
|
||||
macro talloc($Type, usz elements)
|
||||
{
|
||||
$Type* ptr = tmalloc($Type.sizeof * elements, $Type[1].alignof);
|
||||
return ptr[:elements];
|
||||
}
|
||||
|
||||
/**
|
||||
* @require (usize.max / elements > $Type.sizeof)
|
||||
* @require (usz.max / elements > $Type.sizeof)
|
||||
**/
|
||||
macro make($Type, usize elements, Allocator* allocator = mem::current_allocator())
|
||||
macro make($Type, usz elements, Allocator* allocator = mem::current_allocator())
|
||||
{
|
||||
$Type* ptr = allocator.calloc($Type.sizeof * elements)!!;
|
||||
return ptr[:elements];
|
||||
}
|
||||
|
||||
/**
|
||||
* @require (usize.max / elements > $Type.sizeof)
|
||||
* @require (usz.max / elements > $Type.sizeof)
|
||||
**/
|
||||
macro tmake($Type, usize elements)
|
||||
macro tmake($Type, usz elements)
|
||||
{
|
||||
$Type* ptr = tcalloc($Type.sizeof * elements, $Type[1].alignof);
|
||||
return ptr[:elements];
|
||||
|
||||
@@ -14,7 +14,7 @@ private const uint SURROGATE_HIGH_VALUE = 0xD800;
|
||||
fn String join(char[][] s, char[] joiner)
|
||||
{
|
||||
if (!s.len) return (String)null;
|
||||
usize total_size = joiner.len * s.len;
|
||||
usz total_size = joiner.len * s.len;
|
||||
foreach (char[]* &str : s)
|
||||
{
|
||||
total_size += str.len;
|
||||
@@ -29,14 +29,14 @@ fn String join(char[][] s, char[] joiner)
|
||||
return res;
|
||||
}
|
||||
|
||||
fn usize! str_index_of(char[] s, char[] needle)
|
||||
fn usz! str_index_of(char[] s, char[] needle)
|
||||
{
|
||||
usize match = 0;
|
||||
usize needed = needle.len;
|
||||
usz match = 0;
|
||||
usz needed = needle.len;
|
||||
if (!needed) return SearchResult.MISSING!;
|
||||
usize index_start = 0;
|
||||
usz index_start = 0;
|
||||
char search = needle[0];
|
||||
foreach (usize i, char c : s)
|
||||
foreach (usz i, char c : s)
|
||||
{
|
||||
if (c == search)
|
||||
{
|
||||
@@ -57,7 +57,7 @@ fn usize! str_index_of(char[] s, char[] needle)
|
||||
|
||||
fn ZString copy_zstring(char[] s)
|
||||
{
|
||||
usize len = s.len;
|
||||
usz len = s.len;
|
||||
char* str = malloc(len + 1);
|
||||
mem::copy(str, s.ptr, len);
|
||||
str[len] = 0;
|
||||
@@ -66,7 +66,7 @@ fn ZString copy_zstring(char[] s)
|
||||
|
||||
fn ZString tcopy_zstring(char[] s)
|
||||
{
|
||||
usize len = s.len;
|
||||
usz len = s.len;
|
||||
char* str = tmalloc(len + 1);
|
||||
mem::copy(str, s.ptr, len);
|
||||
str[len] = 0;
|
||||
@@ -91,9 +91,9 @@ fault UnicodeResult
|
||||
|
||||
|
||||
|
||||
fn usize utf8_codepoints(char[] utf8)
|
||||
fn usz utf8_codepoints(char[] utf8)
|
||||
{
|
||||
usize len = 0;
|
||||
usz len = 0;
|
||||
foreach (char c : utf8)
|
||||
{
|
||||
if (c & 0xC0 != 0x80) len++;
|
||||
@@ -103,7 +103,7 @@ fn usize utf8_codepoints(char[] utf8)
|
||||
|
||||
fn Char32[]! utf8to32(char[] utf8, Allocator* allocator = mem::current_allocator)
|
||||
{
|
||||
usize codepoints = conv::utf8_codepoints(utf8);
|
||||
usz codepoints = conv::utf8_codepoints(utf8);
|
||||
Char32* data = allocator.alloc(Char32.sizeof * (codepoints + 1))?;
|
||||
conv::utf8to32_unsafe(utf8, data)?;
|
||||
data[codepoints] = 0;
|
||||
@@ -112,7 +112,7 @@ fn Char32[]! utf8to32(char[] utf8, Allocator* allocator = mem::current_allocator
|
||||
|
||||
fn char[] utf32to8(Char32[] utf32, Allocator* allocator = mem::current_allocator)
|
||||
{
|
||||
usize len = conv::utf8len_for_utf32(utf32);
|
||||
usz len = conv::utf8len_for_utf32(utf32);
|
||||
char* data = allocator.alloc(len + 1)!!;
|
||||
conv::utf32to8_unsafe(utf32, data);
|
||||
data[len] = 0;
|
||||
@@ -121,7 +121,7 @@ fn char[] utf32to8(Char32[] utf32, Allocator* allocator = mem::current_allocator
|
||||
|
||||
fn Char16[]! utf8to16(char[] utf8, Allocator* allocator = mem::current_allocator)
|
||||
{
|
||||
usize len16 = conv::utf16len_for_utf8(utf8);
|
||||
usz len16 = conv::utf16len_for_utf8(utf8);
|
||||
Char16* data = allocator.alloc((len16 + 1) * Char16.sizeof)?;
|
||||
conv::utf8to16_unsafe(utf8, data)?;
|
||||
data[len16] = 0;
|
||||
@@ -131,7 +131,7 @@ fn Char16[]! utf8to16(char[] utf8, Allocator* allocator = mem::current_allocator
|
||||
|
||||
fn char[]! utf16to8(Char16[] utf16, Allocator* allocator = mem::current_allocator())
|
||||
{
|
||||
usize len = conv::utf8len_for_utf16(utf16);
|
||||
usz len = conv::utf8len_for_utf16(utf16);
|
||||
char* data = allocator.alloc(len + 1)?;
|
||||
conv::utf16to8_unsafe(utf16, data)?;
|
||||
return data[0 .. len - 1];
|
||||
@@ -139,23 +139,23 @@ fn char[]! utf16to8(Char16[] utf16, Allocator* allocator = mem::current_allocato
|
||||
|
||||
fn char[] copy(char[] s)
|
||||
{
|
||||
usize len = s.len;
|
||||
usz len = s.len;
|
||||
ZString str_copy = copy_zstring(s) @inline;
|
||||
return str_copy[..len];
|
||||
}
|
||||
|
||||
fn char[] tcopy(char[] s)
|
||||
{
|
||||
usize len = s.len;
|
||||
usz len = s.len;
|
||||
ZString str_copy = tcopy_zstring(s) @inline;
|
||||
return str_copy[..len];
|
||||
}
|
||||
|
||||
fn char[] tconcat(char[] s1, char[] s2)
|
||||
{
|
||||
usize full_len = s1.len + s2.len;
|
||||
usz full_len = s1.len + s2.len;
|
||||
char* str = tmalloc(full_len + 1);
|
||||
usize s1_len = s1.len;
|
||||
usz s1_len = s1.len;
|
||||
mem::copy(str, s1.ptr, s1_len);
|
||||
mem::copy(str + s1_len, s2.ptr, s2.len);
|
||||
str[full_len] = 0;
|
||||
@@ -164,18 +164,18 @@ fn char[] tconcat(char[] s1, char[] s2)
|
||||
|
||||
fn char[] concat(char[] s1, char[] s2)
|
||||
{
|
||||
usize full_len = s1.len + s2.len;
|
||||
usz full_len = s1.len + s2.len;
|
||||
char* str = malloc(full_len + 1);
|
||||
usize s1_len = s1.len;
|
||||
usz s1_len = s1.len;
|
||||
mem::copy(str, s1.ptr, s1_len);
|
||||
mem::copy(str + s1_len, s2.ptr, s2.len);
|
||||
str[full_len] = 0;
|
||||
return str[..full_len];
|
||||
}
|
||||
|
||||
fn usize ZString.len(ZString *str)
|
||||
fn usz ZString.len(ZString *str)
|
||||
{
|
||||
usize len = 0;
|
||||
usz len = 0;
|
||||
char* ptr = (char*)*str;
|
||||
while (char c = ptr++[0])
|
||||
{
|
||||
|
||||
@@ -6,14 +6,14 @@ define String = distinct void*;
|
||||
private struct StringData
|
||||
{
|
||||
Allocator* allocator;
|
||||
usize len;
|
||||
usize capacity;
|
||||
usz len;
|
||||
usz capacity;
|
||||
char[*] chars;
|
||||
}
|
||||
|
||||
const usize MIN_CAPACITY = 16;
|
||||
const usz MIN_CAPACITY = 16;
|
||||
|
||||
fn String new_with_capacity(usize capacity, Allocator* allocator = mem::current_allocator())
|
||||
fn String new_with_capacity(usz capacity, Allocator* allocator = mem::current_allocator())
|
||||
{
|
||||
if (capacity < MIN_CAPACITY) capacity = MIN_CAPACITY;
|
||||
StringData* data = allocator.alloc(StringData.sizeof + capacity)!!;
|
||||
@@ -25,7 +25,7 @@ fn String new_with_capacity(usize capacity, Allocator* allocator = mem::current_
|
||||
|
||||
fn String new(char[] c)
|
||||
{
|
||||
usize len = c.len;
|
||||
usz len = c.len;
|
||||
String str = new_with_capacity(len);
|
||||
StringData* data = str.data();
|
||||
if (len)
|
||||
@@ -52,7 +52,7 @@ fn ZString String.zstr(String str)
|
||||
return (ZString)&data.chars[0];
|
||||
}
|
||||
|
||||
fn usize String.len(String this)
|
||||
fn usz String.len(String this)
|
||||
{
|
||||
if (!this) return 0;
|
||||
return this.data().len;
|
||||
@@ -61,7 +61,7 @@ fn usize String.len(String this)
|
||||
/**
|
||||
* @require new_size <= this.len()
|
||||
*/
|
||||
fn void String.chop(String this, usize new_size)
|
||||
fn void String.chop(String this, usz new_size)
|
||||
{
|
||||
if (!this) return;
|
||||
this.data().len = new_size;
|
||||
@@ -86,17 +86,17 @@ fn void String.append_utf32(String* str, Char32[] chars)
|
||||
/**
|
||||
* @require index < str.len()
|
||||
**/
|
||||
fn void String.set(String str, usize index, char c)
|
||||
fn void String.set(String str, usz index, char c)
|
||||
{
|
||||
str.data().chars[index] = c;
|
||||
}
|
||||
|
||||
fn void String.append_repeat(String* str, char c, usize times)
|
||||
fn void String.append_repeat(String* str, char c, usz times)
|
||||
{
|
||||
if (times == 0) return;
|
||||
str.reserve(times);
|
||||
StringData* data = str.data();
|
||||
for (usize i = 0; i < times; i++)
|
||||
for (usz i = 0; i < times; i++)
|
||||
{
|
||||
data.chars[data.len++] = c;
|
||||
}
|
||||
@@ -155,7 +155,7 @@ fn String String.copy(String* str, Allocator* allocator = null)
|
||||
|
||||
fn ZString String.copy_zstr(String* str, Allocator* allocator = mem::current_allocator())
|
||||
{
|
||||
usize str_len = str.len();
|
||||
usz str_len = str.len();
|
||||
if (!str_len)
|
||||
{
|
||||
return (ZString)allocator.calloc(1)!!;
|
||||
@@ -179,7 +179,7 @@ fn bool String.equals(String str, String other_string)
|
||||
if (str1 == str2) return true;
|
||||
if (!str1) return str2.len == 0;
|
||||
if (!str2) return str1.len == 0;
|
||||
usize str1_len = str1.len;
|
||||
usz str1_len = str1.len;
|
||||
if (str1_len != str2.len) return false;
|
||||
for (int i = 0; i < str1_len; i++)
|
||||
{
|
||||
@@ -204,8 +204,8 @@ fn bool String.less(String str, String other_string)
|
||||
if (str1 == str2) return false;
|
||||
if (!str1) return str2.len != 0;
|
||||
if (!str2) return str1.len == 0;
|
||||
usize str1_len = str1.len;
|
||||
usize str2_len = str2.len;
|
||||
usz str1_len = str1.len;
|
||||
usz str2_len = str2.len;
|
||||
if (str1_len != str2_len) return str1_len < str2_len;
|
||||
for (int i = 0; i < str1_len; i++)
|
||||
{
|
||||
@@ -216,7 +216,7 @@ fn bool String.less(String str, String other_string)
|
||||
|
||||
fn void String.append_chars(String* this, char[] str)
|
||||
{
|
||||
usize other_len = str.len;
|
||||
usz other_len = str.len;
|
||||
if (!other_len) return;
|
||||
if (!*this)
|
||||
{
|
||||
@@ -283,7 +283,7 @@ private fn StringData* String.data(String str) @inline
|
||||
return (StringData*)str;
|
||||
}
|
||||
|
||||
private fn void String.reserve(String* str, usize addition)
|
||||
private fn void String.reserve(String* str, usz addition)
|
||||
{
|
||||
StringData* data = str.data();
|
||||
if (!data)
|
||||
@@ -291,9 +291,9 @@ private fn void String.reserve(String* str, usize addition)
|
||||
*str = string::new_with_capacity(addition);
|
||||
return;
|
||||
}
|
||||
usize len = data.len + addition;
|
||||
usz len = data.len + addition;
|
||||
if (data.capacity >= len) return;
|
||||
usize new_capacity = data.capacity * 2;
|
||||
usz new_capacity = data.capacity * 2;
|
||||
if (new_capacity < MIN_CAPACITY) new_capacity = MIN_CAPACITY;
|
||||
*str = (String)data.allocator.realloc(data, StringData.sizeof + new_capacity)!!;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ module std::core::string::iterator;
|
||||
struct StringIterator
|
||||
{
|
||||
char[] utf8;
|
||||
usize current;
|
||||
usz current;
|
||||
}
|
||||
|
||||
fn void StringIterator.reset(StringIterator* this)
|
||||
@@ -15,10 +15,10 @@ fn void StringIterator.reset(StringIterator* this)
|
||||
|
||||
fn Char32! StringIterator.next(StringIterator* this)
|
||||
{
|
||||
usize len = this.utf8.len;
|
||||
usize current = this.current;
|
||||
usz len = this.utf8.len;
|
||||
usz current = this.current;
|
||||
if (current >= len) return IteratorResult.NO_MORE_ELEMENT!;
|
||||
usize read = (len - current < 4 ? len - current : 4);
|
||||
usz read = (len - current < 4 ? len - current : 4);
|
||||
Char32 res = conv::utf8_to_char32(&this.utf8[current], &read)?;
|
||||
this.current += read;
|
||||
return res;
|
||||
|
||||
@@ -226,5 +226,5 @@ enum TypeKind : char
|
||||
struct TypeEnum
|
||||
{
|
||||
TypeKind type;
|
||||
usize elements;
|
||||
usz elements;
|
||||
}
|
||||
|
||||
@@ -139,7 +139,7 @@ fn bool File.eof(File* file) @inline
|
||||
/**
|
||||
* @require file && file.file
|
||||
*/
|
||||
fn usize File.read(File* file, void* buffer, usize items, usize element_size = 1)
|
||||
fn usz File.read(File* file, void* buffer, usz items, usz element_size = 1)
|
||||
{
|
||||
return libc::fread(buffer, element_size, items, file.file);
|
||||
}
|
||||
@@ -152,7 +152,7 @@ fn usize File.read(File* file, void* buffer, usize items, usize element_size = 1
|
||||
* @require file.file `File must be initialized`
|
||||
* @require element_size > 1
|
||||
*/
|
||||
fn usize File.write(File* file, void* buffer, usize items, usize element_size = 1)
|
||||
fn usz File.write(File* file, void* buffer, usz items, usz element_size = 1)
|
||||
{
|
||||
return libc::fwrite(buffer, element_size, items, file.file);
|
||||
}
|
||||
@@ -161,9 +161,9 @@ fn usize File.write(File* file, void* buffer, usize items, usize element_size =
|
||||
* @param [&in] file
|
||||
* @require file.file `File must be initialized`
|
||||
*/
|
||||
fn usize! File.println(File* file, char[] string)
|
||||
fn usz! File.println(File* file, char[] string)
|
||||
{
|
||||
usize len = string.len;
|
||||
usz len = string.len;
|
||||
if (len != libc::fwrite(string.ptr, 1, len, file.file)) return IoError.UNKNOWN_ERROR!;
|
||||
if (!libc::putc('\n', file.file)) return IoError.UNKNOWN_ERROR!;
|
||||
return len + 1;
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
module std::io;
|
||||
|
||||
private fn void! Formatter.left_adjust(Formatter* this, usize len)
|
||||
private fn void! Formatter.left_adjust(Formatter* this, usz len)
|
||||
{
|
||||
if (!this.flags.left) return;
|
||||
for (usize l = len; l < this.width; l++) this.out(' ')?;
|
||||
for (usz l = len; l < this.width; l++) this.out(' ')?;
|
||||
}
|
||||
|
||||
private fn void! Formatter.right_adjust(Formatter* this, usize len)
|
||||
private fn void! Formatter.right_adjust(Formatter* this, usz len)
|
||||
{
|
||||
if (this.flags.left) return;
|
||||
for (usize l = len; l < this.width; l++) this.out(' ')?;
|
||||
for (usz l = len; l < this.width; l++) this.out(' ')?;
|
||||
}
|
||||
|
||||
|
||||
@@ -126,10 +126,10 @@ private fn FloatType float_from_variant(variant arg)
|
||||
* @param maxlen "the maximum len that can be read."
|
||||
* @return "The result of the atoi."
|
||||
**/
|
||||
private fn uint simple_atoi(char* buf, usize maxlen, usize* len_ptr) @inline
|
||||
private fn uint simple_atoi(char* buf, usz maxlen, usz* len_ptr) @inline
|
||||
{
|
||||
uint i = 0;
|
||||
usize len = *len_ptr;
|
||||
usz len = *len_ptr;
|
||||
while (len < maxlen)
|
||||
{
|
||||
char c = buf[len];
|
||||
@@ -144,12 +144,12 @@ private fn uint simple_atoi(char* buf, usize maxlen, usize* len_ptr) @inline
|
||||
|
||||
private fn void! Formatter.out_substr(Formatter *this, char[] str)
|
||||
{
|
||||
usize l = conv::utf8_codepoints(str);
|
||||
usz l = conv::utf8_codepoints(str);
|
||||
uint prec = this.prec;
|
||||
if (this.flags.precision && l < prec) l = prec;
|
||||
this.right_adjust(' ')?;
|
||||
usize index = 0;
|
||||
usize chars = str.len;
|
||||
usz index = 0;
|
||||
usz chars = str.len;
|
||||
char* ptr = str.ptr;
|
||||
while (index < chars)
|
||||
{
|
||||
@@ -242,7 +242,7 @@ private fn void! Formatter.etoa(Formatter* this, FloatType value)
|
||||
if (expval) value /= conv.f;
|
||||
|
||||
// output the floating part
|
||||
usize start_idx = this.idx;
|
||||
usz start_idx = this.idx;
|
||||
PrintFlags old = this.flags;
|
||||
this.flags.adapt_exp = false;
|
||||
this.width = fwidth;
|
||||
@@ -269,7 +269,7 @@ private fn void! Formatter.etoa(Formatter* this, FloatType value)
|
||||
private fn void! Formatter.ftoa(Formatter* this, FloatType value)
|
||||
{
|
||||
char[PRINTF_FTOA_BUFFER_SIZE] buf = void;
|
||||
usize len = 0;
|
||||
usz len = 0;
|
||||
const FloatType[] POW10 = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
|
||||
FloatType diff = 0.0;
|
||||
|
||||
@@ -405,7 +405,7 @@ private fn void! Formatter.ftoa(Formatter* this, FloatType value)
|
||||
private fn void! Formatter.ntoa(Formatter* this, NtoaType value, bool negative, uint base)
|
||||
{
|
||||
char[PRINTF_NTOA_BUFFER_SIZE] buf = void;
|
||||
usize len = 0;
|
||||
usz len = 0;
|
||||
|
||||
// no hash for 0 values
|
||||
if (!value) this.flags.hash = false;
|
||||
@@ -426,7 +426,7 @@ private fn void! Formatter.ntoa(Formatter* this, NtoaType value, bool negative,
|
||||
return this.ntoa_format(buf[:PRINTF_NTOA_BUFFER_SIZE], len, negative, base);
|
||||
}
|
||||
|
||||
private fn void! Formatter.ntoa_format(Formatter* this, char[] buf, usize len, bool negative, uint base)
|
||||
private fn void! Formatter.ntoa_format(Formatter* this, char[] buf, usz len, bool negative, uint base)
|
||||
{
|
||||
// pad leading zeros
|
||||
if (!this.flags.left)
|
||||
@@ -529,12 +529,12 @@ private fn void! Formatter.out_char(Formatter* this, variant arg)
|
||||
|
||||
private fn void! Formatter.out_reverse(Formatter* this, char[] buf)
|
||||
{
|
||||
usize buffer_start_idx = this.idx;
|
||||
usize len = buf.len;
|
||||
usz buffer_start_idx = this.idx;
|
||||
usz len = buf.len;
|
||||
// pad spaces up to given width
|
||||
if (!this.flags.left && !this.flags.zeropad)
|
||||
{
|
||||
for (usize i = len; i < this.width; i++)
|
||||
for (usz i = len; i < this.width; i++)
|
||||
{
|
||||
this.out(' ')?;
|
||||
}
|
||||
@@ -546,19 +546,19 @@ private fn void! Formatter.out_reverse(Formatter* this, char[] buf)
|
||||
return this.left_adjust(this.idx - buffer_start_idx);
|
||||
}
|
||||
|
||||
private fn void! printf_advance_format(usize format_len, usize *index_ptr) @inline
|
||||
private fn void! printf_advance_format(usz format_len, usz *index_ptr) @inline
|
||||
{
|
||||
usize val = ++(*index_ptr);
|
||||
usz val = ++(*index_ptr);
|
||||
if (val >= format_len) return FormattingFault.UNTERMINATED_FORMAT!;
|
||||
}
|
||||
|
||||
private fn variant! next_variant(variant* args_ptr, usize args_len, usize* arg_index_ptr) @inline
|
||||
private fn variant! next_variant(variant* args_ptr, usz args_len, usz* arg_index_ptr) @inline
|
||||
{
|
||||
if (*arg_index_ptr >= args_len) return FormattingFault.MISSING_ARG!;
|
||||
return args_ptr[(*arg_index_ptr)++];
|
||||
}
|
||||
|
||||
private fn int! printf_parse_format_field(variant* args_ptr, usize args_len, usize* args_index_ptr, char* format_ptr, usize format_len, usize* index_ptr) @inline
|
||||
private fn int! printf_parse_format_field(variant* args_ptr, usz args_len, usz* args_index_ptr, char* format_ptr, usz format_len, usz* index_ptr) @inline
|
||||
{
|
||||
char c = format_ptr[*index_ptr];
|
||||
if (c >= '0' && c <= '9') return simple_atoi(format_ptr, format_len, index_ptr);
|
||||
|
||||
@@ -35,7 +35,7 @@ struct Formatter
|
||||
PrintFlags flags;
|
||||
uint width;
|
||||
uint prec;
|
||||
usize idx;
|
||||
usz idx;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,7 +146,7 @@ private fn void! Formatter.out_str(Formatter* this, variant arg)
|
||||
return this.out_substr("<variant>");
|
||||
case ENUM:
|
||||
if (this.print_with_function(arg)?) return;
|
||||
return this.out_substr(arg.type.names[types::variant_to_int(arg, usize)!!]);
|
||||
return this.out_substr(arg.type.names[types::variant_to_int(arg, usz)!!]);
|
||||
case STRUCT:
|
||||
if (this.print_with_function(arg)?) return;
|
||||
return this.out_substr("<struct>");
|
||||
@@ -186,12 +186,12 @@ private fn void! Formatter.out_str(Formatter* this, variant arg)
|
||||
if (this.print_with_function(arg)?) return;
|
||||
// this is SomeType[*] so grab the "SomeType"
|
||||
typeid inner = arg.type.inner;
|
||||
usize size = inner.sizeof;
|
||||
usize len = arg.type.len;
|
||||
usz size = inner.sizeof;
|
||||
usz len = arg.type.len;
|
||||
// Pretend this is a char[]
|
||||
void* ptr = (void*)arg.ptr;
|
||||
this.out('[')?;
|
||||
for (usize i = 0; i < len; i++)
|
||||
for (usz i = 0; i < len; i++)
|
||||
{
|
||||
if (i != 0) this.out_substr(", ")?;
|
||||
this.out_str(variant { ptr, inner })?;
|
||||
@@ -202,12 +202,12 @@ private fn void! Formatter.out_str(Formatter* this, variant arg)
|
||||
if (this.print_with_function(arg)?) return;
|
||||
// this is SomeType[*] so grab the "SomeType"
|
||||
typeid inner = arg.type.inner;
|
||||
usize size = inner.sizeof;
|
||||
usize len = arg.type.len;
|
||||
usz size = inner.sizeof;
|
||||
usz len = arg.type.len;
|
||||
// Pretend this is a char[]
|
||||
void* ptr = (void*)arg.ptr;
|
||||
this.out_substr("[<")?;
|
||||
for (usize i = 0; i < len; i++)
|
||||
for (usz i = 0; i < len; i++)
|
||||
{
|
||||
if (i != 0) this.out_substr(", ")?;
|
||||
this.out_str(variant { ptr, inner })?;
|
||||
@@ -222,13 +222,13 @@ private fn void! Formatter.out_str(Formatter* this, variant arg)
|
||||
{
|
||||
return this.out_substr(*(char[]*)arg);
|
||||
}
|
||||
usize size = inner.sizeof;
|
||||
usz size = inner.sizeof;
|
||||
// Pretend this is a char[]
|
||||
char[]* temp = (void*)arg.ptr;
|
||||
void* ptr = (void*)temp.ptr;
|
||||
usize len = temp.len;
|
||||
usz len = temp.len;
|
||||
this.out('[')?;
|
||||
for (usize i = 0; i < len; i++)
|
||||
for (usz i = 0; i < len; i++)
|
||||
{
|
||||
if (i != 0) this.out_substr(", ")?;
|
||||
this.out_str(variant { ptr, inner })?;
|
||||
@@ -287,34 +287,34 @@ private fn void! out_string_append_fn(char c, void* data)
|
||||
s.append_char(c);
|
||||
}
|
||||
|
||||
fn usize! printf(char[] format, args...) @maydiscard
|
||||
fn usz! printf(char[] format, args...) @maydiscard
|
||||
{
|
||||
Formatter formatter;
|
||||
formatter.init(&out_putchar_fn);
|
||||
return formatter.vprintf(format, args);
|
||||
}
|
||||
|
||||
fn usize! printfln(char[] format, args...) @maydiscard
|
||||
fn usz! printfln(char[] format, args...) @maydiscard
|
||||
{
|
||||
Formatter formatter;
|
||||
formatter.init(&out_putchar_fn);
|
||||
usize len = formatter.vprintf(format, args)?;
|
||||
usz len = formatter.vprintf(format, args)?;
|
||||
putchar('\n');
|
||||
return len + 1;
|
||||
}
|
||||
|
||||
fn usize! String.printf(String* str, char[] format, args...) @maydiscard
|
||||
fn usz! String.printf(String* str, char[] format, args...) @maydiscard
|
||||
{
|
||||
Formatter formatter;
|
||||
formatter.init(&out_string_append_fn, str);
|
||||
return formatter.vprintf(format, args);
|
||||
}
|
||||
|
||||
fn usize! String.printfln(String* str, char[] format, args...) @maydiscard
|
||||
fn usz! String.printfln(String* str, char[] format, args...) @maydiscard
|
||||
{
|
||||
Formatter formatter;
|
||||
formatter.init(&out_string_append_fn, str);
|
||||
usize len = formatter.vprintf(format, args)?;
|
||||
usz len = formatter.vprintf(format, args)?;
|
||||
str.append('\n');
|
||||
return len + 1;
|
||||
}
|
||||
@@ -322,7 +322,7 @@ fn usize! String.printfln(String* str, char[] format, args...) @maydiscard
|
||||
private struct BufferData
|
||||
{
|
||||
char[] buffer;
|
||||
usize written;
|
||||
usz written;
|
||||
}
|
||||
|
||||
fn char[]! bprintf(char[] buffer, char[] format, args...) @maydiscard
|
||||
@@ -330,42 +330,42 @@ fn char[]! bprintf(char[] buffer, char[] format, args...) @maydiscard
|
||||
Formatter formatter;
|
||||
BufferData data = { .buffer = buffer };
|
||||
formatter.init(&out_buffer_fn, &data);
|
||||
usize size = formatter.vprintf(format, args)?;
|
||||
usz size = formatter.vprintf(format, args)?;
|
||||
return buffer[:size];
|
||||
}
|
||||
|
||||
fn usize! File.printf(File file, char[] format, args...) @maydiscard
|
||||
fn usz! File.printf(File file, char[] format, args...) @maydiscard
|
||||
{
|
||||
Formatter formatter;
|
||||
formatter.init(&out_putchar_fn, &file);
|
||||
return formatter.vprintf(format, args)?;
|
||||
}
|
||||
|
||||
fn usize! File.printfln(File file, char[] format, args...) @maydiscard
|
||||
fn usz! File.printfln(File file, char[] format, args...) @maydiscard
|
||||
{
|
||||
Formatter formatter;
|
||||
formatter.init(&out_putchar_fn, &file);
|
||||
usize len = formatter.vprintf(format, args)?;
|
||||
usz len = formatter.vprintf(format, args)?;
|
||||
file.putc('\n')?;
|
||||
file.flush();
|
||||
return len + 1;
|
||||
}
|
||||
|
||||
fn usize! Formatter.printf(Formatter* this, char[] format, args...)
|
||||
fn usz! Formatter.printf(Formatter* this, char[] format, args...)
|
||||
{
|
||||
return this.vprintf(format, args) @inline;
|
||||
}
|
||||
|
||||
fn usize! Formatter.vprintf(Formatter* this, char[] format, variant[] variants)
|
||||
fn usz! Formatter.vprintf(Formatter* this, char[] format, variant[] variants)
|
||||
{
|
||||
if (!this.out_fn)
|
||||
{
|
||||
// use null output function
|
||||
this.out_fn = &out_null_fn;
|
||||
}
|
||||
usize format_len = format.len;
|
||||
usize variant_index = 0;
|
||||
for (usize i = 0; i < format_len; i++)
|
||||
usz format_len = format.len;
|
||||
usz variant_index = 0;
|
||||
for (usz i = 0; i < format_len; i++)
|
||||
{
|
||||
// format specifier? %[flags][width][.precision][length]
|
||||
char c = format[i];
|
||||
|
||||
@@ -53,8 +53,8 @@ extern fn void atexit(TerminateFunction f);
|
||||
extern fn void exit(int status);
|
||||
extern fn char* getenv(char* name);
|
||||
extern fn int system(char* str);
|
||||
extern fn void bsearch(void* key, void *base, usize items, usize size, CompareFunction compare);
|
||||
extern fn void qsort(void* base, usize items, usize size, CompareFunction compare);
|
||||
extern fn void bsearch(void* key, void *base, usz items, usz size, CompareFunction compare);
|
||||
extern fn void qsort(void* base, usz items, usz size, CompareFunction compare);
|
||||
extern fn int abs(int x);
|
||||
extern fn DivResult div(int numer, int denom);
|
||||
extern fn long labs(long x);
|
||||
@@ -65,33 +65,33 @@ extern fn void srand(uint seed);
|
||||
// MB functions omitted
|
||||
|
||||
// string
|
||||
extern fn void* memchr(void* str, int c, usize n);
|
||||
extern fn int memcmp(void* str1, void* str2, usize n);
|
||||
extern fn void* memcpy(void* dest, void* src, usize n);
|
||||
extern fn void* memmove(void* dest, void* src, usize n);
|
||||
extern fn void* memset(void* dest, usize n);
|
||||
extern fn void* memchr(void* str, int c, usz n);
|
||||
extern fn int memcmp(void* str1, void* str2, usz n);
|
||||
extern fn void* memcpy(void* dest, void* src, usz n);
|
||||
extern fn void* memmove(void* dest, void* src, usz n);
|
||||
extern fn void* memset(void* dest, usz n);
|
||||
extern fn char* strcat(char* dest, char* src);
|
||||
extern fn char* strncat(char* dest, char* src, usize n);
|
||||
extern fn char* strncat(char* dest, char* src, usz n);
|
||||
extern fn char* strchr(char* str, int c);
|
||||
extern fn int strcmp(char* str1, char* str2);
|
||||
extern fn int strncmp(char* str1, char* str2, usize n);
|
||||
extern fn int strncmp(char* str1, char* str2, usz n);
|
||||
extern fn int strcoll(char* str1, char* str2);
|
||||
extern fn char* strcpy(char* dst, char* src);
|
||||
extern fn char* strncpy(char* dst, char* src, usize n);
|
||||
extern fn usize strcspn(char* str1, char* str2);
|
||||
extern fn char* strncpy(char* dst, char* src, usz n);
|
||||
extern fn usz strcspn(char* str1, char* str2);
|
||||
extern fn char* strerror(int errn);
|
||||
extern fn usize strlen(char* str);
|
||||
extern fn usz strlen(char* str);
|
||||
extern fn char* strpbrk(char* str1, char* str2);
|
||||
extern fn usize strspn(char* str1, char* str2);
|
||||
extern fn usz strspn(char* str1, char* str2);
|
||||
extern fn char* strstr(char* haystack, char* needle);
|
||||
extern fn char* strtok(char* str, char* delim);
|
||||
extern fn usize strxfrm(char* dest, char* src, usize n);
|
||||
extern fn usz strxfrm(char* dest, char* src, usz n);
|
||||
|
||||
// malloc
|
||||
extern fn void* malloc(usize size);
|
||||
extern fn void* calloc(usize count, usize size);
|
||||
extern fn void* malloc(usz size);
|
||||
extern fn void* calloc(usz count, usz size);
|
||||
extern fn void* free(void*);
|
||||
extern fn void* realloc(void* ptr, usize size);
|
||||
extern fn void* realloc(void* ptr, usz size);
|
||||
|
||||
// stdio
|
||||
|
||||
@@ -103,9 +103,9 @@ $case OsType.LINUX:
|
||||
extern CFile __stdin @extname("stdin");
|
||||
extern CFile __stdout @extname("stdout");
|
||||
extern CFile __stderr @extname("stderr");
|
||||
extern fn usize malloc_usable_size(void* ptr);
|
||||
macro usize malloc_size(void* ptr) { return malloc_usable_size(ptr); }
|
||||
extern fn void* aligned_alloc(usize align, usize size);
|
||||
extern fn usz malloc_usable_size(void* ptr);
|
||||
macro usz malloc_size(void* ptr) { return malloc_usable_size(ptr); }
|
||||
extern fn void* aligned_alloc(usz align, usz size);
|
||||
macro CFile stdin() { return __stdin; }
|
||||
macro CFile stdout() { return __stdout; }
|
||||
macro CFile stderr() { return __stderr; }
|
||||
@@ -113,15 +113,15 @@ $case OsType.MACOSX:
|
||||
extern CFile __stdinp;
|
||||
extern CFile __stdoutp;
|
||||
extern CFile __stderrp;
|
||||
extern fn usize malloc_size(void* ptr);
|
||||
extern fn void* aligned_alloc(usize align, usize size);
|
||||
extern fn usz malloc_size(void* ptr);
|
||||
extern fn void* aligned_alloc(usz align, usz size);
|
||||
macro CFile stdin() { return __stdinp; }
|
||||
macro CFile stdout() { return __stdoutp; }
|
||||
macro CFile stderr() { return __stderrp; }
|
||||
$case OsType.WIN32:
|
||||
extern fn CFile __acrt_iob_func(CInt c);
|
||||
extern fn usize _msize(void* ptr);
|
||||
macro usize malloc_size(void* ptr) { return _msize(ptr); }
|
||||
extern fn usz _msize(void* ptr);
|
||||
macro usz malloc_size(void* ptr) { return _msize(ptr); }
|
||||
macro CFile stdin() { return __acrt_iob_func(0); }
|
||||
macro CFile stdout() { return __acrt_iob_func(1); }
|
||||
macro CFile stderr() { return __acrt_iob_func(2); }
|
||||
@@ -159,22 +159,22 @@ extern fn int ferror(CFile stream);
|
||||
extern fn int fflush(CFile stream);
|
||||
extern fn int fgetpos(CFile stream, Fpos* pos);
|
||||
extern fn CFile fopen(char* filename, char* mode);
|
||||
extern fn usize fread(void* ptr, usize size, usize nmemb, CFile stream);
|
||||
extern fn usz fread(void* ptr, usz size, usz nmemb, CFile stream);
|
||||
extern fn CFile freopen(char* filename, char* mode, CFile stream);
|
||||
extern fn int fseek(CFile stream, SeekIndex offset, int whence);
|
||||
extern fn int fsetpos(CFile stream, Fpos* pos);
|
||||
extern fn SeekIndex ftell(CFile stream);
|
||||
extern fn usize fwrite(void* ptr, usize size, usize nmemb, CFile stream);
|
||||
extern fn usz fwrite(void* ptr, usz size, usz nmemb, CFile stream);
|
||||
extern fn int remove(char* filename);
|
||||
extern fn int rename(char* old_name, char* new_name);
|
||||
extern fn void rewind(CFile stream);
|
||||
extern fn void setbuf(CFile stream, char* buffer);
|
||||
extern fn void setvbuf(CFile stream, char* buffer, int mode, usize size);
|
||||
extern fn void setvbuf(CFile stream, char* buffer, int mode, usz size);
|
||||
extern fn CFile tmpnam(char* str);
|
||||
extern fn int fprintf(CFile stream, char* format, ...);
|
||||
extern fn int printf(char* format, ...);
|
||||
extern fn int sprintf(char* str, char* format, ...);
|
||||
extern fn int snprintf(char* str, usize size, char* format, ...);
|
||||
extern fn int snprintf(char* str, usz size, char* format, ...);
|
||||
extern fn int fscanf(CFile stream, char* format, ...);
|
||||
extern fn int scanf(char* format, ...);
|
||||
extern fn int sscanf(char* str, char* format, ...);
|
||||
@@ -188,7 +188,7 @@ extern fn int putchar(int c);
|
||||
extern fn int puts(char* str);
|
||||
extern fn int ungetc(int c, CFile stream);
|
||||
extern fn void perror(char* str);
|
||||
extern fn isize getline(char** linep, usize* linecapp, CFile stream);
|
||||
extern fn isz getline(char** linep, usz* linecapp, CFile stream);
|
||||
|
||||
// vsprintf vprintf not supported
|
||||
|
||||
@@ -225,7 +225,7 @@ extern fn double difftime(Time time1, Time time2);
|
||||
extern fn Tm* gmtime(Time *timer);
|
||||
extern fn Tm* localtime(Time *timer);
|
||||
extern fn Time mktime(Tm *timeptr);
|
||||
extern fn usize strftime(char* str, usize maxsize, char* format, Tm *timeptr);
|
||||
extern fn usz strftime(char* str, usz maxsize, char* format, Tm *timeptr);
|
||||
extern fn Time time(Time *timer);
|
||||
|
||||
// signal
|
||||
|
||||
@@ -12,7 +12,7 @@ private struct Node
|
||||
|
||||
struct LinkedList
|
||||
{
|
||||
usize size;
|
||||
usz size;
|
||||
Node *first;
|
||||
Node *last;
|
||||
}
|
||||
@@ -69,12 +69,12 @@ fn void LinkedList.free(LinkedList *list)
|
||||
list.size = 0;
|
||||
}
|
||||
|
||||
fn usize LinkedList.len(LinkedList* list) @inline
|
||||
fn usz LinkedList.len(LinkedList* list) @inline
|
||||
{
|
||||
return list.size;
|
||||
}
|
||||
|
||||
fn Type LinkedList.get(LinkedList* list, usize index)
|
||||
fn Type LinkedList.get(LinkedList* list, usz index)
|
||||
{
|
||||
Node* node = list.first;
|
||||
while (index--)
|
||||
|
||||
@@ -5,8 +5,8 @@ module std::array::list<Type>;
|
||||
|
||||
struct List
|
||||
{
|
||||
usize size;
|
||||
usize capacity;
|
||||
usz size;
|
||||
usz capacity;
|
||||
Type *entries;
|
||||
}
|
||||
|
||||
@@ -49,9 +49,9 @@ fn Type List.pop_first(List *list)
|
||||
return value;
|
||||
}
|
||||
|
||||
fn void List.remove_at(List *list, usize index)
|
||||
fn void List.remove_at(List *list, usz index)
|
||||
{
|
||||
for (usize i = index + 1; i < list.size; i++)
|
||||
for (usz i = index + 1; i < list.size; i++)
|
||||
{
|
||||
list.entries[i - 1] = list.entries[i];
|
||||
}
|
||||
@@ -63,10 +63,10 @@ fn void List.push_front(List *list, Type type) @inline
|
||||
list.insert_at(0, type);
|
||||
}
|
||||
|
||||
fn void List.insert_at(List* list, usize index, Type type)
|
||||
fn void List.insert_at(List* list, usz index, Type type)
|
||||
{
|
||||
list.ensure_capacity();
|
||||
for (usize i = list.size; i > index; i--)
|
||||
for (usz i = list.size; i > index; i--)
|
||||
{
|
||||
list.entries[i] = list.entries[i - 1];
|
||||
}
|
||||
@@ -99,12 +99,12 @@ fn bool List.is_empty(List *list)
|
||||
return !list.size;
|
||||
}
|
||||
|
||||
fn usize List.len(List *list) @operator(len)
|
||||
fn usz List.len(List *list) @operator(len)
|
||||
{
|
||||
return list.size;
|
||||
}
|
||||
|
||||
fn Type List.get(List *list, usize index)
|
||||
fn Type List.get(List *list, usz index)
|
||||
{
|
||||
return list.entries[index];
|
||||
}
|
||||
@@ -117,17 +117,17 @@ fn void List.free(List *list)
|
||||
list.entries = null;
|
||||
}
|
||||
|
||||
fn void List.swap(List *list, usize i, usize j)
|
||||
fn void List.swap(List *list, usz i, usz j)
|
||||
{
|
||||
@swap(list.entries[i], list.entries[j]);
|
||||
}
|
||||
|
||||
macro Type List.@item_at(List &list, usize index) @operator([])
|
||||
macro Type List.@item_at(List &list, usz index) @operator([])
|
||||
{
|
||||
return list.entries[index];
|
||||
}
|
||||
|
||||
macro Type* List.@item_ref(List &list, usize index) @operator(&[])
|
||||
macro Type* List.@item_ref(List &list, usz index) @operator(&[])
|
||||
{
|
||||
return &list.entries[index];
|
||||
}
|
||||
|
||||
@@ -122,7 +122,7 @@ fn Key[] HashMap.key_list(HashMap* map, Allocator* allocator = mem::temp_allocat
|
||||
if (!map.count) return Key[] {};
|
||||
|
||||
Key[] list = array::make(Key, map.count, allocator);
|
||||
usize index = 0;
|
||||
usz index = 0;
|
||||
foreach (Entry* entry : map.table)
|
||||
{
|
||||
while (entry)
|
||||
@@ -138,7 +138,7 @@ fn Value[] HashMap.value_list(HashMap* map, Allocator* allocator = mem::temp_all
|
||||
{
|
||||
if (!map.count) return Value[] {};
|
||||
Value[] list = array::make(Value, map.count, allocator);
|
||||
usize index = 0;
|
||||
usz index = 0;
|
||||
foreach (Entry* entry : map.table)
|
||||
{
|
||||
while (entry)
|
||||
|
||||
@@ -34,10 +34,10 @@ struct PriorityQueue
|
||||
fn void PriorityQueue.push(PriorityQueue* pq, Type element)
|
||||
{
|
||||
pq.heap.push(element);
|
||||
usize i = pq.heap.len() - 1;
|
||||
usz i = pq.heap.len() - 1;
|
||||
while (i > 0)
|
||||
{
|
||||
usize parent = (i - 1) / 2;
|
||||
usz parent = (i - 1) / 2;
|
||||
if ((pq.max && greater(pq.heap.get(i), pq.heap.get(parent))) || (!pq.max && less(pq.heap.get(i), pq.heap.get(parent))))
|
||||
{
|
||||
pq.heap.swap(i, parent);
|
||||
@@ -53,14 +53,14 @@ fn void PriorityQueue.push(PriorityQueue* pq, Type element)
|
||||
*/
|
||||
fn Type! PriorityQueue.pop(PriorityQueue* pq)
|
||||
{
|
||||
usize i = 0;
|
||||
usize len = pq.heap.len() @inline;
|
||||
usz i = 0;
|
||||
usz len = pq.heap.len() @inline;
|
||||
if (!len) return IteratorResult.NO_MORE_ELEMENT!;
|
||||
usize newCount = len - 1;
|
||||
usz newCount = len - 1;
|
||||
pq.heap.swap(0, newCount);
|
||||
while ((2 * i + 1) < newCount)
|
||||
{
|
||||
usize j = 2 * i + 1;
|
||||
usz j = 2 * i + 1;
|
||||
if (((j + 1) < newCount) &&
|
||||
((pq.max && greater(pq.heap.get(j + 1), pq.heap[j]))
|
||||
|| (!pq.max && less(pq.heap.get(j + 1), pq.heap.get(j)))))
|
||||
@@ -99,7 +99,7 @@ fn void PriorityQueue.free(PriorityQueue* pq)
|
||||
/**
|
||||
* @require pq != null
|
||||
*/
|
||||
fn usize PriorityQueue.len(PriorityQueue* pq) @operator(len)
|
||||
fn usz PriorityQueue.len(PriorityQueue* pq) @operator(len)
|
||||
{
|
||||
return pq.heap.len();
|
||||
}
|
||||
|
||||
@@ -18,13 +18,13 @@ struct VirtualContainer
|
||||
struct SubArrayContainer
|
||||
{
|
||||
void* ptr;
|
||||
usize len;
|
||||
usz len;
|
||||
}
|
||||
|
||||
struct VarArrayHeader
|
||||
{
|
||||
usize size;
|
||||
usize capacity;
|
||||
usz size;
|
||||
usz capacity;
|
||||
void *allocator;
|
||||
}
|
||||
|
||||
|
||||
@@ -1797,8 +1797,8 @@ extern TypeInfo *poisoned_type_info;
|
||||
|
||||
extern Type *type_bool, *type_void, *type_voidptr;
|
||||
extern Type *type_float16, *type_float, *type_double, *type_f128;
|
||||
extern Type *type_ichar, *type_short, *type_int, *type_long, *type_isize;
|
||||
extern Type *type_char, *type_ushort, *type_uint, *type_ulong, *type_usize;
|
||||
extern Type *type_ichar, *type_short, *type_int, *type_long, *type_isize, *type_isz;
|
||||
extern Type *type_char, *type_ushort, *type_uint, *type_ulong, *type_usize, *type_usz;
|
||||
extern Type *type_iptr, *type_uptr, *type_iptrdiff, *type_uptrdiff;
|
||||
extern Type *type_u128, *type_i128;
|
||||
extern Type *type_typeid, *type_anyerr, *type_typeinfo, *type_member;
|
||||
|
||||
@@ -463,6 +463,7 @@ typedef enum
|
||||
TOKEN_IPTR,
|
||||
TOKEN_IPTRDIFF,
|
||||
TOKEN_ISIZE,
|
||||
TOKEN_ISZ,
|
||||
TOKEN_LONG,
|
||||
TOKEN_SHORT,
|
||||
TOKEN_UINT128,
|
||||
@@ -472,6 +473,7 @@ typedef enum
|
||||
TOKEN_UPTRDIFF,
|
||||
TOKEN_USHORT,
|
||||
TOKEN_USIZE,
|
||||
TOKEN_USZ,
|
||||
TOKEN_FLOAT128,
|
||||
TOKEN_VARIANT,
|
||||
TOKEN_ANYERR,
|
||||
@@ -595,8 +597,8 @@ typedef enum
|
||||
case TOKEN_FLOAT16: case TOKEN_INT128: case TOKEN_ICHAR: case TOKEN_INT: \
|
||||
case TOKEN_IPTR: case TOKEN_IPTRDIFF: case TOKEN_ISIZE: case TOKEN_LONG: \
|
||||
case TOKEN_SHORT: case TOKEN_UINT128: case TOKEN_UINT: case TOKEN_ULONG: \
|
||||
case TOKEN_UPTR: case TOKEN_UPTRDIFF: case TOKEN_USHORT: case TOKEN_USIZE: \
|
||||
case TOKEN_FLOAT128: case TOKEN_TYPEID: case TOKEN_ANYERR: case TOKEN_VARIANT
|
||||
case TOKEN_UPTR: case TOKEN_UPTRDIFF: case TOKEN_USHORT: case TOKEN_USIZE:\
|
||||
case TOKEN_USZ: case TOKEN_ISZ: case TOKEN_FLOAT128: case TOKEN_TYPEID: case TOKEN_ANYERR: case TOKEN_VARIANT
|
||||
#define TYPE_TOKENS NON_VOID_TYPE_TOKENS: case TOKEN_VOID
|
||||
#define TYPELIKE_TOKENS TYPE_TOKENS: case TOKEN_TYPE_IDENT: \
|
||||
case TOKEN_CT_TYPE_IDENT: case TOKEN_CT_TYPEOF: case TOKEN_CT_EVALTYPE: \
|
||||
|
||||
@@ -1680,6 +1680,8 @@ ParseRule rules[TOKEN_EOF + 1] = {
|
||||
[TOKEN_UINT128] = { parse_type_identifier, NULL, PREC_NONE },
|
||||
[TOKEN_ISIZE] = { parse_type_identifier, NULL, PREC_NONE },
|
||||
[TOKEN_USIZE] = { parse_type_identifier, NULL, PREC_NONE },
|
||||
[TOKEN_ISZ] = { parse_type_identifier, NULL, PREC_NONE },
|
||||
[TOKEN_USZ] = { parse_type_identifier, NULL, PREC_NONE },
|
||||
[TOKEN_IPTR] = { parse_type_identifier, NULL, PREC_NONE },
|
||||
[TOKEN_UPTR] = { parse_type_identifier, NULL, PREC_NONE },
|
||||
[TOKEN_IPTRDIFF] = { parse_type_identifier, NULL, PREC_NONE },
|
||||
|
||||
@@ -307,6 +307,10 @@ const char *token_type_to_string(TokenType type)
|
||||
return "isize";
|
||||
case TOKEN_USIZE:
|
||||
return "usize";
|
||||
case TOKEN_ISZ:
|
||||
return "isz";
|
||||
case TOKEN_USZ:
|
||||
return "usz";
|
||||
case TOKEN_IPTR:
|
||||
return "iptr";
|
||||
case TOKEN_UPTR:
|
||||
|
||||
@@ -11,7 +11,7 @@ static struct
|
||||
Type u0, u1, i8, i16, i32, i64, i128, ixx;
|
||||
Type u8, u16, u32, u64, u128;
|
||||
Type f16, f32, f64, f128, fxx;
|
||||
Type usz, isz, uptr, iptr, uptrdiff, iptrdiff;
|
||||
Type usz, isz, usize, isize, uptr, iptr, uptrdiff, iptrdiff;
|
||||
Type voidstar, typeid, anyerr, member, typeinfo, untyped_list;
|
||||
Type any, anyfail;
|
||||
} t;
|
||||
@@ -33,7 +33,8 @@ Type *type_long = &t.i64;
|
||||
Type *type_i128 = &t.i128;
|
||||
Type *type_iptr = &t.iptr;
|
||||
Type *type_iptrdiff = &t.iptrdiff;
|
||||
Type *type_isize = &t.isz;
|
||||
Type *type_isize = &t.isize;
|
||||
Type *type_isz = &t.isz;
|
||||
Type *type_char = &t.u8;
|
||||
Type *type_ushort = &t.u16;
|
||||
Type *type_uint = &t.u32;
|
||||
@@ -41,7 +42,8 @@ Type *type_ulong = &t.u64;
|
||||
Type *type_u128 = &t.u128;
|
||||
Type *type_uptr = &t.uptr;
|
||||
Type *type_uptrdiff = &t.uptrdiff;
|
||||
Type *type_usize = &t.usz;
|
||||
Type *type_usize = &t.usize;
|
||||
Type *type_usz = &t.usz;
|
||||
Type *type_anyerr = &t.anyerr;
|
||||
Type *type_untypedlist = &t.untyped_list;
|
||||
Type *type_anyfail = &t.anyfail;
|
||||
@@ -1436,8 +1438,10 @@ void type_setup(PlatformTarget *target)
|
||||
t.voidstar.pointer = type_void;
|
||||
type_init("variant", &t.any, TYPE_ANY, target->width_pointer * 2, target->align_pointer);
|
||||
|
||||
type_create_alias("usize", &t.usz, type_int_unsigned_by_bitsize(target->width_pointer));
|
||||
type_create_alias("isize", &t.isz, type_int_signed_by_bitsize(target->width_pointer));
|
||||
type_create_alias("usize", &t.usize, type_int_unsigned_by_bitsize(target->width_pointer));
|
||||
type_create_alias("isize", &t.isize, type_int_signed_by_bitsize(target->width_pointer));
|
||||
type_create_alias("usz", &t.usz, type_int_unsigned_by_bitsize(target->width_pointer));
|
||||
type_create_alias("isz", &t.isz, type_int_signed_by_bitsize(target->width_pointer));
|
||||
type_create_alias("uptr", &t.uptr, type_int_unsigned_by_bitsize(target->width_pointer));
|
||||
type_create_alias("iptr", &t.iptr, type_int_signed_by_bitsize(target->width_pointer));
|
||||
|
||||
@@ -1583,6 +1587,8 @@ Type *type_from_token(TokenType type)
|
||||
return type_iptrdiff;
|
||||
case TOKEN_ISIZE:
|
||||
return type_isize;
|
||||
case TOKEN_ISZ:
|
||||
return type_isz;
|
||||
case TOKEN_LONG:
|
||||
return type_long;
|
||||
case TOKEN_SHORT:
|
||||
@@ -1601,6 +1607,8 @@ Type *type_from_token(TokenType type)
|
||||
return type_ushort;
|
||||
case TOKEN_USIZE:
|
||||
return type_usize;
|
||||
case TOKEN_USZ:
|
||||
return type_usz;
|
||||
case TOKEN_TYPEID:
|
||||
return type_typeid;
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user