Compare commits

..

204 Commits

Author SHA1 Message Date
50e23d8a13 Testing ideas for array implementation 2025-04-20 00:21:22 +01:00
3a49dba366 Generate dbl_list for default C types 2025-04-19 22:17:26 +01:00
1cfc52b35e Handle pointer types properly in dbl_list codegen 2025-04-19 21:27:00 +01:00
25395553d7 Fix codegen hidden bugs 2025-04-19 21:11:10 +01:00
9a651665ba Remove forward declaration from dbl_list source 2025-04-19 20:56:15 +01:00
8dbdfa2094 Update dbl_list codegen 2025-04-19 20:54:04 +01:00
add2ba541d Merge branch 'codegen' 2025-04-19 13:52:27 +01:00
63ed2633ea Update code generation to create one file for dbl_list 2025-04-19 13:33:09 +01:00
Abdelrahman Said
3c32b247c0 Upgrade codegen 2025-04-17 09:03:30 +01:00
Abdelrahman Said
aa04fab6ea Update codegen 2025-04-16 10:05:42 +01:00
Abdelrahman Said
2017f6de79 Start updating code for new Str8List 2025-04-16 08:14:02 +01:00
Abdelrahman Said
63acdd1336 Fix codegen bugs 2025-04-16 08:13:31 +01:00
Abdelrahman Said
cfa8094260 Fix codegen hidden bugs 2025-04-16 08:06:05 +01:00
Abdelrahman Said
31373eba03 Update dbl_list functionality 2025-04-16 00:42:20 +01:00
Abdelrahman Said
778a4da092 Add utility to create list node from item 2025-04-16 00:27:01 +01:00
Abdelrahman Said
6fb078a868 Remove codegen and implement dbl_list as generic container 2025-04-16 00:07:11 +01:00
0942643b4e Update Makefile 2025-04-13 22:21:55 +01:00
Abdelrahman Said
0e5942af34 Switch to using Makefile for *nix systems 2025-04-13 20:11:05 +01:00
Abdelrahman Said
c83c652b37 Add UUIDv4 generator 2025-03-22 02:27:59 +00:00
Abdelrahman Said
74428f1caf Rename xorshift functions 2025-03-22 00:30:20 +00:00
Abdelrahman Said
d661312cfa Add xorshift prng 2025-03-16 22:34:56 +00:00
70e075d2f6 Add message at top of codegen files to avoid overwriting them 2025-03-02 13:42:23 +00:00
96db885344 Remove codegen as submodule and just add it in place 2025-03-02 13:28:26 +00:00
fb512e4a15 Add code generation for windows 2025-03-02 13:22:05 +00:00
9361f0fe37 Run code generation for posix platforms 2025-03-02 13:20:51 +00:00
e7d2553400 Add code generation for doubly linked list 2025-03-02 13:20:27 +00:00
509724cc31 Update .gitignore 2025-03-02 13:17:50 +00:00
Abdelrahman Said
82c23eed5e Use the wapp.h header in tests 2025-02-24 08:52:46 +00:00
Abdelrahman Said
9403193f09 Update build scripts 2025-02-24 07:58:42 +00:00
Abdelrahman Said
fa1d9eec0d Fix files not using relative includes 2025-02-24 07:58:22 +00:00
Abdelrahman Said
4c14588d92 Update core headers and sources to use relative includes 2025-02-24 07:51:08 +00:00
Abdelrahman Said
9e66bd60bd Update testing headers to user relative includes 2025-02-24 07:24:26 +00:00
Abdelrahman Said
ce4957d0a0 Update common headers to user relative includes 2025-02-24 07:21:28 +00:00
b9ea290322 Update posix compile script 2025-02-24 00:30:48 +00:00
1479c13417 Move os layer to be part of the core 2025-02-24 00:30:25 +00:00
4520f2269d Reorganise memory utilities 2025-02-24 00:21:14 +00:00
6119cf5c5f Improve Str8 macros ergonomics 2025-02-23 23:53:28 +00:00
358f61e556 Reorder tests 2025-02-23 19:27:54 +00:00
7ac6100a5a Add platform detection to Windows build 2025-02-23 16:26:47 +00:00
19efb08a3a Add platform detection to posix compilation script 2025-02-23 15:32:13 +00:00
491c742189 Add single header and single source entries for all components as well as the full library (#2)
Reviewed-on: #2
Co-authored-by: Abdelrahman <said.abdelrahman89@gmail.com>
Co-committed-by: Abdelrahman <said.abdelrahman89@gmail.com>
2025-02-23 15:19:14 +00:00
4f5dd3900f Remove unused join_root_and_leaf 2025-02-22 22:11:51 +00:00
dbedcb3100 Replace char * with Str8 in cpath dirup 2025-02-22 22:09:04 +00:00
d9314fb41e Update slice and substr functions 2025-02-22 21:34:29 +00:00
fe2bb65b06 Fix spectre warning for MSVC 2025-02-22 18:04:05 +00:00
ed4ec54c7a Switch wapp_cpath_join_path to use Str8 2025-02-22 17:48:25 +00:00
ba5e902a1d Add wapp_str8_push_back and wapp_str8_list_empty 2025-02-22 17:48:04 +00:00
01f066d20c Add test for wapp_str8_format 2025-02-22 16:44:19 +00:00
80bd68f313 Add string formatting to Str8 2025-02-22 16:41:05 +00:00
cda7e413e2 Rename tester directory 2025-02-22 15:26:40 +00:00
54da75f89f Update comment for wapp_str8_lit_ro_initialiser_list 2025-02-22 15:03:13 +00:00
7df64a3168 Fix ASan linking error when compiling with gcc 2025-02-22 14:58:41 +00:00
f9f8f092b5 Fix compilation error in gcc 2025-02-22 14:58:19 +00:00
f89a4bf343 Add wapp_str8_lit_ro_initialiser_list to fix gcc compilation errors 2025-02-22 14:33:15 +00:00
0d795cc0d9 Update wapp_str8_alloc_buf to make a single big allocation 2025-02-22 13:54:03 +00:00
28ac9ed8c8 Modify Str8 substr functionality 2025-02-22 13:39:03 +00:00
99f7dcd794 Rename wapp_str8_buf_alloc to wapp_str8_alloc_buf 2025-02-18 21:02:02 +00:00
c5ca39939d Fix shell commander test on Windows 2025-02-16 22:44:42 +00:00
a308359942 Add wapp_str8_equal_to_count function 2025-02-16 22:44:18 +00:00
a3d9bcf1a1 Update .gitignore 2025-02-16 21:42:14 +00:00
Abdelrahman Said
a534d44db5 Reduce arena size in wapp_shell_commander_execute 2025-02-16 21:39:09 +00:00
11949e69be Update shell_commander to use Str8 instead of const char * 2025-02-16 21:31:22 +00:00
2c9e4c91a0 Add function to copy Str8 to C string buffer 2025-02-16 21:30:25 +00:00
76b078fbc0 Use str8_copy function in tester 2025-02-16 17:35:10 +00:00
0569fca193 Add str8_copy functions 2025-02-16 17:34:48 +00:00
6078e54087 Fix windows bugs 2025-02-16 17:09:40 +00:00
180425707b Use Str8 instead of const char * in termcolour functions 2025-02-16 16:32:52 +00:00
62dcfdaa93 Use Str8 instead of const char * in tester functions 2025-02-16 16:32:29 +00:00
19134d0e15 Address Windows Spectre mitigation warnings 2025-02-16 14:55:17 +00:00
ca2b1cbf23 Reformat 2025-02-16 14:53:29 +00:00
f60442bfcf Fix bug with arena allocation on Windows 2025-02-16 14:52:31 +00:00
a4a1e82c40 Add tests for str8 splitting, joining and concatenation 2025-02-15 23:45:37 +00:00
d50b41acac Add str8 split, rsplit, join, concat and concat_capped functions 2025-02-15 23:45:07 +00:00
71b6242f16 Update allocator free to take object size 2025-02-15 23:44:23 +00:00
241edfc7e4 Change Str8Node to use Str8 instead of Str8RO 2025-02-15 20:36:31 +00:00
d635e03cd8 Add tests for Str8List utilities 2025-02-15 20:29:50 +00:00
68fe421ab0 Add Str8List and str8 equality function 2025-02-15 20:29:15 +00:00
622a4391a0 Update compile script 2025-02-15 10:49:23 +00:00
ff4d3c9e99 Add tests for string allocation functions 2025-02-09 18:47:10 +00:00
023c74d8d9 Add functions to dynamically allocate strings 2025-02-09 18:46:32 +00:00
86fe867011 Reformat 2025-02-09 18:21:36 +00:00
98829b8400 Reformat 2025-02-09 18:19:27 +00:00
a7e98211f9 Update find and rfind tests 2025-02-09 16:41:29 +00:00
4642361969 Add tests for find and rfind 2025-02-09 16:35:42 +00:00
093d0daf6f Add find and rfine functions 2025-02-09 16:35:28 +00:00
f8e0804dd2 Fix unused variable error 2025-02-09 16:34:36 +00:00
fdb0650634 Add -Wextra flag for compilation 2025-02-09 16:34:12 +00:00
d1d6a8e64b Add utils for str8 printing 2025-02-09 15:13:50 +00:00
bbf38499ca Implement read-only string literals 2025-02-09 14:29:54 +00:00
7657ad1b58 Remove libc allocator 2025-02-01 21:39:27 +00:00
70399cb797 Change c16 and c32 defintions 2025-02-01 20:21:45 +00:00
76c3b02e45 Change char type to c8 in Str8 functions 2024-10-17 00:13:35 +01:00
abf9b09495 Implement test for wapp_str8_set 2024-10-17 00:08:08 +01:00
6d09059911 Change wapp_str8_lit implementation to support mutable string literals 2024-10-17 00:07:30 +01:00
894ae028b7 Add aliases for character data types 2024-10-17 00:06:56 +01:00
84a4ec223c Commit the Str8 tests 2024-10-08 23:58:24 +01:00
8f10ac2916 Add string buffer macro and tests for Str8 2024-10-08 23:57:55 +01:00
bfb4e87a1e Remove extra spaces in aliases.h 2024-10-08 22:45:48 +01:00
685682e1c8 revamp-strings (#1)
Co-authored-by: Abdelrahman Said <said.abdelrahman@flawlessai.com>
Reviewed-on: #1
Co-authored-by: Abdelrahman <said.abdelrahman89@gmail.com>
Co-committed-by: Abdelrahman <said.abdelrahman89@gmail.com>
2024-10-07 22:35:56 +00:00
40d301fd95 Reformat 2024-10-06 21:31:04 +01:00
7d8df816a5 Move cpath to os 2024-10-06 20:04:38 +01:00
17f0f0eaf3 Fix cpath tests on Windows 2024-10-06 19:54:34 +01:00
ce656a6275 Fix Spectre warnings for MSVC 2024-10-06 19:54:13 +01:00
8d8fcf9dc5 Improve early exit code in test_cpath 2024-10-06 19:33:13 +01:00
77e634ec2d Fix comment 2024-10-06 19:30:19 +01:00
22ece7215a Move path utils to os, fix bugs with it and write tests 2024-10-06 19:28:34 +01:00
4af4b39d6f Remove unnecessary clang-format comments 2024-10-06 12:59:21 +01:00
a48002996e Reorganise src directory 2024-10-06 12:54:04 +01:00
e9451f10f8 Reformat and increase minimum arena capacity to 4 pages 2024-10-06 12:42:12 +01:00
1de3608b1f Pass MemInitType to platform functions 2024-10-05 22:30:11 +01:00
3a25571105 Reformat 2024-10-05 23:18:23 +01:00
139b4ca589 Restore missing mem_utils code 2024-10-05 23:17:23 +01:00
e431cf729e Reformat 2024-10-05 23:15:03 +01:00
1d1c3ca928 Split the platform specific mem_utils implementations 2024-10-05 23:05:32 +01:00
f2f155744a Declare external functions for termcolour and commander in the headers 2024-10-05 23:04:35 +01:00
d3a86ec6cb Add extern alias 2024-10-05 23:03:19 +01:00
e053aa44ab Remove unnecessary termcolour header files 2024-10-05 22:28:24 +01:00
a546a09565 Split commander windows and posix implementations 2024-10-05 22:25:21 +01:00
12f182f0cf Define macros for C linkage 2024-10-05 22:06:13 +01:00
ffb99a3644 Fix MSVC errors and warnings 2024-10-05 20:07:48 +01:00
1ddc5610f9 Remove aligned_alloc from libc allocator since it's not implemented on Windows 2024-10-05 19:11:13 +01:00
d8c7b3162f Split termcolour implementations by platform and simplify the API 2024-10-05 18:46:14 +01:00
9e1e4688e4 Reformat 2024-10-05 18:45:32 +01:00
cd38672581 Add test for Arena's reallocation functionality 2024-09-14 17:28:07 +01:00
7fea236618 Implement reallocation functionality for Arena allocator 2024-09-14 17:27:33 +01:00
16a1b8fa35 Reformat 2024-09-14 14:49:18 +01:00
a0dfe8acd0 Start implementing realloc functions for Arena allocator 2024-09-14 13:39:59 +01:00
95c47ec940 Update libc allocator realloc function 2024-09-14 13:39:32 +01:00
46f32f2ba9 Update signature of realloc functions to expect old and new sizes 2024-09-14 13:38:55 +01:00
8f794dee15 Reformat 2024-09-07 20:54:49 +01:00
034b105ea1 Reformat 2024-09-07 20:53:20 +01:00
c90874ad10 Reformat 2024-09-07 20:49:13 +01:00
8468bb8e28 Reorganise allocators 2024-09-07 20:27:49 +01:00
d30eee0cf8 Add tests for Allocator implementations 2024-09-07 15:43:47 +01:00
59e56a75d3 Reimplement Allocator API for Arena 2024-09-07 15:43:25 +01:00
5d6ce1d2c3 Restore libc Allocator implementation 2024-09-07 15:43:00 +01:00
ce537b7494 Restore Allocator implementation 2024-09-07 15:42:36 +01:00
b8f6e5f187 Fix bug in memory allocation function 2024-08-24 19:07:00 +01:00
775f0864a8 Rename arena default allocation function 2024-08-23 22:29:56 +01:00
8b6fb23bac Use size utils in arena tests 2024-08-23 21:50:22 +01:00
1fb340561f Add size utils 2024-08-23 21:48:38 +01:00
8ed372d938 Ensure stderr is piped to stdout to capture errors in output 2024-06-17 00:18:27 +01:00
8a91a0ec6b Add tests for the shell commander 2024-06-17 00:15:10 +01:00
802b70f5ee Add option to discard command output 2024-06-17 00:14:34 +01:00
92c2439b56 Ensure cleanup on command failure 2024-06-16 23:29:19 +01:00
781a46713b Add shell commander 2024-06-16 23:19:34 +01:00
7c4725edef Add shell_utils 2024-06-16 23:18:52 +01:00
4fc99f76a5 Move termcolour to shell 2024-06-16 23:17:54 +01:00
25ab75f74f Move memory utilities to common 2024-06-16 15:30:53 +01:00
f6dd7e7aa8 Reorganise files 2024-06-16 12:29:43 +01:00
8f5bee45c6 Reformat compile script 2024-06-12 23:20:49 +01:00
25964d9a3c Run tests before building library 2024-06-09 22:46:17 +01:00
d9cf98da73 Rename print_test_result and exit on any failing function 2024-06-09 22:45:27 +01:00
8c153b5321 Build tests on Windows 2024-06-09 22:31:14 +01:00
3c4112d080 Remove building test.c from the Posix compilation script 2024-06-09 22:30:46 +01:00
aa28dd481b Platform abstraction for terminal colour 2024-06-09 22:30:21 +01:00
42187beee5 Fix auto formatting 2024-06-09 22:29:30 +01:00
8cc17f1490 Reformat and update types 2024-06-09 22:28:07 +01:00
b4c1301600 Update .gitignore 2024-06-09 22:25:46 +01:00
2c556e12f1 Initial implementation of Windows build script 2024-06-09 18:27:34 +01:00
6c82898225 Update dstr to remove MSVC warnings 2024-06-09 18:25:37 +01:00
a85508e0a4 Add padding to StringUpdate struct 2024-06-09 18:25:00 +01:00
cd78913e83 Add padding to arena and test_func_result structs 2024-06-09 17:40:41 +01:00
a0acada9b4 Fix Windows warning about assigning in conditional expression 2024-06-09 17:40:01 +01:00
8eaa4afed1 Add WINDOWS_LEAN_AND_MEAN to mem_utils.c 2024-06-09 17:35:22 +01:00
1ce07e9e4e Add WINDOWS_LEAN_AND_MEAN 2024-06-09 17:34:54 +01:00
162b255e1b Add utility for calculating padding size 2024-06-09 17:34:16 +01:00
99a9cd10f5 Update .gitignore 2024-06-09 16:20:15 +01:00
7e1c25b649 Use out of source build 2024-06-09 16:19:44 +01:00
9008e10651 Fix bug in dirup function 2024-06-09 16:19:26 +01:00
d27fc14b31 Fix termcolour include in tester.c 2024-06-09 15:26:20 +01:00
dbdab01a2c Merge branch 'temp' into 'main' 2024-06-09 01:50:51 +01:00
cf12144230 Add utility macros for arena initialisation 2024-06-09 01:46:23 +01:00
ccc86342cd Add address parameter to wapp_mem_util_alloc 2024-06-09 01:45:29 +01:00
Abdelrahman Said
8d89695fcc Test reserving a massive arena 2024-06-03 08:05:14 +01:00
Abdelrahman Said
6499dd7be9 Rename testing macros 2024-06-03 07:58:24 +01:00
75bbb82058 Add helper macro for running test functions 2024-06-02 23:55:41 +01:00
7fb13f2439 Update compile script to run tests 2024-06-02 23:35:34 +01:00
0b63bc746d Add tests 2024-06-02 23:35:26 +01:00
6ee3c762df Add terminal colour constants 2024-06-02 23:35:04 +01:00
59f1c3eb58 Add testing utilities 2024-06-02 23:34:53 +01:00
55d0c25c90 Reformat 2024-06-02 23:34:38 +01:00
cfc98e0137 Update .gitignore 2024-06-02 21:26:32 +01:00
Abdelrahman Said
32877cdeaa Fix platform detection on Apple platforms 2024-06-02 20:54:20 +01:00
Abdelrahman Said
7e2d7b28b7 Update .gitignore 2024-06-02 20:54:07 +01:00
23886f40e8 Switch to using wapp_mem_util_alloc instead of calloc 2024-06-02 20:42:10 +01:00
57de75c1f8 Set allocation flags per platform 2024-06-02 20:41:30 +01:00
9807334ac9 Complete memory allocation and deallocation function definitions 2024-06-02 20:09:56 +01:00
18448dd7c1 Define memory allocation and deallocation functions 2024-06-02 20:09:08 +01:00
62fdef8601 Add platform detection definitions 2024-06-02 20:08:28 +01:00
61c29ee564 Remove growing_arena and use base_arena 2024-06-02 01:17:03 +01:00
36bc8ab54a Ignore test files 2024-06-02 01:16:46 +01:00
a8e5913254 Add release build and compile test 2024-06-02 01:16:24 +01:00
b59aedad89 Add BasicString and StringView 2024-04-29 23:22:59 +01:00
3f5e3558b9 Add BasicString 2024-04-29 23:06:22 +01:00
bb2db0d30d Return StringUpdate from the dstr update functions 2024-04-29 22:55:40 +01:00
e75846a507 Update dstr to use Arena 2024-04-29 22:48:10 +01:00
05e56d67ea Reorganise the code 2024-04-29 21:42:33 +01:00
7164156c35 Simplify Arena 2024-04-28 22:59:37 +01:00
e67e1df9a5 Rename arena free functions 2024-04-28 22:04:23 +01:00
e206b4f4a6 Remove Allocator functions from Arena 2024-04-24 23:40:10 +01:00
05bfa73509 Revert "Simplify Arena allocator"
This reverts commit d4313fb8e4.
2024-04-24 23:18:19 +01:00
d4313fb8e4 Simplify Arena allocator 2024-04-24 23:10:55 +01:00
92db9206cc Remove complicated memory abstractions 2024-04-24 22:51:42 +01:00
98 changed files with 9134 additions and 1050 deletions

9
.gitignore vendored
View File

@ -1,4 +1,13 @@
.cache
.vscode
test
test.*
*.dSYM
.DS_Store
*.pdb
*.obj
compile_commands.json
libwapp-build
libwapp.so
*.vs
__pycache__

66
Makefile Normal file
View File

@ -0,0 +1,66 @@
CC = clang
BUILD_TYPE = debug
CFLAGS = -Wall -Wextra -Werror -pedantic
LIBFLAGS = -fPIC -shared
KERNEL = $(shell uname -s)
MACHINE = $(shell uname -m)
PLATFORM = $(KERNEL)_$(MACHINE)
TEST_INCLUDE = -Isrc $(shell find tests -type d | xargs -I{} echo -n "-I{} ")
TEST_SRC = src/wapp.c $(shell find tests -type f -name "*.c" | xargs -I{} echo -n "{} ")
BUILD_DIR = libwapp-build/$(PLATFORM)-$(BUILD_TYPE)
LIB_OUT = $(BUILD_DIR)/libwapp.so
TEST_OUT = $(BUILD_DIR)/wapptest
ifeq ($(BUILD_TYPE),debug)
CFLAGS += -g -fsanitize=address,undefined
else ifeq ($(BUILD_TYPE),release)
CFLAGS += -O3
else
$(error Invalid BUILD type '$(BUILD_TYPE)'. Use 'debug' or 'release')
endif
ifeq ($(CC),gcc)
# Used to disable the "ASan runtime does not come first in initial library list" error when compiling with 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
all: clean builddir codegen run-test full
clean:
@rm -rf $(BUILD_DIR)
builddir:
@mkdir -p $(BUILD_DIR)
build-test:
$(CC) $(CFLAGS) $(TEST_INCLUDE) $(TEST_SRC) -o $(TEST_OUT)
run-test: build-test
@$(TEST_OUT)
@rm $(TEST_OUT)
codegen:
python3 -m codegen
build-lib:
$(CC) $(CFLAGS) $(LIBFLAGS) $(LIB_SRC) -o $(LIB_OUT)
full: LIB_SRC = src/wapp.c
full: build-lib
prng: LIB_SRC = src/prng/wapp_prng.c
prng: build-lib
testing: LIB_SRC = src/testing/wapp_testing.c
testing: build-lib
uuid: LIB_SRC = src/uuid/wapp_uuid.c
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

19
build
View File

@ -1,3 +1,20 @@
#!/bin/bash
bear -- ./compile $@
BUILD_TYPE="debug"
ARGS=""
while [[ $# > 0 ]];do
case $1 in
--release)
BUILD_TYPE="release"
shift
;;
*|-*|--*)
rest=("$@")
ARGS+=" ${rest[0]}"
shift
;;
esac
done
bear -- make BUILD_TYPE=$BUILD_TYPE $ARGS

65
build.ps1 Normal file
View File

@ -0,0 +1,65 @@
Param(
[switch]$Release
)
$Compiler = "cl.exe"
$GeneralFlags = "/Wall /WX /wd4996"
$LibraryFlags = "/LD"
$Kernel = (Get-ChildItem Env:OS).Value
$Machine = (Get-ChildItem Env:PROCESSOR_ARCHITECTURE).Value
$Platform = "${Kernel}_${Machine}"
$IncludeDirs = "/I src"
$SrcFiles = "src/wapp.c"
$TestIncludeDirs = Get-ChildItem -Path tests -Recurse -Directory -ErrorAction SilentlyContinue -Force | %{$("/I " + '"' + $_.FullName + '"')}
$TestSrcFiles = Get-ChildItem -Path tests -Recurse -Filter *.c -ErrorAction SilentlyContinue -Force | %{$('"' + $_.FullName + '"')}
If ($Release -eq $True) {
$GeneralFlags += " /O2 /Og"
$BuildType = "release"
} Else {
$GeneralFlags += " /Zi /Od /fsanitize=address"
$BuildType = "debug"
}
$BuildDir = "./libwapp-build/${Platform}-${BuildType}"
$ObjDir = "$BuildDir/objects"
$OutDir = "$BuildDir/output"
$TestsDir = "$BuildDir/tests"
$OutBasename = "libwapp"
$Objects = "/Fo:$ObjDir/"
$Outputs = "/Fd:$OutDir/$OutBasename /Fe:$OutDir/$OutBasename"
$TestOutBasename = "wapptest"
$TestOutputs = "/Fo:$TestsDir/ /Fe:$TestsDir/$TestOutBasename"
If (Test-Path $BuildDir) {
Remove-Item $BuildDir -Recurse -Force
}
mkdir -p $ObjDir > $null
mkdir -p $OutDir > $null
mkdir -p $TestsDir > $null
# Run code generation
Invoke-Expression "python3 -m codegen"
# Build and run tests
Invoke-Expression "$Compiler $GeneralFlags $IncludeDirs $TestIncludeDirs $SrcFiles $TestSrcFiles $TestOutputs" -ErrorAction Stop
Invoke-Expression "$TestsDir/$TestOutBasename.exe"
$Status = $LASTEXITCODE
Remove-Item $TestsDir -Recurse -Force
If ($Status -ne 0) {
Write-Error "Tests failed"
Exit 1
}
# Build library
Invoke-Expression "$Compiler $GeneralFlags $LibraryFlags $SrcFiles $Objects $Outputs"

15
codegen/__main__.py Normal file
View File

@ -0,0 +1,15 @@
from codegen.datatypes import CDataType
from codegen.dbl_list.make_dbl_list import DblListData, make_dbl_list
def main():
gen_dbl_list()
def gen_dbl_list():
datatypes: dict[CDataType, DblListData] = {}
make_dbl_list(datatypes)
if __name__ == "__main__":
main()

5
codegen/constants.py Normal file
View File

@ -0,0 +1,5 @@
from pathlib import Path
PACKAGE_DIR = Path(__file__).parent.resolve()
WAPP_SRC_ROOT = PACKAGE_DIR.parent / "src"

343
codegen/datatypes.py Normal file
View File

@ -0,0 +1,343 @@
from enum import Enum
from pathlib import Path
from typing import Optional, Union
from dataclasses import dataclass, field
class CType(Enum):
VOID = "void"
BOOL = "bool"
CHAR = "char"
C8 = "c8"
C16 = "c16"
C32 = "c32"
I8 = "i8"
I16 = "i16"
I32 = "i32"
I64 = "i64"
U8 = "u8"
U16 = "u16"
U32 = "u32"
U64 = "u64"
F32 = "f32"
F64 = "f64"
F128 = "f128"
IPTR = "iptr"
UPTR = "uptr"
def __str__(self) -> str:
return self.value
class CQualifier(Enum):
NONE = ""
CONST = "const "
EXTERNAL = "external "
INTERNAL = "internal "
PERSISTENT = "persistent "
def __str__(self) -> str:
return self.value
class CPointerType(Enum):
NONE = ""
SINGLE = "*"
DOUBLE = "**"
def __str__(self) -> str:
return self.value
@dataclass
class CPointer:
_type: CPointerType = CPointerType.NONE
qualifier: CQualifier = CQualifier.NONE
def __str__(self) -> str:
return str(self._type) + str(self.qualifier)
@dataclass
class CEnumVal:
name: str
value: Optional[int] = None
def __str__(self) -> str:
return self.name + "" if self.value is None else f" = {self.value}"
@dataclass
class CEnum:
name: str
values: list[CEnumVal]
typedef: bool = False
def __str__(self) -> str:
if self.typedef:
header = "typedef enum {\n"
footer = f"}} {self.name};\n"
else:
header = f"enum {self.name} {{\n"
footer = "};\n"
values = ""
for value in self.values:
values += f" {str(value)},\n"
return header + values + footer
@dataclass
class CMacro:
name: str
value: str
def __str__(self) -> str:
return f"#define {self.name} {self.value}\n"
@dataclass
class CStruct:
name: str
cargs: list["CArg"]
typedef_name: str | None = None
def __str__(self) -> str:
return self.declare() + self.define()
def declare(self) -> str:
declaration = f"typedef struct {self.name} {self.typedef_name if self.typedef_name is not None else self.name};\n"
return declaration
def define(self):
definition = f"struct {self.name} {{\n"
args = ""
for arg in self.cargs:
args += f" {str(arg)};\n"
footer = "};\n"
return definition + args + footer;
CUserType = Union[CStruct, CEnum]
CDataType = Union[CType, CUserType, str]
@dataclass
class CArg:
name: str
_type: CDataType
array: bool = False
pointer: CPointer = field(default_factory=CPointer)
qualifier: CQualifier = CQualifier.NONE
def __str__(self) -> str:
qualifier = str(self.qualifier)
_type = get_datatype_string(self._type) + " "
pointer = str(self.pointer)
array = "[]" if self.array else ""
return qualifier + _type + pointer + self.name + array
@dataclass
class CFunc:
name: str
ret_type: CDataType
args: list[CArg]
body: str
pointer: CPointer = field(default_factory=CPointer)
qualifiers: list[CQualifier] = field(default_factory=list)
def __str__(self) -> str:
qualifiers = ""
for qualifier in self.qualifiers:
if qualifier == CQualifier.NONE:
continue
if len(qualifiers) > 0:
qualifiers += " "
qualifiers += f"{str(qualifier)}"
args = ""
for i, arg in enumerate(self.args):
args += f"{str(arg)}"
if i + 1 < len(self.args):
args += ", "
return qualifiers + get_datatype_string(self.ret_type) + " " + str(self.pointer) + self.name + f"({args})"
def declare(self) -> str:
return f"{str(self)};\n"
def define(self) -> str:
return f"{str(self)} {{\n{self.body}\n}}\n\n"
@dataclass
class CInclude:
header: Union[str, "CHeader"]
local: bool = False
same_dir: bool = False
def __str__(self) -> str:
if isinstance(self.header, CHeader):
name = f"{self.header.name}.{self.header.extension}"
else:
name = self.header
if self.local:
open_symbol = '"'
close_symbol = '"'
if self.same_dir:
name = f"./{name}"
else:
open_symbol = '<'
close_symbol = '>'
return f"#include {open_symbol}{name}{close_symbol}\n"
@dataclass
class CFile:
name: str
extension: str
decl_types: list[CStruct] = field(default_factory=list)
macros: list[CMacro] = field(default_factory=list)
def save(self, output_dir: Path):
output_file = output_dir / f"{self.name}.{self.extension}"
with open(output_file, "w+") as outfile:
outfile.write(str(self))
def __str__(self) -> str:
return """\
/**
* THIS FILE IS AUTOMATICALLY GENERATED. ANY MODIFICATIONS TO IT WILL BE OVERWRITTEN
*/
"""
@dataclass
class CHeader(CFile):
extension: str = "h"
includes: list[CInclude] = field(default_factory=list)
types: list[CUserType] = field(default_factory=list)
funcs: list[CFunc] = field(default_factory=list)
def __str__(self) -> str:
name_upper = self.name.upper()
header_guard_name = f"{name_upper}_H"
header_guard_open = f"#ifndef {header_guard_name}\n#define {header_guard_name}\n\n"
header_guard_close = f"#endif // !{header_guard_name}\n"
c_linkage_open = "#ifdef __cplusplus\nBEGIN_C_LINKAGE\n#endif // !__cplusplus\n\n"
c_linkage_close = "\n#ifdef __cplusplus\nEND_C_LINKAGE\n#endif // !__cplusplus\n\n"
includes = _get_includes_string(self.includes)
macros = ""
for macro in self.macros:
macros += str(macro)
if len(macros) > 0:
macros += "\n"
forward_declarations = ""
for _type in self.decl_types:
forward_declarations += _type.declare()
if len(forward_declarations) > 0:
forward_declarations += "\n"
types = ""
for _type in self.types:
types += str(_type) + "\n"
funcs = ""
for func in self.funcs:
funcs += func.declare()
return (
super().__str__() +
header_guard_open +
includes +
c_linkage_open +
macros +
forward_declarations +
types +
funcs +
c_linkage_close +
header_guard_close
)
@dataclass
class CSource(CFile):
extension: str = "c"
includes: list[CInclude] = field(default_factory=list)
types: list[CUserType] = field(default_factory=list)
internal_funcs: list[CFunc] = field(default_factory=list)
funcs: list[CFunc] = field(default_factory=list)
def __str__(self) -> str:
includes = _get_includes_string(self.includes)
macros = ""
for macro in self.macros:
macros += str(macro)
if len(macros) > 0:
macros += "\n"
forward_declarations = ""
for _type in self.decl_types:
forward_declarations += _type.declare()
if len(forward_declarations) > 0:
forward_declarations += "\n"
types = ""
for _type in self.types:
types += str(_type) + "\n"
internal_funcs_decl = ""
internal_funcs_def = ""
for func in self.internal_funcs:
internal_funcs_decl += func.declare()
internal_funcs_def += func.define()
if len(internal_funcs_decl) > 0:
internal_funcs_decl += "\n"
funcs = ""
for func in self.funcs:
funcs += func.define()
return (
super().__str__() +
includes +
macros +
forward_declarations +
types +
internal_funcs_decl +
funcs +
internal_funcs_def
)
def get_datatype_string(_type: CDataType) -> str:
if isinstance(_type, CType):
return str(_type)
elif isinstance(_type, CStruct) or isinstance(_type, CEnum):
return _type.name
elif isinstance(_type, str):
return _type
def _get_includes_string(includes: list[CInclude]) -> str:
output = ""
for include in sorted(includes, key=lambda inc: inc.local, reverse=True):
output += str(include)
if len(output) > 0:
output += "\n"
return output

View File

@ -0,0 +1,332 @@
from pathlib import Path
from dataclasses import dataclass, field
from codegen.constants import WAPP_SRC_ROOT
from codegen.utils import load_func_body_from_file
from codegen.datatypes import (
CDataType,
CMacro,
CStruct,
CFunc,
CHeader,
CSource,
CArg,
CType,
CPointer,
CPointerType,
CQualifier,
CInclude,
get_datatype_string,
)
@dataclass
class DblListData:
node_typename: str
list_typename: str
hdr_decl_types: list[CStruct] = field(default_factory=list)
src_decl_types: list[CStruct] = field(default_factory=list)
def make_dbl_list(user_datatypes: dict[CDataType, DblListData] = {}):
def __format_func_body(
filename: Path,
type_string: str,
type_string_upper: str,
type_string_lower: str,
node_typename: str,
list_typename: str
):
return load_func_body_from_file(filename).format(
T=type_string,
NodeType=node_typename,
ListType=list_typename,
Tupper=type_string_upper,
Tlower=type_string_lower,
)
out_dir = WAPP_SRC_ROOT / "containers" / "dbl_list"
out_dir.mkdir(parents=True, exist_ok=True)
common_local_include_files = [
(WAPP_SRC_ROOT / "common" / "aliases" / "aliases.h")
]
common_includes: list[CInclude] = [
CInclude(header="stdbool.h")
]
for local_file in common_local_include_files:
common_includes.append(
CInclude(
header=str(local_file.relative_to(out_dir, walk_up=True)),
local=True,
)
)
common_decl_types: list[CStruct] = []
datatypes: dict[CDataType, DblListData] = {
"Str8": DblListData(
node_typename="Str8Node",
list_typename="Str8List",
hdr_decl_types=[
CStruct(name="str8", cargs=[], typedef_name="Str8"),
],
),
}
for _type in CType:
if _type == CType.VOID:
datatypes["void *"] = DblListData(
node_typename="VoidPNode",
list_typename="VoidPList",
)
continue
elif _type == CType.BOOL:
datatypes[_type.value] = DblListData(
node_typename="BoolNode",
list_typename="BoolList",
)
continue
type_title = _type.value.title()
datatypes[_type.value] = DblListData(
node_typename=f"{type_title}Node",
list_typename=f"{type_title}List",
)
datatypes.update(user_datatypes)
snippets_dir = Path(__file__).parent / "snippets"
header = CHeader(
name="dbl_list",
decl_types=[*common_decl_types],
includes=[],
types=[],
funcs=[]
)
source = CSource(
name=header.name,
decl_types=[*common_decl_types],
includes=[CInclude(header, local=True, same_dir=True), CInclude(header="stddef.h")],
internal_funcs=[],
funcs=header.funcs
)
if len(common_includes) > 0:
header.includes.extend(common_includes)
source.includes.extend(common_includes)
for _type, dbl_list_data in datatypes.items():
type_string = get_datatype_string(_type)
clean_type_string = type_string.replace(" ", "").replace("*", "_ptr")
type_string_upper = clean_type_string.upper()
type_string_lower = clean_type_string.lower()
node = CStruct(
name=dbl_list_data.node_typename,
cargs=[
CArg(name="item", _type=type_string, pointer=CPointer(_type=CPointerType.SINGLE)),
],
)
node.cargs.extend([
CArg(name="prev", _type=node, pointer=CPointer(_type=CPointerType.SINGLE)),
CArg(name="next", _type=node, pointer=CPointer(_type=CPointerType.SINGLE)),
])
dl_list = CStruct(
name=dbl_list_data.list_typename,
cargs=[
CArg(name="first", _type=node, pointer=CPointer(_type=CPointerType.SINGLE)),
CArg(name="last", _type=node, pointer=CPointer(_type=CPointerType.SINGLE)),
CArg(name="node_count", _type=CType.U64),
],
)
node_macro = CMacro(
name=f"wapp_{type_string_lower}_list_node(ITEM_PTR)",
value=__format_func_body(
filename=snippets_dir / "list_node",
type_string=type_string,
type_string_upper=type_string_upper,
type_string_lower=type_string_lower,
node_typename=dbl_list_data.node_typename,
list_typename=dbl_list_data.list_typename
),
)
get_func = CFunc(
name=f"wapp_{type_string_lower}_list_get",
ret_type=node,
args=[
CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE), qualifier=CQualifier.CONST),
CArg(name="index", _type=CType.U64),
],
body=__format_func_body(
filename=snippets_dir / "list_get",
type_string=type_string,
type_string_upper=type_string_upper,
type_string_lower=type_string_lower,
node_typename=dbl_list_data.node_typename,
list_typename=dbl_list_data.list_typename
),
pointer=CPointer(CPointerType.SINGLE),
)
push_front_func = CFunc(
name=f"wapp_{type_string_lower}_list_push_front",
ret_type=CType.VOID,
args=[
CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)),
CArg(name="node", _type=node, pointer=CPointer(CPointerType.SINGLE)),
],
body=__format_func_body(
filename=snippets_dir / "list_push_front",
type_string=type_string,
type_string_upper=type_string_upper,
type_string_lower=type_string_lower,
node_typename=dbl_list_data.node_typename,
list_typename=dbl_list_data.list_typename
),
)
push_back_func = CFunc(
name=f"wapp_{type_string_lower}_list_push_back",
ret_type=CType.VOID,
args=[
CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)),
CArg(name="node", _type=node, pointer=CPointer(CPointerType.SINGLE)),
],
body=__format_func_body(
filename=snippets_dir / "list_push_back",
type_string=type_string,
type_string_upper=type_string_upper,
type_string_lower=type_string_lower,
node_typename=dbl_list_data.node_typename,
list_typename=dbl_list_data.list_typename
),
)
insert_func = CFunc(
name=f"wapp_{type_string_lower}_list_insert",
ret_type=CType.VOID,
args=[
CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)),
CArg(name="node", _type=node, pointer=CPointer(CPointerType.SINGLE)),
CArg(name="index", _type=CType.U64),
],
body=__format_func_body(
filename=snippets_dir / "list_insert",
type_string=type_string,
type_string_upper=type_string_upper,
type_string_lower=type_string_lower,
node_typename=dbl_list_data.node_typename,
list_typename=dbl_list_data.list_typename
),
)
pop_front_func = CFunc(
name=f"wapp_{type_string_lower}_list_pop_front",
ret_type=node,
args=[
CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)),
],
body=__format_func_body(
filename=snippets_dir / "list_pop_front",
type_string=type_string,
type_string_upper=type_string_upper,
type_string_lower=type_string_lower,
node_typename=dbl_list_data.node_typename,
list_typename=dbl_list_data.list_typename
),
pointer=CPointer(CPointerType.SINGLE),
)
pop_back_func = CFunc(
name=f"wapp_{type_string_lower}_list_pop_back",
ret_type=node,
args=[
CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)),
],
body=__format_func_body(
filename=snippets_dir / "list_pop_back",
type_string=type_string,
type_string_upper=type_string_upper,
type_string_lower=type_string_lower,
node_typename=dbl_list_data.node_typename,
list_typename=dbl_list_data.list_typename
),
pointer=CPointer(CPointerType.SINGLE),
)
remove_func = CFunc(
name=f"wapp_{type_string_lower}_list_remove",
ret_type=node,
args=[
CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)),
CArg(name="index", _type=CType.U64),
],
body=__format_func_body(
filename=snippets_dir / "list_remove",
type_string=type_string,
type_string_upper=type_string_upper,
type_string_lower=type_string_lower,
node_typename=dbl_list_data.node_typename,
list_typename=dbl_list_data.list_typename
),
pointer=CPointer(CPointerType.SINGLE),
)
empty_func = CFunc(
name=f"wapp_{type_string_lower}_list_empty",
ret_type=CType.VOID,
args=[
CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)),
],
body=__format_func_body(
filename=snippets_dir / "list_empty",
type_string=type_string,
type_string_upper=type_string_upper,
type_string_lower=type_string_lower,
node_typename=dbl_list_data.node_typename,
list_typename=dbl_list_data.list_typename
),
)
node_to_list_func = CFunc(
name=f"{type_string_lower}_node_to_list",
ret_type=dl_list,
args=[
CArg(name="node", _type=node, pointer=CPointer(CPointerType.SINGLE)),
],
body=__format_func_body(
filename=snippets_dir / "node_to_list",
type_string=type_string,
type_string_upper=type_string_upper,
type_string_lower=type_string_lower,
node_typename=dbl_list_data.node_typename,
list_typename=dbl_list_data.list_typename
),
qualifiers=[CQualifier.INTERNAL],
)
header.decl_types.extend(dbl_list_data.hdr_decl_types)
header.macros.append(node_macro)
header.types.extend([node, dl_list])
header.funcs.extend([
get_func,
push_front_func,
push_back_func,
insert_func,
pop_front_func,
pop_back_func,
remove_func,
empty_func,
])
source.decl_types.extend(dbl_list_data.src_decl_types)
source.internal_funcs.append(node_to_list_func)
source.funcs = header.funcs
header.save(out_dir)
source.save(out_dir)

View File

@ -0,0 +1,8 @@
if (!list) {{
return;
}}
u64 count = list->node_count;
for (u64 i = 0; i < count; ++i) {{
wapp_{Tlower}_list_pop_back(list);
}}

View File

@ -0,0 +1,13 @@
if (index >= list->node_count) {{
return NULL;
}}
{NodeType} *output = NULL;
{NodeType} *current = list->first;
for (u64 i = 1; i <= index; ++i) {{
current = current->next;
}}
output = current;
return output;

View File

@ -0,0 +1,28 @@
if (!list || !node || !(node->item)) {{
return;
}}
if (index == 0) {{
wapp_{Tlower}_list_push_front(list, node);
return;
}} else if (index == list->node_count) {{
wapp_{Tlower}_list_push_back(list, node);
return;
}}
{NodeType} *dst_node = wapp_{Tlower}_list_get(list, index);
if (!dst_node) {{
return;
}}
{ListType} node_list = {Tlower}_node_to_list(node);
list->node_count += node_list.node_count;
{NodeType} *prev = dst_node->prev;
dst_node->prev = node_list.last;
prev->next = node_list.first;
node_list.first->prev = prev;
node_list.last->next = dst_node;

View File

@ -0,0 +1 @@
(({NodeType}){{.item = ITEM_PTR}})

View File

@ -0,0 +1,20 @@
{NodeType} *output = NULL;
if (!list || list->node_count == 0) {{
goto RETURN_{Tupper}_LIST_POP_BACK;
}}
output = list->last;
if (list->node_count == 1) {{
*list = ({ListType}){{0}};
goto RETURN_{Tupper}_LIST_POP_BACK;
}}
--(list->node_count);
list->last = output->prev;
output->prev = output->next = NULL;
RETURN_{Tupper}_LIST_POP_BACK:
return output;

View File

@ -0,0 +1,20 @@
{NodeType} *output = NULL;
if (!list || list->node_count == 0) {{
goto RETURN_{Tupper}_LIST_POP_FRONT;
}}
output = list->first;
if (list->node_count == 1) {{
*list = ({ListType}){{0}};
goto RETURN_{Tupper}_LIST_POP_FRONT;
}}
--(list->node_count);
list->first = output->next;
output->prev = output->next = NULL;
RETURN_{Tupper}_LIST_POP_FRONT:
return output;

View File

@ -0,0 +1,20 @@
if (!list || !node || !(node->item)) {{
return;
}}
{ListType} node_list = {Tlower}_node_to_list(node);
if (list->node_count == 0) {{
*list = node_list;
return;
}}
list->node_count += node_list.node_count;
{NodeType} *last = list->last;
if (last) {{
last->next = node_list.first;
}}
list->last = node_list.last;
node_list.first->prev = last;

View File

@ -0,0 +1,20 @@
if (!list || !node || !(node->item)) {{
return;
}}
{ListType} node_list = {Tlower}_node_to_list(node);
if (list->node_count == 0) {{
*list = node_list;
return;
}}
list->node_count += node_list.node_count;
{NodeType} *first = list->first;
if (first) {{
first->prev = node_list.last;
}}
list->first = node_list.first;
node_list.last->next = first;

View File

@ -0,0 +1,27 @@
{NodeType} *output = NULL;
if (!list) {{
goto RETURN_{Tupper}_LIST_REMOVE;
}}
if (index == 0) {{
output = wapp_{Tlower}_list_pop_front(list);
goto RETURN_{Tupper}_LIST_REMOVE;
}} else if (index == list->node_count) {{
output = wapp_{Tlower}_list_pop_back(list);
goto RETURN_{Tupper}_LIST_REMOVE;
}}
output = wapp_{Tlower}_list_get(list, index);
if (!output) {{
goto RETURN_{Tupper}_LIST_REMOVE;
}}
output->prev->next = output->next;
output->next->prev = output->prev;
--(list->node_count);
output->prev = output->next = NULL;
RETURN_{Tupper}_LIST_REMOVE:
return output;

View File

@ -0,0 +1,13 @@
{ListType} output = {{.first = node, .last = node, .node_count = 1}};
while (output.first->prev != NULL) {{
output.first = output.first->prev;
++(output.node_count);
}}
while (output.last->next != NULL) {{
output.last = output.last->next;
++(output.node_count);
}}
return output;

6
codegen/utils.py Normal file
View File

@ -0,0 +1,6 @@
from pathlib import Path
def load_func_body_from_file(filename: Path) -> str:
with open(filename, "r") as infile:
return infile.read().rstrip()

18
compile
View File

@ -1,18 +0,0 @@
#!/bin/bash
CC=clang
INCLUDE="\
-Ialiases \
-Icpath/include \
-Idstr/include \
$(find mem/include -type d | xargs -I{} echo -n "-I{} ") \
"
SRC="\
cpath/src/*.c \
dstr/src/*.c \
mem/src/*/*.c \
"
CFLAGS="-O3 -shared -fPIC -Wall -Werror -pedantic"
OUT="libwapp.so"
(set -x ; $CC $CFLAGS $INCLUDE $SRC -o $OUT)

View File

@ -1,25 +0,0 @@
#ifndef PATH_UTILS_H
#define PATH_UTILS_H
#include "aliases.h"
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
#define NUMPARTS(...) \
(sizeof((const char *[]){"", __VA_ARGS__}) / sizeof(const char *) - 1)
#define wapp_cpath_join_path(DST, ...) \
join_path(DST, NUMPARTS(__VA_ARGS__), __VA_ARGS__)
#define wapp_cpath_dirname(DST, PATH) dirup(DST, 1, PATH)
#define wapp_cpath_dirup(DST, COUNT, PATH) dirup(DST, COUNT, PATH)
void join_path(char *dst, u64 count, ...);
void dirup(char *dst, u64 levels, const char *path);
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // !PATH_UTILS_H

View File

@ -1,87 +0,0 @@
#include "cpath.h"
#include "aliases.h"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#if defined(__unix__) || defined(__APPLE__) || defined(__ANDROID__)
internal char path_sep = '/';
#elif defined(_WIN32) || defined(_WIN64)
internal char path_sep = '\\';
#endif
void join_root_and_leaf(const char *root, const char *leaf, char *dst);
void join_path(char *dst, u64 count, ...) {
va_list args;
va_start(args, count);
for (u64 i = 0; i < count; ++i) {
join_root_and_leaf(dst, va_arg(args, const char *), dst);
}
va_end(args);
}
void dirup(char *dst, u64 levels, const char *path) {
if (levels < 1) {
return;
}
u64 end_index = 0;
u64 sep_count = 0;
u64 full_length;
u64 length;
length = full_length = strlen(path);
if (path[length - 1] == path_sep) {
--length;
}
for (u64 i = length - 1; i >= 0; --i) {
if (path[i] == path_sep) {
++sep_count;
end_index = i;
if (sep_count == levels) {
break;
}
}
}
if (sep_count < levels) {
end_index = 0;
}
if (dst == path) {
memset(&dst[end_index], 0, full_length - end_index);
} else {
u64 dst_length = strlen(dst);
memset(dst, 0, dst_length);
strncpy(dst, path, end_index);
}
}
void join_root_and_leaf(const char *root, const char *leaf, char *dst) {
u64 root_length = strlen(root);
u64 root_end = root_length - 1;
u64 leaf_length = strlen(leaf);
u64 leaf_start = 0;
if (root[root_end] == path_sep) {
--root_end;
}
if (leaf[leaf_start] == path_sep) {
++leaf_start;
}
memcpy(dst, root, ++root_end);
dst[root_end] = path_sep;
memcpy(&(dst[++root_end]), &(leaf[leaf_start]), leaf_length - leaf_start);
}

View File

@ -1,31 +0,0 @@
#ifndef DSTR_H
#define DSTR_H
#include "aliases.h"
#include "mem_allocator.h"
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
typedef struct dstr String;
String *wapp_dstr_with_capacity(u64 capacity, const Allocator *allocator);
String *wapp_dstr_from_string(const char *str, const Allocator *allocator);
void wapp_dstr_update(String **dst, const char *src);
void wapp_dstr_free(String **str);
void wapp_dstr_concat(String **dst, const char *src);
void wapp_dstr_append(String **dst, char c);
void wapp_dstr_resize(String **str);
void wapp_dstr_clear(String *str);
void wapp_dstr_print(const String *str);
i64 wapp_dstr_find(const String *str, const char *substr);
u64 wapp_dstr_length(const String *str);
u64 wapp_dstr_capacity(const String *str);
const char *wapp_dstr_to_cstr(const String *str);
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // !DSTR_H

View File

@ -1,223 +0,0 @@
#include "dstr.h"
#include "aliases.h"
#include "mem_allocator.h"
#include "mem_libc.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Use this scalar to allocate extra memory in order to avoid having to
// constantly reallocate
#define CAPACITY_SCALAR 8
struct dstr {
Allocator allocator;
u64 capacity;
u64 size;
char buf[];
};
String *wapp_dstr_with_capacity(u64 capacity, const Allocator *allocator) {
Allocator alloc;
if (allocator) {
alloc = *allocator;
} else {
alloc = wapp_mem_libc_allocator();
}
String *out =
(String *)wapp_mem_allocator_alloc(&alloc, sizeof(String) + capacity + 1);
if (!out) {
return NULL;
}
out->allocator = alloc;
out->capacity = capacity;
out->size = 0;
return out;
}
String *wapp_dstr_from_string(const char *str, const Allocator *allocator) {
if (!str) {
return NULL;
}
u64 length = strlen(str);
u64 capacity = length * CAPACITY_SCALAR;
String *out = wapp_dstr_with_capacity(capacity, allocator);
if (!out) {
return NULL;
}
out->size = length;
strncpy(out->buf, str, length + 1);
return out;
}
void wapp_dstr_update(String **dst, const char *src) {
if (!dst || !(*dst)) {
return;
}
u64 length = strlen(src);
String *str = *dst;
if (length < str->capacity) {
memset(str->buf, 0, str->capacity);
str->size = length;
strncpy(str->buf, src, length + 1);
} else {
u64 capacity = length * CAPACITY_SCALAR;
String *tmp = (String *)wapp_mem_allocator_realloc(
&(str->allocator), *dst, sizeof(String) + capacity + 1);
if (!tmp) {
return;
}
tmp->capacity = capacity;
tmp->size = length;
strncpy(tmp->buf, src, length + 1);
*dst = tmp;
}
}
void wapp_dstr_free(String **str) {
if (!str || !(*str)) {
return;
}
String *str_ptr = *str;
wapp_mem_allocator_free(&(str_ptr->allocator), (void **)str);
}
void wapp_dstr_concat(String **dst, const char *src) {
if (!dst || !(*dst)) {
return;
}
u64 src_length = strlen(src);
if (src_length == 0) {
return;
}
u64 new_length = (*dst)->size + src_length;
char str[new_length + 1];
memset(str, 0, new_length + 1);
strncpy(str, (*dst)->buf, (*dst)->size);
strncat(str, src, new_length + 1 - (*dst)->size);
wapp_dstr_update(dst, str);
}
void wapp_dstr_append(String **dst, char c) {
if (!dst || !(*dst)) {
return;
}
u64 new_length = (*dst)->size + 1;
char str[new_length + 1];
memset(str, 0, new_length + 1);
strncpy(str, (*dst)->buf, (*dst)->size);
str[(*dst)->size] = c;
wapp_dstr_update(dst, str);
}
void wapp_dstr_resize(String **str) {
if (!str || !(*str)) {
return;
}
String *str_ptr = *str;
u64 capacity = (*str)->size;
String *tmp = (String *)wapp_mem_allocator_realloc(
&(str_ptr->allocator), *str, sizeof(String) + capacity + 1);
if (!tmp) {
return;
}
tmp->capacity = capacity;
*str = tmp;
}
void wapp_dstr_clear(String *str) {
if (!str || str->size == 0) {
return;
}
memset(str->buf, 0, str->capacity);
str->size = 0;
}
void wapp_dstr_print(const String *str) {
if (!str) {
return;
}
printf("%.*s\n", (i32)str->size, str->buf);
}
i64 wapp_dstr_find(const String *str, const char *substr) {
if (!str || !substr) {
return -1;
}
u64 substr_length = strlen(substr);
if (substr_length == 0 || substr_length > str->size) {
return -1;
}
const char *s1;
for (u64 i = 0; i < str->size; ++i) {
if (i + substr_length > str->size) {
break;
}
s1 = &(str->buf[i]);
if (strncmp(s1, substr, substr_length) == 0) {
return i;
}
}
return -1;
}
u64 wapp_dstr_length(const String *str) {
if (!str) {
return 0;
}
return str->size;
}
u64 wapp_dstr_capacity(const String *str) {
if (!str) {
return 0;
}
return str->capacity;
}
const char *wapp_dstr_to_cstr(const String *str) {
if (!str) {
return "";
}
return str->buf;
}

View File

@ -1,28 +0,0 @@
#ifndef MEM_ARENA_H
#define MEM_ARENA_H
#include "aliases.h"
#include "mem_allocator.h"
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
typedef struct growing_arena Arena;
Allocator wapp_mem_arena_allocator(const Arena *arena);
bool wapp_mem_arena_init(Arena **arena, u64 base_capacity);
void *wapp_mem_arena_alloc(Arena *arena, u64 size);
void *wapp_mem_arena_alloc_aligned(Arena *arena, u64 size, u64 alignment);
void *wapp_mem_arena_realloc(Arena *arena, void *ptr, u64 size);
void *wapp_mem_arena_realloc_aligned(Arena *arena, void *ptr, u64 size,
u64 alignment);
void wapp_mem_arena_clear(Arena *arena);
void wapp_mem_arena_free(Arena **arena);
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // !MEM_ARENA_H

View File

@ -1,32 +0,0 @@
#ifndef MEM_CTX_H
#define MEM_CTX_H
#include "mem_allocator.h"
#include "mem_arena.h"
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
typedef enum {
CTX_DEST_BUFFER_MAIN,
CTX_DEST_BUFFER_TEMP,
COUNT_CTX_DEST_BUFFER,
} CTXDestBuffer;
Allocator wapp_mem_ctx_allocator(CTXDestBuffer buffer);
void wapp_mem_ctx_init(u64 main_buf_capacity, u64 temp_buf_capacity);
void *wapp_mem_ctx_alloc(CTXDestBuffer buffer, u64 size);
void *wapp_mem_ctx_alloc_aligned(CTXDestBuffer buffer, u64 size, u64 alignment);
void *wapp_mem_ctx_realloc(CTXDestBuffer buffer, void *ptr, u64 size);
void *wapp_mem_ctx_realloc_aligned(CTXDestBuffer buffer, void *ptr, u64 size,
u64 alignment);
void wapp_mem_ctx_clear(CTXDestBuffer buffer);
void wapp_mem_ctx_free(void);
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // !MEM_CTX_H

View File

@ -1,15 +0,0 @@
#ifndef MEM_LIBC_H
#define MEM_LIBC_H
#include "mem_allocator.h"
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
Allocator wapp_mem_libc_allocator(void);
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // !MEM_LIBC_H

View File

@ -1,406 +0,0 @@
#include "mem_arena.h"
#include "aliases.h"
#include "mem_allocator.h"
#include "mem_utils.h"
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#ifndef DEFAULT_ALIGNMENT
// Why 2 * sizeof(void *) instead of sizeof(void *)
// https://handmade.network/forums/t/6860-alignment_arena_allocator
#define DEFAULT_ALIGNMENT (2 * sizeof(void *))
#endif /* ifndef DEFAULT_ALIGNMENT */
#define HDR_MAGIC_BYTE_COUNT 8
#define HDR_MAGIC \
{ 0x57, 0x41, 0x41, 0x52, 0x4e, 0x48, 0x44, 0x52 }
#define MAX_HDR_SEARCH_LENGTH 256
#define ARENA_MINIMUM_CAPACITY 1024
typedef struct arena_alloc_hdr ArenaAllocHDR;
struct arena_alloc_hdr {
u8 magic[HDR_MAGIC_BYTE_COUNT];
u64 alloc_size;
u64 alignment;
u8 *alloc_start;
};
typedef struct base_arena BaseArena;
struct base_arena {
u8 *buf;
u8 *offset;
u64 capacity;
BaseArena *prev;
BaseArena *next;
};
struct growing_arena {
BaseArena *active_arena;
u64 count;
u64 initial_capacity;
};
internal BaseArena *find_arena_from_pointer(const Arena *arena, void *ptr);
internal bool base_arena_init(BaseArena *arena, u64 capacity);
internal void *base_arena_alloc_aligned(BaseArena *arena, u64 size,
u64 alignment);
internal void base_arena_clear(BaseArena *arena);
internal void base_arena_free(BaseArena *arena);
internal ArenaAllocHDR *find_alloc_header(BaseArena *arena, void *alloc_ptr);
internal void *mem_arena_alloc(u64 size, void *alloc_obj);
internal void *mem_arena_alloc_aligned(u64 size, u64 alignment,
void *alloc_obj);
internal void *mem_arena_realloc(void *ptr, u64 size, void *alloc_obj);
internal void *mem_arena_realloc_aligned(void *ptr, u64 size, u64 alignment,
void *alloc_obj);
// PUBLIC API
Allocator wapp_mem_arena_allocator(const Arena *arena) {
return (Allocator){
.obj = (void *)arena,
.alloc = mem_arena_alloc,
.alloc_aligned = mem_arena_alloc_aligned,
.realloc = mem_arena_realloc,
.realloc_aligned = mem_arena_realloc_aligned,
.free = NULL,
};
}
bool wapp_mem_arena_init(Arena **arena, u64 base_capacity) {
if (!arena || *arena || base_capacity == 0) {
return false;
}
*arena = (Arena *)calloc(1, sizeof(Arena));
Arena *arena_ptr = *arena;
if (!arena_ptr) {
return false;
}
arena_ptr->active_arena = (BaseArena *)calloc(1, sizeof(BaseArena));
if (!(arena_ptr->active_arena)) {
wapp_mem_arena_free(arena);
return false;
}
if (!base_arena_init(arena_ptr->active_arena, base_capacity)) {
wapp_mem_arena_free(arena);
return false;
}
arena_ptr->count = 1;
arena_ptr->initial_capacity = base_capacity;
return true;
}
void *wapp_mem_arena_alloc(Arena *arena, u64 size) {
return wapp_mem_arena_alloc_aligned(arena, size, DEFAULT_ALIGNMENT);
}
void *wapp_mem_arena_alloc_aligned(Arena *arena, u64 size, u64 alignment) {
if (!arena || !(arena->active_arena)) {
return NULL;
}
void *output = base_arena_alloc_aligned(arena->active_arena, size, alignment);
if (!output) {
if (arena->active_arena->next) {
arena->active_arena = arena->active_arena->next;
} else {
arena->active_arena->next = (BaseArena *)calloc(1, sizeof(BaseArena));
if (!(arena->active_arena->next)) {
return NULL;
}
if (!base_arena_init(arena->active_arena->next,
arena->initial_capacity)) {
free(arena->active_arena->next);
return NULL;
}
arena->active_arena->next->prev = arena->active_arena;
arena->active_arena = arena->active_arena->next;
++(arena->count);
}
output = base_arena_alloc_aligned(arena->active_arena, size, alignment);
if (!output) {
return NULL;
}
}
memset(output, 0, size);
return output;
}
void *wapp_mem_arena_realloc(Arena *arena, void *ptr, u64 size) {
return wapp_mem_arena_realloc_aligned(arena, ptr, size, DEFAULT_ALIGNMENT);
}
void *wapp_mem_arena_realloc_aligned(Arena *arena, void *ptr, u64 size,
u64 alignment) {
if (!arena) {
return NULL;
}
BaseArena *base_arena = find_arena_from_pointer(arena, ptr);
if (!base_arena) {
return NULL;
}
ArenaAllocHDR *header = find_alloc_header(base_arena, ptr);
if (!header) {
return NULL;
}
if (header->alloc_start + header->alloc_size == base_arena->offset) {
// Avoid allocating new pointer and copying memory if pointer is at the end
// of the arena
i64 diff = size - header->alloc_size;
u8 *new_offset = base_arena->offset + diff;
u8 *clear_start = diff < 0 ? new_offset : base_arena->offset;
memset(clear_start, 0, llabs(diff));
header->alloc_size = size;
base_arena->offset = new_offset;
return header->alloc_start;
}
void *new_alloc = wapp_mem_arena_alloc_aligned(arena, size, alignment);
if (!new_alloc) {
return NULL;
}
u64 to_copy = size < header->alloc_size ? size : header->alloc_size;
memcpy(new_alloc, ptr, to_copy);
return new_alloc;
}
void wapp_mem_arena_clear(Arena *arena) {
if (!arena) {
return;
}
BaseArena *last_active = NULL;
while (arena->active_arena) {
base_arena_clear(arena->active_arena);
last_active = arena->active_arena;
arena->active_arena = arena->active_arena->prev;
}
arena->active_arena = last_active;
}
void wapp_mem_arena_free(Arena **arena) {
if (!arena) {
return;
}
Arena *arena_ptr = *arena;
if (!arena_ptr) {
return;
}
BaseArena *current;
BaseArena *next;
BaseArena *prev;
current = arena_ptr->active_arena->next;
while (current) {
next = current->next;
base_arena_free(current);
free(current);
current = next;
}
current = arena_ptr->active_arena->prev;
while (current) {
prev = current->prev;
base_arena_free(current);
free(current);
current = prev;
}
base_arena_free(arena_ptr->active_arena);
free(arena_ptr->active_arena);
arena_ptr->active_arena = NULL;
arena_ptr->count = 0;
arena_ptr->initial_capacity = 0;
free(*arena);
*arena = NULL;
}
// INTERNAL FUNCTIONS
internal BaseArena *find_arena_from_pointer(const Arena *arena, void *ptr) {
if (!arena || !ptr) {
return NULL;
}
// Ensure pointer is not out of bounds
u8 *alloc = (u8 *)ptr;
BaseArena *active = arena->active_arena;
if (alloc > active->buf + arena->initial_capacity) {
return NULL;
}
for (u64 i = 0; i < arena->count; ++i) {
if (alloc >= active->buf && alloc < active->buf + arena->initial_capacity) {
return (BaseArena *)active;
}
active = active->prev;
}
return NULL;
}
internal bool base_arena_init(BaseArena *arena, u64 capacity) {
if (!arena || arena->buf || capacity == 0) {
return false;
}
u64 arena_capacity =
capacity >= ARENA_MINIMUM_CAPACITY ? capacity : ARENA_MINIMUM_CAPACITY;
arena->buf = (u8 *)calloc(arena_capacity, sizeof(u8));
if (!(arena->buf)) {
return false;
}
arena->capacity = arena_capacity;
arena->offset = arena->buf;
arena->prev = arena->next = NULL;
return true;
}
internal void *base_arena_alloc_aligned(BaseArena *arena, u64 size,
u64 alignment) {
if (!arena) {
return NULL;
}
u8 *start_offset = arena->offset;
u8 *alloc_start = arena->offset + sizeof(ArenaAllocHDR);
u8 *output = wapp_mem_util_align_forward((void *)alloc_start, alignment);
if (output + size >= arena->buf + arena->capacity) {
return NULL;
}
ArenaAllocHDR *header = (ArenaAllocHDR *)start_offset;
*header = (ArenaAllocHDR){
.magic = HDR_MAGIC,
.alloc_size = size,
.alignment = alignment,
.alloc_start = output,
};
arena->offset = output + size;
return (void *)output;
}
internal void base_arena_clear(BaseArena *arena) {
if (!arena) {
return;
}
memset(arena->buf, 0, arena->offset - arena->buf);
arena->offset = arena->buf;
}
internal void base_arena_free(BaseArena *arena) {
if (!arena) {
return;
}
if (arena->buf) {
free(arena->buf);
}
arena->buf = arena->offset = NULL;
arena->capacity = 0;
arena->prev = arena->next = NULL;
}
internal ArenaAllocHDR *find_alloc_header(BaseArena *arena, void *alloc_ptr) {
persistent const u8 magic[HDR_MAGIC_BYTE_COUNT] = HDR_MAGIC;
u8 *current = (u8 *)alloc_ptr;
u8 *max_search_end = current - MAX_HDR_SEARCH_LENGTH;
u8 *arena_buf_start = arena->buf;
u8 *search_end =
max_search_end > arena_buf_start ? max_search_end : arena_buf_start;
bool match;
for (; current >= search_end; --current) {
match = true;
for (u64 i = 0; i < HDR_MAGIC_BYTE_COUNT; ++i) {
if (current[i] != magic[i]) {
match = false;
break;
}
}
if (match) {
return (ArenaAllocHDR *)current;
}
}
return NULL;
}
internal void *mem_arena_alloc(u64 size, void *alloc_obj) {
Arena *arena = (Arena *)alloc_obj;
return wapp_mem_arena_alloc(arena, size);
}
internal void *mem_arena_alloc_aligned(u64 size, u64 alignment,
void *alloc_obj) {
Arena *arena = (Arena *)alloc_obj;
return wapp_mem_arena_alloc_aligned(arena, size, alignment);
}
internal void *mem_arena_realloc(void *ptr, u64 size, void *alloc_obj) {
Arena *arena = (Arena *)alloc_obj;
return wapp_mem_arena_realloc(arena, ptr, size);
}
internal void *mem_arena_realloc_aligned(void *ptr, u64 size, u64 alignment,
void *alloc_obj) {
Arena *arena = (Arena *)alloc_obj;
return wapp_mem_arena_realloc_aligned(arena, ptr, size, alignment);
}

View File

@ -1,106 +0,0 @@
#include "mem_ctx.h"
#include "aliases.h"
#include "mem_arena.h"
#include <assert.h>
#include <stdlib.h>
typedef struct mem_ctx MemCTX;
struct mem_ctx {
Arena *main;
Arena *temp;
bool main_initialised;
bool temp_initialised;
};
internal MemCTX g_context = {0};
internal Arena *get_arena(CTXDestBuffer buffer);
Allocator wapp_mem_ctx_allocator(CTXDestBuffer buffer) {
Arena *arena = get_arena(buffer);
return wapp_mem_arena_allocator(arena);
}
void wapp_mem_ctx_init(u64 main_buf_capacity, u64 temp_buf_capacity) {
g_context.main_initialised =
wapp_mem_arena_init(&g_context.main, main_buf_capacity);
g_context.temp_initialised =
wapp_mem_arena_init(&g_context.temp, temp_buf_capacity);
}
void *wapp_mem_ctx_alloc(CTXDestBuffer buffer, u64 size) {
Arena *arena = get_arena(buffer);
if (!arena) {
return NULL;
}
return wapp_mem_arena_alloc(arena, size);
}
void *wapp_mem_ctx_alloc_aligned(CTXDestBuffer buffer, u64 size,
u64 alignment) {
Arena *arena = get_arena(buffer);
if (!arena) {
return NULL;
}
return wapp_mem_arena_alloc_aligned(arena, size, alignment);
}
void *wapp_mem_ctx_realloc(CTXDestBuffer buffer, void *ptr, u64 size) {
Arena *arena = get_arena(buffer);
if (!arena) {
return NULL;
}
return wapp_mem_arena_realloc(arena, ptr, size);
}
void *wapp_mem_ctx_realloc_aligned(CTXDestBuffer buffer, void *ptr, u64 size,
u64 alignment) {
Arena *arena = get_arena(buffer);
if (!arena) {
return NULL;
}
return wapp_mem_arena_realloc_aligned(arena, ptr, size, alignment);
}
void wapp_mem_ctx_clear(CTXDestBuffer buffer) {
Arena *arena = get_arena(buffer);
if (!arena) {
return;
}
wapp_mem_arena_clear(arena);
}
void wapp_mem_ctx_free(void) {
wapp_mem_arena_free(&(g_context.main));
g_context.main_initialised = false;
wapp_mem_arena_free(&(g_context.temp));
g_context.temp_initialised = false;
}
internal Arena *get_arena(CTXDestBuffer buffer) {
Arena *output = NULL;
switch (buffer) {
case CTX_DEST_BUFFER_MAIN:
if (g_context.main_initialised) {
output = g_context.main;
}
break;
case CTX_DEST_BUFFER_TEMP:
if (g_context.temp_initialised) {
output = g_context.temp;
}
break;
default:
assert(false && "Not all context destination buffers are handled");
break;
}
return output;
}

View File

@ -1,48 +0,0 @@
#include "mem_libc.h"
#include "aliases.h"
#include "mem_allocator.h"
#include <stdlib.h>
#include <string.h>
internal void *mem_libc_alloc(u64 size, void *alloc_obj);
internal void *mem_libc_alloc_aligned(u64 size, u64 alignment, void *alloc_obj);
internal void *mem_libc_realloc(void *ptr, u64 size, void *alloc_obj);
internal void mem_libc_free(void **ptr, void *alloc_obj);
Allocator wapp_mem_libc_allocator(void) {
return (Allocator){
.obj = NULL,
.alloc = mem_libc_alloc,
.alloc_aligned = mem_libc_alloc_aligned,
.realloc = mem_libc_realloc,
.realloc_aligned = NULL,
.free = mem_libc_free,
};
}
internal void *mem_libc_alloc(u64 size, void *alloc_obj) {
return calloc(1, size);
}
internal void *mem_libc_alloc_aligned(u64 size, u64 alignment,
void *alloc_obj) {
void *output = aligned_alloc(alignment, size);
if (output) {
memset(output, 0, size);
}
return output;
}
internal void *mem_libc_realloc(void *ptr, u64 size, void *alloc_obj) {
return realloc(ptr, size);
}
internal void mem_libc_free(void **ptr, void *alloc_obj) {
if (!ptr || !(*ptr)) {
return;
}
free(*ptr);
*ptr = NULL;
}

View File

@ -3,6 +3,10 @@
#include <stdint.h>
#define c8 uint8_t
#define c16 uint16_t
#define c32 uint32_t
#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
@ -20,11 +24,14 @@
#define uptr uintptr_t
#define iptr intptr_t
#define external extern
#define internal static
#define persistent static
#ifdef __cplusplus
#define class_mem static
#define BEGIN_C_LINKAGE extern "C" {
#define END_C_LINKAGE }
#endif // __cplusplus
#endif // !ALIASES_H

View File

@ -0,0 +1,13 @@
#ifndef MISC_UTILS_H
#define MISC_UTILS_H
#include "../aliases/aliases.h"
#define KB(SIZE) (SIZE * 1024ull)
#define MB(SIZE) (KB(SIZE) * 1024)
#define GB(SIZE) (MB(SIZE) * 1024)
#define TB(SIZE) (GB(SIZE) * 1024)
#define wapp_misc_utils_padding_size(SIZE) u8 reserved_padding[sizeof(void *) - ((SIZE) % sizeof(void *))]
#endif // !MISC_UTILS_H

View File

@ -0,0 +1,61 @@
#ifndef PLATFORM_H
#define PLATFORM_H
#if defined(__ANDROID__)
#define WAPP_PLATFORM_ANDROID
#define WAPP_PLATFORM_POSIX
#elif defined(__FreeBSD__)
#define WAPP_PLATFORM_FREE_BSD
#define WAPP_PLATFORM_BSD
#define WAPP_PLATFORM_POSIX
#elif defined(__NetBSD__)
#define WAPP_PLATFORM_NET_BSD
#define WAPP_PLATFORM_BSD
#define WAPP_PLATFORM_POSIX
#elif defined(__OpenBSD__)
#define WAPP_PLATFORM_OPEN_BSD
#define WAPP_PLATFORM_BSD
#define WAPP_PLATFORM_POSIX
#elif defined(__DragonFly__)
#define WAPP_PLATFORM_DRAGON_FLY
#define WAPP_PLATFORM_BSD
#define WAPP_PLATFORM_POSIX
#elif defined(__bsdi__)
#define WAPP_PLATFORM_BSD
#define WAPP_PLATFORM_POSIX
#elif defined(__linux__) || defined(linux) || defined(__linux) || defined(__gnu_linux__)
#define WAPP_PLATFORM_LINUX
#define WAPP_PLATFORM_POSIX
#elif defined(__GNU__) || defined(__gnu_hurd__)
#define WAPP_PLATFORM_GNU
#define WAPP_PLATFORM_POSIX
#elif defined(__APPLE__) || defined(__MACH__)
#include <TargetConditionals.h>
#if TARGET_OS_IPHONE
#define WAPP_PLATFORM_IOS
#define WAPP_PLATFORM_APPLE
#define WAPP_PLATFORM_POSIX
#elif TARGET_OS_MAC
#define WAPP_PLATFORM_MACOS
#define WAPP_PLATFORM_APPLE
#define WAPP_PLATFORM_POSIX
#else
#error "Unrecognised Apple platform"
#endif
#elif defined(_WIN64)
#define WAPP_PLATFORM_WINDOWS64
#define WAPP_PLATFORM_WINDOWS
#elif defined(_WIN32)
#define WAPP_PLATFORM_WINDOWS32
#define WAPP_PLATFORM_WINDOWS
#elif defined(__CYGWIN__)
#define WAPP_PLATFORM_CYGWIN
#define WAPP_PLATFORM_WINDOWS
#elif defined(__unix__) || defined(__unix)
#define WAPP_PLATFORM_UNIX
#define WAPP_PLATFORM_POSIX
#else
#error "Unrecognised platform"
#endif
#endif // !PLATFORM_H

8
src/common/wapp_common.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef WAPP_COMMON_H
#define WAPP_COMMON_H
#include "aliases/aliases.h"
#include "misc/misc_utils.h"
#include "platform/platform.h"
#endif // !WAPP_COMMON_H

View File

@ -0,0 +1,52 @@
#ifndef ARRAY_H
#define ARRAY_H
#include "../../common/aliases/aliases.h"
#ifdef __cplusplus
BEGIN_C_LINKAGE
#endif // !__cplusplus
typedef struct int_array IntArray;
struct int_array {
int *items;
u64 count;
u64 capacity;
};
#define U64_RSHIFT_OR_1(X) (((u64)X) | (((u64)X) >> 1))
#define U64_RSHIFT_OR_2(X) (((u64)X) | (((u64)X) >> 2))
#define U64_RSHIFT_OR_4(X) (((u64)X) | (((u64)X) >> 4))
#define U64_RSHIFT_OR_8(X) (((u64)X) | (((u64)X) >> 8))
#define U64_RSHIFT_OR_16(X) (((u64)X) | (((u64)X) >> 16))
#define U64_RSHIFT_OR_32(X) (((u64)X) | (((u64)X) >> 32))
#define U64_ROUND_UP_POW2(X) ( \
( \
U64_RSHIFT_OR_32( \
U64_RSHIFT_OR_16( \
U64_RSHIFT_OR_8( \
U64_RSHIFT_OR_4( \
U64_RSHIFT_OR_2( \
U64_RSHIFT_OR_1(X - 1) \
) \
) \
) \
) \
) \
) + 1 \
)
#define INT_ARRAY_COUNT_ARGS(...) (sizeof((int[]){__VA_ARGS__})/sizeof(int))
#define wapp_int_array(...) ((IntArray){ \
.items = (int[U64_ROUND_UP_POW2(INT_ARRAY_COUNT_ARGS(__VA_ARGS__) * 2)]){__VA_ARGS__}, \
.count = INT_ARRAY_COUNT_ARGS(__VA_ARGS__), \
.capacity = U64_ROUND_UP_POW2(INT_ARRAY_COUNT_ARGS(__VA_ARGS__) * 2) \
})
#define wapp_int_array_with_capacity(CAPACITY) ((IntArray){.items = (int[CAPACITY]){0}, .count = 0, .capacity = CAPACITY})
#ifdef __cplusplus
END_C_LINKAGE
#endif // !__cplusplus
#endif // !ARRAY_H

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,483 @@
/**
* THIS FILE IS AUTOMATICALLY GENERATED. ANY MODIFICATIONS TO IT WILL BE OVERWRITTEN
*/
#ifndef DBL_LIST_H
#define DBL_LIST_H
#include "../../common/aliases/aliases.h"
#include <stdbool.h>
#ifdef __cplusplus
BEGIN_C_LINKAGE
#endif // !__cplusplus
#define wapp_str8_list_node(ITEM_PTR) ((Str8Node){.item = ITEM_PTR})
#define wapp_void_ptr_list_node(ITEM_PTR) ((VoidPNode){.item = ITEM_PTR})
#define wapp_bool_list_node(ITEM_PTR) ((BoolNode){.item = ITEM_PTR})
#define wapp_char_list_node(ITEM_PTR) ((CharNode){.item = ITEM_PTR})
#define wapp_c8_list_node(ITEM_PTR) ((C8Node){.item = ITEM_PTR})
#define wapp_c16_list_node(ITEM_PTR) ((C16Node){.item = ITEM_PTR})
#define wapp_c32_list_node(ITEM_PTR) ((C32Node){.item = ITEM_PTR})
#define wapp_i8_list_node(ITEM_PTR) ((I8Node){.item = ITEM_PTR})
#define wapp_i16_list_node(ITEM_PTR) ((I16Node){.item = ITEM_PTR})
#define wapp_i32_list_node(ITEM_PTR) ((I32Node){.item = ITEM_PTR})
#define wapp_i64_list_node(ITEM_PTR) ((I64Node){.item = ITEM_PTR})
#define wapp_u8_list_node(ITEM_PTR) ((U8Node){.item = ITEM_PTR})
#define wapp_u16_list_node(ITEM_PTR) ((U16Node){.item = ITEM_PTR})
#define wapp_u32_list_node(ITEM_PTR) ((U32Node){.item = ITEM_PTR})
#define wapp_u64_list_node(ITEM_PTR) ((U64Node){.item = ITEM_PTR})
#define wapp_f32_list_node(ITEM_PTR) ((F32Node){.item = ITEM_PTR})
#define wapp_f64_list_node(ITEM_PTR) ((F64Node){.item = ITEM_PTR})
#define wapp_f128_list_node(ITEM_PTR) ((F128Node){.item = ITEM_PTR})
#define wapp_iptr_list_node(ITEM_PTR) ((IptrNode){.item = ITEM_PTR})
#define wapp_uptr_list_node(ITEM_PTR) ((UptrNode){.item = ITEM_PTR})
typedef struct str8 Str8;
typedef struct Str8Node Str8Node;
struct Str8Node {
Str8 *item;
Str8Node *prev;
Str8Node *next;
};
typedef struct Str8List Str8List;
struct Str8List {
Str8Node *first;
Str8Node *last;
u64 node_count;
};
typedef struct VoidPNode VoidPNode;
struct VoidPNode {
void * *item;
VoidPNode *prev;
VoidPNode *next;
};
typedef struct VoidPList VoidPList;
struct VoidPList {
VoidPNode *first;
VoidPNode *last;
u64 node_count;
};
typedef struct BoolNode BoolNode;
struct BoolNode {
bool *item;
BoolNode *prev;
BoolNode *next;
};
typedef struct BoolList BoolList;
struct BoolList {
BoolNode *first;
BoolNode *last;
u64 node_count;
};
typedef struct CharNode CharNode;
struct CharNode {
char *item;
CharNode *prev;
CharNode *next;
};
typedef struct CharList CharList;
struct CharList {
CharNode *first;
CharNode *last;
u64 node_count;
};
typedef struct C8Node C8Node;
struct C8Node {
c8 *item;
C8Node *prev;
C8Node *next;
};
typedef struct C8List C8List;
struct C8List {
C8Node *first;
C8Node *last;
u64 node_count;
};
typedef struct C16Node C16Node;
struct C16Node {
c16 *item;
C16Node *prev;
C16Node *next;
};
typedef struct C16List C16List;
struct C16List {
C16Node *first;
C16Node *last;
u64 node_count;
};
typedef struct C32Node C32Node;
struct C32Node {
c32 *item;
C32Node *prev;
C32Node *next;
};
typedef struct C32List C32List;
struct C32List {
C32Node *first;
C32Node *last;
u64 node_count;
};
typedef struct I8Node I8Node;
struct I8Node {
i8 *item;
I8Node *prev;
I8Node *next;
};
typedef struct I8List I8List;
struct I8List {
I8Node *first;
I8Node *last;
u64 node_count;
};
typedef struct I16Node I16Node;
struct I16Node {
i16 *item;
I16Node *prev;
I16Node *next;
};
typedef struct I16List I16List;
struct I16List {
I16Node *first;
I16Node *last;
u64 node_count;
};
typedef struct I32Node I32Node;
struct I32Node {
i32 *item;
I32Node *prev;
I32Node *next;
};
typedef struct I32List I32List;
struct I32List {
I32Node *first;
I32Node *last;
u64 node_count;
};
typedef struct I64Node I64Node;
struct I64Node {
i64 *item;
I64Node *prev;
I64Node *next;
};
typedef struct I64List I64List;
struct I64List {
I64Node *first;
I64Node *last;
u64 node_count;
};
typedef struct U8Node U8Node;
struct U8Node {
u8 *item;
U8Node *prev;
U8Node *next;
};
typedef struct U8List U8List;
struct U8List {
U8Node *first;
U8Node *last;
u64 node_count;
};
typedef struct U16Node U16Node;
struct U16Node {
u16 *item;
U16Node *prev;
U16Node *next;
};
typedef struct U16List U16List;
struct U16List {
U16Node *first;
U16Node *last;
u64 node_count;
};
typedef struct U32Node U32Node;
struct U32Node {
u32 *item;
U32Node *prev;
U32Node *next;
};
typedef struct U32List U32List;
struct U32List {
U32Node *first;
U32Node *last;
u64 node_count;
};
typedef struct U64Node U64Node;
struct U64Node {
u64 *item;
U64Node *prev;
U64Node *next;
};
typedef struct U64List U64List;
struct U64List {
U64Node *first;
U64Node *last;
u64 node_count;
};
typedef struct F32Node F32Node;
struct F32Node {
f32 *item;
F32Node *prev;
F32Node *next;
};
typedef struct F32List F32List;
struct F32List {
F32Node *first;
F32Node *last;
u64 node_count;
};
typedef struct F64Node F64Node;
struct F64Node {
f64 *item;
F64Node *prev;
F64Node *next;
};
typedef struct F64List F64List;
struct F64List {
F64Node *first;
F64Node *last;
u64 node_count;
};
typedef struct F128Node F128Node;
struct F128Node {
f128 *item;
F128Node *prev;
F128Node *next;
};
typedef struct F128List F128List;
struct F128List {
F128Node *first;
F128Node *last;
u64 node_count;
};
typedef struct IptrNode IptrNode;
struct IptrNode {
iptr *item;
IptrNode *prev;
IptrNode *next;
};
typedef struct IptrList IptrList;
struct IptrList {
IptrNode *first;
IptrNode *last;
u64 node_count;
};
typedef struct UptrNode UptrNode;
struct UptrNode {
uptr *item;
UptrNode *prev;
UptrNode *next;
};
typedef struct UptrList UptrList;
struct UptrList {
UptrNode *first;
UptrNode *last;
u64 node_count;
};
Str8Node *wapp_str8_list_get(const Str8List *list, u64 index);
void wapp_str8_list_push_front(Str8List *list, Str8Node *node);
void wapp_str8_list_push_back(Str8List *list, Str8Node *node);
void wapp_str8_list_insert(Str8List *list, Str8Node *node, u64 index);
Str8Node *wapp_str8_list_pop_front(Str8List *list);
Str8Node *wapp_str8_list_pop_back(Str8List *list);
Str8Node *wapp_str8_list_remove(Str8List *list, u64 index);
void wapp_str8_list_empty(Str8List *list);
VoidPNode *wapp_void_ptr_list_get(const VoidPList *list, u64 index);
void wapp_void_ptr_list_push_front(VoidPList *list, VoidPNode *node);
void wapp_void_ptr_list_push_back(VoidPList *list, VoidPNode *node);
void wapp_void_ptr_list_insert(VoidPList *list, VoidPNode *node, u64 index);
VoidPNode *wapp_void_ptr_list_pop_front(VoidPList *list);
VoidPNode *wapp_void_ptr_list_pop_back(VoidPList *list);
VoidPNode *wapp_void_ptr_list_remove(VoidPList *list, u64 index);
void wapp_void_ptr_list_empty(VoidPList *list);
BoolNode *wapp_bool_list_get(const BoolList *list, u64 index);
void wapp_bool_list_push_front(BoolList *list, BoolNode *node);
void wapp_bool_list_push_back(BoolList *list, BoolNode *node);
void wapp_bool_list_insert(BoolList *list, BoolNode *node, u64 index);
BoolNode *wapp_bool_list_pop_front(BoolList *list);
BoolNode *wapp_bool_list_pop_back(BoolList *list);
BoolNode *wapp_bool_list_remove(BoolList *list, u64 index);
void wapp_bool_list_empty(BoolList *list);
CharNode *wapp_char_list_get(const CharList *list, u64 index);
void wapp_char_list_push_front(CharList *list, CharNode *node);
void wapp_char_list_push_back(CharList *list, CharNode *node);
void wapp_char_list_insert(CharList *list, CharNode *node, u64 index);
CharNode *wapp_char_list_pop_front(CharList *list);
CharNode *wapp_char_list_pop_back(CharList *list);
CharNode *wapp_char_list_remove(CharList *list, u64 index);
void wapp_char_list_empty(CharList *list);
C8Node *wapp_c8_list_get(const C8List *list, u64 index);
void wapp_c8_list_push_front(C8List *list, C8Node *node);
void wapp_c8_list_push_back(C8List *list, C8Node *node);
void wapp_c8_list_insert(C8List *list, C8Node *node, u64 index);
C8Node *wapp_c8_list_pop_front(C8List *list);
C8Node *wapp_c8_list_pop_back(C8List *list);
C8Node *wapp_c8_list_remove(C8List *list, u64 index);
void wapp_c8_list_empty(C8List *list);
C16Node *wapp_c16_list_get(const C16List *list, u64 index);
void wapp_c16_list_push_front(C16List *list, C16Node *node);
void wapp_c16_list_push_back(C16List *list, C16Node *node);
void wapp_c16_list_insert(C16List *list, C16Node *node, u64 index);
C16Node *wapp_c16_list_pop_front(C16List *list);
C16Node *wapp_c16_list_pop_back(C16List *list);
C16Node *wapp_c16_list_remove(C16List *list, u64 index);
void wapp_c16_list_empty(C16List *list);
C32Node *wapp_c32_list_get(const C32List *list, u64 index);
void wapp_c32_list_push_front(C32List *list, C32Node *node);
void wapp_c32_list_push_back(C32List *list, C32Node *node);
void wapp_c32_list_insert(C32List *list, C32Node *node, u64 index);
C32Node *wapp_c32_list_pop_front(C32List *list);
C32Node *wapp_c32_list_pop_back(C32List *list);
C32Node *wapp_c32_list_remove(C32List *list, u64 index);
void wapp_c32_list_empty(C32List *list);
I8Node *wapp_i8_list_get(const I8List *list, u64 index);
void wapp_i8_list_push_front(I8List *list, I8Node *node);
void wapp_i8_list_push_back(I8List *list, I8Node *node);
void wapp_i8_list_insert(I8List *list, I8Node *node, u64 index);
I8Node *wapp_i8_list_pop_front(I8List *list);
I8Node *wapp_i8_list_pop_back(I8List *list);
I8Node *wapp_i8_list_remove(I8List *list, u64 index);
void wapp_i8_list_empty(I8List *list);
I16Node *wapp_i16_list_get(const I16List *list, u64 index);
void wapp_i16_list_push_front(I16List *list, I16Node *node);
void wapp_i16_list_push_back(I16List *list, I16Node *node);
void wapp_i16_list_insert(I16List *list, I16Node *node, u64 index);
I16Node *wapp_i16_list_pop_front(I16List *list);
I16Node *wapp_i16_list_pop_back(I16List *list);
I16Node *wapp_i16_list_remove(I16List *list, u64 index);
void wapp_i16_list_empty(I16List *list);
I32Node *wapp_i32_list_get(const I32List *list, u64 index);
void wapp_i32_list_push_front(I32List *list, I32Node *node);
void wapp_i32_list_push_back(I32List *list, I32Node *node);
void wapp_i32_list_insert(I32List *list, I32Node *node, u64 index);
I32Node *wapp_i32_list_pop_front(I32List *list);
I32Node *wapp_i32_list_pop_back(I32List *list);
I32Node *wapp_i32_list_remove(I32List *list, u64 index);
void wapp_i32_list_empty(I32List *list);
I64Node *wapp_i64_list_get(const I64List *list, u64 index);
void wapp_i64_list_push_front(I64List *list, I64Node *node);
void wapp_i64_list_push_back(I64List *list, I64Node *node);
void wapp_i64_list_insert(I64List *list, I64Node *node, u64 index);
I64Node *wapp_i64_list_pop_front(I64List *list);
I64Node *wapp_i64_list_pop_back(I64List *list);
I64Node *wapp_i64_list_remove(I64List *list, u64 index);
void wapp_i64_list_empty(I64List *list);
U8Node *wapp_u8_list_get(const U8List *list, u64 index);
void wapp_u8_list_push_front(U8List *list, U8Node *node);
void wapp_u8_list_push_back(U8List *list, U8Node *node);
void wapp_u8_list_insert(U8List *list, U8Node *node, u64 index);
U8Node *wapp_u8_list_pop_front(U8List *list);
U8Node *wapp_u8_list_pop_back(U8List *list);
U8Node *wapp_u8_list_remove(U8List *list, u64 index);
void wapp_u8_list_empty(U8List *list);
U16Node *wapp_u16_list_get(const U16List *list, u64 index);
void wapp_u16_list_push_front(U16List *list, U16Node *node);
void wapp_u16_list_push_back(U16List *list, U16Node *node);
void wapp_u16_list_insert(U16List *list, U16Node *node, u64 index);
U16Node *wapp_u16_list_pop_front(U16List *list);
U16Node *wapp_u16_list_pop_back(U16List *list);
U16Node *wapp_u16_list_remove(U16List *list, u64 index);
void wapp_u16_list_empty(U16List *list);
U32Node *wapp_u32_list_get(const U32List *list, u64 index);
void wapp_u32_list_push_front(U32List *list, U32Node *node);
void wapp_u32_list_push_back(U32List *list, U32Node *node);
void wapp_u32_list_insert(U32List *list, U32Node *node, u64 index);
U32Node *wapp_u32_list_pop_front(U32List *list);
U32Node *wapp_u32_list_pop_back(U32List *list);
U32Node *wapp_u32_list_remove(U32List *list, u64 index);
void wapp_u32_list_empty(U32List *list);
U64Node *wapp_u64_list_get(const U64List *list, u64 index);
void wapp_u64_list_push_front(U64List *list, U64Node *node);
void wapp_u64_list_push_back(U64List *list, U64Node *node);
void wapp_u64_list_insert(U64List *list, U64Node *node, u64 index);
U64Node *wapp_u64_list_pop_front(U64List *list);
U64Node *wapp_u64_list_pop_back(U64List *list);
U64Node *wapp_u64_list_remove(U64List *list, u64 index);
void wapp_u64_list_empty(U64List *list);
F32Node *wapp_f32_list_get(const F32List *list, u64 index);
void wapp_f32_list_push_front(F32List *list, F32Node *node);
void wapp_f32_list_push_back(F32List *list, F32Node *node);
void wapp_f32_list_insert(F32List *list, F32Node *node, u64 index);
F32Node *wapp_f32_list_pop_front(F32List *list);
F32Node *wapp_f32_list_pop_back(F32List *list);
F32Node *wapp_f32_list_remove(F32List *list, u64 index);
void wapp_f32_list_empty(F32List *list);
F64Node *wapp_f64_list_get(const F64List *list, u64 index);
void wapp_f64_list_push_front(F64List *list, F64Node *node);
void wapp_f64_list_push_back(F64List *list, F64Node *node);
void wapp_f64_list_insert(F64List *list, F64Node *node, u64 index);
F64Node *wapp_f64_list_pop_front(F64List *list);
F64Node *wapp_f64_list_pop_back(F64List *list);
F64Node *wapp_f64_list_remove(F64List *list, u64 index);
void wapp_f64_list_empty(F64List *list);
F128Node *wapp_f128_list_get(const F128List *list, u64 index);
void wapp_f128_list_push_front(F128List *list, F128Node *node);
void wapp_f128_list_push_back(F128List *list, F128Node *node);
void wapp_f128_list_insert(F128List *list, F128Node *node, u64 index);
F128Node *wapp_f128_list_pop_front(F128List *list);
F128Node *wapp_f128_list_pop_back(F128List *list);
F128Node *wapp_f128_list_remove(F128List *list, u64 index);
void wapp_f128_list_empty(F128List *list);
IptrNode *wapp_iptr_list_get(const IptrList *list, u64 index);
void wapp_iptr_list_push_front(IptrList *list, IptrNode *node);
void wapp_iptr_list_push_back(IptrList *list, IptrNode *node);
void wapp_iptr_list_insert(IptrList *list, IptrNode *node, u64 index);
IptrNode *wapp_iptr_list_pop_front(IptrList *list);
IptrNode *wapp_iptr_list_pop_back(IptrList *list);
IptrNode *wapp_iptr_list_remove(IptrList *list, u64 index);
void wapp_iptr_list_empty(IptrList *list);
UptrNode *wapp_uptr_list_get(const UptrList *list, u64 index);
void wapp_uptr_list_push_front(UptrList *list, UptrNode *node);
void wapp_uptr_list_push_back(UptrList *list, UptrNode *node);
void wapp_uptr_list_insert(UptrList *list, UptrNode *node, u64 index);
UptrNode *wapp_uptr_list_pop_front(UptrList *list);
UptrNode *wapp_uptr_list_pop_back(UptrList *list);
UptrNode *wapp_uptr_list_remove(UptrList *list, u64 index);
void wapp_uptr_list_empty(UptrList *list);
#ifdef __cplusplus
END_C_LINKAGE
#endif // !__cplusplus
#endif // !DBL_LIST_H

View File

@ -0,0 +1,6 @@
#ifndef WAPP_CONTAINERS_C
#define WAPP_CONTAINERS_C
#include "dbl_list/dbl_list.c"
#endif // !WAPP_CONTAINERS_C

View File

@ -0,0 +1,8 @@
#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

View File

@ -1,4 +1,5 @@
#include "mem_allocator.h"
#include "../../../common/aliases/aliases.h"
#include <stdlib.h>
void *wapp_mem_allocator_alloc(const Allocator *allocator, u64 size) {
@ -9,8 +10,7 @@ void *wapp_mem_allocator_alloc(const Allocator *allocator, u64 size) {
return allocator->alloc(size, allocator->obj);
}
void *wapp_mem_allocator_alloc_aligned(const Allocator *allocator, u64 size,
u64 alignment) {
void *wapp_mem_allocator_alloc_aligned(const Allocator *allocator, u64 size, u64 alignment) {
if (!allocator || !(allocator->alloc_aligned)) {
return NULL;
}
@ -18,28 +18,27 @@ void *wapp_mem_allocator_alloc_aligned(const Allocator *allocator, u64 size,
return allocator->alloc_aligned(size, alignment, allocator->obj);
}
void *wapp_mem_allocator_realloc(const Allocator *allocator, void *ptr,
u64 size) {
void *wapp_mem_allocator_realloc(const Allocator *allocator, void *ptr, u64 old_size, u64 new_size) {
if (!allocator || !(allocator->realloc)) {
return NULL;
}
return allocator->realloc(ptr, size, allocator->obj);
return allocator->realloc(ptr, old_size, new_size, allocator->obj);
}
void *wapp_mem_allocator_realloc_aligned(const Allocator *allocator, void *ptr,
u64 size, u64 alignment) {
void *wapp_mem_allocator_realloc_aligned(const Allocator *allocator, void *ptr, u64 old_size,
u64 new_size, u64 alignment) {
if (!allocator || !(allocator->realloc_aligned)) {
return NULL;
}
return allocator->realloc_aligned(ptr, size, alignment, allocator->obj);
return allocator->realloc_aligned(ptr, old_size, new_size, alignment, allocator->obj);
}
void wapp_mem_allocator_free(const Allocator *allocator, void **ptr) {
void wapp_mem_allocator_free(const Allocator *allocator, void **ptr, u64 size) {
if (!allocator || !(allocator->free)) {
return;
}
allocator->free(ptr, allocator->obj);
allocator->free(ptr, size, allocator->obj);
}

View File

@ -1,18 +1,19 @@
#ifndef MEM_ALLOCATOR_H
#define MEM_ALLOCATOR_H
#include "aliases.h"
#include "../../../common/aliases/aliases.h"
#include <string.h>
#ifdef __cplusplus
extern "C" {
BEGIN_C_LINKAGE
#endif // __cplusplus
typedef void *(MemAllocFunc)(u64 size, void *alloc_obj);
typedef void *(MemAllocAlignedFunc)(u64 size, u64 alignment, void *alloc_obj);
typedef void *(MemReallocFunc)(void *ptr, u64 size, void *alloc_obj);
typedef void *(MemReallocAlignedFunc)(void *ptr, u64 size, u64 alignment,
void *alloc_obj);
typedef void(MemFreeFunc)(void **ptr, void *alloc_obj);
typedef void *(MemReallocFunc)(void *ptr, u64 old_size, u64 new_size, void *alloc_obj);
typedef void *(MemReallocAlignedFunc)(void *ptr, u64 old_size, u64 new_size, u64 alignment, void *alloc_obj);
typedef void (MemFreeFunc)(void **ptr, u64 size, void *alloc_obj);
typedef struct allocator Allocator;
struct allocator {
@ -24,17 +25,18 @@ struct allocator {
MemFreeFunc *free;
};
#define wapp_mem_allocator_invalid(ALLOCATOR) (memcmp(ALLOCATOR, &((Allocator){0}), sizeof(Allocator)) == 0)
void *wapp_mem_allocator_alloc(const Allocator *allocator, u64 size);
void *wapp_mem_allocator_alloc_aligned(const Allocator *allocator, u64 size,
u64 alignment);
void *wapp_mem_allocator_realloc(const Allocator *allocator, void *ptr,
u64 size);
void *wapp_mem_allocator_realloc_aligned(const Allocator *allocator, void *ptr,
u64 size, u64 alignment);
void wapp_mem_allocator_free(const Allocator *allocator, void **ptr);
void *wapp_mem_allocator_alloc_aligned(const Allocator *allocator, u64 size, u64 alignment);
void *wapp_mem_allocator_realloc(const Allocator *allocator, void *ptr, u64 old_size, u64 new_size);
void *wapp_mem_allocator_realloc_aligned(const Allocator *allocator, void *ptr, u64 old_size,
u64 new_size, u64 alignment);
void wapp_mem_allocator_free(const Allocator *allocator, void **ptr, u64 size);
#ifdef __cplusplus
}
END_C_LINKAGE
#endif // __cplusplus
#endif // !MEM_ALLOCATOR_H

View File

@ -0,0 +1,145 @@
#include "mem_arena.h"
#include "../../../common/aliases/aliases.h"
#include "../../../common/misc/misc_utils.h"
#include "../../os/mem/mem_os.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#ifndef DEFAULT_ALIGNMENT
// Why 2 * sizeof(void *) instead of sizeof(void *)
// https://handmade.network/forums/t/6860-alignment_arena_allocator
#define DEFAULT_ALIGNMENT (2 * sizeof(void *))
#endif /* ifndef DEFAULT_ALIGNMENT */
#define ARENA_MINIMUM_CAPACITY KB(16) // Allocate minimum of 4 pages
struct arena {
u8 *buf;
u8 *offset;
u64 capacity;
bool committed;
#ifdef WAPP_PLATFORM_WINDOWS
wapp_misc_utils_padding_size(sizeof(u8 *) * 2 + sizeof(u64) + sizeof(bool));
#endif // ifdef WAPP_PLATFORM_WINDOWS
};
bool wapp_mem_arena_init_custom(Arena **arena, u64 base_capacity, MemAllocFlags flags, bool zero_buffer) {
if (!arena || *arena || base_capacity == 0) {
return false;
}
*arena = (Arena *)calloc(1, sizeof(Arena));
Arena *arena_ptr = *arena;
if (!arena_ptr) {
return false;
}
u64 arena_capacity = base_capacity >= ARENA_MINIMUM_CAPACITY ? base_capacity : ARENA_MINIMUM_CAPACITY;
arena_ptr->buf = (u8 *)wapp_mem_util_alloc(NULL, arena_capacity, WAPP_MEM_ACCESS_READ_WRITE, flags,
zero_buffer ? WAPP_MEM_INIT_INITIALISED : WAPP_MEM_INIT_UNINITIALISED);
if (!(arena_ptr->buf)) {
wapp_mem_arena_destroy(arena);
return false;
}
arena_ptr->capacity = arena_capacity;
arena_ptr->offset = arena_ptr->buf;
arena_ptr->committed = (flags & WAPP_MEM_ALLOC_COMMIT) == WAPP_MEM_ALLOC_COMMIT;
return true;
}
void *wapp_mem_arena_alloc(Arena *arena, u64 size) {
return wapp_mem_arena_alloc_aligned(arena, size, DEFAULT_ALIGNMENT);
}
void *wapp_mem_arena_alloc_aligned(Arena *arena, u64 size, u64 alignment) {
if (!arena) {
return NULL;
}
u8 *alloc_start = arena->offset;
u8 *output = wapp_mem_util_align_forward((void *)alloc_start, alignment);
if (output + size >= arena->buf + arena->capacity) {
return NULL;
}
arena->offset = output + size;
#ifdef WAPP_PLATFORM_WINDOWS
if (!(arena->committed)) {
wapp_mem_util_alloc(alloc_start, (uptr)(arena->offset) - (uptr)(alloc_start),
WAPP_MEM_ACCESS_READ_WRITE, WAPP_MEM_ALLOC_COMMIT,
WAPP_MEM_INIT_UNINITIALISED);
}
#endif // ifdef WAPP_PLATFORM_WINDOWS
memset(output, 0, size);
return (void *)output;
}
void *wapp_mem_arena_realloc(Arena *arena, void *ptr, u64 old_size, u64 new_size) {
if ((u8*)ptr < arena->buf || (u8*)ptr > arena->offset ||
arena->offset + new_size >= arena->buf + arena->capacity) {
return NULL;
}
void *new_ptr = wapp_mem_arena_alloc(arena, new_size);
if (!new_ptr) {
return NULL;
}
u64 copy_size = new_size <= old_size ? new_size : old_size;
memcpy(new_ptr, ptr, copy_size);
return new_ptr;
}
void *wapp_mem_arena_realloc_aligned(Arena *arena, void *ptr, u64 old_size, u64 new_size, u64 alignment) {
if ((u8*)ptr < arena->buf || (u8*)ptr > arena->offset ||
arena->offset + new_size >= arena->buf + arena->capacity) {
return NULL;
}
void *new_ptr = wapp_mem_arena_alloc_aligned(arena, new_size, alignment);
if (!new_ptr) {
return NULL;
}
u64 copy_size = new_size <= old_size ? new_size : old_size;
memcpy(new_ptr, ptr, copy_size);
return new_ptr;
}
void wapp_mem_arena_clear(Arena *arena) {
if (!arena) {
return;
}
memset(arena->buf, 0, arena->offset - arena->buf);
arena->offset = arena->buf;
}
void wapp_mem_arena_destroy(Arena **arena) {
if (!arena) {
return;
}
Arena *arena_ptr = *arena;
if (arena_ptr->buf) {
wapp_mem_util_free(arena_ptr->buf, arena_ptr->capacity);
}
arena_ptr->buf = arena_ptr->offset = NULL;
arena_ptr->capacity = 0;
free(*arena);
*arena = NULL;
}

View File

@ -0,0 +1,40 @@
#ifndef MEM_ARENA_H
#define MEM_ARENA_H
#include "../../../common/aliases/aliases.h"
#include "../../os/mem/mem_os.h"
#include <stdbool.h>
#ifdef __cplusplus
BEGIN_C_LINKAGE
#endif // __cplusplus
typedef struct arena Arena;
#define wapp_mem_arena_init(arena_dptr, base_capacity) \
(wapp_mem_arena_init_custom(arena_dptr, base_capacity, WAPP_MEM_ALLOC_RESERVE, false))
#define wapp_mem_arena_init_commit(arena_dptr, base_capacity) \
(wapp_mem_arena_init_custom(arena_dptr, base_capacity, WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, false))
#define wapp_mem_arena_init_zero(arena_dptr, base_capacity) \
(wapp_mem_arena_init_custom(arena_dptr, base_capacity, WAPP_MEM_ALLOC_RESERVE, true))
#define wapp_mem_arena_init_commit_and_zero(arena_dptr, base_capacity) \
(wapp_mem_arena_init_custom(arena_dptr, base_capacity, WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, true))
/**
* Arena initialisation function. `wapp_mem_arena_init_custom` provides the most
* control over how the Arena is initialised. Wrapper macros are provided for
* easier use.
*/
bool wapp_mem_arena_init_custom(Arena **arena, u64 base_capacity, MemAllocFlags flags, bool zero_buffer);
void *wapp_mem_arena_alloc(Arena *arena, u64 size);
void *wapp_mem_arena_alloc_aligned(Arena *arena, u64 size, u64 alignment);
void *wapp_mem_arena_realloc(Arena *arena, void *ptr, u64 old_size, u64 new_size);
void *wapp_mem_arena_realloc_aligned(Arena *arena, void *ptr, u64 old_size, u64 new_size, u64 alignment);
void wapp_mem_arena_clear(Arena *arena);
void wapp_mem_arena_destroy(Arena **arena);
#ifdef __cplusplus
END_C_LINKAGE
#endif // __cplusplus
#endif // !MEM_ARENA_H

View File

@ -0,0 +1,57 @@
#include "mem_arena_allocator.h"
#include "mem_arena.h"
#include "../../../common/aliases/aliases.h"
#include "../../os/mem/mem_os.h"
internal inline void *mem_arena_alloc(u64 size, void *alloc_obj);
internal inline void *mem_arena_alloc_aligned(u64 size, u64 alignment, void *alloc_obj);
internal inline void *mem_arena_realloc(void *ptr, u64 old_size, u64 new_size, void *alloc_obj);
internal inline void *mem_arena_realloc_aligned(void *ptr, u64 old_size, u64 new_size, u64 alignment,
void *alloc_obj);
Allocator wapp_mem_arena_allocator_init_custom(u64 base_capacity, MemAllocFlags flags, bool zero_buffer) {
Allocator allocator = {0};
bool initialised = wapp_mem_arena_init_custom((Arena **)(&allocator.obj), base_capacity, flags, zero_buffer);
if (!initialised) {
return allocator;
}
allocator.alloc = mem_arena_alloc;
allocator.alloc_aligned = mem_arena_alloc_aligned;
allocator.realloc = mem_arena_realloc;
allocator.realloc_aligned = mem_arena_realloc_aligned;
return allocator;
}
void wapp_mem_arena_allocator_clear(Allocator *allocator) {
wapp_mem_arena_clear((Arena *)(allocator->obj));
}
void wapp_mem_arena_allocator_destroy(Allocator *allocator) {
wapp_mem_arena_destroy((Arena **)(&(allocator->obj)));
*allocator = (Allocator){0};
}
internal inline void *mem_arena_alloc(u64 size, void *alloc_obj) {
Arena *arena = (Arena *)alloc_obj;
return wapp_mem_arena_alloc(arena, size);
}
internal inline void *mem_arena_alloc_aligned(u64 size, u64 alignment, void *alloc_obj) {
Arena *arena = (Arena *)alloc_obj;
return wapp_mem_arena_alloc_aligned(arena, size, alignment);
}
internal inline void *mem_arena_realloc(void *ptr, u64 old_size, u64 new_size, void *alloc_obj) {
Arena *arena = (Arena *)alloc_obj;
return wapp_mem_arena_realloc(arena, ptr, old_size, new_size);
}
internal inline void *mem_arena_realloc_aligned(void *ptr, u64 old_size, u64 new_size, u64 alignment,
void *alloc_obj) {
Arena *arena = (Arena *)alloc_obj;
return wapp_mem_arena_realloc_aligned(arena, ptr, old_size, new_size, alignment);
}

View File

@ -0,0 +1,41 @@
#ifndef MEM_ARENA_ALLOCATOR_H
#define MEM_ARENA_ALLOCATOR_H
#include "../../../common/aliases/aliases.h"
#include "../allocator/mem_allocator.h"
#include "../../os/mem/mem_os.h"
#include <stdbool.h>
#ifdef __cplusplus
BEGIN_C_LINKAGE
#endif // __cplusplus
#define wapp_mem_arena_allocator_init(base_capacity) \
(wapp_mem_arena_allocator_init_custom(base_capacity, WAPP_MEM_ALLOC_RESERVE, false))
#define wapp_mem_arena_allocator_init_commit(base_capacity) \
(wapp_mem_arena_allocator_init_custom(base_capacity, WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, false))
#define wapp_mem_arena_allocator_init_zero(base_capacity) \
(wapp_mem_arena_allocator_init_custom(base_capacity, WAPP_MEM_ALLOC_RESERVE, true))
#define wapp_mem_arena_allocator_init_commit_and_zero(base_capacity) \
(wapp_mem_arena_allocator_init_custom(base_capacity, WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, true))
/**
* Wraps an Arena in an Allocator object. It attempts to initialise the Arena
* and, if successful, defines the operations supported by it to be used by the
* Allocator.
*
* An Arena allocator only supports normal allocation and aligned allocation.
* Reallocation, aligned reallocation and freeing aren't implemented.
*
* The `wapp_mem_arena_allocator_init_custom` provides the most control over how
* the Arena is initialised. Wrapper macros are provided for easier use.
*/
Allocator wapp_mem_arena_allocator_init_custom(u64 base_capacity, MemAllocFlags flags, bool zero_buffer);
void wapp_mem_arena_allocator_clear(Allocator *allocator);
void wapp_mem_arena_allocator_destroy(Allocator *allocator);
#ifdef __cplusplus
END_C_LINKAGE
#endif // __cplusplus
#endif // !MEM_ARENA_ALLOCATOR_H

View File

@ -1,8 +1,8 @@
#include "mem_utils.h"
#include "aliases.h"
#include <assert.h>
#include "../../../common/aliases/aliases.h"
#include <stdbool.h>
#include <stdlib.h>
#include <stddef.h>
#include <assert.h>
internal bool is_power_of_two(u64 num) { return (num & (num - 1)) == 0; }

View File

@ -1,16 +1,16 @@
#ifndef MEM_UTILS_H
#define MEM_UTILS_H
#include "aliases.h"
#include "../../../common/aliases/aliases.h"
#ifdef __cplusplus
extern "C" {
BEGIN_C_LINKAGE
#endif // __cplusplus
void *wapp_mem_util_align_forward(void *ptr, u64 alignment);
#ifdef __cplusplus
}
END_C_LINKAGE
#endif // __cplusplus
#endif // !MEM_UTILS_H

134
src/core/os/cpath/cpath.c Normal file
View File

@ -0,0 +1,134 @@
#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 <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
u32 wapp_cpath_join_path(Str8 *dst, const Str8List *parts) {
if (!dst || !parts) {
return CPATH_JOIN_INVALID_ARGS;
}
if (parts->node_count == 0) {
return CPATH_JOIN_EMPTY_PARTS;
}
Str8 separator = wapp_str8_buf(4);
wapp_str8_push_back(&separator, PATH_SEP);
u64 required_capacity = parts->node_count * separator.size + wapp_str8_list_total_size(parts);
if (dst->capacity < required_capacity) {
return CPATH_JOIN_INSUFFICIENT_DST_CAPACITY;
}
// Handle first node
const Str8Node *first_node = wapp_str8_list_get(parts, 0);
wapp_str8_copy_str8_capped(dst, first_node->item);
// NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of
// MSVC Spectre mitigation warnings
const Str8Node *node = first_node;
u64 node_index = 1;
bool running = true;
while (running && node->next) {
node = node->next;
if (node->item->size == 0) {
continue;
}
if (dst->size > 0) {
char dst_last = wapp_str8_get(dst, dst->size - 1);
char node_start = wapp_str8_get(node->item, 0);
bool add_path_sep = dst_last != PATH_SEP && node_start != PATH_SEP;
if (add_path_sep) {
wapp_str8_concat_capped(dst, &separator);
}
}
wapp_str8_concat_capped(dst, node->item);
++node_index;
running = node_index < parts->node_count;
}
return CPATH_JOIN_SUCCESS;
}
Str8 *dirup(const Allocator *allocator, Str8RO *path, u64 levels) {
Str8 *output = NULL;
if (!allocator || !path) {
goto RETURN_DIRUP;
}
bool absolute = wapp_str8_get(path, 0) == PATH_SEP;
Str8 separator = wapp_str8_buf(4);
wapp_str8_push_back(&separator, PATH_SEP);
if (path->size == 0) {
output = wapp_str8_alloc_buf(allocator, 16);
if (!output) {
goto RETURN_DIRUP;
}
wapp_str8_push_back(output, absolute ? PATH_SEP : '.');
goto RETURN_DIRUP;
}
if (levels < 1) {
output = wapp_str8_alloc_str8(allocator, path);
goto RETURN_DIRUP;
}
Allocator tmp_arena = wapp_mem_arena_allocator_init(MB(8));
if (wapp_mem_allocator_invalid(&tmp_arena)) {
goto RETURN_DIRUP;
}
Str8List *parts = wapp_str8_split(&tmp_arena, path, &separator);
if (!parts) {
goto RETURN_DIRUP;
}
if (levels >= parts->node_count) {
output = wapp_str8_alloc_buf(allocator, 16);
if (!output) {
goto LIST_CLEANUP_DIRUP;
}
wapp_str8_push_back(output, absolute ? PATH_SEP : '.');
} else {
for (u64 i = 0; i < levels; ++i) {
wapp_str8_list_pop_back(parts);
}
u64 alignment = sizeof(void *) * 2;
u64 alloc_size = wapp_str8_list_total_size(parts) + parts->node_count * separator.size;
u64 modulo = alloc_size & (alignment - 1);
alloc_size += alignment - modulo;
output = wapp_str8_alloc_buf(allocator, alloc_size);
if (output) {
if (absolute) {
wapp_str8_push_back(output, PATH_SEP);
}
Str8 *joined = wapp_str8_join(&tmp_arena, parts, &separator);
if (joined) {
wapp_str8_concat_capped(output, joined);
}
}
}
LIST_CLEANUP_DIRUP:
wapp_mem_arena_allocator_destroy(&tmp_arena);
RETURN_DIRUP:
return output;
}

38
src/core/os/cpath/cpath.h Normal file
View File

@ -0,0 +1,38 @@
#ifndef CPATH_H
#define CPATH_H
#include "../../../common/aliases/aliases.h"
#include "../../../common/platform/platform.h"
#include "../../mem/allocator/mem_allocator.h"
#include "../../strings/str8/str8.h"
#ifdef __cplusplus
BEGIN_C_LINKAGE
#endif // __cplusplus
#ifdef WAPP_PLATFORM_POSIX
#define PATH_SEP '/'
#elif defined(WAPP_PLATFORM_WINDOWS)
#define PATH_SEP '\\'
#else
#error "Unrecognised platform"
#endif
#define wapp_cpath_dirname(ALLOCATOR, PATH) dirup(ALLOCATOR, PATH, 1)
#define wapp_cpath_dirup(ALLOCATOR, PATH, COUNT) dirup(ALLOCATOR, PATH, COUNT)
enum {
CPATH_JOIN_SUCCESS = 0,
CPATH_JOIN_INVALID_ARGS,
CPATH_JOIN_EMPTY_PARTS,
CPATH_JOIN_INSUFFICIENT_DST_CAPACITY,
};
u32 wapp_cpath_join_path(Str8 *dst, const Str8List *parts);
Str8 *dirup(const Allocator *allocator, Str8RO *path, u64 levels);
#ifdef __cplusplus
END_C_LINKAGE
#endif // __cplusplus
#endif // !CPATH_H

29
src/core/os/mem/mem_os.c Normal file
View File

@ -0,0 +1,29 @@
#include "mem_os.h"
#include "mem_os_ops.h"
#include "../../../common/aliases/aliases.h"
#include "../../../common/platform/platform.h"
#include <assert.h>
#include <stdbool.h>
#include <string.h>
#if defined(WAPP_PLATFORM_WINDOWS)
#include "win/mem_os_win.h"
#elif defined(WAPP_PLATFORM_POSIX)
#include "posix/mem_os_posix.h"
#else
#error "Unrecognised platform"
#endif
void *wapp_mem_util_alloc(void *addr, u64 size, MemAccess access, MemAllocFlags flags, MemInitType type) {
void *output = mem_util_allocate(addr, size, access, flags, type);
if (type == WAPP_MEM_INIT_INITIALISED) {
memset(output, 0, size);
}
return output;
}
void wapp_mem_util_free(void *ptr, u64 size) {
mem_util_free(ptr, size);
}

32
src/core/os/mem/mem_os.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef MEM_OS_H
#define MEM_OS_H
#include "../../../common/aliases/aliases.h"
#include "../../../common/platform/platform.h"
#ifdef __cplusplus
BEGIN_C_LINKAGE
#endif // __cplusplus
#include "mem_os_ops.h"
#if defined(WAPP_PLATFORM_WINDOWS)
#include "win/mem_os_win.h"
#elif defined(WAPP_PLATFORM_POSIX)
#include "posix/mem_os_posix.h"
#else
#error "Unrecognised platform"
#endif
void *wapp_mem_util_align_forward(void *ptr, u64 alignment);
void *wapp_mem_util_alloc(void *addr, u64 size, MemAccess access, MemAllocFlags flags, MemInitType type);
void wapp_mem_util_free(void *ptr, u64 size);
external void *mem_util_allocate(void *addr, u64 size, MemAccess access, MemAllocFlags flags, MemInitType type);
external void mem_util_free(void *ptr, u64 size);
#ifdef __cplusplus
END_C_LINKAGE
#endif // __cplusplus
#endif // !MEM_OS_H

View File

@ -0,0 +1,26 @@
#ifndef MEM_OS_OPS_H
#define MEM_OS_OPS_H
#ifdef __cplusplus
BEGIN_C_LINKAGE
#endif // __cplusplus
typedef enum mem_access {
WAPP_MEM_ACCESS_NONE,
WAPP_MEM_ACCESS_READ_ONLY,
WAPP_MEM_ACCESS_EXEC_ONLY,
WAPP_MEM_ACCESS_READ_WRITE,
WAPP_MEM_ACCESS_READ_EXEC,
WAPP_MEM_ACCESS_READ_WRITE_EXEC,
} MemAccess;
typedef enum mem_init_type {
WAPP_MEM_INIT_UNINITIALISED,
WAPP_MEM_INIT_INITIALISED,
} MemInitType;
#ifdef __cplusplus
END_C_LINKAGE
#endif // __cplusplus
#endif // !MEM_OS_OPS_H

View File

@ -0,0 +1,34 @@
#include "../../../../common/aliases/aliases.h"
#include "../../../../common/platform/platform.h"
#ifdef WAPP_PLATFORM_POSIX
#include "mem_os_posix.h"
#include "../mem_os_ops.h"
#include <sys/mman.h>
internal const i32 access_types[] = {
[WAPP_MEM_ACCESS_NONE] = PROT_NONE,
[WAPP_MEM_ACCESS_READ_ONLY] = PROT_READ,
[WAPP_MEM_ACCESS_EXEC_ONLY] = PROT_EXEC,
[WAPP_MEM_ACCESS_READ_WRITE] = PROT_READ | PROT_WRITE,
[WAPP_MEM_ACCESS_READ_EXEC] = PROT_READ | PROT_EXEC,
[WAPP_MEM_ACCESS_READ_WRITE_EXEC] = PROT_READ | PROT_WRITE | PROT_EXEC,
};
void *mem_util_allocate(void *addr, u64 size, MemAccess access, MemAllocFlags flags, MemInitType type) {
(void)type;
i32 alloc_flags = flags | MAP_ANON | MAP_PRIVATE;
#if defined(WAPP_PLATFORM_LINUX) || defined(WAPP_PLATFORM_GNU) || defined(WAPP_PLATFORM_NET_BSD)
alloc_flags |= MAP_NORESERVE;
#endif
return mmap(addr, size, access_types[access], alloc_flags, -1, 0);
}
void mem_util_free(void *ptr, u64 size) {
munmap(ptr, size);
}
#endif // !WAPP_PLATFORM_POSIX

View File

@ -0,0 +1,33 @@
#ifndef MEM_OS_POSIX_H
#define MEM_OS_POSIX_H
#include "../../../../common/platform/platform.h"
#ifdef __cplusplus
BEGIN_C_LINKAGE
#endif // !__cplusplus
#ifdef WAPP_PLATFORM_POSIX
#include <sys/mman.h>
typedef enum mem_alloc_flags {
#if defined(WAPP_PLATFORM_LINUX) || defined(WAPP_PLATFORM_GNU)
WAPP_MEM_ALLOC_RESERVE = 0,
WAPP_MEM_ALLOC_COMMIT = MAP_POPULATE,
#elif defined(WAPP_PLATFORM_FREE_BSD)
WAPP_MEM_ALLOC_RESERVE = 0,
WAPP_MEM_ALLOC_COMMIT = MAP_PREFAULT_READ,
#elif defined(WAPP_PLATFORM_BSD) || defined(WAPP_PLATFORM_UNIX) || defined(WAPP_PLATFORM_APPLE)
WAPP_MEM_ALLOC_RESERVE = 0,
WAPP_MEM_ALLOC_COMMIT = 0,
#endif
} MemAllocFlags;
#endif // !WAPP_PLATFORM_POSIX
#ifdef __cplusplus
END_C_LINKAGE
#endif // !__cplusplus
#endif // !MEM_OS_POSIX_H

View File

@ -0,0 +1,35 @@
#include "../../../../common/aliases/aliases.h"
#include "../../../../common/platform/platform.h"
#ifdef WAPP_PLATFORM_WINDOWS
#include "mem_os_win.h"
#include "../mem_os_ops.h"
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <memoryapi.h>
internal const i32 access_types[] = {
[WAPP_MEM_ACCESS_NONE] = PAGE_NOACCESS,
[WAPP_MEM_ACCESS_READ_ONLY] = PAGE_READONLY,
[WAPP_MEM_ACCESS_EXEC_ONLY] = PAGE_EXECUTE,
[WAPP_MEM_ACCESS_READ_WRITE] = PAGE_READWRITE,
[WAPP_MEM_ACCESS_READ_EXEC] = PAGE_EXECUTE_READ,
[WAPP_MEM_ACCESS_READ_WRITE_EXEC] = PAGE_EXECUTE_READWRITE,
};
void *mem_util_allocate(void *addr, u64 size, MemAccess access, MemAllocFlags flags, MemInitType type) {
// Ensure memory is committed if it's meant to be initialised
if (type == WAPP_MEM_INIT_INITIALISED) {
flags |= WAPP_MEM_ALLOC_COMMIT;
}
return VirtualAlloc(addr, (SIZE_T)size, flags, access_types[access]);
}
void mem_util_free(void *ptr, u64 size) {
VirtualFree(ptr, size, MEM_RELEASE);
}
#endif // !WAPP_PLATFORM_WINDOWS

View File

@ -0,0 +1,27 @@
#ifndef MEM_OS_WIN_H
#define MEM_OS_WIN_H
#include "../../../../common/platform/platform.h"
#ifdef __cplusplus
BEGIN_C_LINKAGE
#endif // !__cplusplus
#ifdef WAPP_PLATFORM_WINDOWS
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <memoryapi.h>
typedef enum mem_alloc_flags {
WAPP_MEM_ALLOC_RESERVE = MEM_RESERVE,
WAPP_MEM_ALLOC_COMMIT = MEM_COMMIT,
} MemAllocFlags;
#endif // !WAPP_PLATFORM_WINDOWS
#ifdef __cplusplus
END_C_LINKAGE
#endif // !__cplusplus
#endif // !MEM_OS_WIN_H

View File

@ -0,0 +1,100 @@
#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 <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CMD_BUF_LEN 8192
#define OUT_BUF_LEN 4096
internal inline CMDResult execute_command(Str8RO *cmd, CMDOutHandling out_handling, Str8 *out_buf);
internal inline CMDError get_command_output(FILE *fp, CMDOutHandling out_handling, Str8 *out_buf);
CMDResult wapp_shell_commander_execute(CMDOutHandling out_handling, Str8 *out_buf, const Str8List *cmd) {
if (!cmd) {
return CMD_NO_EXIT(SHELL_ERR_INVALID_ARGS);
}
Allocator arena = wapp_mem_arena_allocator_init(KB(500));
Str8 *cmd_str = wapp_str8_join(&arena, cmd, &wapp_str8_lit_ro(" "));
if (!cmd_str) {
wapp_mem_arena_allocator_destroy(&arena);
return CMD_NO_EXIT(SHELL_ERR_ALLOCATION_FAIL);
}
// Redirect output
cmd_str = wapp_str8_alloc_concat(&arena, cmd_str, &wapp_str8_lit_ro(" 2>&1"));
CMDResult output = execute_command(cmd_str, out_handling, out_buf);
wapp_mem_arena_allocator_destroy(&arena);
return output;
}
internal inline CMDResult execute_command(Str8RO *cmd, CMDOutHandling out_handling, Str8 *out_buf) {
char cmd_buf[CMD_BUF_LEN] = {0};
wapp_str8_copy_to_cstr(cmd_buf, cmd, CMD_BUF_LEN);
FILE *fp = wapp_shell_utils_popen(cmd_buf, "r");
if (!fp) {
return CMD_NO_EXIT(SHELL_ERR_PROC_START_FAIL);
}
CMDResult output;
CMDError err = get_command_output(fp, out_handling, out_buf);
if (err > SHELL_ERR_NO_ERROR) {
output = CMD_NO_EXIT(err);
goto EXECUTE_COMMAND_CLOSE;
}
i32 st = EXIT_SUCCESS;
err = get_output_status(fp, &st);
if (err > SHELL_ERR_NO_ERROR) {
output = CMD_NO_EXIT(err);
goto EXECUTE_COMMAND_CLOSE;
}
// Process is already closed in get_output_status
fp = NULL;
output = (CMDResult){
.exited = true,
.exit_code = st,
.error = SHELL_ERR_NO_ERROR,
};
EXECUTE_COMMAND_CLOSE:
if (fp) {
wapp_shell_utils_pclose(fp);
}
return output;
}
internal inline CMDError get_command_output(FILE *fp, CMDOutHandling out_handling, Str8 *out_buf) {
Str8 out = wapp_str8_buf(OUT_BUF_LEN);
out.size = fread((void *)out.buf, sizeof(u8), out.capacity, fp);
if (out_handling == SHELL_OUTPUT_CAPTURE && out_buf != NULL) {
if (out.size >= out_buf->capacity) {
return SHELL_ERR_OUT_BUF_FULL;
}
wapp_str8_concat_capped(out_buf, &out);
} else if (out_handling == SHELL_OUTPUT_PRINT) {
printf(WAPP_STR8_SPEC, wapp_str8_varg(out));
}
return SHELL_ERR_NO_ERROR;
}

View File

@ -0,0 +1,25 @@
#ifndef COMMANDER_H
#define COMMANDER_H
#include "commander_output.h"
#include "../../../../common/aliases/aliases.h"
#include "../../../strings/str8/str8.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef __cplusplus
BEGIN_C_LINKAGE
#endif // __cplusplus
#define CMD_NO_EXIT(ERR) ((CMDResult){.exited = false, .exit_code = EXIT_FAILURE, .error = ERR})
CMDResult wapp_shell_commander_execute(CMDOutHandling out_handling, Str8 *out_buf, const Str8List *cmd);
external CMDError get_output_status(FILE *fp, i32 *status_out);
#ifdef __cplusplus
END_C_LINKAGE
#endif // __cplusplus
#endif // !COMMANDER_H

View File

@ -0,0 +1,43 @@
#ifndef COMMANDER_OUTPUT_H
#define COMMANDER_OUTPUT_H
#include "../../../../common/aliases/aliases.h"
#include "../../../../common/platform/platform.h"
#include <stdbool.h>
#ifdef __cplusplus
BEGIN_C_LINKAGE
#endif // __cplusplus
typedef enum {
SHELL_OUTPUT_DISCARD,
SHELL_OUTPUT_PRINT,
SHELL_OUTPUT_CAPTURE,
} CMDOutHandling;
typedef enum {
SHELL_ERR_NO_ERROR,
SHELL_ERR_INVALID_ARGS,
SHELL_ERR_ALLOCATION_FAIL,
SHELL_ERR_PROC_START_FAIL,
SHELL_ERR_OUT_BUF_FULL,
SHELL_ERR_PROC_EXIT_FAIL,
} CMDError;
typedef struct commander_result CMDResult;
struct commander_result {
i32 exit_code;
CMDError error;
bool exited;
#ifdef WAPP_PLATFORM_WINDOWS
#include "misc_utils.h"
wapp_misc_utils_padding_size(sizeof(bool) + sizeof(i32) + sizeof(CMDError));
#endif // !WAPP_PLATFORM_WINDOWS
};
#ifdef __cplusplus
END_C_LINKAGE
#endif // __cplusplus
#endif // !COMMANDER_OUTPUT_H

View File

@ -0,0 +1,23 @@
#include "../../../../../common/aliases/aliases.h"
#include "../../../../../common/platform/platform.h"
#ifdef WAPP_PLATFORM_POSIX
#include "../commander_output.h"
#include "../../utils/shell_utils.h"
#include <stdio.h>
#include <stdlib.h>
CMDError get_output_status(FILE *fp, i32 *status_out) {
*status_out = wapp_shell_utils_pclose(fp);
if (!WIFEXITED(*status_out)) {
return SHELL_ERR_PROC_EXIT_FAIL;
}
*status_out = WEXITSTATUS(*status_out);
return SHELL_ERR_NO_ERROR;
}
#endif // !WAPP_PLATFORM_POSIX

View File

@ -0,0 +1,22 @@
#include "../../../../../common/aliases/aliases.h"
#include "../../../../../common/platform/platform.h"
#ifdef WAPP_PLATFORM_WINDOWS
#include "../commander_output.h"
#include "../../utils/shell_utils.h"
#include <stdio.h>
CMDError get_output_status(FILE *fp, i32 *status_out) {
if (!feof(fp)) {
// Ensure process is closed on failure
wapp_shell_utils_pclose(fp);
return SHELL_ERR_PROC_EXIT_FAIL;
}
*status_out = wapp_shell_utils_pclose(fp);
return SHELL_ERR_NO_ERROR;
}
#endif // !WAPP_PLATFORM_WINDOWS

View File

@ -0,0 +1,34 @@
#include "../../../../../common/aliases/aliases.h"
#include "../../../../../common/platform/platform.h"
#include "../../../../strings/str8/str8.h"
#ifdef WAPP_PLATFORM_POSIX
#include "../terminal_colours.h"
#include <stdio.h>
internal Str8RO colours[COUNT_TERM_COLOUR] = {
[WAPP_TERM_COLOUR_FG_BLACK] = wapp_str8_lit_ro_initialiser_list("\033[30m"),
[WAPP_TERM_COLOUR_FG_RED] = wapp_str8_lit_ro_initialiser_list("\033[31m"),
[WAPP_TERM_COLOUR_FG_GREEN] = wapp_str8_lit_ro_initialiser_list("\033[32m"),
[WAPP_TERM_COLOUR_FG_BLUE] = wapp_str8_lit_ro_initialiser_list("\033[34m"),
[WAPP_TERM_COLOUR_FG_CYAN] = wapp_str8_lit_ro_initialiser_list("\033[36m"),
[WAPP_TERM_COLOUR_FG_MAGENTA] = wapp_str8_lit_ro_initialiser_list("\033[35m"),
[WAPP_TERM_COLOUR_FG_YELLOW] = wapp_str8_lit_ro_initialiser_list("\033[33m"),
[WAPP_TERM_COLOUR_FG_WHITE] = wapp_str8_lit_ro_initialiser_list("\033[37m"),
[WAPP_TERM_COLOUR_FG_BR_BLACK] = wapp_str8_lit_ro_initialiser_list("\033[90m"),
[WAPP_TERM_COLOUR_FG_BR_RED] = wapp_str8_lit_ro_initialiser_list("\033[91m"),
[WAPP_TERM_COLOUR_FG_BR_GREEN] = wapp_str8_lit_ro_initialiser_list("\033[92m"),
[WAPP_TERM_COLOUR_FG_BR_BLUE] = wapp_str8_lit_ro_initialiser_list("\033[94m"),
[WAPP_TERM_COLOUR_FG_BR_CYAN] = wapp_str8_lit_ro_initialiser_list("\033[96m"),
[WAPP_TERM_COLOUR_FG_BR_MAGENTA] = wapp_str8_lit_ro_initialiser_list("\033[95m"),
[WAPP_TERM_COLOUR_FG_BR_YELLOW] = wapp_str8_lit_ro_initialiser_list("\033[93m"),
[WAPP_TERM_COLOUR_FG_BR_WHITE] = wapp_str8_lit_ro_initialiser_list("\033[97m"),
[WAPP_TERM_COLOUR_CLEAR] = wapp_str8_lit_ro_initialiser_list("\033[0m"),
};
void print_coloured_text(Str8RO *text, TerminalColour colour) {
printf(WAPP_STR8_SPEC WAPP_STR8_SPEC, wapp_str8_varg(colours[colour]), wapp_str8_varg((*text)));
}
#endif // !WAPP_PLATFORM_POSIX

View File

@ -0,0 +1,16 @@
#include "termcolour.h"
#include "terminal_colours.h"
#include "../../../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) {
return;
}
print_coloured_text(text, colour);
}
void wapp_shell_termcolour_clear_colour(void) {
Str8RO empty = wapp_str8_lit_ro("");
print_coloured_text(&empty, WAPP_TERM_COLOUR_CLEAR);
}

View File

@ -0,0 +1,21 @@
#ifndef TERM_COLOUR_H
#define TERM_COLOUR_H
#include "terminal_colours.h"
#include "../../../../common/aliases/aliases.h"
#include "../../../strings/str8/str8.h"
#ifdef __cplusplus
BEGIN_C_LINKAGE
#endif // __cplusplus
void wapp_shell_termcolour_print_text(Str8RO *text, TerminalColour colour);
void wapp_shell_termcolour_clear_colour(void);
external void print_coloured_text(Str8RO *text, TerminalColour colour);
#ifdef __cplusplus
END_C_LINKAGE
#endif // __cplusplus
#endif // !TERM_COLOUR_H

View File

@ -0,0 +1,34 @@
#ifndef TERMINAL_COLOURS_H
#define TERMINAL_COLOURS_H
#ifdef __cplusplus
BEGIN_C_LINKAGE
#endif // __cplusplus
typedef enum {
WAPP_TERM_COLOUR_FG_BLACK,
WAPP_TERM_COLOUR_FG_RED,
WAPP_TERM_COLOUR_FG_GREEN,
WAPP_TERM_COLOUR_FG_BLUE,
WAPP_TERM_COLOUR_FG_CYAN,
WAPP_TERM_COLOUR_FG_MAGENTA,
WAPP_TERM_COLOUR_FG_YELLOW,
WAPP_TERM_COLOUR_FG_WHITE,
WAPP_TERM_COLOUR_FG_BR_BLACK,
WAPP_TERM_COLOUR_FG_BR_RED,
WAPP_TERM_COLOUR_FG_BR_GREEN,
WAPP_TERM_COLOUR_FG_BR_BLUE,
WAPP_TERM_COLOUR_FG_BR_CYAN,
WAPP_TERM_COLOUR_FG_BR_MAGENTA,
WAPP_TERM_COLOUR_FG_BR_YELLOW,
WAPP_TERM_COLOUR_FG_BR_WHITE,
WAPP_TERM_COLOUR_CLEAR,
COUNT_TERM_COLOUR,
} TerminalColour;
#ifdef __cplusplus
END_C_LINKAGE
#endif // __cplusplus
#endif // !TERMINAL_COLOURS_H

View File

@ -0,0 +1,71 @@
#include "../../../../../common/aliases/aliases.h"
#include "../../../../../common/platform/platform.h"
#include "../../../../strings/str8/str8.h"
#ifdef WAPP_PLATFORM_WINDOWS
#include "../terminal_colours.h"
#include "../../../../../common/misc/misc_utils.h"
#include <stdio.h>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
typedef struct termcolour_data TermcolourData;
struct termcolour_data {
HANDLE handle;
WORD default_colour;
WORD current_colour;
wapp_misc_utils_padding_size(sizeof(HANDLE) + sizeof(WORD) + sizeof(WORD));
};
internal void init_data(TermcolourData *data);
internal WORD colours[COUNT_TERM_COLOUR] = {
[WAPP_TERM_COLOUR_FG_BLACK] = 0,
[WAPP_TERM_COLOUR_FG_RED] = FOREGROUND_RED,
[WAPP_TERM_COLOUR_FG_GREEN] = FOREGROUND_GREEN,
[WAPP_TERM_COLOUR_FG_BLUE] = FOREGROUND_BLUE,
[WAPP_TERM_COLOUR_FG_CYAN] = FOREGROUND_GREEN | FOREGROUND_BLUE,
[WAPP_TERM_COLOUR_FG_MAGENTA] = FOREGROUND_RED | FOREGROUND_BLUE,
[WAPP_TERM_COLOUR_FG_YELLOW] = FOREGROUND_RED | FOREGROUND_GREEN,
[WAPP_TERM_COLOUR_FG_WHITE] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
[WAPP_TERM_COLOUR_FG_BR_BLACK] = FOREGROUND_INTENSITY,
[WAPP_TERM_COLOUR_FG_BR_RED] = FOREGROUND_RED | FOREGROUND_INTENSITY,
[WAPP_TERM_COLOUR_FG_BR_GREEN] = FOREGROUND_GREEN | FOREGROUND_INTENSITY,
[WAPP_TERM_COLOUR_FG_BR_BLUE] = FOREGROUND_BLUE | FOREGROUND_INTENSITY,
[WAPP_TERM_COLOUR_FG_BR_CYAN] = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
[WAPP_TERM_COLOUR_FG_BR_MAGENTA] = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
[WAPP_TERM_COLOUR_FG_BR_YELLOW] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY,
[WAPP_TERM_COLOUR_FG_BR_WHITE] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
};
void print_coloured_text(Str8RO *text, TerminalColour colour) {
persistent TermcolourData data = {0};
if (data.handle == 0) {
init_data(&data);
}
if (colour == WAPP_TERM_COLOUR_CLEAR) {
data.current_colour = data.default_colour;
} else {
data.current_colour = colours[colour];
}
SetConsoleTextAttribute(data.handle, data.current_colour);
printf(WAPP_STR8_SPEC, wapp_str8_varg((*text)));
}
internal void init_data(TermcolourData *data) {
// create handle
data->handle = GetStdHandle(STD_OUTPUT_HANDLE);
// get console colour information
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(data->handle, &csbi);
data->default_colour = csbi.wAttributes;
data->current_colour = data->default_colour;
}
#endif // !WAPP_PLATFORM_WINDOWS

View File

@ -0,0 +1,15 @@
#ifndef SHELL_UTILS_H
#define SHELL_UTILS_H
#include "../../../../common/platform/platform.h"
#include <stdio.h>
#ifdef WAPP_PLATFORM_WINDOWS
#define wapp_shell_utils_popen _popen
#define wapp_shell_utils_pclose _pclose
#else
#define wapp_shell_utils_popen popen
#define wapp_shell_utils_pclose pclose
#endif /* ifdef WAPP_PLATFORM_WINDOWS */
#endif // !SHELL_UTILS_H

View File

@ -0,0 +1,449 @@
#include "str8.h"
#include "../../../common/aliases/aliases.h"
#include "../../mem/allocator/mem_allocator.h"
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define STR8_BUF_ALLOC_SIZE(CAPACITY) (sizeof(Str8) + sizeof(c8) * CAPACITY)
Str8 *wapp_str8_alloc_buf(const Allocator *allocator, u64 capacity) {
Str8 *str = NULL;
if (!allocator) {
goto RETURN_STR8;
}
str = wapp_mem_allocator_alloc(allocator, STR8_BUF_ALLOC_SIZE(capacity));
if (!str) {
goto RETURN_STR8;
}
str->buf = (u8 *)str + sizeof(Str8);
str->size = 0;
str->capacity = capacity;
RETURN_STR8:
return str;
}
Str8 *wapp_str8_alloc_cstr(const Allocator *allocator, const char *str) {
Str8 *output = NULL;
if (!allocator || !str) {
goto RETURN_ALLOC_CSTR;
}
u64 length = strlen(str);
output = wapp_str8_alloc_buf(allocator, length * 2);
if (!output) {
goto RETURN_ALLOC_CSTR;
}
output->size = length;
memcpy(output->buf, str, length);
RETURN_ALLOC_CSTR:
return output;
}
Str8 *wapp_str8_alloc_str8(const Allocator *allocator, Str8RO *str) {
Str8 *output = NULL;
if (!allocator || !str) {
goto RETURN_ALLOC_STR8;
}
output = wapp_str8_alloc_buf(allocator, str->capacity);
if (!output) {
goto RETURN_ALLOC_STR8;
}
output->size = str->size;
memcpy(output->buf, str->buf, str->size);
RETURN_ALLOC_STR8:
return output;
}
Str8 *wapp_str8_alloc_substr(const Allocator *allocator, Str8RO *str, u64 start, u64 end) {
Str8 *output = NULL;
if (!allocator || !str) {
goto RETURN_ALLOC_SUBSTR;
}
if (start >= str->size || start >= end) {
goto RETURN_ALLOC_SUBSTR;
}
if (end > str->size) {
end = str->size;
}
output = wapp_str8_alloc_buf(allocator, str->capacity);
if (!output) {
goto RETURN_ALLOC_SUBSTR;
}
output->size = end - start;
memcpy(output->buf, str->buf + start, output->size);
RETURN_ALLOC_SUBSTR:
return output;
}
void wapp_str8_dealloc_buf(const Allocator *allocator, Str8 **str) {
if (!allocator || !str || !(*str)) {
return;
}
wapp_mem_allocator_free(allocator, (void **)str, STR8_BUF_ALLOC_SIZE((*str)->capacity));
}
c8 wapp_str8_get(const Str8 *str, u64 index) {
if (index >= str->size) {
return '\0';
}
return str->buf[index];
}
void wapp_str8_set(Str8 *str, u64 index, c8 c) {
if (index >= str->size) {
return;
}
str->buf[index] = c;
}
void wapp_str8_push_back(Str8 *str, c8 c) {
if (!(str->size < str->capacity)) {
return;
}
u64 index = (str->size)++;
wapp_str8_set(str, index, c);
}
bool wapp_str8_equal(Str8RO *s1, Str8RO *s2) {
if (s1->size != s2->size) {
return false;
}
return wapp_str8_equal_to_count(s1, s2, s1->size);
}
bool wapp_str8_equal_to_count(Str8RO* s1, Str8RO* s2, u64 count) {
if (!s1 || !s2) {
return false;
}
return memcmp(s1->buf, s2->buf, count) == 0;
}
Str8 wapp_str8_slice(Str8RO *str, u64 start, u64 end) {
if (start >= str->size || start >= end) {
start = str->size;
end = str->size;
}
if (end > str->size) {
end = str->size;
}
return (Str8RO){
.capacity = end - start,
.size = end - start,
.buf = str->buf + start,
};
}
Str8 *wapp_str8_alloc_concat(const Allocator *allocator, Str8 *dst, Str8RO *src) {
if (!allocator || !dst || !src) {
return NULL;
}
Str8 *output = NULL;
u64 remaining = dst->capacity - dst->size;
if (src->size <= remaining) {
output = dst;
goto COPY_STRING_STR8_CONCAT;
}
u64 capacity = dst->capacity + src->size;
output = wapp_str8_alloc_buf(allocator, capacity);
if (!output) {
goto RETURN_STR8_CONCAT;
}
wapp_str8_concat_capped(output, dst);
COPY_STRING_STR8_CONCAT:
wapp_str8_concat_capped(output, src);
RETURN_STR8_CONCAT:
return output;
}
void wapp_str8_concat_capped(Str8 *dst, Str8RO *src) {
if (!dst || !src) {
return;
}
u64 remaining = dst->capacity - dst->size;
u64 to_copy = remaining < src->size ? remaining : src->size;
memcpy(dst->buf + dst->size, src->buf, to_copy);
dst->size += to_copy;
}
void wapp_str8_copy_cstr_capped(Str8 *dst, const char *src) {
if (!dst || !src) {
return;
}
u64 length = strlen(src);
u64 to_copy = length <= dst->capacity ? length : dst->capacity;
memset(dst->buf, 0, dst->size);
memcpy(dst->buf, src, to_copy);
dst->size = to_copy;
}
void wapp_str8_copy_str8_capped(Str8 *dst, Str8RO *src) {
if (!dst || !src) {
return;
}
u64 to_copy = src->size <= dst->capacity ? src->size : dst->capacity;
memset(dst->buf, 0, dst->size);
memcpy(dst->buf, src->buf, to_copy);
dst->size = to_copy;
}
void wapp_str8_copy_to_cstr(char *dst, Str8RO *src, u64 dst_capacity) {
if (!dst || !src) {
return;
}
u64 to_copy = src->size < dst_capacity ? src->size : dst_capacity - 1;
memset(dst, 0, dst_capacity);
memcpy(dst, src->buf, to_copy);
}
void wapp_str8_format(Str8 *dst, const char *format, ...) {
if (!dst || !format) {
return;
}
va_list args1;
va_list args2;
va_start(args1, format);
va_copy(args2, args1);
u64 total_size = vsnprintf(NULL, 0, format, args1);
dst->size = total_size <= dst->capacity ? total_size : dst->capacity;
vsnprintf((char *)(dst->buf), dst->capacity, format, args2);
va_end(args1);
va_end(args2);
}
i64 wapp_str8_find(Str8RO *str, Str8RO substr) {
if (!str || substr.size > str->size) {
return -1;
}
// NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of
// MSVC Spectre mitigation warnings
u64 char_index = 0;
bool running = true;
while (running) {
const c8 *sub = str->buf + char_index;
if (memcmp(sub, substr.buf, substr.size) == 0) {
return char_index;
}
++char_index;
running = char_index < str->size;
}
return -1;
}
i64 wapp_str8_rfind(Str8RO *str, Str8RO substr) {
if (!str || substr.size > str->size) {
return -1;
}
// NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of
// MSVC Spectre mitigation warnings
i64 char_index = str->size - substr.size;
bool running = true;
while (running) {
const c8 *sub = str->buf + char_index;
if (memcmp(sub, substr.buf, substr.size) == 0) {
return char_index;
}
--char_index;
running = char_index >= 0;
}
return -1;
}
Str8List *wapp_str8_split_with_max(const Allocator *allocator, Str8RO *str, Str8RO *delimiter, i64 max_splits) {
if (!allocator || !str || !delimiter) {
return NULL;
}
Str8List *output = wapp_mem_allocator_alloc(allocator, sizeof(Str8List));
if (delimiter->size > str->size) {
Str8 *full = wapp_str8_alloc_str8(allocator, str);
Str8Node *node = wapp_mem_allocator_alloc(allocator, sizeof(Str8Node));
if (node) {
node->item = full;
wapp_str8_list_push_back(output, node);
}
goto RETURN_STR8_SPLIT;
}
i64 start = 0;
i64 end = 0;
i64 splits = 0;
Str8 *rest = wapp_str8_alloc_str8(allocator, str);
Str8 *before_str;
while ((end = wapp_str8_find(rest, *delimiter)) != -1) {
if (max_splits > 0 && splits >= max_splits) {
break;
}
before_str = wapp_str8_alloc_substr(allocator, str, start, start + end);
Str8Node *node = wapp_mem_allocator_alloc(allocator, sizeof(Str8Node));
if (node) {
node->item = before_str;
wapp_str8_list_push_back(output, node);
}
wapp_mem_allocator_free(allocator, (void **)&rest, sizeof(Str8));
rest = wapp_str8_alloc_substr(allocator, str, start + end + delimiter->size, str->size);
start += end + delimiter->size;
++splits;
}
// Ensure the last part of the string after the delimiter is added to the list
rest = wapp_str8_alloc_substr(allocator, str, start, str->size);
Str8Node *node = wapp_mem_allocator_alloc(allocator, sizeof(Str8Node));
if (node) {
node->item = rest;
wapp_str8_list_push_back(output, node);
}
RETURN_STR8_SPLIT:
return output;
}
Str8List *wapp_str8_rsplit_with_max(const Allocator *allocator, Str8RO *str, Str8RO *delimiter, i64 max_splits) {
if (!allocator || !str || !delimiter) {
return NULL;
}
Str8List *output = wapp_mem_allocator_alloc(allocator, sizeof(Str8List));
if (delimiter->size > str->size) {
Str8 *full = wapp_str8_alloc_str8(allocator, str);
Str8Node *node = wapp_mem_allocator_alloc(allocator, sizeof(Str8Node));
if (node) {
node->item = full;
wapp_str8_list_push_back(output, node);
}
goto RETURN_STR8_SPLIT;
}
i64 end = 0;
i64 splits = 0;
Str8 *rest = wapp_str8_alloc_str8(allocator, str);
Str8 *after_str;
while ((end = wapp_str8_rfind(rest, *delimiter)) != -1) {
if (max_splits > 0 && splits >= max_splits) {
break;
}
after_str = wapp_str8_alloc_substr(allocator, rest, end + delimiter->size, str->size);
Str8Node *node = wapp_mem_allocator_alloc(allocator, sizeof(Str8Node));
if (node) {
node->item = after_str;
wapp_str8_list_push_front(output, node);
}
wapp_mem_allocator_free(allocator, (void **)&rest, sizeof(Str8));
rest = wapp_str8_alloc_substr(allocator, rest, 0, end);
++splits;
}
rest = wapp_str8_alloc_substr(allocator, str, 0, rest->size);
Str8Node *node = wapp_mem_allocator_alloc(allocator, sizeof(Str8Node));
if (node) {
node->item = rest;
wapp_str8_list_push_front(output, node);
}
RETURN_STR8_SPLIT:
return output;
}
Str8 *wapp_str8_join(const Allocator *allocator, const Str8List *list, Str8RO *delimiter) {
if (!allocator || !list || !delimiter) {
return NULL;
}
u64 capacity = wapp_str8_list_total_size(list) + (delimiter->size * (list->node_count - 1));
Str8 *output = wapp_str8_alloc_buf(allocator, capacity * 2);
// NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of
// MSVC Spectre mitigation warnings
Str8Node *node;
u64 node_index = 0;
bool running = true;
while (running) {
node = wapp_str8_list_get(list, node_index);
wapp_str8_concat_capped(output, node->item);
if (node_index + 1 < list->node_count) {
wapp_str8_concat_capped(output, delimiter);
}
++node_index;
running = node_index < list->node_count;
}
return output;
}
u64 wapp_str8_list_total_size(const Str8List *list) {
if (!list) {
return 0;
}
u64 output = 0;
for (u64 i = 0; i < list->node_count; ++i) {
Str8Node *node = wapp_str8_list_get(list, i);
output += node->item->size;
}
return output;
}

View File

@ -0,0 +1,102 @@
#ifndef STR8_H
#define STR8_H
#include "../../../common/aliases/aliases.h"
#include "../../../containers/dbl_list/dbl_list.h"
#include "../../mem/allocator/mem_allocator.h"
#include <string.h>
#include <stdbool.h>
#ifdef __cplusplus
BEGIN_C_LINKAGE
#endif // !__cplusplus
typedef struct str8 Str8;
struct str8 {
u64 capacity;
u64 size;
c8 *buf;
};
typedef const Str8 Str8RO;
/**
* Utilities to be used with printf functions
*/
#define WAPP_STR8_SPEC "%.*s"
#define wapp_str8_varg(STRING) (int)((STRING).size), (STRING).buf
/**
* Str8 stack buffers
*/
#define wapp_str8_buf(CAPACITY) ((Str8){.capacity = CAPACITY, .size = 0, .buf = (c8[CAPACITY]){0}})
// Utilises the fact that memcpy returns pointer to dest buffer and that getting
// address of compound literals is valid in C to create a string on the stack
#define wapp_str8_lit(STRING) ((Str8){.capacity = (sizeof(STRING) - 1) * 2, \
.size = sizeof(STRING) - 1, \
.buf = memcpy(&((c8 [sizeof(STRING) * 2]){0}), STRING, sizeof(STRING))})
#define wapp_str8_lit_ro(STRING) ((Str8RO){.capacity = sizeof(STRING) - 1, \
.size = sizeof(STRING) - 1, \
.buf = (c8 *)STRING})
// To be used only when initialising a static storage variable in compilers that don't support
// initialisers with the syntax of wapp_str8_lit_ro (e.g. gcc). Should only be used when necessary
// and only be assigned to a Str8RO variable to avoid any attempt at modifying the string
#define wapp_str8_lit_ro_initialiser_list(STRING) {.capacity = sizeof(STRING) - 1, \
.size = sizeof(STRING) - 1, \
.buf = (c8 *)STRING}
/**
* Str8 allocated buffers
*/
Str8 *wapp_str8_alloc_buf(const Allocator *allocator, u64 capacity);
Str8 *wapp_str8_alloc_cstr(const Allocator *allocator, const char *str);
Str8 *wapp_str8_alloc_str8(const Allocator *allocator, Str8RO *str);
Str8 *wapp_str8_alloc_substr(const Allocator *allocator, Str8RO *str, u64 start, u64 end);
Str8 *wapp_str8_alloc_concat(const Allocator *allocator, Str8 *dst, Str8RO *src);
// Only needed for allocators like malloc where each allocation has to be freed on its own.
// No need to use it for allocators like Arena.
void wapp_str8_dealloc_buf(const Allocator *allocator, Str8 **str);
/**
* Str8 utilities
*/
c8 wapp_str8_get(Str8RO *str, u64 index);
void wapp_str8_set(Str8 *str, u64 index, c8 c);
void wapp_str8_push_back(Str8 *str, c8 c);
bool wapp_str8_equal(Str8RO *s1, Str8RO *s2);
bool wapp_str8_equal_to_count(Str8RO* s1, Str8RO* s2, u64 count);
Str8 wapp_str8_slice(Str8RO *str, u64 start, u64 end);
void wapp_str8_concat_capped(Str8 *dst, Str8RO *src);
void wapp_str8_copy_cstr_capped(Str8 *dst, const char *src);
void wapp_str8_copy_str8_capped(Str8 *dst, Str8RO *src);
void wapp_str8_copy_to_cstr(char *dst, Str8RO *src, u64 dst_capacity);
void wapp_str8_format(Str8 *dst, const char *format, ...);
/**
* Str8 find functions
*/
i64 wapp_str8_find(Str8RO *str, Str8RO substr);
i64 wapp_str8_rfind(Str8RO *str, Str8RO substr);
/**
* Str8 split and join
*/
#define wapp_str8_split(ALLOCATOR, STR, DELIMITER) wapp_str8_split_with_max(ALLOCATOR, STR, DELIMITER, -1)
#define wapp_str8_rsplit(ALLOCATOR, STR, DELIMITER) wapp_str8_rsplit_with_max(ALLOCATOR, STR, DELIMITER, -1)
Str8List *wapp_str8_split_with_max(const Allocator *allocator, Str8RO *str, Str8RO *delimiter, i64 max_splits);
Str8List *wapp_str8_rsplit_with_max(const Allocator *allocator, Str8RO *str, Str8RO *delimiter, i64 max_splits);
Str8 *wapp_str8_join(const Allocator *allocator, const Str8List *list, Str8RO *delimiter);
/**
* Str8 list utilities
*/
#define wapp_str8_node_from_cstr(STRING) wapp_str8_list_node(&wapp_str8_lit(STRING))
#define wapp_str8_node_from_str8(STRING) wapp_str8_list_node(&(STRING))
u64 wapp_str8_list_total_size(const Str8List *list);
#ifdef __cplusplus
END_C_LINKAGE
#endif // !__cplusplus
#endif // !STR8_H

22
src/core/wapp_core.c Normal file
View File

@ -0,0 +1,22 @@
#ifndef WAPP_CORE_C
#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"
#include "os/shell/commander/posix/commander_posix.c"
#include "os/shell/commander/win/commander_win.c"
#include "os/shell/commander/commander.c"
#include "os/cpath/cpath.c"
#include "os/mem/posix/mem_os_posix.c"
#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"
#endif // !WAPP_CORE_C

22
src/core/wapp_core.h Normal file
View File

@ -0,0 +1,22 @@
#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"
#include "os/shell/commander/commander_output.h"
#include "os/shell/utils/shell_utils.h"
#include "os/cpath/cpath.h"
#include "os/mem/posix/mem_os_posix.h"
#include "os/mem/win/mem_os_win.h"
#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"
#endif // !WAPP_CORE_H

6
src/prng/wapp_prng.c Normal file
View File

@ -0,0 +1,6 @@
#ifndef WAPP_PRNG_C
#define WAPP_PRNG_C
#include "xorshift/xorshift.c"
#endif // !WAPP_PRNG_C

7
src/prng/wapp_prng.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef WAPP_PRNG_H
#define WAPP_PRNG_H
#include "xorshift/xorshift.h"
#include "../common/wapp_common.h"
#endif // !WAPP_PRNG_H

View File

@ -0,0 +1,89 @@
#include "xorshift.h"
#include "../../common/aliases/aliases.h"
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
typedef struct split_mix_64_state SplitMix64State;
struct split_mix_64_state {
u64 seed;
};
internal u64 rol64(u64 x, u64 bits);
internal u64 split_mix_64(SplitMix64State *state);
XOR256State wapp_prng_xorshift_init_state(void) {
persistent bool seeded = false;
if (!seeded) {
seeded = true;
struct timespec ts = {0};
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
srand48(ts.tv_nsec);
}
SplitMix64State sm64 = {.seed = lrand48()};
return (XOR256State){
.x = split_mix_64(&sm64),
.y = split_mix_64(&sm64),
.z = split_mix_64(&sm64),
.w = split_mix_64(&sm64),
};
}
u64 wapp_prng_xorshift_256(XOR256State *state) {
u64 t = state->x ^ (state->x << 11);
state->x = state->y;
state->y = state->z;
state->z = state->w;
state->w = (state->w ^ (state->w >> 19)) ^ (t ^ (t >> 8));
return state->w;
}
u64 wapp_prng_xorshift_256ss(XOR256State *state) {
const u64 result = rol64(state->z * 5, 7) * 9;
const u64 t = state->z << 17;
state->y ^= state->w;
state->x ^= state->z;
state->z ^= state->y;
state->w ^= state->x;
state->y ^= t;
state->x = rol64(state->x, 45);
return result;
}
u64 wapp_prng_xorshift_256p(XOR256State *state) {
const u64 result = state->w + state->x;
const u64 t = state->z << 17;
state->y ^= state->w;
state->x ^= state->z;
state->z ^= state->y;
state->w ^= state->x;
state->y ^= t;
state->x = rol64(state->x, 45);
return result;
}
internal u64 rol64(u64 x, u64 bits) {
return (x << bits) | (x >> (64 - bits));
}
internal u64 split_mix_64(SplitMix64State *state) {
state->seed += 0x9E3779B97f4A7C15;
u64 result = state->seed;
result = (result ^ (result >> 30)) * 0xBF58476D1CE4E5B9;
result = (result ^ (result >> 27)) * 0x94D049BB133111EB;
return result ^ (result >> 31);
}

View File

@ -0,0 +1,27 @@
#ifndef XORSHIFT_H
#define XORSHIFT_H
#include "../../common/aliases/aliases.h"
#ifdef __cplusplus
BEGIN_C_LINKAGE
#endif // __cplusplus
typedef struct xor_256_state XOR256State;
struct xor_256_state {
u64 x;
u64 y;
u64 z;
u64 w;
};
XOR256State wapp_prng_xorshift_init_state(void);
u64 wapp_prng_xorshift_256(XOR256State *state);
u64 wapp_prng_xorshift_256ss(XOR256State *state);
u64 wapp_prng_xorshift_256p(XOR256State *state);
#ifdef __cplusplus
END_C_LINKAGE
#endif // __cplusplus
#endif // !XORSHIFT_H

View File

@ -0,0 +1,53 @@
#include "tester.h"
#include "../../common/aliases/aliases.h"
#include "../../core/os/shell/termcolour/termcolour.h"
#include "../../core/strings/str8/str8.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
internal void handle_test_result(TestFuncResult result);
void run_tests(TestFunc *func1, ...) {
printf("\n");
handle_test_result(func1());
va_list args;
va_start(args, func1);
TestFunc *func = va_arg(args, TestFunc *);
while (func) {
TestFuncResult result = func();
handle_test_result(result);
func = va_arg(args, TestFunc *);
}
va_end(args);
printf("\n");
}
internal void handle_test_result(TestFuncResult result) {
TerminalColour colour;
Str8 result_text = wapp_str8_buf(64);
if (result.passed) {
colour = WAPP_TERM_COLOUR_FG_BR_GREEN;
wapp_str8_copy_cstr_capped(&result_text, "PASSED");
} else {
colour = WAPP_TERM_COLOUR_FG_BR_RED;
wapp_str8_copy_cstr_capped(&result_text, "FAILED");
}
printf("[");
wapp_shell_termcolour_print_text(&result_text, colour);
wapp_shell_termcolour_clear_colour();
printf("] " WAPP_STR8_SPEC "\n", wapp_str8_varg(result.name));
if (!result.passed) {
exit(EXIT_FAILURE);
}
}

View File

@ -0,0 +1,34 @@
#ifndef TESTER_H
#define TESTER_H
#include "../../common/misc/misc_utils.h"
#include "../../common/platform/platform.h"
#include "../../core/strings/str8/str8.h"
#include <stdbool.h>
#ifdef __cplusplus
BEGIN_C_LINKAGE
#endif // __cplusplus
#define wapp_tester_result(PASSED) ((TestFuncResult){.name = wapp_str8_lit_ro(__func__), .passed = PASSED})
#define wapp_tester_run_tests(...) run_tests(__VA_ARGS__, NULL)
typedef struct test_func_result TestFuncResult;
struct test_func_result {
Str8RO name;
bool passed;
#ifdef WAPP_PLATFORM_WINDOWS
wapp_misc_utils_padding_size(sizeof(Str8RO) + sizeof(bool));
#endif // WAPP_PLATFORM_WINDOWS
};
typedef TestFuncResult(TestFunc)(void);
void run_tests(TestFunc *func1, ...);
#ifdef __cplusplus
END_C_LINKAGE
#endif // __cplusplus
#endif // !TESTER_H

View File

@ -0,0 +1,8 @@
#ifndef WAPP_TESTING_C
#define WAPP_TESTING_C
#include "wapp_testing.h"
#include "tester/tester.c"
#include "../core/wapp_core.c"
#endif // !WAPP_TESTING_C

View File

@ -0,0 +1,8 @@
#ifndef WAPP_TESTING_H
#define WAPP_TESTING_H
#include "tester/tester.h"
#include "../common/wapp_common.h"
#include "../core/wapp_core.h"
#endif // !WAPP_TESTING_H

59
src/uuid/uuid.c Normal file
View File

@ -0,0 +1,59 @@
#include "uuid.h"
#include "../common/aliases/aliases.h"
#include "../core/strings/str8/str8.h"
#include "../prng/xorshift/xorshift.h"
#include <stdbool.h>
#include <inttypes.h>
#define UUID_STR_FORMAT ("%.8" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.12" PRIx64)
typedef struct uuid4 UUID4;
struct uuid4 {
u64 high;
u64 low;
};
internal UUID4 generate_uuid4(void);
internal void uuid4_to_uuid(const UUID4* uuid4, UUID *uuid);
UUID *wapp_uuid_init_uuid4(UUID *uuid) {
if (!uuid) {
goto RETURN_INIT_UUID4;
}
UUID4 uuid4 = generate_uuid4();
uuid4_to_uuid(&uuid4, uuid);
RETURN_INIT_UUID4:
return uuid;
}
internal UUID4 generate_uuid4(void) {
persistent XOR256State state = {0};
persistent bool initialised = false;
if (!initialised) {
initialised = true;
state = wapp_prng_xorshift_init_state();
}
UUID4 uuid = (UUID4){
.high = wapp_prng_xorshift_256(&state),
.low = wapp_prng_xorshift_256(&state),
};
uuid.high = (uuid.high & 0xffffffffffff0fff) | 0x0000000000004000;
uuid.low = (uuid.low & 0x3fffffffffffffff) | 0x8000000000000000;
return uuid;
}
internal void uuid4_to_uuid(const UUID4* uuid4, UUID *uuid) {
u64 grp1 = uuid4->high >> 32;
u64 grp2 = (uuid4->high << 32) >> 48;
u64 grp3 = (uuid4->high << 48) >> 48;
u64 grp4 = uuid4->low >> 48;
u64 grp5 = (uuid4->low << 16) >> 16;
wapp_str8_format(&(uuid->uuid), UUID_STR_FORMAT, grp1, grp2, grp3, grp4, grp5);
}

34
src/uuid/uuid.h Normal file
View File

@ -0,0 +1,34 @@
#ifndef UUID_H
#define UUID_H
#include "../common/aliases/aliases.h"
#include "../core/strings/str8/str8.h"
#ifdef __cplusplus
BEGIN_C_LINKAGE
#endif // __cplusplus
#define UUID_BUF_LENGTH 48
#define WAPP_UUID_SPEC WAPP_STR8_SPEC
#define wapp_uuid_varg(UUID) wapp_str8_varg((UUID).uuid)
typedef struct uuid UUID;
struct uuid {
Str8 uuid;
};
#define wapp_uuid_gen_uuid4() *(wapp_uuid_init_uuid4(&wapp_uuid_create()))
/* Low level UUID API */
#define wapp_uuid_create() ((UUID){.uuid = wapp_str8_buf(UUID_BUF_LENGTH)})
// Just returns the same pointer that was passed in with the UUID initialised.
// If the pointer is NULL, it skips initialisation.
UUID *wapp_uuid_init_uuid4(UUID *uuid);
#ifdef __cplusplus
END_C_LINKAGE
#endif // __cplusplus
#endif // !UUID_H

8
src/uuid/wapp_uuid.c Normal file
View File

@ -0,0 +1,8 @@
#ifndef WAPP_UUID_C
#define WAPP_UUID_C
#include "uuid.c"
#include "../core/wapp_core.c"
#include "../prng/wapp_prng.c"
#endif // !WAPP_UUID_C

9
src/uuid/wapp_uuid.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef WAPP_UUID_H
#define WAPP_UUID_H
#include "uuid.h"
#include "../common/wapp_common.h"
#include "../core/wapp_core.h"
#include "../prng/wapp_prng.h"
#endif // !WAPP_UUID_H

11
src/wapp.c Normal file
View File

@ -0,0 +1,11 @@
#ifndef WAPP_C
#define WAPP_C
#include "wapp.h"
#include "containers/wapp_containers.c"
#include "core/wapp_core.c"
#include "prng/wapp_prng.c"
#include "uuid/uuid.c"
#include "testing/wapp_testing.c"
#endif // !WAPP_C

11
src/wapp.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef WAPP_H
#define WAPP_H
#include "common/wapp_common.h"
#include "containers/wapp_containers.h"
#include "core/wapp_core.h"
#include "prng/wapp_prng.h"
#include "uuid/wapp_uuid.h"
#include "testing/wapp_testing.h"
#endif // !WAPP_H

View File

@ -0,0 +1,18 @@
#include "test_allocator.h"
#include "wapp.h"
#include <stdbool.h>
#include <stdlib.h>
TestFuncResult test_arena_allocator(void) {
Allocator allocator = wapp_mem_arena_allocator_init(4096);
bool result = allocator.obj != NULL && allocator.alloc != NULL &&
allocator.alloc_aligned != NULL &&
allocator.realloc != NULL && allocator.realloc_aligned != NULL &&
allocator.free == NULL;
void *ptr = wapp_mem_allocator_alloc(&allocator, 20);
result = result && (ptr != NULL);
wapp_mem_arena_allocator_destroy(&allocator);
return wapp_tester_result(result);
}

View File

@ -0,0 +1,16 @@
#ifndef TEST_ALLOCATOR_H
#define TEST_ALLOCATOR_H
#include "wapp.h"
#ifdef __cplusplus
BEGIN_C_LINKAGE
#endif // __cplusplus
TestFuncResult test_arena_allocator(void);
#ifdef __cplusplus
END_C_LINKAGE
#endif // __cplusplus
#endif // !TEST_ALLOCATOR_H

112
tests/arena/test_arena.c Normal file
View File

@ -0,0 +1,112 @@
#include "test_arena.h"
#include "wapp.h"
#include <stdbool.h>
#include <stdlib.h>
#define ARENA_CAPACITY KB(16)
internal Arena *arena = NULL;
internal i32 count = 20;
internal i32 *array = NULL;
TestFuncResult test_arena_init(void) {
bool result = wapp_mem_arena_init(&arena, ARENA_CAPACITY);
return wapp_tester_result(result);
}
TestFuncResult test_arena_init_succeeds_when_reserving_very_large_size(void) {
Arena *large_arena = NULL;
u64 capacity = GB(512);
bool result = wapp_mem_arena_init(&large_arena, capacity);
if (result) {
wapp_mem_arena_destroy(&large_arena);
}
return wapp_tester_result(result);
}
TestFuncResult test_arena_alloc_succeeds_when_within_capacity(void) {
array = wapp_mem_arena_alloc(arena, count * sizeof(i32));
bool result = array != NULL;
for (i32 i = 0; i < count; ++i) {
array[i] = i * 10;
}
return wapp_tester_result(result);
}
TestFuncResult test_arena_alloc_fails_when_over_capacity(void) {
u8 *bytes = wapp_mem_arena_alloc(arena, ARENA_CAPACITY * 2);
bool result = bytes == NULL;
return wapp_tester_result(result);
}
TestFuncResult test_arena_realloc_bigger_size(void) {
u64 old_count = 10;
u64 new_count = 20;
i32 *bytes = wapp_mem_arena_alloc(arena, old_count * sizeof(i32));
for (u64 i = 0; i < old_count; ++i) {
bytes[i] = (i32)i;
}
i32 *new_bytes = wapp_mem_arena_realloc(arena, bytes, old_count * sizeof(i32), new_count * sizeof(i32));
if (!new_bytes) {
return wapp_tester_result(false);
}
for (u64 i = 0; i < new_count; ++i) {
if (i < old_count && new_bytes[i] != bytes[i]) {
return wapp_tester_result(false);
}
}
return wapp_tester_result(true);
}
TestFuncResult test_arena_realloc_smaller_size(void) {
u64 old_count = 10;
u64 new_count = 5;
i32 *bytes = wapp_mem_arena_alloc(arena, old_count * sizeof(i32));
for (u64 i = 0; i < old_count; ++i) {
bytes[i] = (i32)i;
}
i32 *new_bytes = wapp_mem_arena_realloc(arena, bytes, old_count * sizeof(i32), new_count * sizeof(i32));
if (!new_bytes) {
return wapp_tester_result(false);
}
for (u64 i = 0; i < new_count; ++i) {
if (i < new_count && new_bytes[i] != bytes[i]) {
return wapp_tester_result(false);
}
}
return wapp_tester_result(true);
}
TestFuncResult test_arena_clear(void) {
wapp_mem_arena_clear(arena);
bool result = true;
for (i32 i = 0; i < count; ++i) {
if (array[i] != 0) {
result = false;
break;
}
}
return wapp_tester_result(result);
}
TestFuncResult test_arena_destroy(void) {
wapp_mem_arena_destroy(&arena);
bool result = arena == NULL;
return wapp_tester_result(result);
}

23
tests/arena/test_arena.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef TEST_ARENA_H
#define TEST_ARENA_H
#include "wapp.h"
#ifdef __cplusplus
BEGIN_C_LINKAGE
#endif // __cplusplus
TestFuncResult test_arena_init(void);
TestFuncResult test_arena_init_succeeds_when_reserving_very_large_size(void);
TestFuncResult test_arena_alloc_succeeds_when_within_capacity(void);
TestFuncResult test_arena_alloc_fails_when_over_capacity(void);
TestFuncResult test_arena_realloc_bigger_size(void);
TestFuncResult test_arena_realloc_smaller_size(void);
TestFuncResult test_arena_clear(void);
TestFuncResult test_arena_destroy(void);
#ifdef __cplusplus
END_C_LINKAGE
#endif // __cplusplus
#endif // !TEST_ARENA_H

182
tests/cpath/test_cpath.c Normal file
View File

@ -0,0 +1,182 @@
#include "test_cpath.h"
#include "wapp.h"
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#define MAIN_BUF_SIZE 4096
#define TMP_BUF_SIZE 1024
TestFuncResult test_cpath_join_path(void) {
bool result;
Str8 expected = wapp_str8_buf(MAIN_BUF_SIZE);
Str8 out = wapp_str8_buf(MAIN_BUF_SIZE);
Str8 tmp = wapp_str8_buf(TMP_BUF_SIZE);
wapp_str8_format(&expected, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP);
wapp_str8_format(&tmp, "%c", PATH_SEP);
Str8List parts = {0};
wapp_str8_list_push_back(&parts, &wapp_str8_node_from_str8(tmp));
wapp_str8_list_push_back(&parts, &wapp_str8_node_from_cstr("home"));
wapp_str8_list_push_back(&parts, &wapp_str8_node_from_cstr("abdelrahman"));
wapp_str8_list_push_back(&parts, &wapp_str8_node_from_cstr("Documents"));
wapp_cpath_join_path(&out, &parts);
result = wapp_str8_equal(&out, &expected);
wapp_str8_list_pop_front(&parts);
wapp_str8_format(&expected, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP);
wapp_cpath_join_path(&out, &parts);
result = result && wapp_str8_equal(&out, &expected);
wapp_str8_concat_capped(&tmp, &wapp_str8_lit_ro("home"));
wapp_str8_list_pop_front(&parts);
wapp_str8_list_push_front(&parts, &wapp_str8_node_from_str8(tmp));
wapp_str8_format(&expected, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP);
wapp_cpath_join_path(&out, &parts);
result = result && wapp_str8_equal(&out, &expected);
wapp_str8_format(&tmp, "home%c", PATH_SEP);
wapp_str8_list_pop_front(&parts);
wapp_str8_list_push_front(&parts, &wapp_str8_node_from_cstr("home"));
wapp_str8_format(&expected, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP);
wapp_cpath_join_path(&out, &parts);
result = result && wapp_str8_equal(&out, &expected);
wapp_str8_list_empty(&parts);
wapp_str8_format(&tmp, "%chome", PATH_SEP);
wapp_str8_list_push_back(&parts, &wapp_str8_node_from_str8(tmp));
wapp_str8_list_push_back(&parts, &wapp_str8_node_from_cstr(""));
wapp_str8_format(&expected, "%chome", PATH_SEP);
wapp_cpath_join_path(&out, &parts);
result = result && wapp_str8_equal(&out, &expected);
wapp_str8_list_pop_front(&parts);
wapp_str8_list_push_back(&parts, &wapp_str8_node_from_cstr(""));
wapp_str8_format(&expected, "%s", "");
wapp_cpath_join_path(&out, &parts);
result = result && wapp_str8_equal(&out, &expected);
wapp_str8_list_pop_back(&parts);
wapp_str8_list_push_back(&parts, &wapp_str8_node_from_cstr("home"));
wapp_str8_copy_cstr_capped(&expected, "home");
wapp_cpath_join_path(&out, &parts);
result = result && wapp_str8_equal(&out, &expected);
return wapp_tester_result(result);
}
TestFuncResult test_cpath_dirname(void) {
Allocator arena = wapp_mem_arena_allocator_init(MB(8));
if (wapp_mem_allocator_invalid(&arena)) {
return wapp_tester_result(false);
}
bool result;
Str8 *output = NULL;
Str8 expected = wapp_str8_buf(MAIN_BUF_SIZE);
Str8 tmp = wapp_str8_buf(TMP_BUF_SIZE);
// CASE 1
wapp_str8_format(&tmp, "%c", PATH_SEP);
wapp_str8_format(&expected, "%c", PATH_SEP);
output = wapp_cpath_dirname(&arena, &tmp);
result = output != NULL && wapp_str8_equal(output, &expected);
// CASE 2
wapp_str8_format(&expected, "%s", ".");
output = wapp_cpath_dirname(&arena, &wapp_str8_lit("home"));
result = result && output != NULL && wapp_str8_equal(output, &expected);
// CASE 3
output = wapp_cpath_dirname(&arena, &wapp_str8_lit(""));
result = result && output != NULL && wapp_str8_equal(output, &expected);
// CASE 4
wapp_str8_format(&tmp, "%chome%ctest", PATH_SEP, PATH_SEP);
wapp_str8_format(&expected, "%chome", PATH_SEP);
output = wapp_cpath_dirname(&arena, &tmp);
result = result && output != NULL && wapp_str8_equal(output, &expected);
// CASE 5
wapp_str8_format(&tmp, "%chome%ctest%c", PATH_SEP, PATH_SEP, PATH_SEP);
wapp_str8_format(&expected, "%chome", PATH_SEP);
output = wapp_cpath_dirname(&arena, &tmp);
result = result && output != NULL && wapp_str8_equal(output, &expected);
wapp_mem_arena_allocator_destroy(&arena);
return wapp_tester_result(result);
}
TestFuncResult test_cpath_dirup(void) {
Allocator arena = wapp_mem_arena_allocator_init(MB(8));
if (wapp_mem_allocator_invalid(&arena)) {
return wapp_tester_result(false);
}
bool result;
Str8 *output = NULL;
Str8 expected = wapp_str8_buf(MAIN_BUF_SIZE);
Str8 tmp = wapp_str8_buf(TMP_BUF_SIZE);
// CASE 1
wapp_str8_format(&tmp, "%c", PATH_SEP);
wapp_str8_format(&expected, "%c", PATH_SEP);
output = wapp_cpath_dirup(&arena, &tmp, 3);
result = output != NULL && wapp_str8_equal(output, &expected);
// CASE 2
wapp_str8_format(&tmp, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP);
wapp_str8_format(&expected, "%c", PATH_SEP);
output = wapp_cpath_dirup(&arena, &tmp, 3);
result = result && output != NULL && wapp_str8_equal(output, &expected);
// CASE 3
wapp_str8_format(&tmp, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP);
wapp_str8_copy_cstr_capped(&expected, ".");
output = wapp_cpath_dirup(&arena, &tmp, 3);
result = result && output != NULL && wapp_str8_equal(output, &expected);
// CASE 4
wapp_str8_format(&tmp, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP);
wapp_str8_format(&expected, "%chome", PATH_SEP);
output = wapp_cpath_dirup(&arena, &tmp, 2);
result = result && output != NULL && wapp_str8_equal(output, &expected);
// CASE 5
wapp_str8_format(&tmp, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP);
wapp_str8_copy_cstr_capped(&expected, "home");
output = wapp_cpath_dirup(&arena, &tmp, 2);
result = result && output != NULL && wapp_str8_equal(output, &expected);
wapp_mem_arena_allocator_destroy(&arena);
return wapp_tester_result(result);
}

18
tests/cpath/test_cpath.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef TEST_CPATH_H
#define TEST_CPATH_H
#include "wapp.h"
#ifdef __cplusplus
BEGIN_C_LINKAGE
#endif // __cplusplus
TestFuncResult test_cpath_join_path(void);
TestFuncResult test_cpath_dirname(void);
TestFuncResult test_cpath_dirup(void);
#ifdef __cplusplus
END_C_LINKAGE
#endif // __cplusplus
#endif // !TEST_CPATH_H

View File

@ -0,0 +1,63 @@
#include "test_shell_commander.h"
#include "wapp.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
TestFuncResult test_commander_cmd_success(void) {
Str8List cmd = {0};
wapp_str8_list_push_back(&cmd, &wapp_str8_node_from_cstr("echo"));
wapp_str8_list_push_back(&cmd, &wapp_str8_node_from_cstr("hello world"));
CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_DISCARD, NULL, &cmd);
bool succeeded = result.exited && result.exit_code == EXIT_SUCCESS &&
result.error == SHELL_ERR_NO_ERROR;
return wapp_tester_result(succeeded);
}
TestFuncResult test_commander_cmd_failure(void) {
Str8List cmd = {0};
wapp_str8_list_push_back(&cmd, &wapp_str8_node_from_cstr("grep"));
CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_DISCARD, NULL, &cmd);
bool failed = result.exited && result.exit_code != EXIT_SUCCESS &&
result.error == SHELL_ERR_NO_ERROR;
return wapp_tester_result(failed);
}
TestFuncResult test_commander_cmd_out_buf_success(void) {
Str8 buf = wapp_str8_buf(64);
Str8 expected = wapp_str8_buf(64);
char msg[] = "hello world";
wapp_str8_copy_cstr_capped(&expected, msg);
Str8List cmd = {0};
wapp_str8_list_push_back(&cmd, &wapp_str8_node_from_cstr("echo"));
wapp_str8_list_push_back(&cmd, &wapp_str8_node_from_cstr(msg));
CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, &buf, &cmd);
bool succeeded = result.exited && result.exit_code == EXIT_SUCCESS &&
result.error == SHELL_ERR_NO_ERROR && wapp_str8_equal_to_count(&buf, &expected, strlen(msg));
return wapp_tester_result(succeeded);
}
TestFuncResult test_commander_cmd_out_buf_failure(void) {
Str8 buf = wapp_str8_buf(4);
Str8 expected = wapp_str8_buf(64);
char msg[] = "hello world";
wapp_str8_copy_cstr_capped(&expected, msg);
Str8List cmd = {0};
wapp_str8_list_push_back(&cmd, &wapp_str8_node_from_cstr("echo"));
wapp_str8_list_push_back(&cmd, &wapp_str8_node_from_cstr(msg));
CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, &buf, &cmd);
bool failed = !result.exited && result.exit_code != EXIT_SUCCESS &&
result.error == SHELL_ERR_OUT_BUF_FULL && !wapp_str8_equal(&buf, &expected);
return wapp_tester_result(failed);
}

View File

@ -0,0 +1,19 @@
#ifndef TEST_SHELL_COMMANDER_H
#define TEST_SHELL_COMMANDER_H
#include "wapp.h"
#ifdef __cplusplus
BEGIN_C_LINKAGE
#endif // __cplusplus
TestFuncResult test_commander_cmd_success(void);
TestFuncResult test_commander_cmd_failure(void);
TestFuncResult test_commander_cmd_out_buf_success(void);
TestFuncResult test_commander_cmd_out_buf_failure(void);
#ifdef __cplusplus
END_C_LINKAGE
#endif // __cplusplus
#endif // !TEST_SHELL_COMMANDER_H

614
tests/str8/test_str8.c Normal file
View File

@ -0,0 +1,614 @@
#include "test_str8.h"
#include "wapp.h"
#include <stdbool.h>
#define ARRLEN(ARR) (sizeof(ARR) / sizeof(ARR[0]))
TestFuncResult test_str8_lit(void) {
bool result;
Str8 s1 = wapp_str8_lit("Hello world");
result = s1.capacity == 22 && s1.capacity != s1.size;
Str8 s2 = wapp_str8_lit("Different strokes for different folks");
result = result && s2.capacity == 74 && s2.capacity != s2.size;
Str8 s3 = wapp_str8_lit("Discretion is the better part of valour");
result = result && s3.capacity == 78 && s3.capacity != s3.size;
Str8 s4 = wapp_str8_lit("Distance lends enchantment to the view");
result = result && s4.capacity == 76 && s4.capacity != s4.size;
Str8 s5 = wapp_str8_lit("Do as I say, not as I do");
result = result && s5.capacity == 48 && s5.capacity != s5.size;
Str8 s6 = wapp_str8_lit("Do as you would be done by");
result = result && s6.capacity == 52 && s6.capacity != s6.size;
Str8 s7 = wapp_str8_lit("Do unto others as you would have them do to you");
result = result && s7.capacity == 94 && s7.capacity != s7.size;
return wapp_tester_result(result);
}
TestFuncResult test_str8_lit_ro(void) {
bool result;
Str8RO s1 = wapp_str8_lit_ro("Hello world");
result = s1.capacity == 11 && s1.capacity == s1.size;
Str8RO s2 = wapp_str8_lit_ro("Different strokes for different folks");
result = result && s2.capacity == 37 && s2.capacity == s2.size;
Str8RO s3 = wapp_str8_lit_ro("Discretion is the better part of valour");
result = result && s3.capacity == 39 && s3.capacity == s3.size;
Str8RO s4 = wapp_str8_lit_ro("Distance lends enchantment to the view");
result = result && s4.capacity == 38 && s4.capacity == s4.size;
Str8RO s5 = wapp_str8_lit_ro("Do as I say, not as I do");
result = result && s5.capacity == 24 && s5.capacity == s5.size;
Str8RO s6 = wapp_str8_lit_ro("Do as you would be done by");
result = result && s6.capacity == 26 && s6.capacity == s6.size;
Str8RO s7 = wapp_str8_lit_ro("Do unto others as you would have them do to you");
result = result && s7.capacity == 47 && s7.capacity == s7.size;
return wapp_tester_result(result);
}
TestFuncResult test_str8_buf(void) {
bool result;
Str8 s1 = wapp_str8_buf(1024);
result = s1.capacity == 1024 && s1.size == 0;
Str8 s2 = wapp_str8_buf(2048);
result = result && s2.capacity == 2048 && s2.size == 0;
Str8 s3 = wapp_str8_buf(4096);
result = result && s3.capacity == 4096 && s3.size == 0;
Str8 s4 = wapp_str8_buf(8192);
result = result && s4.capacity == 8192 && s4.size == 0;
return wapp_tester_result(result);
}
TestFuncResult test_str8_alloc_buf(void) {
bool result;
Allocator allocator = wapp_mem_arena_allocator_init(KB(100));
if (wapp_mem_allocator_invalid(&allocator)) {
return wapp_tester_result(false);
}
u64 capacity = 4096;
Str8 *s = wapp_str8_alloc_buf(&allocator, capacity);
if (!s) {
result = false;
goto TEST_ALLOC_BUF_CLEANUP;
}
result = s->capacity == capacity;
const char *cstr = "My name is Abdelrahman";
wapp_str8_copy_cstr_capped(s, cstr);
result = result && s->capacity == capacity && s->size == strlen(cstr) && memcmp(s->buf, cstr, s->size) == 0;
TEST_ALLOC_BUF_CLEANUP:
wapp_mem_arena_allocator_destroy(&allocator);
return wapp_tester_result(result);
}
TestFuncResult test_str8_alloc_cstr(void) {
bool result;
Allocator allocator = wapp_mem_arena_allocator_init(KB(100));
if (wapp_mem_allocator_invalid(&allocator)) {
return wapp_tester_result(false);
}
char *str = "Abdelrahman";
u64 length = strlen(str);
Str8 *s = wapp_str8_alloc_cstr(&allocator, str);
if (!s) {
return wapp_tester_result(false);
}
result = s->size == length && memcmp(s->buf, str, length) == 0;
wapp_mem_arena_allocator_destroy(&allocator);
return wapp_tester_result(result);
}
TestFuncResult test_str8_alloc_str8(void) {
bool result;
Allocator allocator = wapp_mem_arena_allocator_init(KB(100));
if (wapp_mem_allocator_invalid(&allocator)) {
return wapp_tester_result(false);
}
Str8 str = wapp_str8_lit("Abdelrahman");
Str8 *s = wapp_str8_alloc_str8(&allocator, &str);
if (!s) {
return wapp_tester_result(false);
}
result = wapp_str8_equal(s, &str);
wapp_mem_arena_allocator_destroy(&allocator);
return wapp_tester_result(result);
}
TestFuncResult test_str8_alloc_substr(void) {
bool result;
Allocator allocator = wapp_mem_arena_allocator_init(KB(100));
if (wapp_mem_allocator_invalid(&allocator)) {
return wapp_tester_result(false);
}
Str8 str = wapp_str8_lit("Abdelrahman");
Str8 *s = wapp_str8_alloc_substr(&allocator, &str, 3, 8);
if (!s) {
return wapp_tester_result(false);
}
result = s->size == 5 && memcmp(s->buf, "elrah", s->size) == 0;
wapp_mem_arena_allocator_destroy(&allocator);
return wapp_tester_result(result);
}
TestFuncResult test_str8_get_index_within_bounds(void) {
bool result;
Str8RO s1 = wapp_str8_lit_ro("Hello world");
result = wapp_str8_get(&s1, 4) == 'o';
Str8RO s2 = wapp_str8_lit_ro("Different strokes for different folks");
result = result && wapp_str8_get(&s2, 0) == 'D';
Str8RO s3 = wapp_str8_lit_ro("Discretion is the better part of valour");
result = result && wapp_str8_get(&s3, 13) == ' ';
Str8RO s4 = wapp_str8_lit_ro("Distance lends enchantment to the view");
result = result && wapp_str8_get(&s4, 20) == 'n';
Str8RO s5 = wapp_str8_lit_ro("Do as I say, not as I do");
result = result && wapp_str8_get(&s5, 11) == ',';
Str8RO s6 = wapp_str8_lit_ro("Do as you would be done by");
result = result && wapp_str8_get(&s6, 25) == 'y';
Str8RO s7 = wapp_str8_lit_ro("Do unto others as you would have them do to you");
result = result && wapp_str8_get(&s7, 16) == 's';
return wapp_tester_result(result);
}
TestFuncResult test_str8_get_index_out_of_bounds(void) {
Str8 s1 = wapp_str8_lit("Hello world");
bool result = wapp_str8_get(&s1, 20) == '\0';
return wapp_tester_result(result);
}
TestFuncResult test_str8_set(void) {
bool result;
Str8 s1 = wapp_str8_lit("Hello world");
wapp_str8_set(&s1, 4, 'f');
result = wapp_str8_get(&s1, 4) == 'f';
Str8 s2 = wapp_str8_lit("Different strokes for different folks");
wapp_str8_set(&s2, 0, 'A');
result = result && wapp_str8_get(&s2, 0) == 'A';
Str8 s3 = wapp_str8_lit("Discretion is the better part of valour");
wapp_str8_set(&s3, 13, 'u');
result = result && wapp_str8_get(&s3, 13) == 'u';
Str8 s4 = wapp_str8_lit("Distance lends enchantment to the view");
wapp_str8_set(&s4, 20, 'R');
result = result && wapp_str8_get(&s4, 20) == 'R';
Str8 s5 = wapp_str8_lit("Do as I say, not as I do");
wapp_str8_set(&s5, 11, '.');
result = result && wapp_str8_get(&s5, 11) == '.';
Str8 s6 = wapp_str8_lit("Do as you would be done by");
wapp_str8_set(&s6, 25, 'w');
result = result && wapp_str8_get(&s6, 25) == 'w';
Str8 s7 = wapp_str8_lit("Do unto others as you would have them do to you");
wapp_str8_set(&s7, 16, 'i');
result = result && wapp_str8_get(&s7, 16) == 'i';
return wapp_tester_result(result);
}
TestFuncResult test_str8_push_back(void) {
bool result;
Str8 expected = wapp_str8_lit("Abdelrahman");
Str8 buf = wapp_str8_buf(64);
wapp_str8_push_back(&buf, 'A');
wapp_str8_push_back(&buf, 'b');
wapp_str8_push_back(&buf, 'd');
wapp_str8_push_back(&buf, 'e');
wapp_str8_push_back(&buf, 'l');
wapp_str8_push_back(&buf, 'r');
wapp_str8_push_back(&buf, 'a');
wapp_str8_push_back(&buf, 'h');
wapp_str8_push_back(&buf, 'm');
wapp_str8_push_back(&buf, 'a');
wapp_str8_push_back(&buf, 'n');
result = wapp_str8_equal(&buf, &expected);
return wapp_tester_result(result);
}
TestFuncResult test_str8_equal(void) {
bool result;
Str8RO s1 = wapp_str8_lit_ro("hello");
Str8RO s2 = wapp_str8_lit_ro("hell");
Str8RO s3 = wapp_str8_lit_ro("hello");
Str8RO s4 = wapp_str8_lit_ro("goodbye");
result = wapp_str8_equal(&s1, &s2) == false;
result = result && wapp_str8_equal(&s1, &s3) == true;
result = result && wapp_str8_equal(&s1, &s4) == false;
return wapp_tester_result(result);
}
TestFuncResult test_str8_slice(void) {
bool result;
Str8 s = wapp_str8_lit("Different strokes for different folks");
Str8RO sub1 = wapp_str8_slice(&s, 3, 9);
result = sub1.size == 6 && sub1.capacity == 6;
Str8RO sub2 = wapp_str8_slice(&s, 18, 21);
result = result && sub2.size == 3 && sub2.capacity == 3;
Str8RO sub3 = wapp_str8_slice(&s, 5, 1);
result = result && sub3.size == 0 && sub3.capacity == 0;
Str8RO sub4 = wapp_str8_slice(&s, 70, 80);
result = result && sub4.size == 0 && sub4.capacity == 0;
return wapp_tester_result(result);
}
TestFuncResult test_str8_alloc_concat(void) {
bool result;
Allocator arena = wapp_mem_arena_allocator_init(KB(100));
Str8 str = wapp_str8_lit("Hello world");
Str8 suffix1 = wapp_str8_lit(" from me.");
Str8 suffix2 = wapp_str8_lit(" This is my code.");
Str8 concat1 = wapp_str8_lit("Hello world from me.");
Str8 concat2 = wapp_str8_lit("Hello world from me. This is my code.");
Str8 *output;
output = wapp_str8_alloc_concat(&arena, &str, &suffix1);
result = output->size == concat1.size && wapp_str8_equal(output, &concat1);
output = wapp_str8_alloc_concat(&arena, output, &suffix2);
result = result && output->size == concat2.size && wapp_str8_equal(output, &concat2);
wapp_mem_arena_allocator_destroy(&arena);
return wapp_tester_result(result);
}
TestFuncResult test_str8_concat_capped(void) {
bool result;
Str8 str = wapp_str8_lit("Hello world");
Str8 suffix1 = wapp_str8_lit(" from me.");
Str8 suffix2 = wapp_str8_lit(" This is my code.");
Str8 concat1 = wapp_str8_lit("Hello world from me.");
Str8 concat2 = wapp_str8_lit("Hello world from me. T");
wapp_str8_concat_capped(&str, &suffix1);
result = str.size == concat1.size && wapp_str8_equal(&str, &concat1);
wapp_str8_concat_capped(&str, &suffix2);
result = result && str.size == concat2.size && wapp_str8_equal(&str, &concat2);
return wapp_tester_result(result);
}
TestFuncResult test_str8_copy_cstr_capped(void) {
bool result;
Str8 buf = wapp_str8_buf(32);
const char *src1 = "Hello world";
const char *src2 = "Hello world from the Wizard Apprentice standard library";
Str8RO src1_cp = wapp_str8_lit_ro("Hello world");
Str8RO src2_cp = wapp_str8_lit_ro("Hello world from the Wizard Appr");
wapp_str8_copy_cstr_capped(&buf, src1);
result = buf.size == src1_cp.size && wapp_str8_equal(&buf, &src1_cp);
wapp_str8_copy_cstr_capped(&buf, src2);
result = result && buf.size == src2_cp.size && buf.size == buf.capacity && wapp_str8_equal(&buf, &src2_cp);
return wapp_tester_result(result);
}
TestFuncResult test_str8_copy_str8_capped(void) {
bool result;
Str8 buf = wapp_str8_buf(32);
Str8RO src1 = wapp_str8_lit_ro("Hello world");
Str8RO src2 = wapp_str8_lit_ro("Hello world from the Wizard Apprentice standard library");
Str8RO src2_cp = wapp_str8_lit_ro("Hello world from the Wizard Appr");
wapp_str8_copy_str8_capped(&buf, &src1);
result = buf.size == src1.size && wapp_str8_equal(&buf, &src1);
wapp_str8_copy_str8_capped(&buf, &src2);
result = result && buf.size < src2.size && buf.size == buf.capacity && wapp_str8_equal(&buf, &src2_cp);
return wapp_tester_result(result);
}
TestFuncResult test_str8_format(void) {
bool result;
Str8 buf = wapp_str8_buf(128);
Str8 expected = wapp_str8_lit("My name is Abdelrahman and I am 35 years old");
wapp_str8_format(&buf, "My name is %s and I am %u years old", "Abdelrahman", 35);
result = wapp_str8_equal(&buf, &expected);
return wapp_tester_result(result);
}
TestFuncResult test_str8_find(void) {
bool result;
Str8RO s = wapp_str8_lit("Do as I say, not as I do");
result = wapp_str8_find(&s, wapp_str8_lit_ro("d")) != -1;
result = result && (wapp_str8_find(&s, wapp_str8_lit_ro("not")) != -1);
result = result && (wapp_str8_find(&s, wapp_str8_lit_ro("as I say")) != -1);
result = result && (wapp_str8_find(&s, wapp_str8_lit_ro("f")) == -1);
result = result && (wapp_str8_find(&s, wapp_str8_lit_ro("hello")) == -1);
result = result && (wapp_str8_find(&s, wapp_str8_lit_ro("not sa I")) == -1);
result = result && (wapp_str8_find(&s, wapp_str8_lit_ro("Do unto others as you would have them do to you")) == -1);
return wapp_tester_result(result);
}
TestFuncResult test_str8_rfind(void) {
bool result;
Str8RO s = wapp_str8_lit("Do as I say, not as I do");
result = wapp_str8_rfind(&s, wapp_str8_lit_ro("d")) != -1;
result = result && (wapp_str8_rfind(&s, wapp_str8_lit_ro("not")) != -1);
result = result && (wapp_str8_rfind(&s, wapp_str8_lit_ro("as I say")) != -1);
result = result && (wapp_str8_rfind(&s, wapp_str8_lit_ro("f")) == -1);
result = result && (wapp_str8_rfind(&s, wapp_str8_lit_ro("hello")) == -1);
result = result && (wapp_str8_rfind(&s, wapp_str8_lit_ro("not sa I")) == -1);
result = result && (wapp_str8_rfind(&s, wapp_str8_lit_ro("Do unto others as you would have them do to you")) == -1);
return wapp_tester_result(result);
}
TestFuncResult test_str8_split(void) {
bool result;
Allocator arena = wapp_mem_arena_allocator_init(KB(100));
Str8 str = wapp_str8_lit("hello world from me");
Str8 delim1 = wapp_str8_lit(" ");
Str8 delim2 = wapp_str8_lit("from");
Str8List *list1 = wapp_str8_split(&arena, &str, &delim1);
Str8List *list2 = wapp_str8_split(&arena, &str, &delim2);
Str8RO splits1[] = {
wapp_str8_slice(&str, 0, 5),
wapp_str8_slice(&str, 6, 11),
wapp_str8_slice(&str, 12, 16),
wapp_str8_slice(&str, 17, 19),
};
Str8RO splits2[] = {
wapp_str8_slice(&str, 0, 12),
wapp_str8_slice(&str, 16, 19),
};
u64 index1 = 0;
u64 count1 = ARRLEN(splits1);
bool running1 = true;
u64 index2 = 0;
u64 count2 = ARRLEN(splits2);
bool running2 = true;
result = list1->node_count == count1 && wapp_str8_list_total_size(list1) == str.size - 3;
result = result && list2->node_count == count2 && wapp_str8_list_total_size(list2) == str.size - 4;
// NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of
// MSVC Spectre mitigation warnings
while (running1) {
Str8Node *node = wapp_str8_list_get(list1, index1);
result = result && wapp_str8_equal(node->item, &(splits1[index1]));
++index1;
running1 = index1 < count1;
}
// NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of
// MSVC Spectre mitigation warnings
while (running2) {
Str8Node *node = wapp_str8_list_get(list2, index2);
result = result && wapp_str8_equal(node->item, &(splits2[index2]));
++index2;
running2 = index2 < count2;
}
wapp_mem_arena_allocator_destroy(&arena);
return wapp_tester_result(result);
}
TestFuncResult test_str8_split_with_max(void) {
bool result;
Allocator arena = wapp_mem_arena_allocator_init(KB(100));
Str8 str = wapp_str8_lit("hello world from me");
Str8 delim = wapp_str8_lit(" ");
Str8List *list = wapp_str8_split_with_max(&arena, &str, &delim, 2);
Str8RO splits[] = {
wapp_str8_slice(&str, 0, 5),
wapp_str8_slice(&str, 6, 11),
wapp_str8_slice(&str, 12, 19),
};
u64 index = 0;
u64 count = ARRLEN(splits);
bool running = true;
result = list->node_count == count && wapp_str8_list_total_size(list) == str.size - 2;
// NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of
// MSVC Spectre mitigation warnings
while (running) {
Str8Node *node = wapp_str8_list_get(list, index);
result = result && wapp_str8_equal(node->item, &(splits[index]));
++index;
running = index < count;
}
wapp_mem_arena_allocator_destroy(&arena);
return wapp_tester_result(result);
}
TestFuncResult test_str8_rsplit(void) {
bool result;
Allocator arena = wapp_mem_arena_allocator_init(KB(100));
Str8 str = wapp_str8_lit("hello world from me");
Str8 delim1 = wapp_str8_lit(" ");
Str8 delim2 = wapp_str8_lit("from");
Str8List *list1 = wapp_str8_rsplit(&arena, &str, &delim1);
Str8List *list2 = wapp_str8_rsplit(&arena, &str, &delim2);
Str8RO splits1[] = {
wapp_str8_slice(&str, 0, 5),
wapp_str8_slice(&str, 6, 11),
wapp_str8_slice(&str, 12, 16),
wapp_str8_slice(&str, 17, 19),
};
Str8RO splits2[] = {
wapp_str8_slice(&str, 0, 12),
wapp_str8_slice(&str, 16, 19),
};
u64 index1 = 0;
u64 count1 = ARRLEN(splits1);
bool running1 = true;
u64 index2 = 0;
u64 count2 = ARRLEN(splits2);
bool running2 = true;
result = list1->node_count == count1 && wapp_str8_list_total_size(list1) == str.size - 3;
result = result && list2->node_count == count2 && wapp_str8_list_total_size(list2) == str.size - 4;
// NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of
// MSVC Spectre mitigation warnings
while (running1) {
Str8Node *node = wapp_str8_list_get(list1, index1);
result = result && wapp_str8_equal(node->item, &(splits1[index1]));
++index1;
running1 = index1 < count1;
}
// NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of
// MSVC Spectre mitigation warnings
while (running2) {
Str8Node *node = wapp_str8_list_get(list2, index2);
result = result && wapp_str8_equal(node->item, &(splits2[index2]));
++index2;
running2 = index2 < count2;
}
wapp_mem_arena_allocator_destroy(&arena);
return wapp_tester_result(result);
}
TestFuncResult test_str8_rsplit_with_max(void) {
bool result;
Allocator arena = wapp_mem_arena_allocator_init(KB(100));
Str8 str = wapp_str8_lit("hello world from me");
Str8 delim = wapp_str8_lit(" ");
Str8List *list = wapp_str8_rsplit_with_max(&arena, &str, &delim, 2);
Str8RO splits[] = {
wapp_str8_slice(&str, 0, 11),
wapp_str8_slice(&str, 12, 16),
wapp_str8_slice(&str, 17, 19),
};
u64 index = 0;
u64 count = ARRLEN(splits);
bool running = true;
result = list->node_count == count && wapp_str8_list_total_size(list) == str.size - 2;
// NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of
// MSVC Spectre mitigation warnings
while (running) {
Str8Node *node = wapp_str8_list_get(list, index);
result = result && wapp_str8_equal(node->item, &(splits[index]));
++index;
running = index < count;
}
wapp_mem_arena_allocator_destroy(&arena);
return wapp_tester_result(result);
}
TestFuncResult test_str8_join(void) {
bool result;
Allocator arena = wapp_mem_arena_allocator_init(KB(100));
Str8 str = wapp_str8_lit("hello world from me");
Str8 delim1 = wapp_str8_lit(" ");
Str8 delim2 = wapp_str8_lit("from");
Str8List *list1 = wapp_str8_rsplit(&arena, &str, &delim1);
Str8List *list2 = wapp_str8_rsplit(&arena, &str, &delim2);
Str8 *join1 = wapp_str8_join(&arena, list1, &delim1);
Str8 *join2 = wapp_str8_join(&arena, list2, &delim2);
result = join1->size == str.size && wapp_str8_equal(join1, &str);
result = result && join2->size == str.size && wapp_str8_equal(join2, &str);
wapp_mem_arena_allocator_destroy(&arena);
return wapp_tester_result(result);
}

40
tests/str8/test_str8.h Normal file
View File

@ -0,0 +1,40 @@
#ifndef TEST_STR8_H
#define TEST_STR8_H
#include "wapp.h"
#ifdef __cplusplus
BEGIN_C_LINKAGE
#endif // __cplusplus
TestFuncResult test_str8_lit(void);
TestFuncResult test_str8_lit_ro(void);
TestFuncResult test_str8_buf(void);
TestFuncResult test_str8_alloc_buf(void);
TestFuncResult test_str8_alloc_cstr(void);
TestFuncResult test_str8_alloc_str8(void);
TestFuncResult test_str8_alloc_substr(void);
TestFuncResult test_str8_alloc_concat(void);
TestFuncResult test_str8_get_index_within_bounds(void);
TestFuncResult test_str8_get_index_out_of_bounds(void);
TestFuncResult test_str8_set(void);
TestFuncResult test_str8_push_back(void);
TestFuncResult test_str8_equal(void);
TestFuncResult test_str8_slice(void);
TestFuncResult test_str8_concat_capped(void);
TestFuncResult test_str8_copy_cstr_capped(void);
TestFuncResult test_str8_copy_str8_capped(void);
TestFuncResult test_str8_format(void);
TestFuncResult test_str8_find(void);
TestFuncResult test_str8_rfind(void);
TestFuncResult test_str8_split(void);
TestFuncResult test_str8_split_with_max(void);
TestFuncResult test_str8_rsplit(void);
TestFuncResult test_str8_rsplit_with_max(void);
TestFuncResult test_str8_join(void);
#ifdef __cplusplus
END_C_LINKAGE
#endif // __cplusplus
#endif // !TEST_STR8_H

263
tests/str8/test_str8_list.c Normal file
View File

@ -0,0 +1,263 @@
#include "test_str8_list.h"
#include "wapp.h"
TestFuncResult test_str8_list_get(void) {
bool result;
Str8 s1 = wapp_str8_lit("1");
Str8 s2 = wapp_str8_lit("2");
Str8 s3 = wapp_str8_lit("3");
Str8 s4 = wapp_str8_lit("4");
Str8 s5 = wapp_str8_lit("5");
Str8List list = {0};
Str8Node n1 = { .item = &s1 };
Str8Node n2 = { .item = &s2 };
Str8Node n3 = { .item = &s3 };
Str8Node n4 = { .item = &s4 };
Str8Node n5 = { .item = &s5 };
wapp_str8_list_push_back(&list, &n1);
wapp_str8_list_push_back(&list, &n2);
wapp_str8_list_push_back(&list, &n3);
wapp_str8_list_push_back(&list, &n4);
wapp_str8_list_push_back(&list, &n5);
Str8Node *node = wapp_str8_list_get(&list, 0);
result = node->item == &s1 && wapp_str8_equal(node->item, &s1);
node = wapp_str8_list_get(&list, 1);
result = result && node->item == &s2 && wapp_str8_equal(node->item, &s2);
node = wapp_str8_list_get(&list, 2);
result = result && node->item == &s3 && wapp_str8_equal(node->item, &s3);
node = wapp_str8_list_get(&list, 3);
result = result && node->item == &s4 && wapp_str8_equal(node->item, &s4);
node = wapp_str8_list_get(&list, 4);
result = result && node->item == &s5 && wapp_str8_equal(node->item, &s5);
return wapp_tester_result(result);
}
TestFuncResult test_str8_list_push_front(void) {
bool result;
Str8 s1 = wapp_str8_lit("1");
Str8 s2 = wapp_str8_lit("2");
Str8 s3 = wapp_str8_lit("3");
Str8List list = {0};
Str8Node n1 = { .item = &s1 };
Str8Node n2 = { .item = &s2 };
Str8Node n3 = { .item = &s3 };
wapp_str8_list_push_front(&list, &n1);
result = list.first == list.last && list.first == &n1 && list.first->item == &s1 && wapp_str8_list_total_size(&list) == 1 && list.node_count == 1;
wapp_str8_list_push_front(&list, &n2);
result = result && list.first == &n2 && list.first->item == &s2 && wapp_str8_list_total_size(&list) == 2 && list.node_count == 2;
wapp_str8_list_push_front(&list, &n3);
result = result && list.first == &n3 && list.first->item == &s3 && wapp_str8_list_total_size(&list) == 3 && list.node_count == 3;
return wapp_tester_result(result);
}
TestFuncResult test_str8_list_push_back(void) {
bool result;
Str8 s1 = wapp_str8_lit("1");
Str8 s2 = wapp_str8_lit("2");
Str8 s3 = wapp_str8_lit("3");
Str8List list = {0};
Str8Node n1 = { .item = &s1 };
Str8Node n2 = { .item = &s2 };
Str8Node n3 = { .item = &s3 };
wapp_str8_list_push_back(&list, &n1);
result = list.first == list.last && list.last == &n1 && list.last->item == &s1 && wapp_str8_list_total_size(&list) == 1 && list.node_count == 1;
wapp_str8_list_push_back(&list, &n2);
result = result && list.last == &n2 && list.last->item == &s2 && wapp_str8_list_total_size(&list) == 2 && list.node_count == 2;
wapp_str8_list_push_back(&list, &n3);
result = result && list.last == &n3 && list.last->item == &s3 && wapp_str8_list_total_size(&list) == 3 && list.node_count == 3;
return wapp_tester_result(result);
}
TestFuncResult test_str8_list_insert(void) {
bool result;
Str8 s1 = wapp_str8_lit("1");
Str8 s2 = wapp_str8_lit("2");
Str8 s3 = wapp_str8_lit("3");
Str8 s4 = wapp_str8_lit("4");
Str8 s5 = wapp_str8_lit("5");
Str8 s6 = wapp_str8_lit("6");
Str8 s7 = wapp_str8_lit("7");
Str8List list = {0};
Str8Node n1 = { .item = &s1 };
Str8Node n2 = { .item = &s2 };
Str8Node n3 = { .item = &s3 };
Str8Node n4 = { .item = &s4 };
Str8Node n5 = { .item = &s5 };
Str8Node n6 = { .item = &s6 };
Str8Node n7 = { .item = &s7 };
wapp_str8_list_push_back(&list, &n1);
wapp_str8_list_push_back(&list, &n2);
wapp_str8_list_push_back(&list, &n3);
wapp_str8_list_push_back(&list, &n4);
wapp_str8_list_push_back(&list, &n5);
Str8Node *node;
wapp_str8_list_insert(&list, &n6, 2);
node = wapp_str8_list_get(&list, 2);
result = node != NULL && node->item == &s6 && wapp_str8_list_total_size(&list) == 6 && list.node_count == 6;
wapp_str8_list_insert(&list, &n7, 5);
node = wapp_str8_list_get(&list, 5);
result = result && node != NULL && node->item == &s7 && wapp_str8_list_total_size(&list) == 7 && list.node_count == 7;
return wapp_tester_result(result);
}
TestFuncResult test_str8_list_pop_front(void) {
bool result;
Str8 s1 = wapp_str8_lit("1");
Str8 s2 = wapp_str8_lit("2");
Str8 s3 = wapp_str8_lit("3");
Str8 s4 = wapp_str8_lit("4");
Str8 s5 = wapp_str8_lit("5");
Str8List list = {0};
Str8Node n1 = { .item = &s1 };
Str8Node n2 = { .item = &s2 };
Str8Node n3 = { .item = &s3 };
Str8Node n4 = { .item = &s4 };
Str8Node n5 = { .item = &s5 };
wapp_str8_list_push_back(&list, &n1);
wapp_str8_list_push_back(&list, &n2);
wapp_str8_list_push_back(&list, &n3);
wapp_str8_list_push_back(&list, &n4);
wapp_str8_list_push_back(&list, &n5);
Str8Node *node = wapp_str8_list_pop_front(&list);
result = node == &n1 && node->item == &s1 && wapp_str8_equal(node->item, &s1) && wapp_str8_list_total_size(&list) == 4 && list.node_count == 4;
node = wapp_str8_list_pop_front(&list);
result = result && node == &n2 && node->item == &s2 && wapp_str8_equal(node->item, &s2) && wapp_str8_list_total_size(&list) == 3 && list.node_count == 3;
node = wapp_str8_list_pop_front(&list);
result = result && node == &n3 && node->item == &s3 && wapp_str8_equal(node->item, &s3) && wapp_str8_list_total_size(&list) == 2 && list.node_count == 2;
node = wapp_str8_list_pop_front(&list);
result = result && node == &n4 && node->item == &s4 && wapp_str8_equal(node->item, &s4) && wapp_str8_list_total_size(&list) == 1 && list.node_count == 1;
node = wapp_str8_list_pop_front(&list);
result = result && node == &n5 && node->item == &s5 && wapp_str8_equal(node->item, &s5) && wapp_str8_list_total_size(&list) == 0 && list.node_count == 0;
return wapp_tester_result(result);
}
TestFuncResult test_str8_list_pop_back(void) {
bool result;
Str8 s1 = wapp_str8_lit("1");
Str8 s2 = wapp_str8_lit("2");
Str8 s3 = wapp_str8_lit("3");
Str8 s4 = wapp_str8_lit("4");
Str8 s5 = wapp_str8_lit("5");
Str8List list = {0};
Str8Node n1 = { .item = &s1 };
Str8Node n2 = { .item = &s2 };
Str8Node n3 = { .item = &s3 };
Str8Node n4 = { .item = &s4 };
Str8Node n5 = { .item = &s5 };
wapp_str8_list_push_front(&list, &n1);
wapp_str8_list_push_front(&list, &n2);
wapp_str8_list_push_front(&list, &n3);
wapp_str8_list_push_front(&list, &n4);
wapp_str8_list_push_front(&list, &n5);
Str8Node *node = wapp_str8_list_pop_back(&list);
result = node == &n1 && node->item == &s1 && wapp_str8_equal(node->item, &s1) && wapp_str8_list_total_size(&list) == 4 && list.node_count == 4;
node = wapp_str8_list_pop_back(&list);
result = result && node == &n2 && node->item == &s2 && wapp_str8_equal(node->item, &s2) && wapp_str8_list_total_size(&list) == 3 && list.node_count == 3;
node = wapp_str8_list_pop_back(&list);
result = result && node == &n3 && node->item == &s3 && wapp_str8_equal(node->item, &s3) && wapp_str8_list_total_size(&list) == 2 && list.node_count == 2;
node = wapp_str8_list_pop_back(&list);
result = result && node == &n4 && node->item == &s4 && wapp_str8_equal(node->item, &s4) && wapp_str8_list_total_size(&list) == 1 && list.node_count == 1;
node = wapp_str8_list_pop_back(&list);
result = result && node == &n5 && node->item == &s5 && wapp_str8_equal(node->item, &s5) && wapp_str8_list_total_size(&list) == 0 && list.node_count == 0;
return wapp_tester_result(result);
}
TestFuncResult test_str8_list_remove(void) {
bool result;
Str8 s1 = wapp_str8_lit("1");
Str8 s2 = wapp_str8_lit("2");
Str8 s3 = wapp_str8_lit("3");
Str8 s4 = wapp_str8_lit("4");
Str8 s5 = wapp_str8_lit("5");
Str8List list = {0};
Str8Node n1 = { .item = &s1 };
Str8Node n2 = { .item = &s2 };
Str8Node n3 = { .item = &s3 };
Str8Node n4 = { .item = &s4 };
Str8Node n5 = { .item = &s5 };
wapp_str8_list_push_back(&list, &n1);
wapp_str8_list_push_back(&list, &n2);
wapp_str8_list_push_back(&list, &n3);
wapp_str8_list_push_back(&list, &n4);
wapp_str8_list_push_back(&list, &n5);
Str8Node *node = wapp_str8_list_remove(&list, 0);
result = node == &n1 && node->item == &s1 && wapp_str8_equal(node->item, &s1) && wapp_str8_list_total_size(&list) == 4 && list.node_count == 4;
node = wapp_str8_list_remove(&list, 0);
result = result && node == &n2 && node->item == &s2 && wapp_str8_equal(node->item, &s2) && wapp_str8_list_total_size(&list) == 3 && list.node_count == 3;
node = wapp_str8_list_remove(&list, 0);
result = result && node == &n3 && node->item == &s3 && wapp_str8_equal(node->item, &s3) && wapp_str8_list_total_size(&list) == 2 && list.node_count == 2;
node = wapp_str8_list_remove(&list, 0);
result = result && node == &n4 && node->item == &s4 && wapp_str8_equal(node->item, &s4) && wapp_str8_list_total_size(&list) == 1 && list.node_count == 1;
node = wapp_str8_list_remove(&list, 0);
result = result && node == &n5 && node->item == &s5 && wapp_str8_equal(node->item, &s5) && wapp_str8_list_total_size(&list) == 0 && list.node_count == 0;
return wapp_tester_result(result);
}
TestFuncResult test_str8_list_empty(void) {
bool result;
Str8List list = {0};
wapp_str8_list_push_back(&list, &wapp_str8_node_from_cstr("Hello"));
wapp_str8_list_push_back(&list, &wapp_str8_node_from_cstr("from"));
wapp_str8_list_push_back(&list, &wapp_str8_node_from_cstr("wizapp"));
wapp_str8_list_push_back(&list, &wapp_str8_node_from_cstr("stdlib"));
wapp_str8_list_empty(&list);
result = list.first == NULL && list.last == NULL && list.node_count == 0 && wapp_str8_list_total_size(&list) == 0;
return wapp_tester_result(result);
}

View File

@ -0,0 +1,23 @@
#ifndef TEST_STR8_LIST_H
#define TEST_STR8_LIST_H
#include "wapp.h"
#ifdef __cplusplus
BEGIN_C_LINKAGE
#endif // __cplusplus
TestFuncResult test_str8_list_get(void);
TestFuncResult test_str8_list_push_front(void);
TestFuncResult test_str8_list_push_back(void);
TestFuncResult test_str8_list_insert(void);
TestFuncResult test_str8_list_pop_front(void);
TestFuncResult test_str8_list_pop_back(void);
TestFuncResult test_str8_list_remove(void);
TestFuncResult test_str8_list_empty(void);
#ifdef __cplusplus
END_C_LINKAGE
#endif // __cplusplus
#endif // !TEST_STR8_LIST_H

61
tests/wapptest.c Normal file
View File

@ -0,0 +1,61 @@
#include "test_str8.h"
#include "test_str8_list.h"
#include "test_allocator.h"
#include "test_arena.h"
#include "test_cpath.h"
#include "test_shell_commander.h"
#include "wapp.h"
#include <stdlib.h>
int main(void) {
wapp_tester_run_tests(test_arena_allocator,
test_arena_init,
test_arena_init_succeeds_when_reserving_very_large_size,
test_arena_alloc_succeeds_when_within_capacity,
test_arena_alloc_fails_when_over_capacity,
test_arena_realloc_bigger_size,
test_arena_realloc_smaller_size,
test_arena_clear,
test_arena_destroy,
test_str8_lit,
test_str8_lit_ro,
test_str8_buf,
test_str8_alloc_buf,
test_str8_alloc_cstr,
test_str8_alloc_str8,
test_str8_alloc_substr,
test_str8_alloc_concat,
test_str8_get_index_within_bounds,
test_str8_get_index_out_of_bounds,
test_str8_set,
test_str8_equal,
test_str8_slice,
test_str8_concat_capped,
test_str8_copy_cstr_capped,
test_str8_copy_str8_capped,
test_str8_format,
test_str8_find,
test_str8_rfind,
test_str8_split,
test_str8_split_with_max,
test_str8_rsplit,
test_str8_rsplit_with_max,
test_str8_join,
test_str8_list_get,
test_str8_list_push_front,
test_str8_list_push_back,
test_str8_list_insert,
test_str8_list_pop_front,
test_str8_list_pop_back,
test_str8_list_remove,
test_str8_list_empty,
test_cpath_join_path,
test_cpath_dirname,
test_cpath_dirup,
test_commander_cmd_success,
test_commander_cmd_failure,
test_commander_cmd_out_buf_success,
test_commander_cmd_out_buf_failure);
return EXIT_SUCCESS;
}