Complete transition to fn. Introduce global/threadlocal

This commit is contained in:
Christoffer Lerno
2021-11-16 17:19:12 +01:00
committed by Christoffer Lerno
parent e2621617f1
commit b52b42d4da
331 changed files with 1279 additions and 1261 deletions

View File

@@ -8,18 +8,18 @@ struct Adler32
uint b;
}
func void Adler32.init(Adler32 *this)
fn void Adler32.init(Adler32 *this)
{
*this = { 1, 0 };
}
func void Adler32.updatec(Adler32* this, char c)
fn void Adler32.updatec(Adler32* this, char c)
{
this.a = (this.a + c) % ADLER_CONST;
this.b = (this.b + this.a) % ADLER_CONST;
}
func void Adler32.update(Adler32* this, char[] data)
fn void Adler32.update(Adler32* this, char[] data)
{
uint a = this.a;
uint b = this.b;
@@ -31,12 +31,12 @@ func void Adler32.update(Adler32* this, char[] data)
*this = { a, b };
}
func uint Adler32.final(Adler32* this)
fn uint Adler32.final(Adler32* this)
{
return (this.b << 16) | this.a;
}
func uint encode(char[] data)
fn uint encode(char[] data)
{
uint a = 1;
uint b = 0;

View File

@@ -5,17 +5,17 @@ struct Crc32
uint result;
}
func void Crc32.init(Crc32* this, uint seed = 0)
fn void Crc32.init(Crc32* this, uint seed = 0)
{
this.result = ~seed;
}
func void Crc32.updatec(Crc32* this, char c)
fn void Crc32.updatec(Crc32* this, char c)
{
this.result = (this.result >> 8) ^ CRC32_TABLE[(this.result ^ c) & 0xFF];
}
func void Crc32.update(Crc32* this, char[] data)
fn void Crc32.update(Crc32* this, char[] data)
{
uint result = this.result;
foreach (char x : data)
@@ -25,12 +25,12 @@ func void Crc32.update(Crc32* this, char[] data)
this.result = result;
}
func uint Crc32.final(Crc32* this)
fn uint Crc32.final(Crc32* this)
{
return ~this.result;
}
func uint encode(char[] data)
fn uint encode(char[] data)
{
uint result = ~(uint)(0);
foreach (char x : data)

View File

@@ -5,17 +5,17 @@ struct Crc64
ulong result;
}
func void Crc64.init(Crc64* this, uint seed = 0)
fn void Crc64.init(Crc64* this, uint seed = 0)
{
this.result = seed;
}
func void Crc64.updatec(Crc64* this, char c)
fn void Crc64.updatec(Crc64* this, char c)
{
this.result = (this.result << 8) ^ CRC64_TABLE[(char)((this.result >> 56) ^ c)];
}
func void Crc64.update(Crc64* this, char[] data)
fn void Crc64.update(Crc64* this, char[] data)
{
ulong result = this.result;
foreach (char x : data)
@@ -25,12 +25,12 @@ func void Crc64.update(Crc64* this, char[] data)
this.result = result;
}
func ulong Crc64.final(Crc64* this)
fn ulong Crc64.final(Crc64* this)
{
return this.result;
}
func ulong encode(char[] data)
fn ulong encode(char[] data)
{
ulong result = (ulong)(0);
foreach (char x : data)

View File

@@ -29,18 +29,18 @@ struct File
void *file;
}
extern func int _puts(char* message) @extname("puts");
extern func int printf(char* message, ...);
extern func int _putchar(char c) @extname("putchar");
extern fn int _puts(char* message) @extname("puts");
extern fn int printf(char* message, ...);
extern fn int _putchar(char c) @extname("putchar");
extern File *__stdinp;
func int putchar(char c) @inline
fn int putchar(char c) @inline
{
return _putchar(c);
}
func int print(char *message)
fn int print(char *message)
{
char* pointer = message;
while (*pointer != '\0')
@@ -51,13 +51,13 @@ func int print(char *message)
return 1;
}
func int println(char *message = "") @inline
fn int println(char *message = "") @inline
{
return _puts(message);
}
func void! File.open(File* file, char[] filename, char[] mode)
fn void! File.open(File* file, char[] filename, char[] mode)
{
char* filename_copy = mem::talloc(filename.len + 1)!!;

View File

@@ -15,12 +15,12 @@ struct LinkedList
Node *last;
}
func void LinkedList.push(LinkedList *list, Type value)
fn void LinkedList.push(LinkedList *list, Type value)
{
list.linkLast(value);
}
private func void LinkedList.linkFirst(LinkedList *list, Type value)
private fn void LinkedList.linkFirst(LinkedList *list, Type value)
{
Node *first = list.first;
Node *new_node = @mem::malloc(Node);
@@ -37,7 +37,7 @@ private func void LinkedList.linkFirst(LinkedList *list, Type value)
list.size++;
}
private func void LinkedList.linkLast(LinkedList *list, Type value)
private fn void LinkedList.linkLast(LinkedList *list, Type value)
{
Node *last = list.last;
Node *new_node = mem::alloc($sizeof(Node));
@@ -54,7 +54,7 @@ private func void LinkedList.linkLast(LinkedList *list, Type value)
list.size++;
}
func void LinkedList.free(LinkedList *list)
fn void LinkedList.free(LinkedList *list)
{
for (Node* node = list.first; node != null;)
{
@@ -67,12 +67,12 @@ func void LinkedList.free(LinkedList *list)
list.size = 0;
}
func usize LinkedList.len(LinkedList* list) @inline
fn usize LinkedList.len(LinkedList* list) @inline
{
return list.size;
}
func Type LinkedList.get(LinkedList* list, usize index)
fn Type LinkedList.get(LinkedList* list, usize index)
{
Node* node = list.first;
while (index--)
@@ -84,7 +84,7 @@ func Type LinkedList.get(LinkedList* list, usize index)
/**
* @require succ != null
**/
private func void LinkedList.linkBefore(LinkedList *list, Node *succ, Type value)
private fn void LinkedList.linkBefore(LinkedList *list, Node *succ, Type value)
{
Node* pred = succ.prev;
Node* new_node = @mem::malloc(Node);
@@ -104,7 +104,7 @@ private func void LinkedList.linkBefore(LinkedList *list, Node *succ, Type value
/**
* @require f == list.first && f != null
**/
private func void unlinkFirst(LinkedList* list, Node* f)
private fn void unlinkFirst(LinkedList* list, Node* f)
{
Node* next = f.next;
mem::free(f);
@@ -123,7 +123,7 @@ private func void unlinkFirst(LinkedList* list, Node* f)
/**
* @require l == list.last && l != null
**/
private func void LinkedList.unlinkLast(LinkedList *list, Node* l)
private fn void LinkedList.unlinkLast(LinkedList *list, Node* l)
{
Node* prev = l.prev;
list.last = prev;
@@ -142,7 +142,7 @@ private func void LinkedList.unlinkLast(LinkedList *list, Node* l)
/**
* @require x != null
**/
private func void LinkedList.unlink(LinkedList* list, Node* x)
private fn void LinkedList.unlink(LinkedList* list, Node* x)
{
Node* next = x.next;
Node* prev = x.prev;

View File

@@ -8,7 +8,7 @@ struct List
Type *entries;
}
private func void List.ensureCapacity(List *list) @inline
private fn void List.ensureCapacity(List *list) @inline
{
if (list.capacity == list.size)
{
@@ -17,12 +17,12 @@ private func void List.ensureCapacity(List *list) @inline
}
}
func void List.push(List *list, Type element) @inline
fn void List.push(List *list, Type element) @inline
{
list.append(element);
}
func void List.append(List *list, Type element)
fn void List.append(List *list, Type element)
{
list.ensureCapacity();
list.entries[list.size++] = element;
@@ -31,7 +31,7 @@ func void List.append(List *list, Type element)
/**
* @require list.size > 0
*/
func Type List.pop(List *list)
fn Type List.pop(List *list)
{
return list.entries[--list.size];
}
@@ -39,14 +39,14 @@ func Type List.pop(List *list)
/**
* @require list.size > 0
*/
func Type List.popFirst(List *list)
fn Type List.popFirst(List *list)
{
Type value = list.entries[0];
list.removeAt(0);
return value;
}
func void List.removeAt(List *list, usize index)
fn void List.removeAt(List *list, usize index)
{
for (usize i = index + 1; i < list.size; i++)
{
@@ -55,12 +55,12 @@ func void List.removeAt(List *list, usize index)
list.size--;
}
func void List.pushFront(List *list, Type type) @inline
fn void List.pushFront(List *list, Type type) @inline
{
list.insertAt(0, type);
}
func void List.insertAt(List *list, usize index, Type type)
fn void List.insertAt(List *list, usize index, Type type)
{
list.ensureCapacity();
for (usize i = list.size; i > index; i--)
@@ -71,42 +71,42 @@ func void List.insertAt(List *list, usize index, Type type)
list.entries[index] = type;
}
func void List.removeLast(List *list)
fn void List.removeLast(List *list)
{
list.size--;
}
func void List.removeFirst(List *list)
fn void List.removeFirst(List *list)
{
list.removeAt(0);
}
func Type* List.first(List *list)
fn Type* List.first(List *list)
{
return list.size ? &list.entries[0] : null;
}
func Type* List.last(List *list)
fn Type* List.last(List *list)
{
return list.size ? &list.entries[list.size - 1] : null;
}
func bool List.isEmpty(List *list)
fn bool List.isEmpty(List *list)
{
return list.size;
}
func usize List.len(List *list)
fn usize List.len(List *list)
{
return list.size;
}
func Type List.get(List *list, usize index)
fn Type List.get(List *list, usize index)
{
return list.entries[index];
}
func void List.free(List *list)
fn void List.free(List *list)
{
mem::free(list.entries);
list.capacity = 0;

View File

@@ -1,9 +1,9 @@
module std::mem;
extern func void* _malloc(usize bytes) @extname("malloc");
extern func void* _realloc(void* ptr, usize bytes) @extname("realloc");
extern func void* _calloc(usize bytes, usize elements) @extname("calloc");
extern func void _free(void* ptr) @extname("free");
extern fn void* _malloc(usize bytes) @extname("malloc");
extern fn void* _realloc(void* ptr, usize bytes) @extname("realloc");
extern fn void* _calloc(usize bytes, usize elements) @extname("calloc");
extern fn void _free(void* ptr) @extname("free");
macro volatile_load(&x)
{
@@ -27,7 +27,7 @@ errtype AllocationFailure
OUT_OF_MEMORY
}
define AllocatorFunction = func void!(void *data, void** pointer, usize bytes, usize alignment, AllocationKind kind);
define AllocatorFunction = fn void!(void *data, void** pointer, usize bytes, usize alignment, AllocationKind kind);
struct Allocator
{
@@ -35,12 +35,12 @@ struct Allocator
void *data;
}
func void copy(char* dst, char* src, usize size)
fn void copy(char* dst, char* src, usize size)
{
for (usize i = 0; i < size; i++) dst[i] = src[i];
}
func void! system_malloc_function(void *unused, void** pointer, usize bytes, usize alignment, AllocationKind kind) @inline
fn void! system_malloc_function(void *unused, void** pointer, usize bytes, usize alignment, AllocationKind kind) @inline
{
switch (kind)
{
@@ -72,7 +72,7 @@ struct SlotAllocator
usize current_page;
}
func void*! SlotAllocator.alloc(SlotAllocator *allocator, usize size)
fn void*! SlotAllocator.alloc(SlotAllocator *allocator, usize size)
{
void* active_page = (char*)(allocator.pages) + allocator.current_page * allocator.page_size;
void** page_pointer = (void**)(active_page);
@@ -101,7 +101,7 @@ struct RingAllocator
}
func void* RingAllocator.alloc(RingAllocator *allocator, usize size)
fn void* RingAllocator.alloc(RingAllocator *allocator, usize size)
{
if (size > allocator.size) return null;
// Wraparound? If so, start at the beginning.
@@ -115,7 +115,7 @@ func void* RingAllocator.alloc(RingAllocator *allocator, usize size)
return data;
}
func void* RingAllocator.realloc(RingAllocator *allocator, void* ptr, usize size)
fn void* RingAllocator.realloc(RingAllocator *allocator, void* ptr, usize size)
{
if (size > allocator.size) return null;
assert(allocator.data >= ptr && ptr < allocator.data + size, "Realloc on other allocator.");
@@ -159,22 +159,22 @@ macro malloc($Type)
return ($Type*)(mem::alloc($sizeof($Type)));
}
func void* alloc(usize size, usize count = 1) @inline
fn void* alloc(usize size, usize count = 1) @inline
{
return _malloc(size * count);
}
func void* calloc(usize size, usize elements = 1) @inline
fn void* calloc(usize size, usize elements = 1) @inline
{
return _calloc(size, elements);
}
func void* realloc(void *ptr, usize size) @inline
fn void* realloc(void *ptr, usize size) @inline
{
return _realloc(ptr, size);
}
func void free(void* ptr) @inline
fn void free(void* ptr) @inline
{
_free(ptr);
}
@@ -194,7 +194,7 @@ SlotAllocator default_allocator = {
.current_page = 0,
};
func void*! talloc(usize size)
fn void*! talloc(usize size)
{
return default_allocator.alloc(size);
}