fn alloc() -> T* { return (T*)malloc(sizeof(T)); } fn zalloc() -> T* { return (T*)calloc(1, sizeof(T)); } fn alloc_n(n: usize) -> T* { return (T*)malloc(sizeof(T) * n); } struct Box { ptr: T*; } impl Box { fn new() -> Self { return Self { ptr: calloc(1, sizeof(T)) }; } fn from_ptr(p: T*) -> Self { return Self { ptr: p }; } fn get(self) -> T* { return self.ptr; } fn is_null(self) -> bool { return self.ptr == NULL; } fn free(self) { if self.ptr != NULL { free(self.ptr); } } } struct Slice { data: T*; len: usize; } impl Slice { fn new(data: T*, len: usize) -> Self { return Self { data: data, len: len }; } fn get(self, i: usize) -> T { return self.data[i]; } fn set(self, i: usize, val: T) { self.data[i] = val; } fn is_empty(self) -> bool { return self.len == 0; } } fn mem_zero(ptr: T*, count: usize) { memset(ptr, 0, sizeof(T) * count); } fn mem_copy(dst: T*, src: T*, count: usize) { memcpy(dst, src, sizeof(T) * count); } fn swap(a: T*, b: T*) { var tmp = *a; *a = *b; *b = tmp; }