diff --git a/lib/std/core/allocators/arena_allocator.c3 b/lib/std/core/allocators/arena_allocator.c3 index 1e84dadb5..fac0229d9 100644 --- a/lib/std/core/allocators/arena_allocator.c3 +++ b/lib/std/core/allocators/arena_allocator.c3 @@ -25,7 +25,7 @@ private fn void*! arena_allocator_function(Allocator* data, usize size, usize al if (!size) return null; alignment = alignment_for_allocation(alignment); 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; case ALIGNED_REALLOC: case REALLOC: @@ -114,6 +114,6 @@ private fn void*! ArenaAllocator._realloc(ArenaAllocator* this, void *old_pointe } // Otherwise just allocate new memory. 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; } \ No newline at end of file diff --git a/lib/std/core/allocators/dynamic_arena.c3 b/lib/std/core/allocators/dynamic_arena.c3 index 8a04f4e10..4172e9c0f 100644 --- a/lib/std/core/allocators/dynamic_arena.c3 +++ b/lib/std/core/allocators/dynamic_arena.c3 @@ -58,7 +58,7 @@ private fn void*! DynamicArenaAllocator._realloc(DynamicArenaAllocator* this, vo return old_pointer; } 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; } @@ -164,7 +164,7 @@ private fn void*! dynamic_arena_allocator_function(Allocator* data, usize size, assert(!old_pointer, "Unexpected no old pointer for calloc."); if (!size) return null; 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; case ALLOC: case ALIGNED_ALLOC: diff --git a/lib/std/core/allocators/mem_allocator_fn.c3 b/lib/std/core/allocators/mem_allocator_fn.c3 index b0edc477f..2fb46d31d 100644 --- a/lib/std/core/allocators/mem_allocator_fn.c3 +++ b/lib/std/core/allocators/mem_allocator_fn.c3 @@ -66,7 +66,7 @@ private fn void* _libc_aligned_realloc(void* old_pointer, usize bytes, usize ali AlignedBlock* desc = (AlignedBlock*)old_pointer - 1; void* data_start = desc.start; 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); return new_data; } diff --git a/lib/std/core/allocators/temp_allocator.c3 b/lib/std/core/allocators/temp_allocator.c3 index c57c6648e..e60253fcf 100644 --- a/lib/std/core/allocators/temp_allocator.c3 +++ b/lib/std/core/allocators/temp_allocator.c3 @@ -134,7 +134,7 @@ private fn void*! TempAllocator._realloc_page(TempAllocator* this, TempAllocator usize page_size = page.pagesize(); // Clear on size > original size. 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()) { 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 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; } @@ -190,7 +190,7 @@ private fn void*! TempAllocator._alloc(TempAllocator* this, usize size, usize al TempAllocatorChunk* chunk_start = mem - TempAllocatorChunk.sizeof; chunk_start.size = size; 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; } diff --git a/lib/std/core/builtin.c3 b/lib/std/core/builtin.c3 index ee2a879eb..72e715ef6 100644 --- a/lib/std/core/builtin.c3 +++ b/lib/std/core/builtin.c3 @@ -97,7 +97,7 @@ macro bitcast(expr, $Type) @builtin var $size = (usize)($sizeof(expr)); $assert($size == $Type.sizeof, "Cannot bitcast between types of different size."); $Type x = void; - mem::memcpy(&x, &expr, $size, false, $alignof($Type), $alignof(expr)); + mem::copy(&x, &expr, $size, $alignof($Type), $alignof(expr)); return x; } diff --git a/lib/std/core/mem.c3 b/lib/std/core/mem.c3 index 0b50323c7..3b683ce82 100644 --- a/lib/std/core/mem.c3 +++ b/lib/std/core/mem.c3 @@ -35,24 +35,19 @@ fn bool ptr_is_aligned(void* ptr, usize alignment) @inline 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); -} - -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); + $$memset(dst, (char)0, len, $is_volatile, $dst_align); } macro @clone(&value) @builtin @@ -62,22 +57,19 @@ macro @clone(&value) @builtin 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 -{ - return ((char*)thread_allocator.alloc(bytes))[:bytes]!!; -} - -fn void* alloc(usize size) +fn void* malloc(usize size) @builtin @inline { return thread_allocator.alloc(size)!!; } -fn void*! alloc_checked(usize size) +fn void*! malloc_checked(usize size) @builtin @inline { return thread_allocator.alloc(size); } @@ -85,17 +77,28 @@ fn void*! alloc_checked(usize size) /** * @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); } -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)!!; } -fn void*! calloc_checked(usize size) +fn void*! calloc_checked(usize size) @builtin @inline { return thread_allocator.calloc(size); } @@ -103,18 +106,17 @@ fn void*! calloc_checked(usize size) /** * @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); } - -fn void* realloc(void *ptr, usize new_size) +fn void* realloc(void *ptr, usize new_size) @builtin @inline { 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); } @@ -122,17 +124,17 @@ fn void*! realloc_checked(void *ptr, usize new_size) /** * @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); } -fn void free(void* ptr) @builtin +fn void free(void* ptr) @builtin @inline { 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)!!; } @@ -159,17 +161,22 @@ macro void @tscoped(;@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)!!; } -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)!!; } -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)!!; } diff --git a/lib/std/core/mem_array.c3 b/lib/std/core/mem_array.c3 index a9e7bcabe..76658b593 100644 --- a/lib/std/core/mem_array.c3 +++ b/lib/std/core/mem_array.c3 @@ -8,7 +8,7 @@ module std::core::mem::array; **/ macro alloc($Type, usize elements) { - $Type* ptr = mem::alloc($Type.sizeof * elements, $alignof($Type)); + $Type* ptr = malloc($Type.sizeof * elements); return ptr[:elements]; } @@ -17,7 +17,7 @@ macro alloc($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]; } @@ -26,7 +26,7 @@ macro talloc($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]; } @@ -35,6 +35,6 @@ macro make($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]; } diff --git a/lib/std/core/str.c3 b/lib/std/core/str.c3 index 5d99e336f..257717110 100644 --- a/lib/std/core/str.c3 +++ b/lib/std/core/str.c3 @@ -32,7 +32,7 @@ fn String join(char[][] s, char[] joiner) fn ZString copy_zstring(char[] s) { usize len = s.len; - char* str = mem::alloc(len + 1); + char* str = malloc(len + 1); mem::copy(str, s.ptr, len); str[len] = 0; return (ZString)str; @@ -41,7 +41,7 @@ fn ZString copy_zstring(char[] s) fn ZString tcopy_zstring(char[] s) { usize len = s.len; - char* str = mem::talloc(len + 1); + char* str = tmalloc(len + 1); mem::copy(str, s.ptr, len); str[len] = 0; return (ZString)str; @@ -128,7 +128,7 @@ fn char[] tcopy(char[] s) fn char[] tconcat(char[] s1, char[] s2) { 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; mem::copy(str, s1.ptr, s1_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) { 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; mem::copy(str, s1.ptr, s1_len); mem::copy(str + s1_len, s2.ptr, s2.len); diff --git a/lib/std/io.c3 b/lib/std/io.c3 index 3c67b1a5c..4d199c9af 100644 --- a/lib/std/io.c3 +++ b/lib/std/io.c3 @@ -41,8 +41,8 @@ fn int println(char *message = "") @inline fn void! File.open(File* file, char[] filename, char[] mode) { - char* filename_copy = mem::talloc(filename.len + 1); - char* mode_copy = mem::talloc(mode.len + 1); + char* filename_copy = tmalloc(filename.len + 1); + char* mode_copy = tmalloc(mode.len + 1); mem::copy(filename_copy, (char*)(filename), filename.len); mem::copy(mode_copy, (char*)(mode), mode.len); diff --git a/lib/std/linkedlist.c3 b/lib/std/linkedlist.c3 index 8c0efecf5..9513f8454 100644 --- a/lib/std/linkedlist.c3 +++ b/lib/std/linkedlist.c3 @@ -25,7 +25,7 @@ fn void LinkedList.push(LinkedList *list, Type value) private fn void LinkedList.linkFirst(LinkedList *list, Type value) { Node *first = list.first; - Node *new_node = mem::malloc(Node); + Node *new_node = mem::alloc(Node); *new_node = { .next = first, .value = value }; list.first = new_node; if (!first) @@ -42,7 +42,7 @@ private fn void LinkedList.linkFirst(LinkedList *list, Type value) private fn void LinkedList.linkLast(LinkedList *list, Type value) { Node *last = list.last; - Node *new_node = mem::alloc(Node.sizeof); + Node *new_node = mem::alloc(Node); *new_node = { .prev = last, .value = value }; list.last = new_node; if (!last) @@ -61,7 +61,7 @@ fn void LinkedList.free(LinkedList *list) for (Node* node = list.first; node != null;) { Node* next = node.next; - mem::free(node); + free(node); node = next; } 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) { Node* pred = succ.prev; - Node* new_node = mem::malloc(Node); + Node* new_node = mem::alloc(Node); *new_node = { .prev = pred, .next = succ, .value = value }; succ.prev = new_node; 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) { Node* next = f.next; - mem::free(f); + free(f); list.first = next; if (!next) { @@ -129,7 +129,7 @@ private fn void LinkedList.unlinkLast(LinkedList *list, Node* l) { Node* prev = l.prev; list.last = prev; - mem::free(l); + free(l); if (!prev) { list.first = null; @@ -164,6 +164,6 @@ private fn void LinkedList.unlink(LinkedList* list, Node* x) { next.prev = prev; } - mem::free(x); + free(x); list.size--; } diff --git a/lib/std/list.c3 b/lib/std/list.c3 index e54889c9b..7b789ff47 100644 --- a/lib/std/list.c3 +++ b/lib/std/list.c3 @@ -15,7 +15,7 @@ private fn void List.ensure_capacity(List *list) @inline if (list.capacity == list.size) { 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) { - mem::free(list.entries); + free(list.entries); list.capacity = 0; list.size = 0; list.entries = null; diff --git a/resources/examples/contextfree/boolerr.c3 b/resources/examples/contextfree/boolerr.c3 index 7c8f3731f..690c38068 100644 --- a/resources/examples/contextfree/boolerr.c3 +++ b/resources/examples/contextfree/boolerr.c3 @@ -44,7 +44,7 @@ fn bool contains(char[] haystack, char[] needle) macro @dupe(value) { - $typeof(&value) temp = mem::alloc_checked($sizeof(value))?; + $typeof(&value) temp = malloc_checked($sizeof(value))?; *temp = value; return temp; } diff --git a/resources/examples/contextfree/guess_number.c3 b/resources/examples/contextfree/guess_number.c3 index 926e71148..d86863d88 100644 --- a/resources/examples/contextfree/guess_number.c3 +++ b/resources/examples/contextfree/guess_number.c3 @@ -32,7 +32,7 @@ fn int! askGuess(int high) fn char[]! readLine() { - char* chars = mem::talloc(1024)?; + char* chars = tmalloc(1024)?; isize loaded = getline(&chars, &&(usize)1023, libc::stdin()); if (loaded < 0) return InputResult.FAILED_TO_READ!; chars[loaded] = 0; diff --git a/resources/examples/gameoflife.c3 b/resources/examples/gameoflife.c3 index e1d52f400..eb47f7a5d 100644 --- a/resources/examples/gameoflife.c3 +++ b/resources/examples/gameoflife.c3 @@ -69,8 +69,8 @@ fn int main(int c, char** v) GameBoard board; board.w = w; board.h = h; - board.world = mem::alloc((ulong)(h * w)); - board.temp = mem::alloc((ulong)(h * w)); + board.world = malloc((ulong)(h * w)); + board.temp = malloc((ulong)(h * w)); for (int i = h * w - 1; i >= 0; i--) { diff --git a/resources/examples/notworking/toml_parser_c2.c3 b/resources/examples/notworking/toml_parser_c2.c3 index d35fbc0ed..0ebb9751e 100644 --- a/resources/examples/notworking/toml_parser_c2.c3 +++ b/resources/examples/notworking/toml_parser_c2.c3 @@ -118,8 +118,8 @@ fn void! Parser.parse(Parser* p, char* input, char* diagMsg, Blocks* blocks) p.errorMsg[0] = 0; p.blocks = blocks; - memset(p.parents, 0, sizeof(Node*)*MaxDepth); - memset(p.lastChild, 0, sizeof(Node*)*MaxDepth); + mem::set(p.parents, 0, sizeof(Node*)*MaxDepth); + mem::set(p.lastChild, 0, sizeof(Node*)*MaxDepth); p.numParents = 0; p.topParent = nil; @@ -438,13 +438,13 @@ fn const Node* Reader.findNode(const Reader* r, const char* key) switch (*cp) { case 0: len = cast(cp - start); - memcpy(name, start, len); + mem::copy(name, start, len); name[len] = 0; node = r.blocks.findNode(name, node); return node; case '.': len = cast(cp - start); - memcpy(name, start, len); + mem::copy(name, start, len); name[len] = 0; start = cp + 1; node = r.blocks.findNode(name, node); @@ -644,7 +644,7 @@ public type Blocks struct { } @(opaque) fn void Blocks.init(Blocks* b) { - memset(b, 0, sizeof(Blocks)); + mem::set(b, 0, sizeof(Blocks)); b.nodes = calloc(MaxNodes, sizeof(Node)); b.namesSize = MaxNames; @@ -659,7 +659,7 @@ fn void Blocks.init(Blocks* b) { b.lastCache = 0; //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) { @@ -702,7 +702,7 @@ fn u32 Blocks.addNode(Blocks* b, const char* name, NodeKind k) { nameOffset = b.namesOffset; node.nameOffset = nameOffset; char* newname = &b.names[nameOffset]; - memcpy(newname, name, len); + mem::copy(newname, name, len); b.namesCache[b.lastCache] = nameOffset; b.lastCache = (b.lastCache + 1) % NamesCacheSize; b.namesOffset += len; @@ -716,7 +716,7 @@ fn u32 Blocks.addValue(Blocks* b, const char* value) { if (value[0] == 0) return 0; u32 off = b.valuesOffset; u32 len = cast(strlen(value)) + 1; - memcpy(&b.values[off], value, len); + mem::copy(&b.values[off], value, len); b.valuesOffset += len; return off; } diff --git a/resources/examples/notworking/toml_tokenizer_c2.c3 b/resources/examples/notworking/toml_tokenizer_c2.c3 index fcec7e945..9c28afa8c 100644 --- a/resources/examples/notworking/toml_tokenizer_c2.c3 +++ b/resources/examples/notworking/toml_tokenizer_c2.c3 @@ -269,7 +269,7 @@ fn void Tokenizer.parseText(Tokenizer* t, Token* result) uint len = (uint)(t.current - start); // assert(len < MaxText); - memcpy(t.text as start, len); + mem::copy(t.text as start, len); t.text[len] = 0; result.kind = TokenKind.Text; result.text = t.text; @@ -310,7 +310,7 @@ fn void! Tokenizer.parseMultiText(Tokenizer* t, Token* result) uint len = uint(t.current - start); // assert(len < MaxText); - memcpy(t.text, start, len); + mem::copy(t.text, start, len); t.text[len] = 0; result.kind = TokenKind.Text; result.text = t.text; @@ -349,7 +349,7 @@ fn void Tokenizer.parseKey(Tokenizer* t, Token* result) uint len = (uint)(t.current - start); // assert(len < MaxText); - memcpy(t.text, start, len); + mem::copy(t.text, start, len); t.text[len] = 0; result.kind = TokenKind.Word; result.text = t.text; diff --git a/resources/testfragments/allocators_testing.c3 b/resources/testfragments/allocators_testing.c3 index b77bcd05f..f22a72dd9 100644 --- a/resources/testfragments/allocators_testing.c3 +++ b/resources/testfragments/allocators_testing.c3 @@ -37,41 +37,41 @@ fn void testAllocator(Allocator* a, int val) } fn void main() { - char* small = mem::talloc(128); + char* small = tmalloc(128); setstring(small, "small"); libc::printf("Small1: %p %s\n", small, small); print_pages(); - small = mem::trealloc(small, 129, 1024 * 16); + small = trealloc(small, 129, 1024 * 16); libc::printf("Small2: %p %s\n", small, small); print_pages(); - small = mem::trealloc(small, 12933); + small = trealloc(small, 12933); libc::printf("Small3: %p %s\n", small, small); print_pages(); - char* first_big = mem::talloc(9512); - void *big = mem::talloc(4095); + char* first_big = tmalloc(9512); + void *big = tmalloc(4095); io::printf("Big: %p\n", big); - io::printf("Small: %p\n", mem::talloc(13)); + io::printf("Small: %p\n", tmalloc(13)); print_pages(); @pool() { - big = mem::trealloc(big, 5067); + big = trealloc(big, 5067); print_pages(); - void* hidden = mem::talloc(4096); + void* hidden = tmalloc(4096); io::printf("Hidden: %p\n", hidden); 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("First big: %p\n", first_big); print_pages(); }; mem::@tscoped() { - io::printf("Malloc: %p\n", mem::alloc(23)); - io::printf("Malloc: %p\n", mem::alloc(23)); + io::printf("Malloc: %p\n", malloc(23)); + io::printf("Malloc: %p\n", malloc(23)); }; - io::printf("Malloc: %p\n", mem::alloc(23)); + io::printf("Malloc: %p\n", malloc(23)); @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(), 12600); diff --git a/resources/testfragments/casting.c3 b/resources/testfragments/casting.c3 index 3e04eeb7a..28347f7c6 100644 --- a/resources/testfragments/casting.c3 +++ b/resources/testfragments/casting.c3 @@ -33,7 +33,7 @@ fn void getComm(uint len, char* src) uint size; size = len - 2; char* comm = malloc(size + 1); - memcpy(comm, src, size); + mem::copy(comm, src, size); } 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); size = ntohl(*p++); 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; return p + XDR_QUADLEN(size); } diff --git a/resources/testfragments/tmem.c3 b/resources/testfragments/tmem.c3 index 54388d0a5..ff683f3dd 100644 --- a/resources/testfragments/tmem.c3 +++ b/resources/testfragments/tmem.c3 @@ -13,14 +13,14 @@ struct String fn void String.init(String *s, char[] c) { s.capacity = c.len + 16; - s.ptr = mem::_malloc(s.capacity); + s.ptr = malloc(s.capacity); s.len = c.len; mem::copy(s.ptr, (char*)(c), c.len); } 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); c[s.len] = 0; return c; @@ -31,7 +31,7 @@ fn void String.appendc(String *s, char c) if (s.capacity == s.len) { s.capacity *= 2; - char* new_ptr = mem::_malloc(s.capacity); + char* new_ptr = malloc(s.capacity); mem::copy(new_ptr, s.ptr, s.len); s.ptr = new_ptr; } @@ -47,7 +47,7 @@ fn void String.append(String *s, char[] other_string) s.capacity *= 2; } 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); s.ptr = new_ptr; } @@ -64,7 +64,7 @@ fn void String.concat(String *s, String* other_string) s.capacity *= 2; } 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); s.ptr = new_ptr; } diff --git a/src/compiler/sema_name_resolution.c b/src/compiler/sema_name_resolution.c index 884d9dabb..25faf43fc 100644 --- a/src/compiler/sema_name_resolution.c +++ b/src/compiler/sema_name_resolution.c @@ -78,6 +78,19 @@ static Decl *sema_find_decl_in_imports(Decl **imports, NameResolve *name_resolve // Did we already have a match? 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. name_resolve->ambiguous_other_decl = found; continue; @@ -237,6 +250,19 @@ static Decl *sema_find_decl_in_global_new(CompilationUnit *unit, DeclTable *tabl { ambiguous = decl; 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; @@ -320,13 +346,25 @@ static Decl *sema_resolve_no_path_symbol(SemaContext *context, NameResolve *name if (decl) return decl; - // Search in the module. decl = module_find_symbol(unit->module, symbol); if (decl) return decl; 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); } diff --git a/test/test_suite/errors/error_regression_2.c3t b/test/test_suite/errors/error_regression_2.c3t index c10b1cc7c..38534b7e0 100644 --- a/test/test_suite/errors/error_regression_2.c3t +++ b/test/test_suite/errors/error_regression_2.c3t @@ -39,7 +39,7 @@ fn bool contains(char[] haystack, char[] needle) macro dupe(value) { - $typeof(&value) temp = mem::alloc($sizeof(value)); + $typeof(&value) temp = malloc($sizeof(value)); if (!temp) return ReadError.OUT_OF_MEMORY!; *temp = value; return temp; @@ -59,7 +59,7 @@ fn Doc! readDoc(char[] url) if (contains(url, "title-empty")) return { dupe(Head { .title = dupe((char[])"")? })? }; // Not particularly elegant due to missing string functions. 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!; libc::snprintf(str, len + 1, "Title of %.*s", (int)url.len, url.ptr); return { dupe(Head { .title = dupe(str[..len - 1])? })? }; @@ -368,7 +368,7 @@ if.then15: ; preds = %if.exit9 store %"char[]"* null, %"char[]"** %33, align 8 %34 = load %Head, %Head* %literal18, 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* store %Head* %ptrptr, %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 %54 = getelementptr inbounds %Head, %Head* %literal32, i32 0, i32 0 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[]"* store %"char[]"* %ptrptr36, %"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 %62 = load %Head, %Head* %literal32, 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* store %Head* %ptrptr43, %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 %siuiext = sext i32 %77 to i64 %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 %79 = load i8*, i8** %str, align 8 %not50 = icmp eq i8* %79, null @@ -522,7 +522,7 @@ if.exit52: ; preds = %if.exit49 %93 = insertvalue %"char[]" undef, i8* %ptroffset, 0 %94 = insertvalue %"char[]" %93, i64 %size, 1 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[]"* store %"char[]"* %ptrptr64, %"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 %102 = load %Head, %Head* %literal60, 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* store %Head* %ptrptr71, %Head** %temp70, align 8 %104 = load %Head*, %Head** %temp70, align 8 diff --git a/test/test_suite2/errors/error_regression_2.c3t b/test/test_suite2/errors/error_regression_2.c3t index 00e3d8c0f..61b497dcc 100644 --- a/test/test_suite2/errors/error_regression_2.c3t +++ b/test/test_suite2/errors/error_regression_2.c3t @@ -39,7 +39,7 @@ fn bool contains(char[] haystack, char[] needle) macro dupe(value) { - $typeof(&value) temp = mem::alloc($sizeof(value)); + $typeof(&value) temp = malloc($sizeof(value)); if (!temp) return ReadError.OUT_OF_MEMORY!; *temp = value; return temp; @@ -59,7 +59,7 @@ fn Doc! readDoc(char[] url) if (contains(url, "title-empty")) return { dupe(Head { .title = dupe((char[])"")? })? }; // Not particularly elegant due to missing string functions. 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!; libc::snprintf(str, len + 1, "Title of %.*s", (int)url.len, url.ptr); return { dupe(Head { .title = dupe(str[..len - 1])? })? }; @@ -358,7 +358,7 @@ if.then15: ; preds = %if.exit9 store ptr null, ptr %25, align 8 %26 = load %Head, ptr %literal18, 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 %28 = load ptr, ptr %temp, align 8 %not = icmp eq ptr %28, null @@ -402,7 +402,7 @@ if.then27: ; preds = %if.exit21 store ptr null, ptr %literal32, align 8 %39 = getelementptr inbounds %Head, ptr %literal32, i32 0, i32 0 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 %41 = load ptr, ptr %temp35, align 8 %not36 = icmp eq ptr %41, null @@ -426,7 +426,7 @@ noerr_block40: ; preds = %if.exit38 store ptr %44, ptr %39, align 8 %45 = load %Head, ptr %literal32, 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 %47 = load ptr, ptr %temp41, align 8 %not42 = icmp eq ptr %47, null @@ -462,7 +462,7 @@ if.exit47: ; preds = %if.exit21 %56 = load i32, ptr %len, align 4 %siuiext = sext i32 %56 to i64 %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 %58 = load ptr, ptr %str, align 8 %not48 = icmp eq ptr %58, null @@ -495,7 +495,7 @@ if.exit50: ; preds = %if.exit47 %71 = insertvalue %"char[]" undef, ptr %ptroffset, 0 %72 = insertvalue %"char[]" %71, i64 %size, 1 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 %74 = load ptr, ptr %temp61, align 8 %not62 = icmp eq ptr %74, null @@ -519,7 +519,7 @@ noerr_block66: ; preds = %if.exit64 store ptr %77, ptr %67, align 8 %78 = load %Head, ptr %literal58, 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 %80 = load ptr, ptr %temp67, align 8 %not68 = icmp eq ptr %80, null