mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Fix of shadowing bug. Allow pointer and subarrays to be constant initialized. Compile time values may now pass around anything considered compile time constant. It's possible to index into an initializer list at compile time. (Some work still remains on this)
This commit is contained in:
committed by
Christoffer Lerno
parent
1b103a3e22
commit
e4c7dde30b
@@ -25,6 +25,11 @@ struct Allocator
|
||||
void *data;
|
||||
}
|
||||
|
||||
func 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
|
||||
{
|
||||
switch (kind)
|
||||
@@ -47,6 +52,37 @@ func void! system_malloc_function(void *unused, void** pointer, usize bytes, usi
|
||||
$unreachable;
|
||||
}
|
||||
|
||||
|
||||
struct SlotAllocator
|
||||
{
|
||||
void* pages;
|
||||
usize page_size;
|
||||
usize page_count;
|
||||
usize bitmask;
|
||||
usize current_page;
|
||||
}
|
||||
|
||||
func 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);
|
||||
if (*page_pointer)
|
||||
{
|
||||
mem::free(*page_pointer);
|
||||
*page_pointer = null;
|
||||
}
|
||||
if (size > allocator.page_size - $sizeof(page_pointer))
|
||||
{
|
||||
void* mem = mem::_malloc(size);
|
||||
if (!mem) return AllocationFailure.OUT_OF_MEMORY!;
|
||||
*page_pointer = mem;
|
||||
allocator.current_page = (allocator.current_page + 1) & (allocator.bitmask);
|
||||
return mem;
|
||||
}
|
||||
allocator.current_page = (allocator.current_page + 1) & (allocator.bitmask);
|
||||
return &page_pointer[1];
|
||||
}
|
||||
|
||||
struct RingAllocator
|
||||
{
|
||||
char *data;
|
||||
@@ -131,4 +167,24 @@ func void* realloc(void *ptr, usize size) @inline
|
||||
func void free(void* ptr) @inline
|
||||
{
|
||||
_free(ptr);
|
||||
}
|
||||
|
||||
|
||||
const TEMP_BLOCK_SIZE = 1024;
|
||||
const TEMP_PAGES = 64;
|
||||
|
||||
private char[TEMP_BLOCK_SIZE * TEMP_PAGES] allocator_static_storage;
|
||||
private void*[TEMP_PAGES] allocator_static_page_storage;
|
||||
|
||||
SlotAllocator default_allocator = {
|
||||
.pages = &allocator_static_storage,
|
||||
.page_size = TEMP_BLOCK_SIZE,
|
||||
.page_count = TEMP_PAGES,
|
||||
.bitmask = TEMP_PAGES - 1,
|
||||
.current_page = 0,
|
||||
};
|
||||
|
||||
func void*! talloc(usize size)
|
||||
{
|
||||
return default_allocator.alloc(size);
|
||||
}
|
||||
86
resources/testfragments/tmem.c3
Normal file
86
resources/testfragments/tmem.c3
Normal file
@@ -0,0 +1,86 @@
|
||||
module tmem;
|
||||
import std::mem;
|
||||
import std::io;
|
||||
|
||||
struct String
|
||||
{
|
||||
Allocator allocator;
|
||||
usize len;
|
||||
usize capacity;
|
||||
char* ptr;
|
||||
}
|
||||
|
||||
func void String.init(String *s, char[] c)
|
||||
{
|
||||
s.capacity = c.len + 16;
|
||||
s.ptr = mem::_malloc(s.capacity);
|
||||
s.len = c.len;
|
||||
mem::copy(s.ptr, (char*)(c), c.len);
|
||||
}
|
||||
|
||||
func char* String.zstr(String *s)
|
||||
{
|
||||
char* c = mem::_malloc(s.len + 1);
|
||||
mem::copy(c, s.ptr, s.len);
|
||||
c[s.len] = 0;
|
||||
return c;
|
||||
}
|
||||
|
||||
func void String.appendc(String *s, char c)
|
||||
{
|
||||
if (s.capacity == s.len)
|
||||
{
|
||||
s.capacity *= 2;
|
||||
char* new_ptr = mem::_malloc(s.capacity);
|
||||
mem::copy(new_ptr, s.ptr, s.len);
|
||||
s.ptr = new_ptr;
|
||||
}
|
||||
s.ptr[s.len++] = c;
|
||||
}
|
||||
|
||||
func void String.append(String *s, char[] other_string)
|
||||
{
|
||||
if (s.capacity < s.len + other_string.len)
|
||||
{
|
||||
do
|
||||
{
|
||||
s.capacity *= 2;
|
||||
}
|
||||
while (s.capacity < s.len + other_string.len);
|
||||
char* new_ptr = mem::_malloc(s.capacity);
|
||||
mem::copy(new_ptr, s.ptr, s.len);
|
||||
s.ptr = new_ptr;
|
||||
}
|
||||
mem::copy(s.ptr + s.len, (char*)(other_string), other_string.len);
|
||||
s.len += other_string.len;
|
||||
}
|
||||
|
||||
func void String.concat(String *s, String* other_string)
|
||||
{
|
||||
if (s.capacity < s.len + other_string.len)
|
||||
{
|
||||
do
|
||||
{
|
||||
s.capacity *= 2;
|
||||
}
|
||||
while (s.capacity < s.len + other_string.len);
|
||||
char* new_ptr = mem::_malloc(s.capacity);
|
||||
mem::copy(new_ptr, s.ptr, s.len);
|
||||
s.ptr = new_ptr;
|
||||
}
|
||||
mem::copy(s.ptr + s.len, other_string.ptr, other_string.len);
|
||||
s.len += other_string.len;
|
||||
}
|
||||
|
||||
func void main()
|
||||
{
|
||||
String s;
|
||||
s.init("Hello");
|
||||
s.appendc(' ');
|
||||
s.appendc('W');
|
||||
s.append("orld!");
|
||||
String w;
|
||||
w.init("Yo man!");
|
||||
s.concat(&w);
|
||||
io::printf("Message was: %s\n", s.zstr());
|
||||
}
|
||||
Reference in New Issue
Block a user