41 Commits

Author SHA1 Message Date
abdelrahman f073e5a21f v1.1.1
Release / release (push) Successful in 6s
2026-05-25 17:35:05 +01:00
abdelrahman 536a1a3b01 v1.1.1
Release / release (push) Has been cancelled
2026-05-25 16:38:40 +01:00
abdelrahman a6b697dd0e Pad logger struct for MSVC
Release / release (push) Successful in 3s
2026-05-17 17:46:50 +01:00
abdelrahman c67a448d00 Add basic logging functionality
Release / release (push) Successful in 6s
2026-05-17 18:40:10 +01:00
abdelrahman 2e5163ba33 Implement functions to get stdin, stdout & stderr
Release / release (push) Successful in 2s
2026-05-17 12:21:23 +01:00
abdelrahman 515493b963 Fix queue MSVC errors 2026-05-17 12:20:47 +01:00
abdelrahman 8061692801 Add standard streams
Release / release (push) Successful in 3s
2026-05-17 11:13:40 +01:00
abdelrahman 70997f091f Avoid wrapping whole Makefile in bear
Release / release (push) Successful in 6s
2026-05-17 10:17:54 +01:00
abdelrahman f61cb3cae0 Minor shell commander tweaks 2026-05-17 10:17:36 +01:00
abdelrahman 946bcc9b59 Replace extern 2026-05-17 09:44:11 +01:00
abdelrahman 7ffebe7dce Fix missing semi-colon
Release / release (push) Successful in 3s
2026-05-10 02:22:46 +01:00
abdelrahman 9c727950d8 Call UUID lambdas
Release / release (push) Successful in 3s
2026-05-10 02:21:38 +01:00
abdelrahman c7c4f88866 Fix wapp_uuid_gen_uuid4 for C++
Release / release (push) Successful in 5s
2026-05-10 02:19:34 +01:00
abdelrahman 6346765e32 Fix UUID for C++
Release / release (push) Successful in 5s
2026-05-10 02:13:41 +01:00
Abdelrahman Said 270dbfe5ca Update README
Release / release (push) Successful in 7s
2026-03-09 22:32:04 +00:00
Abdelrahman Said 7fd808fe7b Add LICENSE file
Release / release (push) Successful in 11s
2026-03-09 22:17:05 +00:00
abdelrahman 95deda1f59 v1.0.0
Release / release (push) Successful in 3s
2026-03-08 23:56:50 +00:00
abdelrahman 3f3d1e1e5d Ensure all tags are fetched when checking out the repo
Release / release (push) Successful in 3s
2026-03-08 23:55:19 +00:00
abdelrahman 877b8e9a04 Update release workflow
Release / release (push) Failing after 2s
2026-03-08 23:52:05 +00:00
abdelrahman 4ce59f537c Trigger workflow one more time
Release / release (push) Failing after 3s
2026-03-08 23:44:02 +00:00
abdelrahman 7d13bde13b Trigger workflow 2026-03-08 23:40:18 +00:00
abdelrahman 3aa792a620 Add release workflow 2026-03-08 23:34:09 +00:00
abdelrahman c0b667847c Update package script 2026-03-08 23:33:59 +00:00
abdelrahman ce2956f072 Update package script 2026-03-08 23:14:16 +00:00
abdelrahman a1182016af Add VERSION 2026-03-08 21:38:47 +00:00
abdelrahman 59423e294a Add packaging script 2026-03-08 21:38:41 +00:00
abdelrahman ef5c9376a9 Fix mistake when building with no runtime assert 2026-03-08 20:22:21 +00:00
abdelrahman eeed101228 Move large file source def to code 2026-03-08 13:10:17 +00:00
abdelrahman 6b88d7e3fe Add TODO for updating UUID implementation 2026-02-09 00:02:21 +00:00
abdelrahman 58dab46902 Revert "Update uuid C++ implementation"
This reverts commit 1cdb08a81a.
2026-02-08 23:57:09 +00:00
abdelrahman 269ee5d9ab Revert "Update uuid C++ implementation"
This reverts commit 610df6869f.
2026-02-08 23:57:05 +00:00
abdelrahman 610df6869f Update uuid C++ implementation 2026-02-08 23:53:30 +00:00
abdelrahman 1cdb08a81a Update uuid C++ implementation 2026-02-08 23:48:02 +00:00
abdelrahman 8d9ef89329 Fix bug with queue 2026-02-08 19:16:36 +00:00
Abdelrahman Said 3d3452f523 Update queue implementation to use ring buffer 2026-01-24 20:46:05 +00:00
Abdelrahman Said 8e41b627bc Add function to create array from preallocated buffer 2026-01-24 20:45:52 +00:00
Abdelrahman Said 7a54c28c0f Update array allocation functions 2026-01-24 12:17:15 +00:00
Abdelrahman Said bd659e64fc Ensure array count is set correctly when allocating 2026-01-22 06:09:43 +00:00
Abdelrahman Said 21ac756fad Pass init flags to allocating array utilities 2026-01-19 06:09:36 +00:00
Abdelrahman Said 243f04c0ca Handle lseek64 not existing on macOS 2026-01-19 06:00:09 +00:00
abdelrahman 4cc8cb3d25 Fix MSVC errors 2026-01-11 23:48:35 +00:00
33 changed files with 929 additions and 181 deletions
+81
View File
@@ -0,0 +1,81 @@
name: Release
on:
push:
branches:
- main # or your default branch
jobs:
release:
runs-on: self-hosted
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # fetch full history
tags: true # fetch all tags
- name: Check/Create Tag
id: tag
run: |
VERSION=$(cat VERSION | tr -d '[:space:]') # read version and trim whitespace
echo "Version: $VERSION"
if git rev-parse "v$VERSION" >/dev/null 2>&1; then
echo "Tag v$VERSION already exists. Skipping release."
exit 0
fi
# Tag does not exist → create it
echo "Creating tag v$VERSION"
git tag "v$VERSION"
git push origin "v$VERSION"
echo "version=$VERSION" >> $GITHUB_OUTPUT
- name: Build Artifacts
if: steps.tag.outputs.version
run: |
chmod +x ./package
./package
ls -l dist/
- name: Create Release
if: steps.tag.outputs.version
id: create_release
run: |
VERSION="${{ steps.tag.outputs.version }}"
RESPONSE=$(curl -s -X POST \
-H "Authorization: token ${{ secrets.GITEA_TOKEN }}" \
-H "Content-Type: application/json" \
"https://git.thewizardapprentice.com/api/v1/repos/${{ gitea.repository }}/releases" \
-d '{
"tag_name": "v'"$VERSION"'",
"name": "wapp-v'"$VERSION"'",
"body": "Automated release for wapp-v'"$VERSION"'",
"draft": false,
"prerelease": false
}')
echo "$RESPONSE"
RELEASE_ID=$(echo $RESPONSE | jq -r '.id')
echo "release_id=$RELEASE_ID" >> $GITHUB_OUTPUT
- name: Upload Artifacts
if: steps.tag.outputs.version
run: |
RELEASE_ID="${{ steps.create_release.outputs.release_id }}"
for FILE in dist/*; do
FILENAME=$(basename "$FILE")
echo "Uploading $FILENAME"
curl -X POST \
-H "Authorization: token ${{ secrets.GITEA_TOKEN }}" \
-H "Content-Type: application/octet-stream" \
--data-binary @"$FILE" \
"https://git.thewizardapprentice.com/api/v1/repos/${{ gitea.repository }}/releases/$RELEASE_ID/assets?name=$FILENAME"
done
- name: Cleanup
if: steps.tag.outputs.version
run: rm -rf dist/*
+201
View File
@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [2026] [Abdelrahman Said]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+5 -5
View File
@@ -10,7 +10,7 @@ INSTALL_PREFIX = dist
RUNTIME_ASSERT = true RUNTIME_ASSERT = true
# Internal variables # Internal variables
override CFLAGS = -Wall -Wextra -Werror -pedantic -Isrc -D_LARGEFILE64_SOURCE override CFLAGS = -Wall -Wextra -Werror -pedantic -Isrc
override LIBFLAGS = -fPIC override LIBFLAGS = -fPIC
override CSTD := -std=gnu11 override CSTD := -std=gnu11
override CXXSTD := -std=gnu++11 override CXXSTD := -std=gnu++11
@@ -46,7 +46,7 @@ endif
# Disable runtime asserts # Disable runtime asserts
ifeq ($(RUNTIME_ASSERT), false) ifeq ($(RUNTIME_ASSERT), false)
override BUILD_FLAGS += WAPP_NO_RUNTIME_ASSERT override BUILD_FLAGS += -DWAPP_NO_RUNTIME_ASSERT
endif endif
ifeq ($(CC),gcc) ifeq ($(CC),gcc)
@@ -153,7 +153,7 @@ builddir:
@mkdir -p "$(BUILD_DIR)" @mkdir -p "$(BUILD_DIR)"
build-c-test: build-c-test:
$(CC) $(CSTD) $(CFLAGS) $(BUILD_FLAGS) $(TEST_INCLUDE) $(TEST_C_SRC) -o "$(TEST_C_OUT)" bear -- $(CC) $(CSTD) $(CFLAGS) $(BUILD_FLAGS) $(TEST_INCLUDE) $(TEST_C_SRC) -o "$(TEST_C_OUT)"
run-c-test: build-c-test run-c-test: build-c-test
@echo -e "\n\033[34;1mRUNNING C TESTS\033[0m" @echo -e "\n\033[34;1mRUNNING C TESTS\033[0m"
@@ -161,7 +161,7 @@ run-c-test: build-c-test
@rm "$(TEST_C_OUT)" @rm "$(TEST_C_OUT)"
build-cc-test: build-cc-test:
$(CXX) $(CXXSTD) $(CFLAGS) $(BUILD_FLAGS) $(TEST_INCLUDE) $(TEST_CXX_SRC) "$(LIB_OUT)" -o "$(TEST_CXX_OUT)" bear -a -- $(CXX) $(CXXSTD) $(CFLAGS) $(BUILD_FLAGS) $(TEST_INCLUDE) $(TEST_CXX_SRC) "$(LIB_OUT)" -o "$(TEST_CXX_OUT)"
run-cc-test: build-cc-test run-cc-test: build-cc-test
@echo -e "\n\033[34;1mRUNNING C++ TESTS\033[0m" @echo -e "\n\033[34;1mRUNNING C++ TESTS\033[0m"
@@ -175,6 +175,6 @@ install: build-lib
@bash $(HEADER_INSTALL_CMD) $(LIB_SRC) "$(INCLUDE_INSTALL)" $(INCLUDES) @bash $(HEADER_INSTALL_CMD) $(LIB_SRC) "$(INCLUDE_INSTALL)" $(INCLUDES)
build-lib: builddir build-lib: builddir
$(CC) -c $(CSTD) $(CFLAGS) $(BUILD_FLAGS) $(LIBFLAGS) $(LIB_SRC) -o "$(OBJ_OUT)" bear -a -- $(CC) -c $(CSTD) $(CFLAGS) $(BUILD_FLAGS) $(LIBFLAGS) $(LIB_SRC) -o "$(OBJ_OUT)"
$(AR) r "$(LIB_OUT)" "$(OBJ_OUT)" $(AR) r "$(LIB_OUT)" "$(OBJ_OUT)"
@rm "$(OBJ_OUT)" @rm "$(OBJ_OUT)"
+3 -1
View File
@@ -1,3 +1,5 @@
# Wizard Apprentice Standard Library # Wizard Apprentice Standard Library
A group of utilities for C/C++ projects **Wizard Apprentice Standard Library** (`wapp`) is a lightweight collection of reusable utilities for **C and C++** projects.
For more information about the library and how to use it, check the [wiki](https://git.thewizardapprentice.com/abdelrahman/wizapp-stdlib/wiki).
+1
View File
@@ -0,0 +1 @@
1.1.1
+1 -1
View File
@@ -55,4 +55,4 @@ if ! contains ${BUILD_TYPE} "${ACCEPTED_BUILD_TYPES[@]}"; then
exit 1 exit 1
fi fi
bear -- make BUILD_TYPE=$BUILD_TYPE $ARGS make BUILD_TYPE=$BUILD_TYPE $ARGS
Executable
+11
View File
@@ -0,0 +1,11 @@
#!/bin/bash
cp -r src wapp
mkdir -p dist
ARCHIVE_NAME="wapp-v$(cat VERSION)"
tar -czvf dist/$ARCHIVE_NAME.tar.gz wapp
zip -r dist/$ARCHIVE_NAME.zip wapp
rm -rf wapp
+49 -15
View File
@@ -104,7 +104,8 @@ void _array_copy_capped(GenericArray dst, const GenericArray src, u64 item_size)
dst_header->count = copy_count; dst_header->count = copy_count;
} }
GenericArray _array_append_alloc(const Allocator *allocator, GenericArray array, void *value, u64 item_size) { GenericArray _array_append_alloc(const Allocator *allocator, GenericArray array, void *value,
ArrayInitFlags flags, u64 item_size) {
wapp_runtime_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); wapp_runtime_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL");
_array_validate(array, item_size); _array_validate(array, item_size);
@@ -113,7 +114,8 @@ GenericArray _array_append_alloc(const Allocator *allocator, GenericArray array,
ArrayHeader *header = _array_header(array); ArrayHeader *header = _array_header(array);
if (header->count >= header->capacity) { if (header->count >= header->capacity) {
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(header->capacity * 2); u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(header->capacity * 2);
output = (GenericArray )_array_alloc_capacity(allocator, new_capacity, header->item_size, false); output = (GenericArray )_array_alloc_capacity(allocator, new_capacity, flags,
header->item_size);
if (!output) { if (!output) {
output = array; output = array;
goto RETURN_ARRAY_APPEND_ALLOC; goto RETURN_ARRAY_APPEND_ALLOC;
@@ -123,11 +125,16 @@ GenericArray _array_append_alloc(const Allocator *allocator, GenericArray array,
_array_append_capped(output, value, item_size); _array_append_capped(output, value, item_size);
if ((flags & ARRAY_INIT_FILLED) == ARRAY_INIT_FILLED) {
_array_set_count(output, _array_capacity(output));
}
RETURN_ARRAY_APPEND_ALLOC: RETURN_ARRAY_APPEND_ALLOC:
return output; return output;
} }
GenericArray _array_extend_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src, u64 item_size) { GenericArray _array_extend_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src,
ArrayInitFlags flags, u64 item_size) {
wapp_runtime_assert(allocator != NULL && dst != NULL && src != NULL, "`allocator`, `dst` and `src` should not be NULL"); wapp_runtime_assert(allocator != NULL && dst != NULL && src != NULL, "`allocator`, `dst` and `src` should not be NULL");
_array_validate(dst, item_size); _array_validate(dst, item_size);
_array_validate(src, item_size); _array_validate(src, item_size);
@@ -139,7 +146,8 @@ GenericArray _array_extend_alloc(const Allocator *allocator, GenericArray dst, c
u64 remaining_capacity = dst_header->capacity - dst_header->count; u64 remaining_capacity = dst_header->capacity - dst_header->count;
if (src_header->count >= remaining_capacity) { if (src_header->count >= remaining_capacity) {
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst_header->capacity * 2); u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst_header->capacity * 2);
output = (GenericArray )_array_alloc_capacity(allocator, new_capacity, dst_header->item_size, false); output = (GenericArray )_array_alloc_capacity(allocator, new_capacity,
flags, dst_header->item_size);
if (!output) { if (!output) {
output = dst; output = dst;
goto RETURN_ARRAY_EXTEND_ALLOC; goto RETURN_ARRAY_EXTEND_ALLOC;
@@ -149,11 +157,16 @@ GenericArray _array_extend_alloc(const Allocator *allocator, GenericArray dst, c
_array_extend_capped(output, src, item_size); _array_extend_capped(output, src, item_size);
if ((flags & ARRAY_INIT_FILLED) == ARRAY_INIT_FILLED) {
_array_set_count(output, _array_capacity(output));
}
RETURN_ARRAY_EXTEND_ALLOC: RETURN_ARRAY_EXTEND_ALLOC:
return output; return output;
} }
GenericArray _array_copy_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src, u64 item_size) { GenericArray _array_copy_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src,
ArrayInitFlags flags, u64 item_size) {
wapp_runtime_assert(allocator != NULL && dst != NULL && src != NULL, "`allocator`, `dst` and `src` should not be NULL"); wapp_runtime_assert(allocator != NULL && dst != NULL && src != NULL, "`allocator`, `dst` and `src` should not be NULL");
_array_validate(dst, item_size); _array_validate(dst, item_size);
_array_validate(src, item_size); _array_validate(src, item_size);
@@ -165,7 +178,7 @@ GenericArray _array_copy_alloc(const Allocator *allocator, GenericArray dst, con
if (src_header->count >= dst_header->capacity) { if (src_header->count >= dst_header->capacity) {
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst_header->capacity * 2); u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst_header->capacity * 2);
output = (GenericArray )_array_alloc_capacity(allocator, new_capacity, output = (GenericArray )_array_alloc_capacity(allocator, new_capacity,
src_header->item_size, false); flags, src_header->item_size);
if (!output) { if (!output) {
output = dst; output = dst;
goto RETURN_ARRAY_COPY_ALLOC; goto RETURN_ARRAY_COPY_ALLOC;
@@ -174,6 +187,10 @@ GenericArray _array_copy_alloc(const Allocator *allocator, GenericArray dst, con
_array_copy_capped(output, src, item_size); _array_copy_capped(output, src, item_size);
if ((flags & ARRAY_INIT_FILLED) == ARRAY_INIT_FILLED) {
_array_set_count(output, _array_capacity(output));
}
RETURN_ARRAY_COPY_ALLOC: RETURN_ARRAY_COPY_ALLOC:
return output; return output;
} }
@@ -203,28 +220,45 @@ u64 _array_calc_alloc_size(u64 capacity, u64 item_size) {
return sizeof(ArrayHeader) + item_size * capacity; return sizeof(ArrayHeader) + item_size * capacity;
} }
GenericArray _array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size, GenericArray _array_alloc_capacity(const Allocator *allocator, u64 capacity, ArrayInitFlags flags,
ArrayInitFlags flags) { u64 item_size) {
wapp_runtime_assert(allocator != NULL, "`allocator` should not be NULL"); wapp_runtime_assert(allocator != NULL, "`allocator` should not be NULL");
GenericArray output = NULL; GenericArray output = NULL;
u64 allocation_size = _array_calc_alloc_size(capacity, item_size); u64 allocation_size = _array_calc_alloc_size(capacity, item_size);
ArrayHeader *header = wapp_mem_allocator_alloc(allocator, allocation_size); void *buffer = wapp_mem_allocator_alloc(allocator, allocation_size);
if (!header) { if (!buffer) {
goto RETURN_ARRAY_ALLOC; goto RETURN_ARRAY_ALLOC;
} }
output = (u8 *)(header + 1); output = _array_from_preallocated_buffer(buffer, allocation_size, flags, item_size);
header->magic = WAPP_ARRAY_MAGIC;
header->count = flags & ARRAY_INIT_FILLED ? capacity : 0;
header->capacity = capacity;
header->item_size = item_size;
RETURN_ARRAY_ALLOC: RETURN_ARRAY_ALLOC:
return output; return output;
} }
GenericArray _array_from_preallocated_buffer(void *buffer, u64 buffer_size, ArrayInitFlags flags,
u64 item_size) {
wapp_runtime_assert(buffer != NULL, "`buffer` should not be NULL");
i64 data_buffer_size = (i64)buffer_size - (i64)(sizeof(ArrayHeader));
if (data_buffer_size <= 0) {
return NULL;
}
u64 item_capacity = (u64)data_buffer_size / item_size;
ArrayHeader *header = (ArrayHeader *)buffer;
GenericArray output = (u8 *)(header + 1);
header->magic = WAPP_ARRAY_MAGIC;
header->count = flags & ARRAY_INIT_FILLED ? item_capacity : 0;
header->capacity = item_capacity;
header->item_size = item_size;
return output;
}
wapp_persist inline void _array_validate(const GenericArray array, u64 item_size) { wapp_persist inline void _array_validate(const GenericArray array, u64 item_size) {
ArrayHeader *header = _array_header(array); ArrayHeader *header = _array_header(array);
wapp_runtime_assert(WAPP_ARRAY_MAGIC == header->magic, "`array` is not a valid wapp array"); wapp_runtime_assert(WAPP_ARRAY_MAGIC == header->magic, "`array` is not a valid wapp array");
+39 -29
View File
@@ -17,9 +17,6 @@ BEGIN_C_LINKAGE
#define _calc_array_count(TYPE, ...) wapp_misc_utils_va_args_count(TYPE, __VA_ARGS__) #define _calc_array_count(TYPE, ...) wapp_misc_utils_va_args_count(TYPE, __VA_ARGS__)
#define _calc_array_capacity(TYPE, ...) wapp_misc_utils_u64_round_up_pow2(_calc_array_count(TYPE, __VA_ARGS__) * 2) #define _calc_array_capacity(TYPE, ...) wapp_misc_utils_u64_round_up_pow2(_calc_array_count(TYPE, __VA_ARGS__) * 2)
#define wapp_array_alloc_capacity(TYPE, ALLOCATOR_PTR, CAPACITY, FLAGS) \
((TYPE *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(TYPE), FLAGS))
typedef struct Str8 Str8; typedef struct Str8 Str8;
// NOTE (Abdelrahman): Typedefs to distinguish arrays from regular pointers // NOTE (Abdelrahman): Typedefs to distinguish arrays from regular pointers
@@ -138,40 +135,48 @@ typedef enum {
sizeof(TYPE))) sizeof(TYPE)))
#define wapp_array_set(TYPE, ARRAY, INDEX, VALUE_PTR) \ #define wapp_array_set(TYPE, ARRAY, INDEX, VALUE_PTR) \
(_array_set((GenericArray)ARRAY, \ (_array_set((GenericArray)ARRAY, \
INDEX, \ INDEX, \
(u8 *)VALUE_PTR, \ (u8 *)VALUE_PTR, \
sizeof(TYPE))) sizeof(TYPE)))
#define wapp_array_append_capped(TYPE, ARRAY, VALUE_PTR) \ #define wapp_array_append_capped(TYPE, ARRAY, VALUE_PTR) \
(_array_append_capped((GenericArray)ARRAY, \ (_array_append_capped((GenericArray)ARRAY, \
(u8 *)VALUE_PTR, \ (u8 *)VALUE_PTR, \
sizeof(TYPE))) sizeof(TYPE)))
#define wapp_array_extend_capped(TYPE, DST_ARRAY, SRC_ARRAY) \ #define wapp_array_extend_capped(TYPE, DST_ARRAY, SRC_ARRAY) \
(_array_extend_capped((GenericArray)DST_ARRAY, \ (_array_extend_capped((GenericArray)DST_ARRAY, \
(GenericArray)SRC_ARRAY, \ (GenericArray)SRC_ARRAY, \
sizeof(TYPE))) sizeof(TYPE)))
#define wapp_array_copy_capped(TYPE, DST_ARRAY, SRC_ARRAY) \ #define wapp_array_copy_capped(TYPE, DST_ARRAY, SRC_ARRAY) \
(_array_copy_capped((GenericArray)DST_ARRAY, \ (_array_copy_capped((GenericArray)DST_ARRAY, \
(GenericArray)SRC_ARRAY, \ (GenericArray)SRC_ARRAY, \
sizeof(TYPE))) sizeof(TYPE)))
#define wapp_array_append_alloc(TYPE, ALLOCATOR_PTR, ARRAY, VALUE_PTR) \ #define wapp_array_append_alloc(TYPE, ALLOCATOR_PTR, ARRAY, VALUE_PTR, FLAGS) \
((TYPE *)_array_append_alloc(ALLOCATOR_PTR, \ ((TYPE *)_array_append_alloc(ALLOCATOR_PTR, \
(GenericArray)ARRAY, \ (GenericArray)ARRAY, \
(u8 *)VALUE_PTR, \ (u8 *)VALUE_PTR, \
sizeof(TYPE))) FLAGS, \
#define wapp_array_extend_alloc(TYPE, ALLOCATOR_PTR, DST_ARRAY, SRC_ARRAY) \ sizeof(TYPE)))
#define wapp_array_extend_alloc(TYPE, ALLOCATOR_PTR, DST_ARRAY, SRC_ARRAY, FLAGS) \
((TYPE *)_array_extend_alloc(ALLOCATOR_PTR, \ ((TYPE *)_array_extend_alloc(ALLOCATOR_PTR, \
(GenericArray)DST_ARRAY, \ (GenericArray)DST_ARRAY, \
(GenericArray)SRC_ARRAY, \ (GenericArray)SRC_ARRAY, \
sizeof(TYPE))) FLAGS, \
#define wapp_array_copy_alloc(TYPE, ALLOCATOR_PTR, DST_ARRAY, SRC_ARRAY) \ sizeof(TYPE)))
#define wapp_array_copy_alloc(TYPE, ALLOCATOR_PTR, DST_ARRAY, SRC_ARRAY, FLAGS) \
((TYPE *)_array_copy_alloc(ALLOCATOR_PTR, \ ((TYPE *)_array_copy_alloc(ALLOCATOR_PTR, \
(GenericArray)DST_ARRAY, \ (GenericArray)DST_ARRAY, \
(GenericArray)SRC_ARRAY, \ (GenericArray)SRC_ARRAY, \
sizeof(TYPE))) FLAGS, \
sizeof(TYPE)))
#define wapp_array_clear(TYPE, ARRAY) \ #define wapp_array_clear(TYPE, ARRAY) \
(_array_clear((GenericArray)ARRAY, \ (_array_clear((GenericArray)ARRAY, \
sizeof(TYPE))) sizeof(TYPE)))
#define wapp_array_calc_alloc_size(TYPE, CAPACITY) _array_calc_alloc_size(CAPACITY, sizeof(TYPE)) #define wapp_array_calc_alloc_size(TYPE, CAPACITY) _array_calc_alloc_size(CAPACITY, sizeof(TYPE))
#define wapp_array_alloc_capacity(TYPE, ALLOCATOR_PTR, CAPACITY, FLAGS) \
((TYPE *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, FLAGS, sizeof(TYPE)))
#define wapp_array_from_preallcated_buffer(TYPE, BUFFER, BUFFER_SIZE) \
((TYPE *)_array_from_preallcated_buffer(BUFFER, BUFFER_SIZE, sizeof(TYPE)))
typedef struct header ArrayHeader; typedef struct header ArrayHeader;
struct header { struct header {
@@ -190,14 +195,19 @@ void _array_set(GenericArray array, u64 index, void *value, u64 item_siz
void _array_append_capped(GenericArray array, void *value, u64 item_size); void _array_append_capped(GenericArray array, void *value, u64 item_size);
void _array_extend_capped(GenericArray dst, const GenericArray src, u64 item_size); void _array_extend_capped(GenericArray dst, const GenericArray src, u64 item_size);
void _array_copy_capped(GenericArray dst, const GenericArray src, u64 item_size); void _array_copy_capped(GenericArray dst, const GenericArray src, u64 item_size);
GenericArray _array_append_alloc(const Allocator *allocator, GenericArray array, void *value, u64 item_size); GenericArray _array_append_alloc(const Allocator *allocator, GenericArray array, void *value,
GenericArray _array_extend_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src, u64 item_size); ArrayInitFlags flags, u64 item_size);
GenericArray _array_copy_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src, u64 item_size); GenericArray _array_extend_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src,
ArrayInitFlags flags, u64 item_size);
GenericArray _array_copy_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src,
ArrayInitFlags flags, u64 item_size);
void *_array_pop(GenericArray array, u64 item_size); void *_array_pop(GenericArray array, u64 item_size);
void _array_clear(GenericArray array, u64 item_size); void _array_clear(GenericArray array, u64 item_size);
u64 _array_calc_alloc_size(u64 capacity, u64 item_size); u64 _array_calc_alloc_size(u64 capacity, u64 item_size);
GenericArray _array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size, GenericArray _array_alloc_capacity(const Allocator *allocator, u64 capacity, ArrayInitFlags flags,
ArrayInitFlags flags); u64 item_size);
GenericArray _array_from_preallocated_buffer(void *buffer, u64 buffer_size, ArrayInitFlags flags,
u64 item_size);
#ifdef WAPP_PLATFORM_CPP #ifdef WAPP_PLATFORM_CPP
END_C_LINKAGE END_C_LINKAGE
+95 -7
View File
@@ -3,18 +3,106 @@
#include "queue.h" #include "queue.h"
#include "../array/array.h" #include "../array/array.h"
#include "../../common/assert/assert.h" #include "../../common/assert/assert.h"
#include "../../common/misc/misc_utils.h"
#include <string.h> #include <string.h>
void _queue_pop_front(GenericQueue *queue, void *output, u64 item_size) { void _queue_push(GenericQueue *queue, void *item, u64 item_size) {
wapp_debug_assert(queue != NULL && output != NULL, "`queue` and `output` should not be NULL"); wapp_debug_assert(queue != NULL, "`queue` should not be NULL");
wapp_runtime_assert(item_size == wapp_array_item_size(queue->items), "Invalid type"); wapp_runtime_assert(item_size == wapp_array_item_size(queue->items), "Invalid type");
memcpy(output, queue->items, item_size); u64 capacity = wapp_array_capacity(queue->items);
if (queue->count >= capacity) { return; }
u64 new_count = wapp_array_count(queue->items) - 1; u64 index = (queue->back)++;
for (u64 i = 0; i < new_count; ++i) { _array_set(queue->items, index, item, item_size);
_array_set(queue->items, i, _array_get(queue->items, i + 1, item_size), item_size); ++(queue->count);
if (queue->back >= capacity) {
queue->back = 0;
}
}
GenericQueue *_queue_push_alloc(const Allocator *allocator, GenericQueue *queue, void *item, u64 item_size) {
wapp_debug_assert(allocator != NULL && queue != NULL && item != NULL,
"`allocator`, `queue` and `item` should not be NULL");
wapp_runtime_assert(item_size == wapp_array_item_size(queue->items), "Invalid type");
GenericQueue *output = queue;
u64 capacity = wapp_array_capacity(queue->items);
// NOTE (Abdelrahman): Extracted into variable to fix MSVC error
b8 queue_full = queue->count >= capacity;
if (queue_full) {
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(capacity * 2);
u64 array_size = _array_calc_alloc_size(new_capacity, item_size);
u64 alloc_size = sizeof(GenericQueue) + array_size;
void *buffer = wapp_mem_allocator_alloc(allocator, alloc_size);
if (!buffer) {
goto RETURN_QUEUE_PUSH_ALLOC;
}
memset((void *)buffer, 0, alloc_size);
output = (GenericQueue *)buffer;
output->items = _array_from_preallocated_buffer((void *)(output + 1), array_size, ARRAY_INIT_FILLED, item_size);
// NOTE (Abdelrahman): When the queue is full, the front and back indices should
// always be the same
u64 front_count = capacity - queue->front;
u64 back_count = queue->back;
void *copy_boundary = (void *)((uptr)(queue->items) + (queue->front * item_size));
memcpy(output->items, copy_boundary, front_count * item_size);
/**
* NOTE (Abdelrahman): Since this is a ring buffer, the elements at the beginning of the array
* aren't always the ones at the front of the queue. When that's the case, the memcpy above
* will only copy a subset of the elements. This is why we need to copy the remaining ones.
*
* Example: Take a queue that looks like this with a capacity of 5 elements
*
* 0 1 | 2 3 4
* ---------------|-----------------------
* | * | * | * | * | * |
* ---------------|-----------------------
* |
* queue_front = 2
* queue_back = 2
*
* In this case, the first memcpy will only copy elements 2-4. The memcpy below will be
* responsible for copying elements 0-1.
*/
b8 items_left_to_copy = back_count > 0;
if (items_left_to_copy) {
void *back_copy_dst = (void *)((uptr)(output->items) + (front_count * item_size));
memcpy(back_copy_dst, queue->items, back_count * item_size);
}
output->front = 0;
output->back = front_count + back_count;
output->count = queue->count;
} }
wapp_array_set_count(queue->items, new_count); _queue_push(output, item, item_size);
RETURN_QUEUE_PUSH_ALLOC:
return output;
}
void *_queue_pop(GenericQueue *queue, u64 item_size) {
wapp_debug_assert(queue != NULL, "`queue` should not be NULL");
wapp_runtime_assert(item_size == wapp_array_item_size(queue->items), "Invalid type");
if (queue->count == 0) { return NULL; }
u64 index = (queue->front)++;
--(queue->count);
u64 capacity = wapp_array_capacity(queue->items);
if (queue->front >= capacity) {
queue->front = 0;
}
return _array_get(queue->items, index, item_size);
} }
+32 -13
View File
@@ -4,6 +4,7 @@
#define QUEUE_H #define QUEUE_H
#include "../array/array.h" #include "../array/array.h"
#include "../mem/allocator/mem_allocator.h"
#include "../../common/aliases/aliases.h" #include "../../common/aliases/aliases.h"
#include "../../common/platform/platform.h" #include "../../common/platform/platform.h"
@@ -13,6 +14,9 @@ BEGIN_C_LINKAGE
typedef struct { typedef struct {
GenericArray items; GenericArray items;
u64 front;
u64 back;
u64 count;
} GenericQueue; } GenericQueue;
// NOTE (Abdelrahman): GenericQueue typedefs for readability // NOTE (Abdelrahman): GenericQueue typedefs for readability
@@ -38,41 +42,56 @@ typedef GenericQueue Str8Queue;
#ifdef WAPP_PLATFORM_CPP #ifdef WAPP_PLATFORM_CPP
#define wapp_queue(TYPE, CAPACITY) ([&]() { \ #define wapp_queue(TYPE, CAPACITY) ([&]() { \
wapp_persist GenericArray arr = wapp_array_with_capacity(TYPE, CAPACITY, ARRAY_INIT_NONE); \ wapp_persist GenericArray arr = wapp_array_with_capacity(TYPE, CAPACITY, ARRAY_INIT_FILLED); \
wapp_persist GenericQueue queue = {arr}; \ wapp_persist GenericQueue queue = { \
arr, \
0, \
0, \
0, \
}; \
\ \
return queue; \ return queue; \
}()) }())
#define wapp_queue_alloc(TYPE, ALLOCATOR_PTR, CAPACITY) ([&]() { \ #define wapp_queue_alloc(TYPE, ALLOCATOR_PTR, CAPACITY) ([&]() { \
wapp_persist GenericQueue queue = { \ wapp_persist GenericQueue queue = { \
wapp_array_alloc_capacity(TYPE, ALLOCATOR_PTR, CAPACITY, ARRAY_INIT_NONE) \ wapp_array_alloc_capacity(TYPE, ALLOCATOR_PTR, CAPACITY, ARRAY_INIT_FILLED), \
0, \
0, \
0, \
}; \ }; \
\ \
return queue; \ return queue; \
}()) }())
#else #else
#define wapp_queue(TYPE, CAPACITY) ((GenericQueue){ \ #define wapp_queue(TYPE, CAPACITY) ((GenericQueue){ \
.items = wapp_array_with_capacity(TYPE, CAPACITY, ARRAY_INIT_NONE) \ .items = wapp_array_with_capacity(TYPE, CAPACITY, ARRAY_INIT_FILLED), \
.front = 0, \
.back = 0, \
.count = 0, \
}) })
#define wapp_queue_alloc(TYPE, ALLOCATOR_PTR, CAPACITY) ((GenericQueue){ \ #define wapp_queue_alloc(TYPE, ALLOCATOR_PTR, CAPACITY) ((GenericQueue){ \
.items = wapp_array_alloc_capacity(TYPE, ALLOCATOR_PTR, CAPACITY, ARRAY_INIT_NONE) \ .items = wapp_array_alloc_capacity(TYPE, ALLOCATOR_PTR, CAPACITY, ARRAY_INIT_FILLED), \
.front = 0, \
.back = 0, \
.count = 0, \
}) })
#endif // !WAPP_PLATFORM_CPP #endif // !WAPP_PLATFORM_CPP
#define wapp_queue_count(QUEUE_PTR) (wapp_array_count((QUEUE_PTR)->items))
#define wapp_queue_capacity(QUEUE_PTR) (wapp_array_capacity((QUEUE_PTR)->items)) #define wapp_queue_capacity(QUEUE_PTR) (wapp_array_capacity((QUEUE_PTR)->items))
#define wapp_queue_item_size(QUEUE_PTR) (wapp_array_item_size((QUEUE_PTR)->items)) #define wapp_queue_item_size(QUEUE_PTR) (wapp_array_item_size((QUEUE_PTR)->items))
#define wapp_queue_push_back(TYPE, QUEUE_PTR, VALUE_PTR) ( \ #define wapp_queue_push(TYPE, QUEUE_PTR, VALUE_PTR) ( \
wapp_array_append_capped(TYPE, (QUEUE_PTR)->items, VALUE_PTR) \ _queue_push(QUEUE_PTR, VALUE_PTR, sizeof(TYPE)) \
) )
#define wapp_queue_push_back_alloc(TYPE, ALLOCATOR_PTR, QUEUE_PTR, VALUE_PTR) ( \ #define wapp_queue_push_alloc(TYPE, ALLOCATOR_PTR, QUEUE_PTR, VALUE_PTR) ( \
wapp_array_append_alloc(TYPE, ALLOCATOR_PTR, (QUEUE_PTR)->items, VALUE_PTR) \ _queue_push_alloc(ALLOCATOR_PTR, QUEUE_PTR, VALUE_PTR, sizeof(TYPE)) \
) )
#define wapp_queue_pop_front(TYPE, QUEUE_PTR, OUTPUT_PTR) ( \ #define wapp_queue_pop(TYPE, QUEUE_PTR) ( \
_queue_pop_front(QUEUE_PTR, OUTPUT_PTR, sizeof(TYPE)) \ (TYPE *)_queue_pop(QUEUE_PTR, sizeof(TYPE)) \
) )
void _queue_pop_front(GenericQueue *queue, void *output, u64 item_size); void _queue_push(GenericQueue *queue, void *item, u64 item_size);
GenericQueue *_queue_push_alloc(const Allocator *allocator, GenericQueue *queue, void *item, u64 item_size);
void *_queue_pop(GenericQueue *queue, u64 item_size);
#ifdef WAPP_PLATFORM_CPP #ifdef WAPP_PLATFORM_CPP
END_C_LINKAGE END_C_LINKAGE
+1 -1
View File
@@ -60,7 +60,7 @@ typedef intptr_t iptr;
#ifdef WAPP_PLATFORM_CPP #ifdef WAPP_PLATFORM_CPP
#define wapp_class_mem static #define wapp_class_mem static
#define BEGIN_C_LINKAGE extern "C" { #define BEGIN_C_LINKAGE wapp_extern "C" {
#define END_C_LINKAGE } #define END_C_LINKAGE }
#endif // WAPP_PLATFORM_CPP #endif // WAPP_PLATFORM_CPP
+1 -1
View File
@@ -13,7 +13,7 @@
BEGIN_C_LINKAGE BEGIN_C_LINKAGE
#endif // !WAPP_PLATFORM_CPP #endif // !WAPP_PLATFORM_CPP
#define wapp_static_assert(EXPR, MSG) extern char ASSERTION_FAILED[EXPR ? 1 : -1] #define wapp_static_assert(EXPR, MSG) wapp_extern char ASSERTION_FAILED[EXPR ? 1 : -1]
#ifndef WAPP_NO_RUNTIME_ASSERT #ifndef WAPP_NO_RUNTIME_ASSERT
#define wapp_runtime_assert(EXPR, MSG) __wapp_runtime_assert(EXPR, MSG) #define wapp_runtime_assert(EXPR, MSG) __wapp_runtime_assert(EXPR, MSG)
+102
View File
@@ -0,0 +1,102 @@
// vim:fileencoding=utf-8:foldmethod=marker
#include "log.h"
#include "../common/aliases/aliases.h"
#include "../common/assert/assert.h"
#include "../os/file/file.h"
#include "common/misc/misc_utils.h"
#define LOG_LEVEL_STR_LENGTH 8
#define LOG_PREFIX_BUF_LENGTH 16
typedef struct {
WFile *outlog;
WFile *errlog;
LogLevel level;
wapp_misc_utils_reserve_padding(2 * sizeof(WFile *) + sizeof(LogLevel));
} LogConfig;
wapp_intern LogConfig LOG_CONFIG = {
.level = WAPP_LOG_DEBUG,
};
wapp_intern Str8RO LOG_LEVEL_STRINGS[COUNT_LOG_LEVEL] = {
[WAPP_LOG_FATAL] = wapp_str8_lit_ro_initialiser_list("[ FATAL ] "),
[WAPP_LOG_CRITICAL] = wapp_str8_lit_ro_initialiser_list("[ CRITICAL ] "),
[WAPP_LOG_ERROR] = wapp_str8_lit_ro_initialiser_list("[ ERROR ] "),
[WAPP_LOG_WARNING] = wapp_str8_lit_ro_initialiser_list("[ WARNING ] "),
[WAPP_LOG_INFO] = wapp_str8_lit_ro_initialiser_list("[ INFO ] "),
[WAPP_LOG_DEBUG] = wapp_str8_lit_ro_initialiser_list("[ DEBUG ] "),
};
wapp_intern void _write_log_line(WFile *fp, const Logger *logger, Str8 msg, LogLevel level);
void wapp_log_set_level(LogLevel level) {
LOG_CONFIG.level = level;
}
void wapp_log_configure(WFile *outlog, WFile *errlog, LogLevel level) {
LOG_CONFIG.outlog = outlog;
LOG_CONFIG.errlog = errlog;
LOG_CONFIG.level = level;
}
Logger wapp_log_make_logger(Str8 name) {
return (Logger){ .name = name };
}
void wapp_log_debug(const Logger *logger, Str8 msg) {
wapp_debug_assert(logger != NULL, "`logger` should not be NULL");
if (LOG_CONFIG.level < WAPP_LOG_DEBUG) { return; }
WFile *fp = LOG_CONFIG.outlog != NULL ? LOG_CONFIG.outlog : wapp_file_stdout();
_write_log_line(fp, logger, msg, WAPP_LOG_DEBUG);
}
void wapp_log_info(const Logger *logger, Str8 msg) {
wapp_debug_assert(logger != NULL, "`logger` should not be NULL");
if (LOG_CONFIG.level < WAPP_LOG_INFO) { return; }
WFile *fp = LOG_CONFIG.outlog != NULL ? LOG_CONFIG.outlog : wapp_file_stdout();
_write_log_line(fp, logger, msg, WAPP_LOG_INFO);
}
void wapp_log_warning(const Logger *logger, Str8 msg) {
wapp_debug_assert(logger != NULL, "`logger` should not be NULL");
if (LOG_CONFIG.level < WAPP_LOG_WARNING) { return; }
WFile *fp = LOG_CONFIG.outlog != NULL ? LOG_CONFIG.outlog : wapp_file_stdout();
_write_log_line(fp, logger, msg, WAPP_LOG_WARNING);
}
void wapp_log_error(const Logger *logger, Str8 msg) {
wapp_debug_assert(logger != NULL, "`logger` should not be NULL");
if (LOG_CONFIG.level < WAPP_LOG_ERROR) { return; }
WFile *fp = LOG_CONFIG.errlog != NULL ? LOG_CONFIG.errlog : wapp_file_stderr();
_write_log_line(fp, logger, msg, WAPP_LOG_ERROR);
}
void wapp_log_critical(const Logger *logger, Str8 msg) {
wapp_debug_assert(logger != NULL, "`logger` should not be NULL");
if (LOG_CONFIG.level < WAPP_LOG_CRITICAL) { return; }
WFile *fp = LOG_CONFIG.errlog != NULL ? LOG_CONFIG.errlog : wapp_file_stderr();
_write_log_line(fp, logger, msg, WAPP_LOG_CRITICAL);
}
void wapp_log_fatal(const Logger *logger, Str8 msg) {
wapp_debug_assert(logger != NULL, "`logger` should not be NULL");
if (LOG_CONFIG.level < WAPP_LOG_FATAL) { return; }
WFile *fp = LOG_CONFIG.errlog != NULL ? LOG_CONFIG.errlog : wapp_file_stderr();
_write_log_line(fp, logger, msg, WAPP_LOG_FATAL);
}
wapp_intern void _write_log_line(WFile *fp, const Logger *logger, Str8 msg, LogLevel level) {
wapp_file_write((void *)LOG_LEVEL_STRINGS[level].buf, fp, LOG_LEVEL_STRINGS[level].size);
wapp_file_write((void *)logger->name.buf, fp, logger->name.size);
wapp_file_write((void *)": ", fp, 2);
wapp_file_write((void *)msg.buf, fp, msg.size);
wapp_file_write((void *)"\n", fp, 1);
}
+34
View File
@@ -0,0 +1,34 @@
// vim:fileencoding=utf-8:foldmethod=marker
#ifndef LOG_H
#define LOG_H
#include "../os/file/file.h"
#include "../base/strings/str8/str8.h"
typedef enum {
WAPP_LOG_FATAL,
WAPP_LOG_CRITICAL,
WAPP_LOG_ERROR,
WAPP_LOG_WARNING,
WAPP_LOG_INFO,
WAPP_LOG_DEBUG,
COUNT_LOG_LEVEL,
} LogLevel;
typedef struct {
Str8 name;
} Logger;
void wapp_log_set_level(LogLevel level);
void wapp_log_configure(WFile *outlog, WFile *errlog, LogLevel level);
Logger wapp_log_make_logger(Str8 name);
void wapp_log_debug(const Logger *logger, Str8 msg);
void wapp_log_info(const Logger *logger, Str8 msg);
void wapp_log_warning(const Logger *logger, Str8 msg);
void wapp_log_error(const Logger *logger, Str8 msg);
void wapp_log_critical(const Logger *logger, Str8 msg);
void wapp_log_fatal(const Logger *logger, Str8 msg);
#endif // !LOG_H
+10
View File
@@ -0,0 +1,10 @@
// vim:fileencoding=utf-8:foldmethod=marker
#ifndef WAPP_LOG_C
#define WAPP_LOG_C
#include "log.c"
#include "../base/wapp_base.c"
#include "../os/wapp_os.c"
#endif // !WAPP_LOG_C
+11
View File
@@ -0,0 +1,11 @@
// vim:fileencoding=utf-8:foldmethod=marker
#ifndef WAPP_LOG_H
#define WAPP_LOG_H
#include "log.h"
#include "../common/wapp_common.h"
#include "../base/wapp_base.h"
#include "../os/wapp_os.h"
#endif // !WAPP_LOG_H
+20 -8
View File
@@ -34,6 +34,18 @@ typedef enum {
FILE_SEEK_ORIGIN_COUNT, FILE_SEEK_ORIGIN_COUNT,
} FileSeekOrigin; } FileSeekOrigin;
// Return value should not be cached as it's not guaranteed to remain the same. Always call
// wapp_file_stdin to get the standard input stream
wapp_extern WFile *wapp_file_stdin(void);
// Return value should not be cached as it's not guaranteed to remain the same. Always call
// wapp_file_stdout to get the standard output stream
wapp_extern WFile *wapp_file_stdout(void);
// Return value should not be cached as it's not guaranteed to remain the same. Always call
// wapp_file_stderr to get the standard error stream
wapp_extern WFile *wapp_file_stderr(void);
WFile *wapp_file_open(const Allocator *allocator, Str8RO *filepath, FileAccessMode mode); WFile *wapp_file_open(const Allocator *allocator, Str8RO *filepath, FileAccessMode mode);
i64 wapp_file_get_current_position(WFile *file); i64 wapp_file_get_current_position(WFile *file);
i64 wapp_file_seek(WFile *file, i64 offset, FileSeekOrigin origin); i64 wapp_file_seek(WFile *file, i64 offset, FileSeekOrigin origin);
@@ -47,14 +59,14 @@ i32 wapp_file_close(WFile *file);
i32 wapp_file_rename(Str8RO *old_filepath, Str8RO *new_filepath); i32 wapp_file_rename(Str8RO *old_filepath, Str8RO *new_filepath);
i32 wapp_file_remove(Str8RO *filepath); i32 wapp_file_remove(Str8RO *filepath);
extern WFile *_file_open(const Allocator *allocator, Str8RO *filepath, FileAccessMode mode); wapp_extern WFile *_file_open(const Allocator *allocator, Str8RO *filepath, FileAccessMode mode);
extern i64 _file_seek(WFile *file, i64 offset, FileSeekOrigin origin); wapp_extern i64 _file_seek(WFile *file, i64 offset, FileSeekOrigin origin);
extern u64 _file_read(void *dst_buf, u64 byte_count, WFile *file, u64 file_length); wapp_extern u64 _file_read(void *dst_buf, u64 byte_count, WFile *file, u64 file_length);
extern i64 _file_write(const void *src_buf, WFile *file, u64 byte_count); wapp_extern i64 _file_write(const void *src_buf, WFile *file, u64 byte_count);
extern i32 _file_flush(WFile *file); wapp_extern i32 _file_flush(WFile *file);
extern i32 _file_close(WFile *file); wapp_extern i32 _file_close(WFile *file);
extern i32 _file_rename(Str8RO *old_filepath, Str8RO *new_filepath); wapp_extern i32 _file_rename(Str8RO *old_filepath, Str8RO *new_filepath);
extern i32 _file_remove(Str8RO *filepath); wapp_extern i32 _file_remove(Str8RO *filepath);
#ifdef WAPP_PLATFORM_CPP #ifdef WAPP_PLATFORM_CPP
END_C_LINKAGE END_C_LINKAGE
+22
View File
@@ -10,6 +10,13 @@
#include "../../../common/aliases/aliases.h" #include "../../../common/aliases/aliases.h"
#include "../../../base/array/array.h" #include "../../../base/array/array.h"
#include "../../../base/strings/str8/str8.h" #include "../../../base/strings/str8/str8.h"
#ifdef WAPP_PLATFORM_APPLE
#define _FILE_OFFSET_BITS 64
#define lseek64 lseek
#endif // !WAPP_PLATFORM_APPLE
#define __USE_LARGEFILE64
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
@@ -42,6 +49,21 @@ wapp_intern i32 file_seek_origins[FILE_SEEK_ORIGIN_COUNT] = {
[WAPP_SEEK_END] = SEEK_END, [WAPP_SEEK_END] = SEEK_END,
}; };
WFile *wapp_file_stdin(void) {
wapp_persist WFile _stdin = { .fd = STDIN_FILENO };
return &_stdin;
}
WFile *wapp_file_stdout(void) {
wapp_persist WFile _stdout = { .fd = STDOUT_FILENO };
return &_stdout;
}
WFile *wapp_file_stderr(void) {
wapp_persist WFile _stderr = { .fd = STDERR_FILENO };
return &_stderr;
}
WFile *_file_open(const Allocator *allocator, Str8RO *filepath, FileAccessMode mode) { WFile *_file_open(const Allocator *allocator, Str8RO *filepath, FileAccessMode mode) {
wapp_persist c8 tmp[WAPP_PATH_MAX] = {0}; wapp_persist c8 tmp[WAPP_PATH_MAX] = {0};
memset(tmp, 0, WAPP_PATH_MAX); memset(tmp, 0, WAPP_PATH_MAX);
+18
View File
@@ -54,6 +54,24 @@ wapp_intern DWORD file_seek_origins[FILE_SEEK_ORIGIN_COUNT] = {
[WAPP_SEEK_END] = FILE_END, [WAPP_SEEK_END] = FILE_END,
}; };
WFile *wapp_file_stdin(void) {
wapp_persist WFile _stdin = { .fh = INVALID_HANDLE_VALUE };
_stdin.fh = GetStdHandle(STD_INPUT_HANDLE);
return &_stdin;
}
WFile *wapp_file_stdout(void) {
wapp_persist WFile _stdout = { .fh = INVALID_HANDLE_VALUE };
_stdout.fh = GetStdHandle(STD_OUTPUT_HANDLE);
return &_stdout;
}
WFile *wapp_file_stderr(void) {
wapp_persist WFile _stderr = { .fh = INVALID_HANDLE_VALUE };
_stderr.fh = GetStdHandle(STD_ERROR_HANDLE);
return &_stderr;
}
WFile *_file_open(const Allocator *allocator, Str8RO *filepath, FileAccessMode mode) { WFile *_file_open(const Allocator *allocator, Str8RO *filepath, FileAccessMode mode) {
wapp_persist c8 tmp[WAPP_PATH_MAX] = {0}; wapp_persist c8 tmp[WAPP_PATH_MAX] = {0};
memset(tmp, 0, WAPP_PATH_MAX); memset(tmp, 0, WAPP_PATH_MAX);
+1 -1
View File
@@ -86,7 +86,7 @@ EXECUTE_COMMAND_CLOSE:
wapp_intern CMDError get_command_output(FILE *fp, CMDOutHandling out_handling, Str8 *out_buf) { wapp_intern CMDError get_command_output(FILE *fp, CMDOutHandling out_handling, Str8 *out_buf) {
Str8 out = wapp_str8_buf(OUT_BUF_LEN); Str8 out = wapp_str8_buf(OUT_BUF_LEN);
out.size = fread((void *)out.buf, sizeof(u8), out.capacity, fp); out.size = fread((void *)out.buf, sizeof(c8), out.capacity, fp);
if (out_handling == SHELL_OUTPUT_CAPTURE && out_buf != NULL) { if (out_handling == SHELL_OUTPUT_CAPTURE && out_buf != NULL) {
if (out.size >= out_buf->capacity) { if (out.size >= out_buf->capacity) {
return SHELL_ERR_OUT_BUF_FULL; return SHELL_ERR_OUT_BUF_FULL;
+2
View File
@@ -10,6 +10,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
// TODO (Abdelrahman): This module needs rethinking
#ifdef WAPP_PLATFORM_CPP #ifdef WAPP_PLATFORM_CPP
BEGIN_C_LINKAGE BEGIN_C_LINKAGE
#endif // !WAPP_PLATFORM_CPP #endif // !WAPP_PLATFORM_CPP
+2
View File
@@ -12,6 +12,8 @@
BEGIN_C_LINKAGE BEGIN_C_LINKAGE
#endif // !WAPP_PLATFORM_CPP #endif // !WAPP_PLATFORM_CPP
// TODO (Abdelrahman): Look into moving away from stdio in the implementation
void wapp_shell_termcolour_print_text(Str8RO *text, TerminalColour colour); void wapp_shell_termcolour_print_text(Str8RO *text, TerminalColour colour);
void wapp_shell_termcolour_clear_colour(void); void wapp_shell_termcolour_clear_colour(void);
+13
View File
@@ -20,11 +20,24 @@ struct WUUID {
Str8 uuid; Str8 uuid;
}; };
// TODO (Abdelrahman): Update UUID implementation to work properly with C++ and tests for validation
#ifdef WAPP_PLATFORM_CPP
#define wapp_uuid_gen_uuid4() ([&](){ \
wapp_persist WUUID uuid = wapp_uuid_create(); \
return *(wapp_uuid_init_uuid4(&uuid)); \
}())
#else
#define wapp_uuid_gen_uuid4() *(wapp_uuid_init_uuid4(&wapp_uuid_create())) #define wapp_uuid_gen_uuid4() *(wapp_uuid_init_uuid4(&wapp_uuid_create()))
#endif
/* Low level UUID API */ /* Low level UUID API */
#ifdef WAPP_PLATFORM_CPP
#define wapp_uuid_create() ([&](){ return WUUID{wapp_str8_buf(UUID_BUF_LENGTH)}; }())
#else
#define wapp_uuid_create() ((WUUID){.uuid = wapp_str8_buf(UUID_BUF_LENGTH)}) #define wapp_uuid_create() ((WUUID){.uuid = wapp_str8_buf(UUID_BUF_LENGTH)})
#endif
// Just returns the same pointer that was passed in with the UUID initialised. // Just returns the same pointer that was passed in with the UUID initialised.
// Fails when passed a NULL pointer. // Fails when passed a NULL pointer.
+2 -1
View File
@@ -6,8 +6,9 @@
#include "wapp.h" #include "wapp.h"
#include "base/wapp_base.c" #include "base/wapp_base.c"
#include "os/wapp_os.c" #include "os/wapp_os.c"
#include "log/wapp_log.c"
#include "prng/wapp_prng.c" #include "prng/wapp_prng.c"
#include "uuid/uuid.c" #include "uuid/wapp_uuid.c"
#include "testing/wapp_testing.c" #include "testing/wapp_testing.c"
#endif // !WAPP_C #endif // !WAPP_C
+1
View File
@@ -6,6 +6,7 @@
#include "common/wapp_common.h" #include "common/wapp_common.h"
#include "base/wapp_base.h" #include "base/wapp_base.h"
#include "os/wapp_os.h" #include "os/wapp_os.h"
#include "log/wapp_log.h"
#include "prng/wapp_prng.h" #include "prng/wapp_prng.h"
#include "uuid/wapp_uuid.h" #include "uuid/wapp_uuid.h"
#include "testing/wapp_testing.h" #include "testing/wapp_testing.h"
+6 -6
View File
@@ -166,7 +166,7 @@ TestFuncResult test_i32_array_append_alloc(void) {
I32Array array1 = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7, 8); I32Array array1 = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7, 8);
I32Array array2 = wapp_array(i32, 1, 2); I32Array array2 = wapp_array(i32, 1, 2);
I32Array arr_ptr = wapp_array_append_alloc(i32, &allocator, array1, &((i32){10})); I32Array arr_ptr = wapp_array_append_alloc(i32, &allocator, array1, &((i32){10}), ARRAY_INIT_NONE);
result = arr_ptr == array1; result = arr_ptr == array1;
u64 count = 4; u64 count = 4;
@@ -174,7 +174,7 @@ TestFuncResult test_i32_array_append_alloc(void) {
b8 running = true; b8 running = true;
while (running) { while (running) {
i32 num = (i32)index; i32 num = (i32)index;
arr_ptr = wapp_array_append_alloc(i32, &allocator, array2, &num); arr_ptr = wapp_array_append_alloc(i32, &allocator, array2, &num, ARRAY_INIT_NONE);
++index; ++index;
running = index < count; running = index < count;
@@ -194,10 +194,10 @@ TestFuncResult test_i32_array_extend_alloc(void) {
I32Array array2 = wapp_array(i32, 1, 2); I32Array array2 = wapp_array(i32, 1, 2);
I32Array array3 = wapp_array(i32, 1, 2, 3, 4); I32Array array3 = wapp_array(i32, 1, 2, 3, 4);
I32Array arr_ptr = wapp_array_extend_alloc(i32, &allocator, array1, array3); I32Array arr_ptr = wapp_array_extend_alloc(i32, &allocator, array1, array3, ARRAY_INIT_NONE);
result = arr_ptr == array1; result = arr_ptr == array1;
arr_ptr = wapp_array_extend_alloc(i32, &allocator, array2, array3); arr_ptr = wapp_array_extend_alloc(i32, &allocator, array2, array3, ARRAY_INIT_NONE);
result = result && arr_ptr != array2; result = result && arr_ptr != array2;
wapp_mem_arena_allocator_destroy(&allocator); wapp_mem_arena_allocator_destroy(&allocator);
@@ -215,7 +215,7 @@ TestFuncResult test_i32_array_copy_alloc(void) {
I32Array array = NULL; I32Array array = NULL;
u64 expected_count = 5; u64 expected_count = 5;
array = wapp_array_copy_alloc(i32, &allocator, dst1, src); array = wapp_array_copy_alloc(i32, &allocator, dst1, src, ARRAY_INIT_NONE);
result = wapp_array_count(array) == expected_count && array == dst1; result = wapp_array_count(array) == expected_count && array == dst1;
u64 index = 0; u64 index = 0;
@@ -228,7 +228,7 @@ TestFuncResult test_i32_array_copy_alloc(void) {
} }
expected_count = 5; expected_count = 5;
array = wapp_array_copy_alloc(i32, &allocator, dst2, src); array = wapp_array_copy_alloc(i32, &allocator, dst2, src, ARRAY_INIT_NONE);
result = result && wapp_array_count(array) == expected_count && array != dst2; result = result && wapp_array_count(array) == expected_count && array != dst2;
index = 0; index = 0;
+6 -6
View File
@@ -169,7 +169,7 @@ TestFuncResult test_i32_array_append_alloc(void) {
I32Array array2 = wapp_array(i32, 1, 2); I32Array array2 = wapp_array(i32, 1, 2);
i32 num = 10; i32 num = 10;
I32Array arr_ptr = wapp_array_append_alloc(i32, &allocator, array1, &num); I32Array arr_ptr = wapp_array_append_alloc(i32, &allocator, array1, &num, ARRAY_INIT_NONE);
result = arr_ptr == array1; result = arr_ptr == array1;
u64 count = 4; u64 count = 4;
@@ -177,7 +177,7 @@ TestFuncResult test_i32_array_append_alloc(void) {
b8 running = true; b8 running = true;
while (running) { while (running) {
num = (i32)index; num = (i32)index;
arr_ptr = wapp_array_append_alloc(i32, &allocator, array2, &num); arr_ptr = wapp_array_append_alloc(i32, &allocator, array2, &num, ARRAY_INIT_NONE);
++index; ++index;
running = index < count; running = index < count;
@@ -197,10 +197,10 @@ TestFuncResult test_i32_array_extend_alloc(void) {
I32Array array2 = wapp_array(i32, 1, 2); I32Array array2 = wapp_array(i32, 1, 2);
I32Array array3 = wapp_array(i32, 1, 2, 3, 4); I32Array array3 = wapp_array(i32, 1, 2, 3, 4);
I32Array array = wapp_array_extend_alloc(i32, &allocator, array1, array3); I32Array array = wapp_array_extend_alloc(i32, &allocator, array1, array3, ARRAY_INIT_NONE);
result = array == array1; result = array == array1;
array = wapp_array_extend_alloc(i32, &allocator, array2, array3); array = wapp_array_extend_alloc(i32, &allocator, array2, array3, ARRAY_INIT_NONE);
result = result && array != array2; result = result && array != array2;
wapp_mem_arena_allocator_destroy(&allocator); wapp_mem_arena_allocator_destroy(&allocator);
@@ -218,7 +218,7 @@ TestFuncResult test_i32_array_copy_alloc(void) {
I32Array array = nullptr; I32Array array = nullptr;
u64 expected_count = 5; u64 expected_count = 5;
array = wapp_array_copy_alloc(i32, &allocator, dst1, src); array = wapp_array_copy_alloc(i32, &allocator, dst1, src, ARRAY_INIT_NONE);
result = wapp_array_count(array) == expected_count && array == dst1; result = wapp_array_count(array) == expected_count && array == dst1;
u64 index = 0; u64 index = 0;
@@ -231,7 +231,7 @@ TestFuncResult test_i32_array_copy_alloc(void) {
} }
expected_count = 5; expected_count = 5;
array = wapp_array_copy_alloc(i32, &allocator, dst2, src); array = wapp_array_copy_alloc(i32, &allocator, dst2, src, ARRAY_INIT_NONE);
result = result && wapp_array_count(array) == expected_count && array != dst2; result = result && wapp_array_count(array) == expected_count && array != dst2;
index = 0; index = 0;
+75 -40
View File
@@ -3,61 +3,96 @@
#include "wapp.h" #include "wapp.h"
#include "test_queue.h" #include "test_queue.h"
wapp_persist Allocator arena = {0}; #define CAPACITY 8
wapp_persist I32Queue queue;
TestFuncResult test_queue_push_back(void) { TestFuncResult test_queue_push(void) {
arena = wapp_mem_arena_allocator_init(MB(64)); I32Queue queue = wapp_queue(i32, CAPACITY);
for (u64 i = 0; i < CAPACITY; ++i) {
i32 item = (i32)i;
wapp_queue_push(i32, &queue, &item);
}
b8 result = true; b8 result = true;
queue = wapp_queue_alloc(i32, &arena, 64); for (u64 i = 0; i < CAPACITY; ++i) {
i32 *value = ((i32 *)(queue.items)) + i;
result = result && queue.items != NULL && wapp_queue_capacity(&queue) == 64 && wapp_queue_count(&queue) == 0; result = result && value != NULL && *value == (i32)i;
}
i32 n1 = 1;
i32 n2 = 2;
i32 n3 = 3;
i32 n4 = 4;
i32 n5 = 5;
wapp_queue_push_back(i32, &queue, &n1);
result = result && wapp_queue_count(&queue) == 1;
wapp_queue_push_back(i32, &queue, &n2);
result = result && wapp_queue_count(&queue) == 2;
wapp_queue_push_back(i32, &queue, &n3);
result = result && wapp_queue_count(&queue) == 3;
wapp_queue_push_back(i32, &queue, &n4);
result = result && wapp_queue_count(&queue) == 4;
wapp_queue_push_back(i32, &queue, &n5);
result = result && wapp_queue_count(&queue) == 5;
return wapp_tester_result(result); return wapp_tester_result(result);
} }
TestFuncResult test_queue_pop_front(void) { TestFuncResult test_queue_push_alloc(void) {
Allocator arena = wapp_mem_arena_allocator_init(MiB(64));
I32Queue queue = wapp_queue(i32, CAPACITY);
for (u64 i = 0; i < CAPACITY; ++i) {
i32 item = (i32)i;
wapp_queue_push(i32, &queue, &item);
}
b8 result = true; b8 result = true;
i32 num; i32 item = 8;
u64 new_capacity = CAPACITY * 2;
I32Queue *new_queue = wapp_queue_push_alloc(i32, &arena, &queue, &item);
if (new_queue && new_queue != &queue) {
queue = *new_queue;
}
u64 capacity = wapp_queue_capacity(&queue);
result = result && capacity == new_capacity;
wapp_queue_pop_front(i32, &queue, &num); for (u64 i = 0; i < 2; ++i) {
result = result && num == 1; wapp_queue_pop(i32, &queue);
}
wapp_queue_pop_front(i32, &queue, &num); u64 remaining = wapp_queue_capacity(&queue) - queue.count;
result = result && num == 2; for (u64 i = 0; i < remaining; ++i) {
item = (i32)(remaining + i);
wapp_queue_push(i32, &queue, &item);
}
wapp_queue_pop_front(i32, &queue, &num); ++item;
result = result && num == 3; new_queue = wapp_queue_push_alloc(i32, &arena, &queue, &item);
if (new_queue && new_queue != &queue) {
queue = *new_queue;
}
wapp_queue_pop_front(i32, &queue, &num); result = result && wapp_queue_capacity(&queue) == new_capacity * 2;
result = result && num == 4;
wapp_queue_pop_front(i32, &queue, &num); i32 *arr = (i32 *)queue.items;
result = result && num == 5; for (u64 i = 0; i < queue.count; ++i) {
// NOTE (Abdelrahman): First queue value is currently 2
result = result && arr[i] == (i32)(2 + i);
}
wapp_mem_arena_allocator_destroy(&arena); wapp_mem_arena_allocator_destroy(&arena);
return wapp_tester_result(result); return wapp_tester_result(result);
} }
TestFuncResult test_queue_pop(void) {
I32Queue queue = wapp_queue(i32, CAPACITY);
for (u64 i = 0; i < CAPACITY; ++i) {
i32 item = (i32)i;
wapp_queue_push(i32, &queue, &item);
}
b8 result = true;
u64 half_count = queue.count / 2;
for (u64 i = 0; i < half_count; ++i) {
i32 *value = wapp_queue_pop(i32, &queue);
result = result && value != NULL && *value == (i32)i;
}
for (u64 i = 0; i < half_count; ++i) {
i32 item = (i32)i + CAPACITY;
wapp_queue_push(i32, &queue, &item);
}
for (u64 i = 0; i < CAPACITY; ++i) {
i32 *value = wapp_queue_pop(i32, &queue);
result = result && value != NULL && *value == (i32)half_count + (i32)i;
}
return wapp_tester_result(result);
}
+75 -40
View File
@@ -1,61 +1,96 @@
#include "wapp.h" #include "wapp.h"
#include "test_queue.h" #include "test_queue.h"
wapp_persist Allocator arena = {}; #define CAPACITY 8
wapp_persist I32Queue queue;
TestFuncResult test_queue_push_back(void) { TestFuncResult test_queue_push(void) {
arena = wapp_mem_arena_allocator_init(MB(64)); I32Queue queue = wapp_queue(i32, CAPACITY);
for (u64 i = 0; i < CAPACITY; ++i) {
i32 item = (i32)i;
wapp_queue_push(i32, &queue, &item);
}
b8 result = true; b8 result = true;
queue = wapp_queue_alloc(i32, &arena, 64); for (u64 i = 0; i < CAPACITY; ++i) {
i32 *value = ((i32 *)(queue.items)) + i;
result = result && queue.items != NULL && wapp_queue_capacity(&queue) == 64 && wapp_queue_count(&queue) == 0; result = result && value != NULL && *value == (i32)i;
}
i32 n1 = 1;
i32 n2 = 2;
i32 n3 = 3;
i32 n4 = 4;
i32 n5 = 5;
wapp_queue_push_back(i32, &queue, &n1);
result = result && wapp_queue_count(&queue) == 1;
wapp_queue_push_back(i32, &queue, &n2);
result = result && wapp_queue_count(&queue) == 2;
wapp_queue_push_back(i32, &queue, &n3);
result = result && wapp_queue_count(&queue) == 3;
wapp_queue_push_back(i32, &queue, &n4);
result = result && wapp_queue_count(&queue) == 4;
wapp_queue_push_back(i32, &queue, &n5);
result = result && wapp_queue_count(&queue) == 5;
return wapp_tester_result(result); return wapp_tester_result(result);
} }
TestFuncResult test_queue_pop_front(void) { TestFuncResult test_queue_push_alloc(void) {
Allocator arena = wapp_mem_arena_allocator_init(MiB(64));
I32Queue queue = wapp_queue(i32, CAPACITY);
for (u64 i = 0; i < CAPACITY; ++i) {
i32 item = (i32)i;
wapp_queue_push(i32, &queue, &item);
}
b8 result = true; b8 result = true;
i32 num; i32 item = 8;
u64 new_capacity = CAPACITY * 2;
I32Queue *new_queue = wapp_queue_push_alloc(i32, &arena, &queue, &item);
if (new_queue && new_queue != &queue) {
queue = *new_queue;
}
u64 capacity = wapp_queue_capacity(&queue);
result = result && capacity == new_capacity;
wapp_queue_pop_front(i32, &queue, &num); for (u64 i = 0; i < 2; ++i) {
result = result && num == 1; wapp_queue_pop(i32, &queue);
}
wapp_queue_pop_front(i32, &queue, &num); u64 remaining = wapp_queue_capacity(&queue) - queue.count;
result = result && num == 2; for (u64 i = 0; i < remaining; ++i) {
item = (i32)(remaining + i);
wapp_queue_push(i32, &queue, &item);
}
wapp_queue_pop_front(i32, &queue, &num); ++item;
result = result && num == 3; new_queue = wapp_queue_push_alloc(i32, &arena, &queue, &item);
if (new_queue && new_queue != &queue) {
queue = *new_queue;
}
wapp_queue_pop_front(i32, &queue, &num); result = result && wapp_queue_capacity(&queue) == new_capacity * 2;
result = result && num == 4;
wapp_queue_pop_front(i32, &queue, &num); i32 *arr = (i32 *)queue.items;
result = result && num == 5; for (u64 i = 0; i < queue.count; ++i) {
// NOTE (Abdelrahman): First queue value is currently 2
result = result && arr[i] == (i32)(2 + i);
}
wapp_mem_arena_allocator_destroy(&arena); wapp_mem_arena_allocator_destroy(&arena);
return wapp_tester_result(result); return wapp_tester_result(result);
} }
TestFuncResult test_queue_pop(void) {
I32Queue queue = wapp_queue(i32, CAPACITY);
for (u64 i = 0; i < CAPACITY; ++i) {
i32 item = (i32)i;
wapp_queue_push(i32, &queue, &item);
}
b8 result = true;
u64 half_count = queue.count / 2;
for (u64 i = 0; i < half_count; ++i) {
i32 *value = wapp_queue_pop(i32, &queue);
result = result && value != NULL && *value == (i32)i;
}
for (u64 i = 0; i < half_count; ++i) {
i32 item = (i32)i + CAPACITY;
wapp_queue_push(i32, &queue, &item);
}
for (u64 i = 0; i < CAPACITY; ++i) {
i32 *value = wapp_queue_pop(i32, &queue);
result = result && value != NULL && *value == (i32)half_count + (i32)i;
}
return wapp_tester_result(result);
}
+3 -2
View File
@@ -5,7 +5,8 @@
#include "wapp.h" #include "wapp.h"
TestFuncResult test_queue_push_back(void); TestFuncResult test_queue_push(void);
TestFuncResult test_queue_pop_front(void); TestFuncResult test_queue_push_alloc(void);
TestFuncResult test_queue_pop(void);
#endif // !TEST_QUEUE_H #endif // !TEST_QUEUE_H
+3 -2
View File
@@ -44,8 +44,9 @@ int main(void) {
test_i32_array_copy_alloc, test_i32_array_copy_alloc,
test_i32_array_pop, test_i32_array_pop,
test_i32_array_clear, test_i32_array_clear,
test_queue_push_back, test_queue_push,
test_queue_pop_front, test_queue_push_alloc,
test_queue_pop,
test_str8_lit, test_str8_lit,
test_str8_lit_ro, test_str8_lit_ro,
test_str8_buf, test_str8_buf,
+3 -2
View File
@@ -44,8 +44,9 @@ int main(void) {
test_i32_array_copy_alloc, test_i32_array_copy_alloc,
test_i32_array_pop, test_i32_array_pop,
test_i32_array_clear, test_i32_array_clear,
test_queue_push_back, test_queue_push,
test_queue_pop_front, test_queue_push_alloc,
test_queue_pop,
test_str8_lit, test_str8_lit,
test_str8_lit_ro, test_str8_lit_ro,
test_str8_buf, test_str8_buf,