Basic allocators with unmanaged list (broken)
This commit is contained in:
4
examples/Alloc.nct
Normal file
4
examples/Alloc.nct
Normal file
@@ -0,0 +1,4 @@
|
||||
record Alloc {
|
||||
u8* userdata;
|
||||
u8*(u8* userdata, u8* ptr, u32 sz)* realloc;
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
record Allocator {
|
||||
u8* userdata;
|
||||
u8*(u8* userdata, u8* original, u32 size) realloc;
|
||||
}
|
||||
|
||||
43
examples/ListDLU.nct
Normal file
43
examples/ListDLU.nct
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* ListDLU: Dynamic, Linear growth, unmanaged
|
||||
*/
|
||||
|
||||
use Alloc;
|
||||
|
||||
record ListDLU[T, S; growth] {
|
||||
S capacity;
|
||||
S size;
|
||||
T[?]* data;
|
||||
}
|
||||
|
||||
ListDLU_remove: [T, S; growth]u0(ListDLU[T, S; growth]* this, S index) -> {
|
||||
T* data0 = &((*((*this).data))[index]);
|
||||
T* data1 = data0 + @sizeof T;
|
||||
S sz = (*this).size;
|
||||
(*this).size = (*this).size - 1;
|
||||
loop {
|
||||
if(index == sz) {
|
||||
break;
|
||||
}
|
||||
|
||||
*data0 = *data1;
|
||||
|
||||
data0 = data0 + @sizeof T;
|
||||
data1 = data1 + @sizeof T;
|
||||
index = index + 1;
|
||||
}
|
||||
return;
|
||||
};
|
||||
|
||||
ListDLU_add: [T, S; growth]u0(ListDLU[T, S; growth]* this, Alloc *alloc, T value) -> {
|
||||
if((*this).size == (*this).capacity) {
|
||||
u32 newcap = (*this).capacity + growth;
|
||||
(*this).capacity = newcap;
|
||||
(*this).data = ((*alloc).realloc)((*alloc).userdata, (*this).data, newcap * @sizeof T);
|
||||
}
|
||||
|
||||
(*((*this).data))[(*this).size] = value;
|
||||
(*this).size = (*this).size + 1;
|
||||
|
||||
return;
|
||||
};
|
||||
26
examples/UserListDLU.nct
Normal file
26
examples/UserListDLU.nct
Normal file
@@ -0,0 +1,26 @@
|
||||
use ListDLU;
|
||||
|
||||
@section(".text");
|
||||
|
||||
@instantiate ListDLU_remove[u32, u32; 9];
|
||||
@instantiate ListDLU_add[u32, u32; 9];
|
||||
|
||||
extern u8*(u8* ptr, u32 sz) realloc;
|
||||
libc_realloc: u8*(u8* userdata, u8* ptr, u32 sz) -> {
|
||||
return realloc(ptr, sz);
|
||||
};
|
||||
|
||||
main: u0() -> {
|
||||
Alloc libc;
|
||||
libc.realloc = &libc_realloc;
|
||||
|
||||
ListDLU[u32, u32; 9] list;
|
||||
ListDLU_add[u32, u32; 9](&list, &libc, 1234);
|
||||
ListDLU_add[u32, u32; 9](&list, &libc, 4321);
|
||||
ListDLU_add[u32, u32; 9](&list, &libc, 7777);
|
||||
ListDLU_add[u32, u32; 9](&list, &libc, 6969);
|
||||
ListDLU_remove[u32, u32; 9](&list, 1);
|
||||
return;
|
||||
};
|
||||
|
||||
@section(".bss");
|
||||
Reference in New Issue
Block a user