Fix ordering of @builtin. malloc <-> alloc, malloc, calloc, realloc, free builtins.

This commit is contained in:
Christoffer Lerno
2022-08-04 01:35:35 +02:00
parent f966250185
commit 6d2ab0c985
22 changed files with 158 additions and 113 deletions

View File

@@ -25,7 +25,7 @@ private fn void*! arena_allocator_function(Allocator* data, usize size, usize al
if (!size) return null; if (!size) return null;
alignment = alignment_for_allocation(alignment); alignment = alignment_for_allocation(alignment);
void* mem = arena._alloc(size, alignment, offset)?; void* mem = arena._alloc(size, alignment, offset)?;
if (clear) mem::memset(mem, 0, size, false, DEFAULT_MEM_ALIGNMENT); if (clear) mem::clear(mem, size, DEFAULT_MEM_ALIGNMENT);
return mem; return mem;
case ALIGNED_REALLOC: case ALIGNED_REALLOC:
case REALLOC: case REALLOC:
@@ -114,6 +114,6 @@ private fn void*! ArenaAllocator._realloc(ArenaAllocator* this, void *old_pointe
} }
// Otherwise just allocate new memory. // Otherwise just allocate new memory.
void* mem = this._alloc(size, alignment, offset)?; void* mem = this._alloc(size, alignment, offset)?;
mem::memcpy(mem, old_pointer, old_size, false, DEFAULT_MEM_ALIGNMENT, DEFAULT_MEM_ALIGNMENT); mem::copy(mem, old_pointer, old_size, DEFAULT_MEM_ALIGNMENT, DEFAULT_MEM_ALIGNMENT);
return mem; return mem;
} }

View File

@@ -58,7 +58,7 @@ private fn void*! DynamicArenaAllocator._realloc(DynamicArenaAllocator* this, vo
return old_pointer; return old_pointer;
} }
void* new_mem = this._alloc(size, alignment, offset)?; void* new_mem = this._alloc(size, alignment, offset)?;
mem::memcpy(new_mem, old_pointer, old_size, false, DEFAULT_MEM_ALIGNMENT); mem::copy(new_mem, old_pointer, old_size, DEFAULT_MEM_ALIGNMENT);
return new_mem; return new_mem;
} }
@@ -164,7 +164,7 @@ private fn void*! dynamic_arena_allocator_function(Allocator* data, usize size,
assert(!old_pointer, "Unexpected no old pointer for calloc."); assert(!old_pointer, "Unexpected no old pointer for calloc.");
if (!size) return null; if (!size) return null;
void* mem = allocator._alloc(size, alignment, offset)?; void* mem = allocator._alloc(size, alignment, offset)?;
mem::memset(mem, 0, size, false, DEFAULT_MEM_ALIGNMENT); mem::clear(mem, size, DEFAULT_MEM_ALIGNMENT);
return mem; return mem;
case ALLOC: case ALLOC:
case ALIGNED_ALLOC: case ALIGNED_ALLOC:

View File

@@ -66,7 +66,7 @@ private fn void* _libc_aligned_realloc(void* old_pointer, usize bytes, usize ali
AlignedBlock* desc = (AlignedBlock*)old_pointer - 1; AlignedBlock* desc = (AlignedBlock*)old_pointer - 1;
void* data_start = desc.start; void* data_start = desc.start;
void* new_data = _libc_aligned_calloc(bytes, alignment, offset); void* new_data = _libc_aligned_calloc(bytes, alignment, offset);
mem::memcpy(new_data, old_pointer, desc.len > bytes ? desc.len : bytes, false, DEFAULT_MEM_ALIGNMENT, DEFAULT_MEM_ALIGNMENT); mem::copy(new_data, old_pointer, desc.len > bytes ? desc.len : bytes, DEFAULT_MEM_ALIGNMENT, DEFAULT_MEM_ALIGNMENT);
libc::free(data_start); libc::free(data_start);
return new_data; return new_data;
} }

View File

@@ -134,7 +134,7 @@ private fn void*! TempAllocator._realloc_page(TempAllocator* this, TempAllocator
usize page_size = page.pagesize(); usize page_size = page.pagesize();
// Clear on size > original size. // Clear on size > original size.
void* data = this._alloc(size, alignment, offset, false)?; void* data = this._alloc(size, alignment, offset, false)?;
mem::memcpy(data, &page.data[0], page_size, false, DEFAULT_MEM_ALIGNMENT, DEFAULT_MEM_ALIGNMENT); mem::copy(data, &page.data[0], page_size, DEFAULT_MEM_ALIGNMENT, DEFAULT_MEM_ALIGNMENT);
if (page.is_aligned()) if (page.is_aligned())
{ {
this.backing_allocator.free_aligned(real_pointer)?; this.backing_allocator.free_aligned(real_pointer)?;
@@ -161,7 +161,7 @@ private fn void*! TempAllocator._realloc(TempAllocator* this, void* pointer, usi
// TODO optimize last allocation // TODO optimize last allocation
TempAllocatorChunk* data = this._alloc(size, alignment, offset, size > chunk.size)?; TempAllocatorChunk* data = this._alloc(size, alignment, offset, size > chunk.size)?;
mem::memcpy(data, pointer, chunk.size, false, DEFAULT_MEM_ALIGNMENT, DEFAULT_MEM_ALIGNMENT); mem::copy(data, pointer, chunk.size, DEFAULT_MEM_ALIGNMENT, DEFAULT_MEM_ALIGNMENT);
return data; return data;
} }
@@ -190,7 +190,7 @@ private fn void*! TempAllocator._alloc(TempAllocator* this, usize size, usize al
TempAllocatorChunk* chunk_start = mem - TempAllocatorChunk.sizeof; TempAllocatorChunk* chunk_start = mem - TempAllocatorChunk.sizeof;
chunk_start.size = size; chunk_start.size = size;
this.used = new_usage; this.used = new_usage;
if (clear) mem::memset(mem, 0, size, false, DEFAULT_MEM_ALIGNMENT); if (clear) mem::clear(mem, size, DEFAULT_MEM_ALIGNMENT);
return mem; return mem;
} }

View File

@@ -97,7 +97,7 @@ macro bitcast(expr, $Type) @builtin
var $size = (usize)($sizeof(expr)); var $size = (usize)($sizeof(expr));
$assert($size == $Type.sizeof, "Cannot bitcast between types of different size."); $assert($size == $Type.sizeof, "Cannot bitcast between types of different size.");
$Type x = void; $Type x = void;
mem::memcpy(&x, &expr, $size, false, $alignof($Type), $alignof(expr)); mem::copy(&x, &expr, $size, $alignof($Type), $alignof(expr));
return x; return x;
} }

View File

@@ -35,24 +35,19 @@ fn bool ptr_is_aligned(void* ptr, usize alignment) @inline
return (uptr)ptr & ((uptr)alignment - 1) == 0; return (uptr)ptr & ((uptr)alignment - 1) == 0;
} }
fn void copy(char* dst, char* src, usize size) @inline macro void copy(void* dst, void* src, usize len, usize $dst_align = 0, usize $src_align = 0, bool $is_volatile = false)
{ {
memcpy(dst, src, size); $$memcpy(dst, src, len, $is_volatile, $dst_align, $src_align);
} }
macro void memcpy(void* dst, void* src, usize size, bool $is_volatile = false, usize $dst_align = 0, usize $src_align = 0) macro void set(void* dst, char val, usize len, usize $dst_align = 0, bool $is_volatile = false)
{ {
$$memcpy(dst, src, size, $is_volatile, $dst_align, $src_align); $$memset(dst, val, len, $is_volatile, $dst_align);
} }
fn void set(void* dst, char val, usize bytes) @inline macro void clear(void* dst, usize len, usize $dst_align = 0, bool $is_volatile = false)
{ {
memset(dst, val, bytes); $$memset(dst, (char)0, len, $is_volatile, $dst_align);
}
macro void memset(void* dst, char val, usize bytes, bool $is_volatile = false, usize $dst_align = 0)
{
$$memset(dst, val, bytes, $is_volatile, $dst_align);
} }
macro @clone(&value) @builtin macro @clone(&value) @builtin
@@ -62,22 +57,19 @@ macro @clone(&value) @builtin
return x; return x;
} }
macro malloc($Type) @builtin macro @tclone(&value) @builtin
{ {
return ($Type*)(mem::alloc($Type.sizeof)); $typeof(value)* x = talloc($typeof(value));
*x = value;
return x;
} }
fn char[] alloc_bytes(usize bytes) @inline fn void* malloc(usize size) @builtin @inline
{
return ((char*)thread_allocator.alloc(bytes))[:bytes]!!;
}
fn void* alloc(usize size)
{ {
return thread_allocator.alloc(size)!!; return thread_allocator.alloc(size)!!;
} }
fn void*! alloc_checked(usize size) fn void*! malloc_checked(usize size) @builtin @inline
{ {
return thread_allocator.alloc(size); return thread_allocator.alloc(size);
} }
@@ -85,17 +77,28 @@ fn void*! alloc_checked(usize size)
/** /**
* @require alignment && math::is_power_of_2(alignment) * @require alignment && math::is_power_of_2(alignment)
*/ */
fn void*! alloc_aligned(usize size, usize alignment) fn void*! malloc_aligned(usize size, usize alignment) @builtin @inline
{ {
return thread_allocator.alloc_aligned(size, alignment); return thread_allocator.alloc_aligned(size, alignment);
} }
fn void* calloc(usize size) fn char[] alloc_bytes(usize bytes) @inline
{
return ((char*)thread_allocator.alloc(bytes))[:bytes]!!;
}
macro alloc($Type)
{
return ($Type*)thread_allocator.alloc($Type.sizeof)!!;
}
fn void* calloc(usize size) @builtin @inline
{ {
return thread_allocator.calloc(size)!!; return thread_allocator.calloc(size)!!;
} }
fn void*! calloc_checked(usize size) fn void*! calloc_checked(usize size) @builtin @inline
{ {
return thread_allocator.calloc(size); return thread_allocator.calloc(size);
} }
@@ -103,18 +106,17 @@ fn void*! calloc_checked(usize size)
/** /**
* @require alignment && math::is_power_of_2(alignment) * @require alignment && math::is_power_of_2(alignment)
*/ */
fn void*! calloc_aligned(usize size, usize alignment) fn void*! calloc_aligned(usize size, usize alignment) @builtin @inline
{ {
return thread_allocator.calloc_aligned(size, alignment); return thread_allocator.calloc_aligned(size, alignment);
} }
fn void* realloc(void *ptr, usize new_size) @builtin @inline
fn void* realloc(void *ptr, usize new_size)
{ {
return thread_allocator.realloc(ptr, new_size)!!; return thread_allocator.realloc(ptr, new_size)!!;
} }
fn void*! realloc_checked(void *ptr, usize new_size) fn void*! realloc_checked(void *ptr, usize new_size) @builtin @inline
{ {
return thread_allocator.realloc(ptr, new_size); return thread_allocator.realloc(ptr, new_size);
} }
@@ -122,17 +124,17 @@ fn void*! realloc_checked(void *ptr, usize new_size)
/** /**
* @require alignment && math::is_power_of_2(alignment) * @require alignment && math::is_power_of_2(alignment)
*/ */
fn void*! realloc_aligned(void *ptr, usize new_size, usize alignment) fn void*! realloc_aligned(void *ptr, usize new_size, usize alignment) @builtin @inline
{ {
return thread_allocator.realloc_aligned(ptr, new_size, alignment); return thread_allocator.realloc_aligned(ptr, new_size, alignment);
} }
fn void free(void* ptr) @builtin fn void free(void* ptr) @builtin @inline
{ {
return thread_allocator.free(ptr)!!; return thread_allocator.free(ptr)!!;
} }
fn void free_aligned(void* ptr) fn void free_aligned(void* ptr) @builtin @inline
{ {
return thread_allocator.free_aligned(ptr)!!; return thread_allocator.free_aligned(ptr)!!;
} }
@@ -159,17 +161,22 @@ macro void @tscoped(;@body())
@body(); @body();
} }
fn void* talloc(usize size, usize alignment = 0) macro talloc($Type) @builtin
{
return temp_allocator().alloc_aligned($Type.sizeof, $alignof($Type))!!;
}
fn void* tmalloc(usize size, usize alignment = 0) @builtin @inline
{ {
return temp_allocator().alloc_aligned(size, alignment)!!; return temp_allocator().alloc_aligned(size, alignment)!!;
} }
fn void* tcalloc(usize size, usize alignment = 0) fn void* tcalloc(usize size, usize alignment = 0) @builtin @inline
{ {
return temp_allocator().calloc_aligned(size, alignment)!!; return temp_allocator().calloc_aligned(size, alignment)!!;
} }
fn void* trealloc(void* ptr, usize size, usize alignment = 0) fn void* trealloc(void* ptr, usize size, usize alignment = 0) @builtin @inline
{ {
return temp_allocator().realloc_aligned(ptr, size, alignment)!!; return temp_allocator().realloc_aligned(ptr, size, alignment)!!;
} }

View File

@@ -8,7 +8,7 @@ module std::core::mem::array;
**/ **/
macro alloc($Type, usize elements) macro alloc($Type, usize elements)
{ {
$Type* ptr = mem::alloc($Type.sizeof * elements, $alignof($Type)); $Type* ptr = malloc($Type.sizeof * elements);
return ptr[:elements]; return ptr[:elements];
} }
@@ -17,7 +17,7 @@ macro alloc($Type, usize elements)
**/ **/
macro talloc($Type, usize elements) macro talloc($Type, usize elements)
{ {
$Type* ptr = mem::talloc($Type.sizeof * elements, $alignof($Type[1])); $Type* ptr = tmalloc($Type.sizeof * elements, $alignof($Type[1]));
return ptr[:elements]; return ptr[:elements];
} }
@@ -26,7 +26,7 @@ macro talloc($Type, usize elements)
**/ **/
macro make($Type, usize elements) macro make($Type, usize elements)
{ {
$Type* ptr = mem::calloc($sizeof($Type) * elements, $alignof($Type[1])); $Type* ptr = calloc($sizeof($Type) * elements);
return ptr[:elements]; return ptr[:elements];
} }
@@ -35,6 +35,6 @@ macro make($Type, usize elements)
**/ **/
macro tmake($Type, usize elements) macro tmake($Type, usize elements)
{ {
$Type* ptr = mem::tcalloc($sizeof($Type) * elements, $alignof($Type[1])); $Type* ptr = tcalloc($sizeof($Type) * elements, $alignof($Type[1]));
return ptr[:elements]; return ptr[:elements];
} }

View File

@@ -32,7 +32,7 @@ fn String join(char[][] s, char[] joiner)
fn ZString copy_zstring(char[] s) fn ZString copy_zstring(char[] s)
{ {
usize len = s.len; usize len = s.len;
char* str = mem::alloc(len + 1); char* str = malloc(len + 1);
mem::copy(str, s.ptr, len); mem::copy(str, s.ptr, len);
str[len] = 0; str[len] = 0;
return (ZString)str; return (ZString)str;
@@ -41,7 +41,7 @@ fn ZString copy_zstring(char[] s)
fn ZString tcopy_zstring(char[] s) fn ZString tcopy_zstring(char[] s)
{ {
usize len = s.len; usize len = s.len;
char* str = mem::talloc(len + 1); char* str = tmalloc(len + 1);
mem::copy(str, s.ptr, len); mem::copy(str, s.ptr, len);
str[len] = 0; str[len] = 0;
return (ZString)str; return (ZString)str;
@@ -128,7 +128,7 @@ fn char[] tcopy(char[] s)
fn char[] tconcat(char[] s1, char[] s2) fn char[] tconcat(char[] s1, char[] s2)
{ {
usize full_len = s1.len + s2.len; usize full_len = s1.len + s2.len;
char* str = mem::talloc(full_len + 1); char* str = tmalloc(full_len + 1);
usize s1_len = s1.len; usize s1_len = s1.len;
mem::copy(str, s1.ptr, s1_len); mem::copy(str, s1.ptr, s1_len);
mem::copy(str + s1_len, s2.ptr, s2.len); mem::copy(str + s1_len, s2.ptr, s2.len);
@@ -139,7 +139,7 @@ fn char[] tconcat(char[] s1, char[] s2)
fn char[] concat(char[] s1, char[] s2) fn char[] concat(char[] s1, char[] s2)
{ {
usize full_len = s1.len + s2.len; usize full_len = s1.len + s2.len;
char* str = mem::alloc(full_len + 1); char* str = malloc(full_len + 1);
usize s1_len = s1.len; usize s1_len = s1.len;
mem::copy(str, s1.ptr, s1_len); mem::copy(str, s1.ptr, s1_len);
mem::copy(str + s1_len, s2.ptr, s2.len); mem::copy(str + s1_len, s2.ptr, s2.len);

View File

@@ -41,8 +41,8 @@ fn int println(char *message = "") @inline
fn 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); char* filename_copy = tmalloc(filename.len + 1);
char* mode_copy = mem::talloc(mode.len + 1); char* mode_copy = tmalloc(mode.len + 1);
mem::copy(filename_copy, (char*)(filename), filename.len); mem::copy(filename_copy, (char*)(filename), filename.len);
mem::copy(mode_copy, (char*)(mode), mode.len); mem::copy(mode_copy, (char*)(mode), mode.len);

View File

@@ -25,7 +25,7 @@ fn void LinkedList.push(LinkedList *list, Type value)
private fn void LinkedList.linkFirst(LinkedList *list, Type value) private fn void LinkedList.linkFirst(LinkedList *list, Type value)
{ {
Node *first = list.first; Node *first = list.first;
Node *new_node = mem::malloc(Node); Node *new_node = mem::alloc(Node);
*new_node = { .next = first, .value = value }; *new_node = { .next = first, .value = value };
list.first = new_node; list.first = new_node;
if (!first) if (!first)
@@ -42,7 +42,7 @@ private fn void LinkedList.linkFirst(LinkedList *list, Type value)
private fn void LinkedList.linkLast(LinkedList *list, Type value) private fn void LinkedList.linkLast(LinkedList *list, Type value)
{ {
Node *last = list.last; Node *last = list.last;
Node *new_node = mem::alloc(Node.sizeof); Node *new_node = mem::alloc(Node);
*new_node = { .prev = last, .value = value }; *new_node = { .prev = last, .value = value };
list.last = new_node; list.last = new_node;
if (!last) if (!last)
@@ -61,7 +61,7 @@ fn void LinkedList.free(LinkedList *list)
for (Node* node = list.first; node != null;) for (Node* node = list.first; node != null;)
{ {
Node* next = node.next; Node* next = node.next;
mem::free(node); free(node);
node = next; node = next;
} }
list.first = null; list.first = null;
@@ -89,7 +89,7 @@ fn Type LinkedList.get(LinkedList* list, usize index)
private fn 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* pred = succ.prev;
Node* new_node = mem::malloc(Node); Node* new_node = mem::alloc(Node);
*new_node = { .prev = pred, .next = succ, .value = value }; *new_node = { .prev = pred, .next = succ, .value = value };
succ.prev = new_node; succ.prev = new_node;
if (!pred) if (!pred)
@@ -109,7 +109,7 @@ private fn void LinkedList.linkBefore(LinkedList *list, Node *succ, Type value)
private fn void unlinkFirst(LinkedList* list, Node* f) private fn void unlinkFirst(LinkedList* list, Node* f)
{ {
Node* next = f.next; Node* next = f.next;
mem::free(f); free(f);
list.first = next; list.first = next;
if (!next) if (!next)
{ {
@@ -129,7 +129,7 @@ private fn void LinkedList.unlinkLast(LinkedList *list, Node* l)
{ {
Node* prev = l.prev; Node* prev = l.prev;
list.last = prev; list.last = prev;
mem::free(l); free(l);
if (!prev) if (!prev)
{ {
list.first = null; list.first = null;
@@ -164,6 +164,6 @@ private fn void LinkedList.unlink(LinkedList* list, Node* x)
{ {
next.prev = prev; next.prev = prev;
} }
mem::free(x); free(x);
list.size--; list.size--;
} }

View File

@@ -15,7 +15,7 @@ private fn void List.ensure_capacity(List *list) @inline
if (list.capacity == list.size) if (list.capacity == list.size)
{ {
list.capacity = list.capacity ? 2 * list.capacity : 16; list.capacity = list.capacity ? 2 * list.capacity : 16;
list.entries = mem::realloc(list.entries, Type.sizeof * list.capacity); list.entries = realloc(list.entries, Type.sizeof * list.capacity);
} }
} }
@@ -111,7 +111,7 @@ fn Type List.get(List *list, usize index)
fn void List.free(List *list) fn void List.free(List *list)
{ {
mem::free(list.entries); free(list.entries);
list.capacity = 0; list.capacity = 0;
list.size = 0; list.size = 0;
list.entries = null; list.entries = null;

View File

@@ -44,7 +44,7 @@ fn bool contains(char[] haystack, char[] needle)
macro @dupe(value) macro @dupe(value)
{ {
$typeof(&value) temp = mem::alloc_checked($sizeof(value))?; $typeof(&value) temp = malloc_checked($sizeof(value))?;
*temp = value; *temp = value;
return temp; return temp;
} }

View File

@@ -32,7 +32,7 @@ fn int! askGuess(int high)
fn char[]! readLine() fn char[]! readLine()
{ {
char* chars = mem::talloc(1024)?; char* chars = tmalloc(1024)?;
isize loaded = getline(&chars, &&(usize)1023, libc::stdin()); isize loaded = getline(&chars, &&(usize)1023, libc::stdin());
if (loaded < 0) return InputResult.FAILED_TO_READ!; if (loaded < 0) return InputResult.FAILED_TO_READ!;
chars[loaded] = 0; chars[loaded] = 0;

View File

@@ -69,8 +69,8 @@ fn int main(int c, char** v)
GameBoard board; GameBoard board;
board.w = w; board.w = w;
board.h = h; board.h = h;
board.world = mem::alloc((ulong)(h * w)); board.world = malloc((ulong)(h * w));
board.temp = mem::alloc((ulong)(h * w)); board.temp = malloc((ulong)(h * w));
for (int i = h * w - 1; i >= 0; i--) for (int i = h * w - 1; i >= 0; i--)
{ {

View File

@@ -118,8 +118,8 @@ fn void! Parser.parse(Parser* p, char* input, char* diagMsg, Blocks* blocks)
p.errorMsg[0] = 0; p.errorMsg[0] = 0;
p.blocks = blocks; p.blocks = blocks;
memset(p.parents, 0, sizeof(Node*)*MaxDepth); mem::set(p.parents, 0, sizeof(Node*)*MaxDepth);
memset(p.lastChild, 0, sizeof(Node*)*MaxDepth); mem::set(p.lastChild, 0, sizeof(Node*)*MaxDepth);
p.numParents = 0; p.numParents = 0;
p.topParent = nil; p.topParent = nil;
@@ -438,13 +438,13 @@ fn const Node* Reader.findNode(const Reader* r, const char* key)
switch (*cp) { switch (*cp) {
case 0: case 0:
len = cast<u32>(cp - start); len = cast<u32>(cp - start);
memcpy(name, start, len); mem::copy(name, start, len);
name[len] = 0; name[len] = 0;
node = r.blocks.findNode(name, node); node = r.blocks.findNode(name, node);
return node; return node;
case '.': case '.':
len = cast<u32>(cp - start); len = cast<u32>(cp - start);
memcpy(name, start, len); mem::copy(name, start, len);
name[len] = 0; name[len] = 0;
start = cp + 1; start = cp + 1;
node = r.blocks.findNode(name, node); node = r.blocks.findNode(name, node);
@@ -644,7 +644,7 @@ public type Blocks struct {
} @(opaque) } @(opaque)
fn void Blocks.init(Blocks* b) { fn void Blocks.init(Blocks* b) {
memset(b, 0, sizeof(Blocks)); mem::set(b, 0, sizeof(Blocks));
b.nodes = calloc(MaxNodes, sizeof(Node)); b.nodes = calloc(MaxNodes, sizeof(Node));
b.namesSize = MaxNames; b.namesSize = MaxNames;
@@ -659,7 +659,7 @@ fn void Blocks.init(Blocks* b) {
b.lastCache = 0; b.lastCache = 0;
//memset(b.namesCache, 0, sizeof(b.namesCache)); // sizeof(struct member) not supported yet //memset(b.namesCache, 0, sizeof(b.namesCache)); // sizeof(struct member) not supported yet
memset(b.namesCache, 0, sizeof(u32)*NamesCacheSize); mem::set(b.namesCache, 0, sizeof(u32)*NamesCacheSize);
} }
fn void Blocks.destroy(Blocks* b) { fn void Blocks.destroy(Blocks* b) {
@@ -702,7 +702,7 @@ fn u32 Blocks.addNode(Blocks* b, const char* name, NodeKind k) {
nameOffset = b.namesOffset; nameOffset = b.namesOffset;
node.nameOffset = nameOffset; node.nameOffset = nameOffset;
char* newname = &b.names[nameOffset]; char* newname = &b.names[nameOffset];
memcpy(newname, name, len); mem::copy(newname, name, len);
b.namesCache[b.lastCache] = nameOffset; b.namesCache[b.lastCache] = nameOffset;
b.lastCache = (b.lastCache + 1) % NamesCacheSize; b.lastCache = (b.lastCache + 1) % NamesCacheSize;
b.namesOffset += len; b.namesOffset += len;
@@ -716,7 +716,7 @@ fn u32 Blocks.addValue(Blocks* b, const char* value) {
if (value[0] == 0) return 0; if (value[0] == 0) return 0;
u32 off = b.valuesOffset; u32 off = b.valuesOffset;
u32 len = cast<u32>(strlen(value)) + 1; u32 len = cast<u32>(strlen(value)) + 1;
memcpy(&b.values[off], value, len); mem::copy(&b.values[off], value, len);
b.valuesOffset += len; b.valuesOffset += len;
return off; return off;
} }

View File

@@ -269,7 +269,7 @@ fn void Tokenizer.parseText(Tokenizer* t, Token* result)
uint len = (uint)(t.current - start); uint len = (uint)(t.current - start);
// assert(len < MaxText); // assert(len < MaxText);
memcpy(t.text as start, len); mem::copy(t.text as start, len);
t.text[len] = 0; t.text[len] = 0;
result.kind = TokenKind.Text; result.kind = TokenKind.Text;
result.text = t.text; result.text = t.text;
@@ -310,7 +310,7 @@ fn void! Tokenizer.parseMultiText(Tokenizer* t, Token* result)
uint len = uint(t.current - start); uint len = uint(t.current - start);
// assert(len < MaxText); // assert(len < MaxText);
memcpy(t.text, start, len); mem::copy(t.text, start, len);
t.text[len] = 0; t.text[len] = 0;
result.kind = TokenKind.Text; result.kind = TokenKind.Text;
result.text = t.text; result.text = t.text;
@@ -349,7 +349,7 @@ fn void Tokenizer.parseKey(Tokenizer* t, Token* result)
uint len = (uint)(t.current - start); uint len = (uint)(t.current - start);
// assert(len < MaxText); // assert(len < MaxText);
memcpy(t.text, start, len); mem::copy(t.text, start, len);
t.text[len] = 0; t.text[len] = 0;
result.kind = TokenKind.Word; result.kind = TokenKind.Word;
result.text = t.text; result.text = t.text;

View File

@@ -37,41 +37,41 @@ fn void testAllocator(Allocator* a, int val)
} }
fn void main() fn void main()
{ {
char* small = mem::talloc(128); char* small = tmalloc(128);
setstring(small, "small"); setstring(small, "small");
libc::printf("Small1: %p %s\n", small, small); libc::printf("Small1: %p %s\n", small, small);
print_pages(); print_pages();
small = mem::trealloc(small, 129, 1024 * 16); small = trealloc(small, 129, 1024 * 16);
libc::printf("Small2: %p %s\n", small, small); libc::printf("Small2: %p %s\n", small, small);
print_pages(); print_pages();
small = mem::trealloc(small, 12933); small = trealloc(small, 12933);
libc::printf("Small3: %p %s\n", small, small); libc::printf("Small3: %p %s\n", small, small);
print_pages(); print_pages();
char* first_big = mem::talloc(9512); char* first_big = tmalloc(9512);
void *big = mem::talloc(4095); void *big = tmalloc(4095);
io::printf("Big: %p\n", big); io::printf("Big: %p\n", big);
io::printf("Small: %p\n", mem::talloc(13)); io::printf("Small: %p\n", tmalloc(13));
print_pages(); print_pages();
@pool() { @pool() {
big = mem::trealloc(big, 5067); big = trealloc(big, 5067);
print_pages(); print_pages();
void* hidden = mem::talloc(4096); void* hidden = tmalloc(4096);
io::printf("Hidden: %p\n", hidden); io::printf("Hidden: %p\n", hidden);
io::printf("Big: %p\n", big); io::printf("Big: %p\n", big);
big = mem::trealloc(big, 4096, 256); big = trealloc(big, 4096, 256);
io::printf("Big: %p\n", big); io::printf("Big: %p\n", big);
io::printf("First big: %p\n", first_big); io::printf("First big: %p\n", first_big);
print_pages(); print_pages();
}; };
mem::@tscoped() mem::@tscoped()
{ {
io::printf("Malloc: %p\n", mem::alloc(23)); io::printf("Malloc: %p\n", malloc(23));
io::printf("Malloc: %p\n", mem::alloc(23)); io::printf("Malloc: %p\n", malloc(23));
}; };
io::printf("Malloc: %p\n", mem::alloc(23)); io::printf("Malloc: %p\n", malloc(23));
@pool() @pool()
{ {
io::printf("Talloc: %p\n", mem::talloc(22)); io::printf("Talloc: %p\n", tmalloc(22));
}; };
testAllocator(mem::temp_allocator(), 126); testAllocator(mem::temp_allocator(), 126);
testAllocator(mem::temp_allocator(), 12600); testAllocator(mem::temp_allocator(), 12600);

View File

@@ -33,7 +33,7 @@ fn void getComm(uint len, char* src)
uint size; uint size;
size = len - 2; size = len - 2;
char* comm = malloc(size + 1); char* comm = malloc(size + 1);
memcpy(comm, src, size); mem::copy(comm, src, size);
} }
fn uint* decode_fh(uint* p, SvcFh* fhp) fn uint* decode_fh(uint* p, SvcFh* fhp)
@@ -42,7 +42,7 @@ fn uint* decode_fh(uint* p, SvcFh* fhp)
fh_init(fhp, NFS3_FHSIZE); fh_init(fhp, NFS3_FHSIZE);
size = ntohl(*p++); size = ntohl(*p++);
if (size > NFS3_FHSIZE) return NULL; if (size > NFS3_FHSIZE) return NULL;
memcpy(&fhp.fh_handle.fh_base, p, size); mem::copy(&fhp.fh_handle.fh_base, p, size);
fhp.fh_handle.fh_size = size; fhp.fh_handle.fh_size = size;
return p + XDR_QUADLEN(size); return p + XDR_QUADLEN(size);
} }

View File

@@ -13,14 +13,14 @@ struct String
fn void String.init(String *s, char[] c) fn void String.init(String *s, char[] c)
{ {
s.capacity = c.len + 16; s.capacity = c.len + 16;
s.ptr = mem::_malloc(s.capacity); s.ptr = malloc(s.capacity);
s.len = c.len; s.len = c.len;
mem::copy(s.ptr, (char*)(c), c.len); mem::copy(s.ptr, (char*)(c), c.len);
} }
fn char* String.zstr(String *s) fn char* String.zstr(String *s)
{ {
char* c = mem::_malloc(s.len + 1); char* c = malloc(s.len + 1);
mem::copy(c, s.ptr, s.len); mem::copy(c, s.ptr, s.len);
c[s.len] = 0; c[s.len] = 0;
return c; return c;
@@ -31,7 +31,7 @@ fn void String.appendc(String *s, char c)
if (s.capacity == s.len) if (s.capacity == s.len)
{ {
s.capacity *= 2; s.capacity *= 2;
char* new_ptr = mem::_malloc(s.capacity); char* new_ptr = malloc(s.capacity);
mem::copy(new_ptr, s.ptr, s.len); mem::copy(new_ptr, s.ptr, s.len);
s.ptr = new_ptr; s.ptr = new_ptr;
} }
@@ -47,7 +47,7 @@ fn void String.append(String *s, char[] other_string)
s.capacity *= 2; s.capacity *= 2;
} }
while (s.capacity < s.len + other_string.len); while (s.capacity < s.len + other_string.len);
char* new_ptr = mem::_malloc(s.capacity); char* new_ptr = malloc(s.capacity);
mem::copy(new_ptr, s.ptr, s.len); mem::copy(new_ptr, s.ptr, s.len);
s.ptr = new_ptr; s.ptr = new_ptr;
} }
@@ -64,7 +64,7 @@ fn void String.concat(String *s, String* other_string)
s.capacity *= 2; s.capacity *= 2;
} }
while (s.capacity < s.len + other_string.len); while (s.capacity < s.len + other_string.len);
char* new_ptr = mem::_malloc(s.capacity); char* new_ptr = malloc(s.capacity);
mem::copy(new_ptr, s.ptr, s.len); mem::copy(new_ptr, s.ptr, s.len);
s.ptr = new_ptr; s.ptr = new_ptr;
} }

View File

@@ -78,6 +78,19 @@ static Decl *sema_find_decl_in_imports(Decl **imports, NameResolve *name_resolve
// Did we already have a match? // Did we already have a match?
if (decl) if (decl)
{ {
if (!path)
{
// Prefer already found builtin over new found no builtin
if (decl->is_autoimport && !found->is_autoimport) continue;
// Prefer new builtin over non-builtin
if (found->is_autoimport && !decl->is_autoimport)
{
decl = found;
name_resolve->private_decl = NULL;
name_resolve->ambiguous_other_decl = NULL;
continue;
}
}
// 11. Then set an ambiguous match. // 11. Then set an ambiguous match.
name_resolve->ambiguous_other_decl = found; name_resolve->ambiguous_other_decl = found;
continue; continue;
@@ -237,6 +250,19 @@ static Decl *sema_find_decl_in_global_new(CompilationUnit *unit, DeclTable *tabl
{ {
ambiguous = decl; ambiguous = decl;
decl = candidate; decl = candidate;
if (ambiguous)
{
// If we have a same match but one is builtin, prefer builtin.
if (!ambiguous->is_autoimport && decl->is_autoimport)
{
ambiguous = NULL;
}
else if (ambiguous->is_autoimport && !decl->is_autoimport)
{
decl = ambiguous;
ambiguous = NULL;
}
}
} }
} }
name_resolve->ambiguous_other_decl = ambiguous; name_resolve->ambiguous_other_decl = ambiguous;
@@ -320,13 +346,25 @@ static Decl *sema_resolve_no_path_symbol(SemaContext *context, NameResolve *name
if (decl) return decl; if (decl) return decl;
// Search in the module. // Search in the module.
decl = module_find_symbol(unit->module, symbol); decl = module_find_symbol(unit->module, symbol);
if (decl) return decl; if (decl) return decl;
decl = sema_find_decl_in_imports(unit->imports, name_resolve, false); decl = sema_find_decl_in_imports(unit->imports, name_resolve, false);
// Special case: the declaration in import is not autoimport and is not a type (which won't be @builtin)
// e.g we have a malloc builtin and libc::malloc
if (decl && !decl->is_autoimport && !decl_is_user_defined_type(decl))
{
// Find the global
NameResolve copy = *name_resolve;
Decl *global = sema_find_decl_in_global_new(context->unit, &global_context.symbols, NULL, name_resolve, false);
// If it exists and is autoimport, then prefer it.
if (global && global->is_autoimport) return global;
*name_resolve = copy;
return decl;
}
return decl ? decl : sema_find_decl_in_global_new(context->unit, &global_context.symbols, NULL, name_resolve, false); return decl ? decl : sema_find_decl_in_global_new(context->unit, &global_context.symbols, NULL, name_resolve, false);
} }

View File

@@ -39,7 +39,7 @@ fn bool contains(char[] haystack, char[] needle)
macro dupe(value) macro dupe(value)
{ {
$typeof(&value) temp = mem::alloc($sizeof(value)); $typeof(&value) temp = malloc($sizeof(value));
if (!temp) return ReadError.OUT_OF_MEMORY!; if (!temp) return ReadError.OUT_OF_MEMORY!;
*temp = value; *temp = value;
return temp; return temp;
@@ -59,7 +59,7 @@ fn Doc! readDoc(char[] url)
if (contains(url, "title-empty")) return { dupe(Head { .title = dupe((char[])"")? })? }; if (contains(url, "title-empty")) return { dupe(Head { .title = dupe((char[])"")? })? };
// Not particularly elegant due to missing string functions. // Not particularly elegant due to missing string functions.
int len = libc::snprintf(null, 0, "Title of %.*s", (int)url.len, url.ptr); int len = libc::snprintf(null, 0, "Title of %.*s", (int)url.len, url.ptr);
char* str = mem::alloc(len + 1); char* str = malloc(len + 1);
if (!str) return ReadError.OUT_OF_MEMORY!; if (!str) return ReadError.OUT_OF_MEMORY!;
libc::snprintf(str, len + 1, "Title of %.*s", (int)url.len, url.ptr); libc::snprintf(str, len + 1, "Title of %.*s", (int)url.len, url.ptr);
return { dupe(Head { .title = dupe(str[..len - 1])? })? }; return { dupe(Head { .title = dupe(str[..len - 1])? })? };
@@ -368,7 +368,7 @@ if.then15: ; preds = %if.exit9
store %"char[]"* null, %"char[]"** %33, align 8 store %"char[]"* null, %"char[]"** %33, align 8
%34 = load %Head, %Head* %literal18, align 8 %34 = load %Head, %Head* %literal18, align 8
store %Head %34, %Head* %value, align 8 store %Head %34, %Head* %value, align 8
%35 = call i8* @std_core_mem_alloc(i64 8) %35 = call i8* @std_core_mem_malloc(i64 8) #2
%ptrptr = bitcast i8* %35 to %Head* %ptrptr = bitcast i8* %35 to %Head*
store %Head* %ptrptr, %Head** %temp, align 8 store %Head* %ptrptr, %Head** %temp, align 8
%36 = load %Head*, %Head** %temp, align 8 %36 = load %Head*, %Head** %temp, align 8
@@ -420,7 +420,7 @@ if.then27: ; preds = %if.exit21
store %"char[]"* null, %"char[]"** %53, align 8 store %"char[]"* null, %"char[]"** %53, align 8
%54 = getelementptr inbounds %Head, %Head* %literal32, i32 0, i32 0 %54 = getelementptr inbounds %Head, %Head* %literal32, i32 0, i32 0
store %"char[]" zeroinitializer, %"char[]"* %value34, align 8 store %"char[]" zeroinitializer, %"char[]"* %value34, align 8
%55 = call i8* @std_core_mem_alloc(i64 16) %55 = call i8* @std_core_mem_malloc(i64 16) #2
%ptrptr36 = bitcast i8* %55 to %"char[]"* %ptrptr36 = bitcast i8* %55 to %"char[]"*
store %"char[]"* %ptrptr36, %"char[]"** %temp35, align 8 store %"char[]"* %ptrptr36, %"char[]"** %temp35, align 8
%56 = load %"char[]"*, %"char[]"** %temp35, align 8 %56 = load %"char[]"*, %"char[]"** %temp35, align 8
@@ -447,7 +447,7 @@ noerr_block41: ; preds = %if.exit39
store %"char[]"* %61, %"char[]"** %54, align 8 store %"char[]"* %61, %"char[]"** %54, align 8
%62 = load %Head, %Head* %literal32, align 8 %62 = load %Head, %Head* %literal32, align 8
store %Head %62, %Head* %value31, align 8 store %Head %62, %Head* %value31, align 8
%63 = call i8* @std_core_mem_alloc(i64 8) %63 = call i8* @std_core_mem_malloc(i64 8) #2
%ptrptr43 = bitcast i8* %63 to %Head* %ptrptr43 = bitcast i8* %63 to %Head*
store %Head* %ptrptr43, %Head** %temp42, align 8 store %Head* %ptrptr43, %Head** %temp42, align 8
%64 = load %Head*, %Head** %temp42, align 8 %64 = load %Head*, %Head** %temp42, align 8
@@ -488,7 +488,7 @@ if.exit49: ; preds = %if.exit21
%77 = load i32, i32* %len, align 4 %77 = load i32, i32* %len, align 4
%siuiext = sext i32 %77 to i64 %siuiext = sext i32 %77 to i64
%add = add i64 %siuiext, 1 %add = add i64 %siuiext, 1
%78 = call i8* @std_core_mem_alloc(i64 %add) %78 = call i8* @std_core_mem_malloc(i64 %add) #2
store i8* %78, i8** %str, align 8 store i8* %78, i8** %str, align 8
%79 = load i8*, i8** %str, align 8 %79 = load i8*, i8** %str, align 8
%not50 = icmp eq i8* %79, null %not50 = icmp eq i8* %79, null
@@ -522,7 +522,7 @@ if.exit52: ; preds = %if.exit49
%93 = insertvalue %"char[]" undef, i8* %ptroffset, 0 %93 = insertvalue %"char[]" undef, i8* %ptroffset, 0
%94 = insertvalue %"char[]" %93, i64 %size, 1 %94 = insertvalue %"char[]" %93, i64 %size, 1
store %"char[]" %94, %"char[]"* %value62, align 8 store %"char[]" %94, %"char[]"* %value62, align 8
%95 = call i8* @std_core_mem_alloc(i64 16) %95 = call i8* @std_core_mem_malloc(i64 16) #2
%ptrptr64 = bitcast i8* %95 to %"char[]"* %ptrptr64 = bitcast i8* %95 to %"char[]"*
store %"char[]"* %ptrptr64, %"char[]"** %temp63, align 8 store %"char[]"* %ptrptr64, %"char[]"** %temp63, align 8
%96 = load %"char[]"*, %"char[]"** %temp63, align 8 %96 = load %"char[]"*, %"char[]"** %temp63, align 8
@@ -549,7 +549,7 @@ noerr_block69: ; preds = %if.exit67
store %"char[]"* %101, %"char[]"** %89, align 8 store %"char[]"* %101, %"char[]"** %89, align 8
%102 = load %Head, %Head* %literal60, align 8 %102 = load %Head, %Head* %literal60, align 8
store %Head %102, %Head* %value59, align 8 store %Head %102, %Head* %value59, align 8
%103 = call i8* @std_core_mem_alloc(i64 8) %103 = call i8* @std_core_mem_malloc(i64 8) #2
%ptrptr71 = bitcast i8* %103 to %Head* %ptrptr71 = bitcast i8* %103 to %Head*
store %Head* %ptrptr71, %Head** %temp70, align 8 store %Head* %ptrptr71, %Head** %temp70, align 8
%104 = load %Head*, %Head** %temp70, align 8 %104 = load %Head*, %Head** %temp70, align 8

View File

@@ -39,7 +39,7 @@ fn bool contains(char[] haystack, char[] needle)
macro dupe(value) macro dupe(value)
{ {
$typeof(&value) temp = mem::alloc($sizeof(value)); $typeof(&value) temp = malloc($sizeof(value));
if (!temp) return ReadError.OUT_OF_MEMORY!; if (!temp) return ReadError.OUT_OF_MEMORY!;
*temp = value; *temp = value;
return temp; return temp;
@@ -59,7 +59,7 @@ fn Doc! readDoc(char[] url)
if (contains(url, "title-empty")) return { dupe(Head { .title = dupe((char[])"")? })? }; if (contains(url, "title-empty")) return { dupe(Head { .title = dupe((char[])"")? })? };
// Not particularly elegant due to missing string functions. // Not particularly elegant due to missing string functions.
int len = libc::snprintf(null, 0, "Title of %.*s", (int)url.len, url.ptr); int len = libc::snprintf(null, 0, "Title of %.*s", (int)url.len, url.ptr);
char* str = mem::alloc(len + 1); char* str = malloc(len + 1);
if (!str) return ReadError.OUT_OF_MEMORY!; if (!str) return ReadError.OUT_OF_MEMORY!;
libc::snprintf(str, len + 1, "Title of %.*s", (int)url.len, url.ptr); libc::snprintf(str, len + 1, "Title of %.*s", (int)url.len, url.ptr);
return { dupe(Head { .title = dupe(str[..len - 1])? })? }; return { dupe(Head { .title = dupe(str[..len - 1])? })? };
@@ -358,7 +358,7 @@ if.then15: ; preds = %if.exit9
store ptr null, ptr %25, align 8 store ptr null, ptr %25, align 8
%26 = load %Head, ptr %literal18, align 8 %26 = load %Head, ptr %literal18, align 8
store %Head %26, ptr %value, align 8 store %Head %26, ptr %value, align 8
%27 = call ptr @std_core_mem_alloc(i64 8) %27 = call ptr @std_core_mem_malloc(i64 8)
store ptr %27, ptr %temp, align 8 store ptr %27, ptr %temp, align 8
%28 = load ptr, ptr %temp, align 8 %28 = load ptr, ptr %temp, align 8
%not = icmp eq ptr %28, null %not = icmp eq ptr %28, null
@@ -402,7 +402,7 @@ if.then27: ; preds = %if.exit21
store ptr null, ptr %literal32, align 8 store ptr null, ptr %literal32, align 8
%39 = getelementptr inbounds %Head, ptr %literal32, i32 0, i32 0 %39 = getelementptr inbounds %Head, ptr %literal32, i32 0, i32 0
store %"char[]" zeroinitializer, ptr %value34, align 8 store %"char[]" zeroinitializer, ptr %value34, align 8
%40 = call ptr @std_core_mem_alloc(i64 16) %40 = call ptr @std_core_mem_malloc(i64 16)
store ptr %40, ptr %temp35, align 8 store ptr %40, ptr %temp35, align 8
%41 = load ptr, ptr %temp35, align 8 %41 = load ptr, ptr %temp35, align 8
%not36 = icmp eq ptr %41, null %not36 = icmp eq ptr %41, null
@@ -426,7 +426,7 @@ noerr_block40: ; preds = %if.exit38
store ptr %44, ptr %39, align 8 store ptr %44, ptr %39, align 8
%45 = load %Head, ptr %literal32, align 8 %45 = load %Head, ptr %literal32, align 8
store %Head %45, ptr %value31, align 8 store %Head %45, ptr %value31, align 8
%46 = call ptr @std_core_mem_alloc(i64 8) %46 = call ptr @std_core_mem_malloc(i64 8)
store ptr %46, ptr %temp41, align 8 store ptr %46, ptr %temp41, align 8
%47 = load ptr, ptr %temp41, align 8 %47 = load ptr, ptr %temp41, align 8
%not42 = icmp eq ptr %47, null %not42 = icmp eq ptr %47, null
@@ -462,7 +462,7 @@ if.exit47: ; preds = %if.exit21
%56 = load i32, ptr %len, align 4 %56 = load i32, ptr %len, align 4
%siuiext = sext i32 %56 to i64 %siuiext = sext i32 %56 to i64
%add = add i64 %siuiext, 1 %add = add i64 %siuiext, 1
%57 = call ptr @std_core_mem_alloc(i64 %add) %57 = call ptr @std_core_mem_malloc(i64 %add)
store ptr %57, ptr %str, align 8 store ptr %57, ptr %str, align 8
%58 = load ptr, ptr %str, align 8 %58 = load ptr, ptr %str, align 8
%not48 = icmp eq ptr %58, null %not48 = icmp eq ptr %58, null
@@ -495,7 +495,7 @@ if.exit50: ; preds = %if.exit47
%71 = insertvalue %"char[]" undef, ptr %ptroffset, 0 %71 = insertvalue %"char[]" undef, ptr %ptroffset, 0
%72 = insertvalue %"char[]" %71, i64 %size, 1 %72 = insertvalue %"char[]" %71, i64 %size, 1
store %"char[]" %72, ptr %value60, align 8 store %"char[]" %72, ptr %value60, align 8
%73 = call ptr @std_core_mem_alloc(i64 16) %73 = call ptr @std_core_mem_malloc(i64 16)
store ptr %73, ptr %temp61, align 8 store ptr %73, ptr %temp61, align 8
%74 = load ptr, ptr %temp61, align 8 %74 = load ptr, ptr %temp61, align 8
%not62 = icmp eq ptr %74, null %not62 = icmp eq ptr %74, null
@@ -519,7 +519,7 @@ noerr_block66: ; preds = %if.exit64
store ptr %77, ptr %67, align 8 store ptr %77, ptr %67, align 8
%78 = load %Head, ptr %literal58, align 8 %78 = load %Head, ptr %literal58, align 8
store %Head %78, ptr %value57, align 8 store %Head %78, ptr %value57, align 8
%79 = call ptr @std_core_mem_alloc(i64 8) %79 = call ptr @std_core_mem_malloc(i64 8)
store ptr %79, ptr %temp67, align 8 store ptr %79, ptr %temp67, align 8
%80 = load ptr, ptr %temp67, align 8 %80 = load ptr, ptr %temp67, align 8
%not68 = icmp eq ptr %80, null %not68 = icmp eq ptr %80, null