stlib arraylist
This commit is contained in:
@@ -34,6 +34,10 @@ _empty_slice func($T type, count usize) []mut T {
|
||||
return pointer[..count]
|
||||
}
|
||||
|
||||
empty func($T type) []mut T {
|
||||
return _empty_slice(T, 0)
|
||||
}
|
||||
|
||||
alloc func($T type, allocator Allocator, count usize) []mut T ! AllocError {
|
||||
if count == 0 {
|
||||
return _empty_slice(T, 0)
|
||||
@@ -55,6 +59,43 @@ alloc func($T type, allocator Allocator, count usize) []mut T ! AllocError {
|
||||
return .out_of_memory
|
||||
}
|
||||
|
||||
realloc func($T type, allocator Allocator, memory []mut T, new_count usize) []mut T ! AllocError {
|
||||
if new_count == memory.len {
|
||||
return memory
|
||||
}
|
||||
if new_count == 0 {
|
||||
free(T, allocator, memory)
|
||||
return _empty_slice(T, 0)
|
||||
}
|
||||
|
||||
element_size usize :: size_of(T)
|
||||
if element_size == 0 {
|
||||
return _empty_slice(T, new_count)
|
||||
}
|
||||
if new_count > max_value(usize) / element_size {
|
||||
return .out_of_memory
|
||||
}
|
||||
|
||||
old_memory ?*mut u8 = none
|
||||
old_size usize = 0
|
||||
if memory.len != 0 {
|
||||
old_memory = ptr_cast(u8, memory.ptr)
|
||||
old_size = memory.len * element_size
|
||||
}
|
||||
resized ?*mut u8 = raw_realloc(
|
||||
allocator,
|
||||
old_memory,
|
||||
old_size,
|
||||
new_count * element_size,
|
||||
align_of(T),
|
||||
)
|
||||
if resized |bytes| {
|
||||
pointer *mut T :: ptr_cast(T, bytes)
|
||||
return pointer[..new_count]
|
||||
}
|
||||
return .out_of_memory
|
||||
}
|
||||
|
||||
free func($T type, allocator Allocator, memory []mut T) void {
|
||||
if memory.len != 0 and size_of(T) != 0 {
|
||||
raw_free(allocator, ptr_cast(u8, memory.ptr), memory.len * size_of(T), align_of(T))
|
||||
|
||||
Reference in New Issue
Block a user