refactored code from tcpu
This commit is contained in:
39
src/collections/List.c
Executable file
39
src/collections/List.c
Executable file
@@ -0,0 +1,39 @@
|
||||
#include "collections/List.h"
|
||||
|
||||
List List_alloc_size(u32 initial_size){
|
||||
if(initial_size == 0)
|
||||
return List_construct_size(NULL, 0, 0);
|
||||
u32 allocated_size = ALIGN_TO(initial_size, sizeof(void*));
|
||||
return List_construct_size(malloc(allocated_size), 0, allocated_size);
|
||||
}
|
||||
|
||||
void* List_expand(List* ptr, u32 expansion_size){
|
||||
u32 occupied_size = ptr->size;
|
||||
u32 expanded_alloc_size = ptr->allocated_size;
|
||||
if(expanded_alloc_size == 0)
|
||||
expanded_alloc_size = 64;
|
||||
ptr->size += expansion_size;
|
||||
while(ptr->size > expanded_alloc_size){
|
||||
expanded_alloc_size *= 2;
|
||||
}
|
||||
// if ptr->data is null, realloc acts like malloc
|
||||
ptr->data = realloc(ptr->data, expanded_alloc_size);
|
||||
ptr->allocated_size = expanded_alloc_size;
|
||||
return (u8*)(ptr->data) + occupied_size;
|
||||
}
|
||||
|
||||
void List_push_size(List* ptr, void* values, u32 size){
|
||||
void* empty_cell_ptr = List_expand(ptr, size);
|
||||
memcpy(empty_cell_ptr, values, size);
|
||||
}
|
||||
|
||||
bool List_removeAt_size(List* ptr, u32 i, u32 remove_size){
|
||||
if(i + remove_size >= ptr->size)
|
||||
return false;
|
||||
|
||||
ptr->size -= remove_size;
|
||||
u8* src = (u8*)ptr->data + i + remove_size;
|
||||
u8* dst = (u8*)ptr->data + i;
|
||||
memmove(dst, src, ptr->size - i - remove_size);
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user