Refactor strings and allocator to primitives
This commit is contained in:
parent
163283f77f
commit
f444911452
6
Makefile
6
Makefile
@ -24,7 +24,7 @@ ifeq ($(CC),gcc)
|
||||
export ASAN_OPTIONS=verify_asan_link_order=0
|
||||
endif
|
||||
|
||||
.PHONY: all clean builddir build-test run-test codegen build-lib full prng testing uuid core containers
|
||||
.PHONY: all clean builddir build-test run-test codegen build-lib full prng testing uuid core primitives
|
||||
|
||||
all: clean builddir codegen run-test full
|
||||
|
||||
@ -62,5 +62,5 @@ uuid: build-lib
|
||||
core: LIB_SRC = src/core/wapp_core.c
|
||||
core: build-lib
|
||||
|
||||
containers: LIB_SRC = src/core/wapp_containers.c
|
||||
containers: build-lib
|
||||
primitives: LIB_SRC = src/core/wapp_primitives.c
|
||||
primitives: build-lib
|
||||
|
@ -1,177 +0,0 @@
|
||||
#include "array.h"
|
||||
#include "../../common/aliases/aliases.h"
|
||||
#include "../../common/misc/misc_utils.h"
|
||||
#include "../../core/mem/allocator/mem_allocator.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
i32 *wapp_i32_array_get(const I32Array *array, u64 index) {
|
||||
if (!array || index >= array->count) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return &(array->items[index]);
|
||||
}
|
||||
|
||||
void wapp_i32_array_set(I32Array *array, u64 index, i32 item) {
|
||||
if (!array || index >= array->count) {
|
||||
return;
|
||||
}
|
||||
|
||||
array->items[index] = item;
|
||||
}
|
||||
|
||||
void wapp_i32_array_append_capped(I32Array *array, i32 item) {
|
||||
if (!array || array->count >= array->capacity) {
|
||||
return;
|
||||
}
|
||||
|
||||
array->items[(array->count)++] = item;
|
||||
}
|
||||
|
||||
void wapp_i32_array_extend_capped(I32Array *array, const I32Array *other) {
|
||||
if (!array || !other) {
|
||||
return;
|
||||
}
|
||||
|
||||
u64 remaining_capacity = array->capacity - array->count;
|
||||
if (other->count >= remaining_capacity) {
|
||||
return;
|
||||
}
|
||||
|
||||
i32 *item;
|
||||
u64 items_to_add = other->count;
|
||||
for (u64 i = 0; i < items_to_add; ++i) {
|
||||
item = wapp_i32_array_get(other, i);
|
||||
if (!item) {
|
||||
continue;
|
||||
}
|
||||
|
||||
wapp_i32_array_append_capped(array, *item);
|
||||
}
|
||||
}
|
||||
|
||||
void wapp_i32_array_clear(I32Array *array) {
|
||||
if (!array) {
|
||||
return;
|
||||
}
|
||||
|
||||
array->count = 0;
|
||||
}
|
||||
|
||||
i32 wapp_i32_array_pop(I32Array *array) {
|
||||
if (!array || array->count == 0) {
|
||||
return (i32){0};
|
||||
}
|
||||
|
||||
return array->items[--(array->count)];
|
||||
}
|
||||
|
||||
void wapp_i32_array_copy_capped(const I32Array *src, I32Array *dst) {
|
||||
if (!src || !dst) {
|
||||
return;
|
||||
}
|
||||
|
||||
wapp_i32_array_clear(dst);
|
||||
|
||||
i32 *item;
|
||||
u64 to_copy = src->count < dst->capacity ? src->count : dst->capacity;
|
||||
for (u64 i = 0; i < to_copy; ++i) {
|
||||
item = wapp_i32_array_get(src, i);
|
||||
if (!item) {
|
||||
continue;
|
||||
}
|
||||
|
||||
wapp_i32_array_append_capped(dst, *item);
|
||||
}
|
||||
}
|
||||
|
||||
I32Array *wapp_i32_array_alloc_capacity(const Allocator *allocator, u64 capacity) {
|
||||
u64 allocation_size = sizeof(I32Array) + sizeof(i32) * capacity;
|
||||
I32Array *array = NULL;
|
||||
|
||||
if (!allocator) {
|
||||
goto RETURN_I32_ARRAY_ALLOC;
|
||||
}
|
||||
|
||||
array = wapp_mem_allocator_alloc(allocator, allocation_size);
|
||||
if (!array) {
|
||||
goto RETURN_I32_ARRAY_ALLOC;
|
||||
}
|
||||
|
||||
array->items = (i32 *)((u8 *)array + sizeof(I32Array));
|
||||
array->count = 0;
|
||||
array->capacity = capacity;
|
||||
|
||||
RETURN_I32_ARRAY_ALLOC:
|
||||
return array;
|
||||
}
|
||||
|
||||
I32Array *wapp_i32_array_append_alloc(const Allocator *allocator, I32Array *array, i32 item) {
|
||||
I32Array *output = array;
|
||||
|
||||
if (!allocator || !array) {
|
||||
goto RETURN_I32_ARRAY_APPEND_ALLOC;
|
||||
}
|
||||
|
||||
if (array->count >= array->capacity) {
|
||||
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2);
|
||||
output = wapp_i32_array_alloc_capacity(allocator, new_capacity);
|
||||
if (!output) {
|
||||
output = array;
|
||||
goto RETURN_I32_ARRAY_APPEND_ALLOC;
|
||||
}
|
||||
wapp_i32_array_copy_capped(array, output);
|
||||
}
|
||||
|
||||
wapp_i32_array_append_capped(output, item);
|
||||
|
||||
RETURN_I32_ARRAY_APPEND_ALLOC:
|
||||
return output;
|
||||
}
|
||||
|
||||
I32Array *wapp_i32_array_extend_alloc(const Allocator *allocator, I32Array *array, const I32Array *other) {
|
||||
I32Array *output = array;
|
||||
|
||||
if (!allocator || !array || !other) {
|
||||
goto RETURN_I32_ARRAY_EXTEND_ALLOC;
|
||||
}
|
||||
|
||||
u64 remaining_capacity = array->capacity - array->count;
|
||||
if (other->count >= remaining_capacity) {
|
||||
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2);
|
||||
output = wapp_i32_array_alloc_capacity(allocator, new_capacity);
|
||||
if (!output) {
|
||||
output = array;
|
||||
goto RETURN_I32_ARRAY_EXTEND_ALLOC;
|
||||
}
|
||||
wapp_i32_array_copy_capped(array, output);
|
||||
}
|
||||
|
||||
wapp_i32_array_extend_capped(output, other);
|
||||
|
||||
RETURN_I32_ARRAY_EXTEND_ALLOC:
|
||||
return output;
|
||||
}
|
||||
|
||||
I32Array *wapp_i32_array_copy_alloc(const Allocator *allocator, const I32Array *src, I32Array *dst) {
|
||||
I32Array *output = dst;
|
||||
|
||||
if (!allocator || !src || !dst) {
|
||||
goto RETURN_I32_ARRAY_COPY_ALLOC;
|
||||
}
|
||||
|
||||
if (src->count >= dst->capacity) {
|
||||
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2);
|
||||
output = wapp_i32_array_alloc_capacity(allocator, new_capacity);
|
||||
if (!output) {
|
||||
output = dst;
|
||||
goto RETURN_I32_ARRAY_COPY_ALLOC;
|
||||
}
|
||||
}
|
||||
|
||||
wapp_i32_array_clear(output);
|
||||
wapp_i32_array_copy_capped(src, output);
|
||||
|
||||
RETURN_I32_ARRAY_COPY_ALLOC:
|
||||
return output;
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
#ifndef ARRAY_H
|
||||
#define ARRAY_H
|
||||
|
||||
#include "../../common/aliases/aliases.h"
|
||||
#include "../../common/misc/misc_utils.h"
|
||||
#include "../../common/platform/platform.h"
|
||||
#include "../../core/mem/allocator/mem_allocator.h"
|
||||
|
||||
#ifdef WAPP_PLATFORM_CPP
|
||||
BEGIN_C_LINKAGE
|
||||
#endif // !WAPP_PLATFORM_CPP
|
||||
|
||||
typedef struct i32_array I32Array;
|
||||
struct i32_array {
|
||||
i32 *items;
|
||||
u64 count;
|
||||
u64 capacity;
|
||||
};
|
||||
|
||||
#define wapp_i32_array(...) ((I32Array){ \
|
||||
.items = (i32[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i32, __VA_ARGS__) * 2)]){__VA_ARGS__}, \
|
||||
.count = wapp_misc_utils_va_args_count(i32, __VA_ARGS__), \
|
||||
.capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i32, __VA_ARGS__) * 2) \
|
||||
})
|
||||
#define wapp_i32_array_with_capacity(CAPACITY) ((I32Array){.items = (i32[CAPACITY]){0}, .count = 0, .capacity = CAPACITY})
|
||||
|
||||
i32 *wapp_i32_array_get(const I32Array *array, u64 index);
|
||||
void wapp_i32_array_set(I32Array *array, u64 index, i32 item);
|
||||
void wapp_i32_array_append_capped(I32Array *array, i32 item);
|
||||
void wapp_i32_array_extend_capped(I32Array *array, const I32Array *other);
|
||||
void wapp_i32_array_clear(I32Array *array);
|
||||
i32 wapp_i32_array_pop(I32Array *array);
|
||||
void wapp_i32_array_copy_capped(const I32Array *src, I32Array *dst);
|
||||
|
||||
I32Array *wapp_i32_array_alloc_capacity(const Allocator *allocator, u64 capacity);
|
||||
I32Array *wapp_i32_array_append_alloc(const Allocator *allocator, I32Array *array, i32 item);
|
||||
I32Array *wapp_i32_array_extend_alloc(const Allocator *allocator, I32Array *array, const I32Array *other);
|
||||
I32Array *wapp_i32_array_copy_alloc(const Allocator *allocator, const I32Array *src, I32Array *dst);
|
||||
|
||||
#ifdef WAPP_PLATFORM_CPP
|
||||
END_C_LINKAGE
|
||||
#endif // !WAPP_PLATFORM_CPP
|
||||
|
||||
#endif // !ARRAY_H
|
@ -1,8 +0,0 @@
|
||||
#ifndef WAPP_CONTAINERS_C
|
||||
#define WAPP_CONTAINERS_C
|
||||
|
||||
#include "wapp_containers.h"
|
||||
#include "array/array.c"
|
||||
#include "dbl_list/dbl_list.c"
|
||||
|
||||
#endif // !WAPP_CONTAINERS_C
|
@ -1,8 +0,0 @@
|
||||
#ifndef WAPP_CONTAINERS_H
|
||||
#define WAPP_CONTAINERS_H
|
||||
|
||||
#include "dbl_list/dbl_list.h"
|
||||
#include "array/array.h"
|
||||
#include "../common/wapp_common.h"
|
||||
|
||||
#endif // !WAPP_CONTAINERS_H
|
@ -3,7 +3,7 @@
|
||||
|
||||
#include "../../../common/aliases/aliases.h"
|
||||
#include "../../../common/platform/platform.h"
|
||||
#include "../allocator/mem_allocator.h"
|
||||
#include "../../../primitives/mem_allocator/mem_allocator.h"
|
||||
#include "../../os/mem/mem_os.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
|
@ -1,10 +1,10 @@
|
||||
#include "cpath.h"
|
||||
#include "../../../common/aliases/aliases.h"
|
||||
#include "../../../common/misc/misc_utils.h"
|
||||
#include "../../mem/allocator/mem_allocator.h"
|
||||
#include "../../mem/arena/mem_arena_allocator.h"
|
||||
#include "../../strings/str8/str8.h"
|
||||
#include "../../../containers/dbl_list/dbl_list.h"
|
||||
#include "../../../primitives/dbl_list/dbl_list.h"
|
||||
#include "../../../primitives/mem_allocator/mem_allocator.h"
|
||||
#include "../../../primitives/strings/str8/str8.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
@ -3,8 +3,8 @@
|
||||
|
||||
#include "../../../common/aliases/aliases.h"
|
||||
#include "../../../common/platform/platform.h"
|
||||
#include "../../mem/allocator/mem_allocator.h"
|
||||
#include "../../strings/str8/str8.h"
|
||||
#include "../../../primitives/mem_allocator/mem_allocator.h"
|
||||
#include "../../../primitives/strings/str8/str8.h"
|
||||
|
||||
#ifdef WAPP_PLATFORM_CPP
|
||||
BEGIN_C_LINKAGE
|
||||
|
@ -1,12 +1,12 @@
|
||||
#include "commander.h"
|
||||
#include "commander_output.h"
|
||||
#include "../utils/shell_utils.h"
|
||||
#include "../../../mem/allocator/mem_allocator.h"
|
||||
#include "../../../mem/arena/mem_arena_allocator.h"
|
||||
#include "../../../strings/str8/str8.h"
|
||||
#include "../../../../containers/dbl_list/dbl_list.h"
|
||||
#include "../../../../common/aliases/aliases.h"
|
||||
#include "../../../../common/misc/misc_utils.h"
|
||||
#include "../../../../primitives/dbl_list/dbl_list.h"
|
||||
#include "../../../../primitives/mem_allocator/mem_allocator.h"
|
||||
#include "../../../../primitives/strings/str8/str8.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
@ -4,7 +4,7 @@
|
||||
#include "commander_output.h"
|
||||
#include "../../../../common/aliases/aliases.h"
|
||||
#include "../../../../common/platform/platform.h"
|
||||
#include "../../../strings/str8/str8.h"
|
||||
#include "../../../../primitives/strings/str8/str8.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "../../../../../common/aliases/aliases.h"
|
||||
#include "../../../../../common/platform/platform.h"
|
||||
#include "../../../../strings/str8/str8.h"
|
||||
#include "../../../../../primitives/strings/str8/str8.h"
|
||||
|
||||
#ifdef WAPP_PLATFORM_POSIX
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "termcolour.h"
|
||||
#include "terminal_colours.h"
|
||||
#include "../../../strings/str8/str8.h"
|
||||
#include "../../../../primitives/strings/str8/str8.h"
|
||||
|
||||
void wapp_shell_termcolour_print_text(Str8RO *text, TerminalColour colour) {
|
||||
if (colour < WAPP_TERM_COLOUR_FG_BLACK || colour > WAPP_TERM_COLOUR_FG_BR_WHITE) {
|
||||
|
@ -4,7 +4,7 @@
|
||||
#include "terminal_colours.h"
|
||||
#include "../../../../common/aliases/aliases.h"
|
||||
#include "../../../../common/platform/platform.h"
|
||||
#include "../../../strings/str8/str8.h"
|
||||
#include "../../../../primitives/strings/str8/str8.h"
|
||||
|
||||
#ifdef WAPP_PLATFORM_CPP
|
||||
BEGIN_C_LINKAGE
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "../../../../../common/aliases/aliases.h"
|
||||
#include "../../../../../common/platform/platform.h"
|
||||
#include "../../../../strings/str8/str8.h"
|
||||
#include "../../../../../primitives/strings/str8/str8.h"
|
||||
|
||||
#ifdef WAPP_PLATFORM_WINDOWS
|
||||
|
||||
|
@ -2,7 +2,6 @@
|
||||
#define WAPP_CORE_C
|
||||
|
||||
#include "wapp_core.h"
|
||||
#include "strings/str8/str8.c"
|
||||
#include "os/shell/termcolour/posix/termcolour_posix.c"
|
||||
#include "os/shell/termcolour/win/termcolour_win.c"
|
||||
#include "os/shell/termcolour/termcolour.c"
|
||||
@ -14,9 +13,8 @@
|
||||
#include "os/mem/win/mem_os_win.c"
|
||||
#include "os/mem/mem_os.c"
|
||||
#include "mem/utils/mem_utils.c"
|
||||
#include "mem/allocator/mem_allocator.c"
|
||||
#include "mem/arena/mem_arena.c"
|
||||
#include "mem/arena/mem_arena_allocator.c"
|
||||
#include "../containers/wapp_containers.c"
|
||||
#include "../primitives/wapp_primitives.c"
|
||||
|
||||
#endif // !WAPP_CORE_C
|
||||
|
@ -1,7 +1,6 @@
|
||||
#ifndef WAPP_CORE_H
|
||||
#define WAPP_CORE_H
|
||||
|
||||
#include "strings/str8/str8.h"
|
||||
#include "os/shell/termcolour/termcolour.h"
|
||||
#include "os/shell/termcolour/terminal_colours.h"
|
||||
#include "os/shell/commander/commander.h"
|
||||
@ -13,10 +12,9 @@
|
||||
#include "os/mem/mem_os_ops.h"
|
||||
#include "os/mem/mem_os.h"
|
||||
#include "mem/utils/mem_utils.h"
|
||||
#include "mem/allocator/mem_allocator.h"
|
||||
#include "mem/arena/mem_arena_allocator.h"
|
||||
#include "mem/arena/mem_arena.h"
|
||||
#include "../common/wapp_common.h"
|
||||
#include "../containers/wapp_containers.h"
|
||||
#include "../primitives/wapp_primitives.h"
|
||||
|
||||
#endif // !WAPP_CORE_H
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include "mem_allocator.h"
|
||||
#include "../../../common/aliases/aliases.h"
|
||||
#include "../../common/aliases/aliases.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void *wapp_mem_allocator_alloc(const Allocator *allocator, u64 size) {
|
@ -1,8 +1,8 @@
|
||||
#ifndef MEM_ALLOCATOR_H
|
||||
#define MEM_ALLOCATOR_H
|
||||
|
||||
#include "../../../common/aliases/aliases.h"
|
||||
#include "../../../common/platform/platform.h"
|
||||
#include "../../common/aliases/aliases.h"
|
||||
#include "../../common/platform/platform.h"
|
||||
#include <string.h>
|
||||
|
||||
#ifdef WAPP_PLATFORM_CPP
|
@ -1,6 +1,6 @@
|
||||
#include "str8.h"
|
||||
#include "../../../common/aliases/aliases.h"
|
||||
#include "../../mem/allocator/mem_allocator.h"
|
||||
#include "../../mem_allocator/mem_allocator.h"
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
@ -3,8 +3,8 @@
|
||||
|
||||
#include "../../../common/aliases/aliases.h"
|
||||
#include "../../../common/platform/platform.h"
|
||||
#include "../../../containers/dbl_list/dbl_list.h"
|
||||
#include "../../mem/allocator/mem_allocator.h"
|
||||
#include "../../../primitives/dbl_list/dbl_list.h"
|
||||
#include "../../mem_allocator/mem_allocator.h"
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
10
src/primitives/wapp_primitives.c
Normal file
10
src/primitives/wapp_primitives.c
Normal file
@ -0,0 +1,10 @@
|
||||
#ifndef WAPP_PRIMITIVES_C
|
||||
#define WAPP_PRIMITIVES_C
|
||||
|
||||
#include "wapp_primitives.h"
|
||||
#include "array/array.c"
|
||||
#include "dbl_list/dbl_list.c"
|
||||
#include "mem_allocator/mem_allocator.c"
|
||||
#include "strings/str8/str8.c"
|
||||
|
||||
#endif // !WAPP_PRIMITIVES_C
|
10
src/primitives/wapp_primitives.h
Normal file
10
src/primitives/wapp_primitives.h
Normal file
@ -0,0 +1,10 @@
|
||||
#ifndef WAPP_PRIMITIVES_H
|
||||
#define WAPP_PRIMITIVES_H
|
||||
|
||||
#include "dbl_list/dbl_list.h"
|
||||
#include "array/array.h"
|
||||
#include "mem_allocator/mem_allocator.h"
|
||||
#include "strings/str8/str8.h"
|
||||
#include "../common/wapp_common.h"
|
||||
|
||||
#endif // !WAPP_PRIMITIVES_H
|
@ -1,7 +1,7 @@
|
||||
#include "tester.h"
|
||||
#include "../../common/aliases/aliases.h"
|
||||
#include "../../core/os/shell/termcolour/termcolour.h"
|
||||
#include "../../core/strings/str8/str8.h"
|
||||
#include "../../primitives/strings/str8/str8.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -3,7 +3,7 @@
|
||||
|
||||
#include "../../common/misc/misc_utils.h"
|
||||
#include "../../common/platform/platform.h"
|
||||
#include "../../core/strings/str8/str8.h"
|
||||
#include "../../primitives/strings/str8/str8.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef WAPP_PLATFORM_CPP
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "uuid.h"
|
||||
#include "../common/aliases/aliases.h"
|
||||
#include "../core/strings/str8/str8.h"
|
||||
#include "../primitives/strings/str8/str8.h"
|
||||
#include "../prng/xorshift/xorshift.h"
|
||||
#include <stdbool.h>
|
||||
#include <inttypes.h>
|
||||
|
@ -3,7 +3,7 @@
|
||||
|
||||
#include "../common/aliases/aliases.h"
|
||||
#include "../common/platform/platform.h"
|
||||
#include "../core/strings/str8/str8.h"
|
||||
#include "../primitives/strings/str8/str8.h"
|
||||
|
||||
#ifdef WAPP_PLATFORM_CPP
|
||||
BEGIN_C_LINKAGE
|
||||
|
@ -2,7 +2,7 @@
|
||||
#define WAPP_C
|
||||
|
||||
#include "wapp.h"
|
||||
#include "containers/wapp_containers.c"
|
||||
#include "primitives/wapp_primitives.c"
|
||||
#include "core/wapp_core.c"
|
||||
#include "prng/wapp_prng.c"
|
||||
#include "uuid/uuid.c"
|
||||
|
@ -2,7 +2,7 @@
|
||||
#define WAPP_H
|
||||
|
||||
#include "common/wapp_common.h"
|
||||
#include "containers/wapp_containers.h"
|
||||
#include "primitives/wapp_primitives.h"
|
||||
#include "core/wapp_core.h"
|
||||
#include "prng/wapp_prng.h"
|
||||
#include "uuid/wapp_uuid.h"
|
||||
|
Loading…
x
Reference in New Issue
Block a user