vtables, protocol
This commit is contained in:
@@ -31,12 +31,8 @@ context : Context = ---;
|
||||
|
||||
// --- Slice & string allocation ---
|
||||
|
||||
context_alloc :: (size: s64) -> *void {
|
||||
if context.allocator.ctx != null then context.allocator.alloc(size) else malloc(size);
|
||||
}
|
||||
|
||||
cstring :: (size: s64) -> string {
|
||||
raw := context_alloc(size + 1);
|
||||
raw := context.allocator.alloc(size + 1);
|
||||
memset(raw, 0, size + 1);
|
||||
s : string = ---;
|
||||
s.ptr = xx raw;
|
||||
@@ -45,7 +41,7 @@ cstring :: (size: s64) -> string {
|
||||
}
|
||||
|
||||
alloc_slice :: ($T: Type, count: s64) -> []T {
|
||||
raw := context_alloc(count * size_of(T));
|
||||
raw := context.allocator.alloc(count * size_of(T));
|
||||
memset(raw, 0, count * size_of(T));
|
||||
s : []T = ---;
|
||||
s.ptr = xx raw;
|
||||
@@ -337,19 +333,19 @@ List :: struct ($T: Type) {
|
||||
items: [*]T = null;
|
||||
len: s64 = 0;
|
||||
cap: s64 = 0;
|
||||
}
|
||||
|
||||
append ::(list: *List($T), item: T) {
|
||||
if list.len >= list.cap {
|
||||
new_cap := if list.cap == 0 then 4 else list.cap * 2;
|
||||
new_items : [*]T = xx malloc(new_cap * size_of(T));
|
||||
if list.len > 0 {
|
||||
memcpy(new_items, list.items, list.len * size_of(T));
|
||||
free(list.items);
|
||||
append :: (list: *List(T), item: T) {
|
||||
if list.len >= list.cap {
|
||||
new_cap := if list.cap == 0 then 4 else list.cap * 2;
|
||||
new_items : [*]T = xx context.allocator.alloc(new_cap * size_of(T));
|
||||
if list.len > 0 {
|
||||
memcpy(new_items, list.items, list.len * size_of(T));
|
||||
context.allocator.dealloc(list.items);
|
||||
}
|
||||
list.items = new_items;
|
||||
list.cap = new_cap;
|
||||
}
|
||||
list.items = new_items;
|
||||
list.cap = new_cap;
|
||||
list.items[list.len] = item;
|
||||
list.len += 1;
|
||||
}
|
||||
list.items[list.len] = item;
|
||||
list.len += 1;
|
||||
}
|
||||
Reference in New Issue
Block a user