mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 20:11:17 +00:00
Compare commits
188 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bdc9f339c9 | ||
|
|
3188d4d858 | ||
|
|
1bb76b1a49 | ||
|
|
9584efd84c | ||
|
|
c84bc8a8f3 | ||
|
|
edc55a2afd | ||
|
|
480325177c | ||
|
|
03cfa42eb6 | ||
|
|
b25c573ae3 | ||
|
|
7f5757d66b | ||
|
|
a3a275c3d5 | ||
|
|
557f007b12 | ||
|
|
542406c16f | ||
|
|
1fa870411f | ||
|
|
c096487eea | ||
|
|
97a8e0cdd4 | ||
|
|
eb20a5c051 | ||
|
|
5c6acf89da | ||
|
|
9dfe7ddbde | ||
|
|
8285720180 | ||
|
|
a4a1a42842 | ||
|
|
3c3217ab2b | ||
|
|
17ee3887dd | ||
|
|
db75da65db | ||
|
|
cf95257c81 | ||
|
|
b40036c203 | ||
|
|
b18661a8b0 | ||
|
|
bc0d52142a | ||
|
|
24041ed80d | ||
|
|
1a03e6b22e | ||
|
|
68fb916195 | ||
|
|
dfb8a1b8cb | ||
|
|
6c38409c57 | ||
|
|
27fd7a9088 | ||
|
|
0e62423e06 | ||
|
|
3f45ed14b9 | ||
|
|
ca4b782912 | ||
|
|
1976a11154 | ||
|
|
e7d8f64a49 | ||
|
|
5cf1f13328 | ||
|
|
fba706f10b | ||
|
|
c50630989e | ||
|
|
3832be94d0 | ||
|
|
900c1152d3 | ||
|
|
4ea50a8a85 | ||
|
|
0132fd4101 | ||
|
|
0e90ce3b8a | ||
|
|
9368ebfbd3 | ||
|
|
8381dbbd8f | ||
|
|
343ccaa2ef | ||
|
|
3f62775f4b | ||
|
|
c3ecad96b7 | ||
|
|
2ffb0cf5f7 | ||
|
|
ef716f3a69 | ||
|
|
cc935862b7 | ||
|
|
85a535dd0c | ||
|
|
ab626fe3eb | ||
|
|
05011df13a | ||
|
|
fcdb25c426 | ||
|
|
cc9ca35e04 | ||
|
|
4a50de8318 | ||
|
|
12051e7544 | ||
|
|
210508fe4f | ||
|
|
ba5b045351 | ||
|
|
9a19eeacb3 | ||
|
|
10ed03d6bf | ||
|
|
3be1bf4384 | ||
|
|
3396b20661 | ||
|
|
c9e1140189 | ||
|
|
416cd30b42 | ||
|
|
d66a07cc55 | ||
|
|
ce17dbe240 | ||
|
|
326fc501e2 | ||
|
|
91ad3ee0a2 | ||
|
|
2993c422c1 | ||
|
|
6f8cdde7e4 | ||
|
|
f521a0dd77 | ||
|
|
12fdb58da6 | ||
|
|
09876cefde | ||
|
|
d1e2ea7635 | ||
|
|
7b131f2a45 | ||
|
|
f3d5e3d4c2 | ||
|
|
492f83f5e2 | ||
|
|
7dcd1618d8 | ||
|
|
e2a39aa12e | ||
|
|
043833be7b | ||
|
|
ad394c19d5 | ||
|
|
05592183b1 | ||
|
|
079cbb8f68 | ||
|
|
3bddde20ab | ||
|
|
0a8a63bc15 | ||
|
|
fd2491446a | ||
|
|
26f3fe37f4 | ||
|
|
4cff80ecea | ||
|
|
83fe94d497 | ||
|
|
616bde2c4d | ||
|
|
0b971c2bd0 | ||
|
|
201b1b7fbc | ||
|
|
b0b976ee52 | ||
|
|
7020569f45 | ||
|
|
e153c76719 | ||
|
|
e7f9c11a14 | ||
|
|
f2e5c5e9b9 | ||
|
|
e02f73417c | ||
|
|
41db9c43e5 | ||
|
|
0dc2f0e923 | ||
|
|
5940d5ddad | ||
|
|
684850dda1 | ||
|
|
e8e615f4db | ||
|
|
559b060b6b | ||
|
|
581262d736 | ||
|
|
8878a49a1d | ||
|
|
3a7bc4d253 | ||
|
|
316982fb8f | ||
|
|
cfaea34053 | ||
|
|
8fd1d895d6 | ||
|
|
b592ecf6f5 | ||
|
|
65a8826158 | ||
|
|
c9fab898cc | ||
|
|
819049d596 | ||
|
|
147dee6ec7 | ||
|
|
b0b885d506 | ||
|
|
c94610f8a9 | ||
|
|
21fa006850 | ||
|
|
e293c435af | ||
|
|
321c5ec756 | ||
|
|
f04d93f9aa | ||
|
|
9436efe554 | ||
|
|
3acbf708d3 | ||
|
|
92979984ea | ||
|
|
a16d41a1e1 | ||
|
|
ff8b78fc99 | ||
|
|
97c9bd7ce0 | ||
|
|
c40c93340d | ||
|
|
094c105464 | ||
|
|
e36c696624 | ||
|
|
555a4ab4c5 | ||
|
|
7d8cc8776d | ||
|
|
960646ac8a | ||
|
|
ed9f15becf | ||
|
|
b09aa74f2f | ||
|
|
60805fd11d | ||
|
|
89ecd4b33d | ||
|
|
237f142a87 | ||
|
|
a21647a1aa | ||
|
|
e9afe4ee25 | ||
|
|
acd067582a | ||
|
|
82227e8901 | ||
|
|
8b6735a6aa | ||
|
|
9ed8831500 | ||
|
|
e7d726cc2c | ||
|
|
11a3dd26c8 | ||
|
|
04738586b9 | ||
|
|
18b4fce1ca | ||
|
|
3b9babe745 | ||
|
|
a4a85b7bbf | ||
|
|
e8f0275d8e | ||
|
|
3251f58d46 | ||
|
|
204fb211ac | ||
|
|
6cade814e1 | ||
|
|
eb2fbabbb1 | ||
|
|
ee9c5db719 | ||
|
|
d8af01dc46 | ||
|
|
5bead069f2 | ||
|
|
63e1345780 | ||
|
|
3df988d0b8 | ||
|
|
656202dc0d | ||
|
|
eec3253669 | ||
|
|
d9423201b8 | ||
|
|
c6087bc369 | ||
|
|
b7077c7967 | ||
|
|
4acb07f1cb | ||
|
|
5207022a4a | ||
|
|
1a25746343 | ||
|
|
0d7ceb625b | ||
|
|
95fb5f904f | ||
|
|
a0309855d7 | ||
|
|
546754e803 | ||
|
|
86461909d3 | ||
|
|
feebd2a733 | ||
|
|
75e7176675 | ||
|
|
bae5d9c7f8 | ||
|
|
4ba033fc84 | ||
|
|
f0dd0e8f92 | ||
|
|
7ea3d230bb | ||
|
|
b7f4fd9074 | ||
|
|
9a114b38d3 | ||
|
|
bec1116f86 |
14
.github/FUNDING.yml
vendored
Normal file
14
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
# These are supported funding model platforms
|
||||
|
||||
github: [c3lang]
|
||||
patreon: # Replace with a single Patreon username
|
||||
open_collective: # Replace with a single Open Collective username
|
||||
ko_fi: c3lang
|
||||
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
|
||||
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
|
||||
liberapay: # Replace with a single Liberapay username
|
||||
issuehunt: # Replace with a single IssueHunt username
|
||||
otechie: # Replace with a single Otechie username
|
||||
lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
|
||||
polar: # Replace with a single Polar username
|
||||
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
|
||||
87
.github/workflows/main.yml
vendored
87
.github/workflows/main.yml
vendored
@@ -2,13 +2,14 @@ name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master, dev, ci_testing ]
|
||||
branches: [ master, dev, ci_testing, experiments ]
|
||||
pull_request:
|
||||
branches: [ master ]
|
||||
branches: [ master, dev ]
|
||||
|
||||
env:
|
||||
LLVM_RELEASE_VERSION: 16
|
||||
|
||||
LLVM_RELEASE_VERSION_WINDOWS: 18
|
||||
LLVM_RELEASE_VERSION_MAC: 18
|
||||
LLVM_RELEASE_VERSION_LINUX: 18
|
||||
jobs:
|
||||
|
||||
build-msvc:
|
||||
@@ -33,9 +34,9 @@ jobs:
|
||||
- name: Compile and run some examples
|
||||
run: |
|
||||
cd resources
|
||||
..\build\${{ matrix.build_type }}\c3c.exe compile-run examples\hello_world_many.c3
|
||||
..\build\${{ matrix.build_type }}\c3c.exe compile-run examples\time.c3
|
||||
..\build\${{ matrix.build_type }}\c3c.exe compile-run examples\fannkuch-redux.c3
|
||||
..\build\${{ matrix.build_type }}\c3c.exe compile-run --print-linking examples\hello_world_many.c3
|
||||
..\build\${{ matrix.build_type }}\c3c.exe compile-run --print-linking examples\time.c3
|
||||
..\build\${{ matrix.build_type }}\c3c.exe compile-run --print-linking examples\fannkuch-redux.c3
|
||||
..\build\${{ matrix.build_type }}\c3c.exe compile-run examples\contextfree\boolerr.c3
|
||||
..\build\${{ matrix.build_type }}\c3c.exe compile-run examples\ls.c3
|
||||
..\build\${{ matrix.build_type }}\c3c.exe compile-run examples\load_world.c3
|
||||
@@ -45,6 +46,7 @@ jobs:
|
||||
|
||||
- name: Build testproject
|
||||
run: |
|
||||
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" x64
|
||||
cd resources/testproject
|
||||
..\..\build\${{ matrix.build_type }}\c3c.exe --debug-log --emit-llvm run hello_world_win32
|
||||
dir build\llvm_ir
|
||||
@@ -55,6 +57,7 @@ jobs:
|
||||
- name: Build testproject lib
|
||||
run: |
|
||||
cd resources/testproject
|
||||
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" x64
|
||||
..\..\build\${{ matrix.build_type }}\c3c.exe --debug-log build hello_world_win32_lib
|
||||
|
||||
- name: Vendor-fetch
|
||||
@@ -112,8 +115,8 @@ jobs:
|
||||
install: git binutils mingw-w64-x86_64-clang mingw-w64-x86_64-ninja mingw-w64-x86_64-cmake mingw-w64-x86_64-toolchain mingw-w64-x86_64-python
|
||||
- shell: msys2 {0}
|
||||
run: |
|
||||
pacman --noconfirm -U https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-llvm-17.0.6-1-any.pkg.tar.zst
|
||||
pacman --noconfirm -U https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-lld-17.0.6-1-any.pkg.tar.zst
|
||||
pacman --noconfirm -U https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-llvm-18.1.8-1-any.pkg.tar.zst
|
||||
pacman --noconfirm -U https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-lld-18.1.8-1-any.pkg.tar.zst
|
||||
- name: CMake
|
||||
run: |
|
||||
cmake -B build -G Ninja -DCMAKE_C_COMPILER=clang -DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
|
||||
@@ -122,11 +125,11 @@ jobs:
|
||||
- name: Compile and run some examples
|
||||
run: |
|
||||
cd resources
|
||||
../build/c3c compile-run examples/hello_world_many.c3
|
||||
../build/c3c compile-run examples/time.c3
|
||||
../build/c3c compile-run examples/fannkuch-redux.c3
|
||||
../build/c3c compile-run examples/contextfree/boolerr.c3
|
||||
../build/c3c compile-run examples/load_world.c3
|
||||
../build/c3c compile-run --print-linking examples/hello_world_many.c3
|
||||
../build/c3c compile-run --print-linking examples/time.c3
|
||||
../build/c3c compile-run --print-linking examples/fannkuch-redux.c3
|
||||
../build/c3c compile-run --print-linking examples/contextfree/boolerr.c3
|
||||
../build/c3c compile-run --print-linking examples/load_world.c3
|
||||
../build/c3c compile --test -g -O0 --threads 1 --target macos-x64 examples/constants.c3
|
||||
|
||||
- name: Build testproject
|
||||
@@ -141,7 +144,7 @@ jobs:
|
||||
- name: Build testproject lib
|
||||
run: |
|
||||
cd resources/testproject
|
||||
../../build/c3c build hello_world_lib --debug-log
|
||||
../../build/c3c build hello_world_lib --cc cc --debug-log
|
||||
|
||||
- name: run compiler tests
|
||||
run: |
|
||||
@@ -220,19 +223,26 @@ jobs:
|
||||
if [[ "${{matrix.llvm_version}}" < 16 ]]; then
|
||||
sudo apt remove libllvm15
|
||||
fi
|
||||
if [[ "${{matrix.llvm_version}}" < 19 ]]; then
|
||||
if [[ "${{matrix.llvm_version}}" < 18 ]]; then
|
||||
sudo add-apt-repository "deb http://apt.llvm.org/focal/ llvm-toolchain-focal-${{matrix.llvm_version}} main"
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y -t llvm-toolchain-focal-${{matrix.llvm_version}} libpolly-${{matrix.llvm_version}}-dev \
|
||||
clang-${{matrix.llvm_version}} llvm-${{matrix.llvm_version}} llvm-${{matrix.llvm_version}}-dev \
|
||||
lld-${{matrix.llvm_version}} liblld-${{matrix.llvm_version}}-dev libmlir-${{matrix.llvm_version}} \
|
||||
libmlir-${{matrix.llvm_version}}-dev mlir-${{matrix.llvm_version}}-tools
|
||||
libmlir-${{matrix.llvm_version}}-dev mlir-${{matrix.llvm_version}}-tools
|
||||
else
|
||||
sudo add-apt-repository "deb http://apt.llvm.org/focal/ llvm-toolchain-focal main"
|
||||
sudo apt-get install -y -t llvm-toolchain-focal libpolly-${{matrix.llvm_version}}-dev \
|
||||
clang-${{matrix.llvm_version}} llvm-${{matrix.llvm_version}} llvm-${{matrix.llvm_version}}-dev \
|
||||
lld-${{matrix.llvm_version}} liblld-${{matrix.llvm_version}}-dev libmlir-${{matrix.llvm_version}} \
|
||||
libmlir-${{matrix.llvm_version}}-dev mlir-${{matrix.llvm_version}}-tools
|
||||
if [[ "${{matrix.llvm_version}}" < 19 ]]; then
|
||||
sudo add-apt-repository "deb http://apt.llvm.org/focal/ llvm-toolchain-focal-${{matrix.llvm_version}} main"
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y -t llvm-toolchain-focal-${{matrix.llvm_version}} libpolly-${{matrix.llvm_version}}-dev \
|
||||
clang-${{matrix.llvm_version}} llvm-${{matrix.llvm_version}} llvm-${{matrix.llvm_version}}-dev \
|
||||
lld-${{matrix.llvm_version}} liblld-${{matrix.llvm_version}}-dev
|
||||
else
|
||||
sudo add-apt-repository "deb http://apt.llvm.org/focal/ llvm-toolchain-focal main"
|
||||
sudo apt-get install -y -t llvm-toolchain-focal libpolly-${{matrix.llvm_version}}-dev \
|
||||
clang-${{matrix.llvm_version}} llvm-${{matrix.llvm_version}} llvm-${{matrix.llvm_version}}-dev \
|
||||
lld-${{matrix.llvm_version}} liblld-${{matrix.llvm_version}}-dev
|
||||
fi
|
||||
fi
|
||||
- name: CMake
|
||||
if: matrix.llvm_version != 18
|
||||
@@ -265,7 +275,7 @@ jobs:
|
||||
|
||||
- name: Compile and run some examples
|
||||
run: |
|
||||
cd resources
|
||||
cd resources
|
||||
../build/c3c compile examples/base64.c3
|
||||
../build/c3c compile examples/binarydigits.c3
|
||||
../build/c3c compile examples/brainfk.c3
|
||||
@@ -285,7 +295,7 @@ jobs:
|
||||
../build/c3c compile examples/contextfree/dynscope.c3
|
||||
../build/c3c compile examples/contextfree/guess_number.c3
|
||||
../build/c3c compile examples/contextfree/multi.c3
|
||||
../build/c3c compile examples/contextfree/cleanup.c3
|
||||
../build/c3c compile examples/contextfree/cleanup.c3
|
||||
../build/c3c compile-run examples/hello_world_many.c3
|
||||
../build/c3c compile-run examples/time.c3
|
||||
../build/c3c compile-run examples/fannkuch-redux.c3
|
||||
@@ -293,7 +303,7 @@ jobs:
|
||||
../build/c3c compile-run examples/load_world.c3
|
||||
../build/c3c compile-run examples/process.c3
|
||||
../build/c3c compile-run examples/ls.c3
|
||||
../build/c3c compile-run --system-linker=no linux_stack.c3
|
||||
../build/c3c compile-run --linker=builtin linux_stack.c3
|
||||
../build/c3c compile-run linux_stack.c3
|
||||
|
||||
- name: Compile run unit tests
|
||||
@@ -309,7 +319,7 @@ jobs:
|
||||
- name: Build testproject direct linker
|
||||
run: |
|
||||
cd resources/testproject
|
||||
../../build/c3c run --debug-log --system-linker=no
|
||||
../../build/c3c run --debug-log --linker=builtin
|
||||
|
||||
- name: run compiler tests
|
||||
run: |
|
||||
@@ -317,7 +327,7 @@ jobs:
|
||||
python3 src/tester.py ../build/c3c test_suite/
|
||||
|
||||
- name: bundle_output
|
||||
if: matrix.llvm_version == 16
|
||||
if: matrix.llvm_version == env.LLVM_RELEASE_VERSION_LINUX
|
||||
run: |
|
||||
mkdir linux
|
||||
cp -r lib linux
|
||||
@@ -326,7 +336,7 @@ jobs:
|
||||
tar czf c3-linux-${{matrix.build_type}}.tar.gz linux
|
||||
|
||||
- name: upload artifacts
|
||||
if: matrix.llvm_version == 16
|
||||
if: matrix.llvm_version == env.LLVM_RELEASE_VERSION_LINUX
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: c3-linux-${{matrix.build_type}}
|
||||
@@ -400,7 +410,7 @@ jobs:
|
||||
../build/c3c compile-run examples/factorial_macro.c3
|
||||
../build/c3c compile-run examples/fasta.c3
|
||||
../build/c3c compile-run examples/process.c3
|
||||
../build/c3c compile-run --system-linker=no linux_stack.c3
|
||||
../build/c3c compile-run --linker=builtin linux_stack.c3
|
||||
../build/c3c compile-run linux_stack.c3
|
||||
|
||||
- name: Compile run unit tests
|
||||
@@ -416,7 +426,7 @@ jobs:
|
||||
- name: Build testproject direct linker
|
||||
run: |
|
||||
cd resources/testproject
|
||||
../../build/c3c run --debug-log --system-linker=no
|
||||
../../build/c3c run --debug-log --linker=builtin
|
||||
|
||||
- name: run compiler tests
|
||||
run: |
|
||||
@@ -446,20 +456,26 @@ jobs:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
build_type: [Release, Debug]
|
||||
llvm_version: [15, 16, 17]
|
||||
llvm_version: [15, 16, 17, 18]
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Download LLVM
|
||||
run: |
|
||||
brew install llvm@${{ matrix.llvm_version }} ninja curl
|
||||
echo "/usr/local/opt/llvm@${{ matrix.llvm_version }}/bin" >> $GITHUB_PATH
|
||||
echo "/opt/homebrew/opt/llvm@${{ matrix.llvm_version }}/bin" >> $GITHUB_PATH
|
||||
TMP_PATH=$(xcrun --show-sdk-path)/user/include
|
||||
echo "CPATH=$TMP_PATH" >> $GITHUB_ENV
|
||||
|
||||
- name: CMake
|
||||
if: matrix.llvm_version != 18
|
||||
run: |
|
||||
cmake -B build -G Ninja -DC3_LLVM_VERSION=${{matrix.llvm_version}} -DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
|
||||
cmake --build build
|
||||
- name: CMake18
|
||||
if: matrix.llvm_version == 18
|
||||
run: |
|
||||
cmake -B build -G Ninja -DC3_LLVM_VERSION=${{matrix.llvm_version}}.1 -DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
|
||||
cmake --build build
|
||||
|
||||
- name: Vendor-fetch
|
||||
run: |
|
||||
@@ -474,6 +490,7 @@ jobs:
|
||||
../build/c3c compile-run examples/contextfree/boolerr.c3
|
||||
../build/c3c compile-run examples/process.c3
|
||||
../build/c3c compile-run examples/load_world.c3
|
||||
../build/c3c compile-run -O5 examples/load_world.c3
|
||||
|
||||
- name: Compile run unit tests
|
||||
run: |
|
||||
@@ -488,7 +505,7 @@ jobs:
|
||||
- name: Build testproject direct linker
|
||||
run: |
|
||||
cd resources/testproject
|
||||
../../build/c3c run --debug-log --system-linker=no
|
||||
../../build/c3c run --debug-log --linker=builtin
|
||||
|
||||
- name: Build testproject lib
|
||||
run: |
|
||||
@@ -501,7 +518,7 @@ jobs:
|
||||
python3 src/tester.py ../build/c3c test_suite/
|
||||
|
||||
- name: bundle_output
|
||||
if: matrix.llvm_version == env.LLVM_RELEASE_VERSION
|
||||
if: matrix.llvm_version == env.LLVM_RELEASE_VERSION_MAC
|
||||
run: |
|
||||
mkdir macos
|
||||
cp -r lib macos
|
||||
@@ -510,7 +527,7 @@ jobs:
|
||||
zip -r c3-macos-${{matrix.build_type}}.zip macos
|
||||
|
||||
- name: upload artifacts
|
||||
if: matrix.llvm_version == env.LLVM_RELEASE_VERSION
|
||||
if: matrix.llvm_version == env.LLVM_RELEASE_VERSION_MAC
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: c3-macos-${{matrix.build_type}}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
cmake_minimum_required(VERSION 3.15)
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
# Grab the version
|
||||
file(READ "src/version.h" ver)
|
||||
@@ -10,6 +10,11 @@ endif()
|
||||
project(c3c VERSION ${CMAKE_MATCH_1})
|
||||
message("C3C version: ${CMAKE_PROJECT_VERSION}")
|
||||
|
||||
# Avoid warning for FetchContent
|
||||
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
|
||||
cmake_policy(SET CMP0135 NEW)
|
||||
endif()
|
||||
|
||||
if (NOT DEFINED CMAKE_INSTALL_LIBDIR)
|
||||
if (MSVC)
|
||||
set(CMAKE_INSTALL_LIBDIR "c:\\c3c\\lib")
|
||||
@@ -32,6 +37,7 @@ set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
if(MSVC)
|
||||
message(STATUS "MSVC version ${MSVC_VERSION}")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /O2 /EHsc")
|
||||
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /O2 /EHsc")
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Od /Zi /EHa")
|
||||
@@ -97,15 +103,15 @@ endif()
|
||||
|
||||
if(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
|
||||
if (C3_LLVM_VERSION STREQUAL "auto")
|
||||
set(C3_LLVM_VERSION "16")
|
||||
set(C3_LLVM_VERSION "18")
|
||||
endif()
|
||||
FetchContent_Declare(
|
||||
LLVM_Windows
|
||||
URL https://github.com/c3lang/win-llvm/releases/download/llvm_16_0_2/llvm-16.0.2-windows-amd64-msvc17-libcmt.7z
|
||||
URL https://github.com/c3lang/win-llvm/releases/download/llvm_18_1_8_with_rt/llvm-18.1.8-windows-amd64-msvc17-libcmt.7z
|
||||
)
|
||||
FetchContent_Declare(
|
||||
LLVM_Windows_debug
|
||||
URL https://github.com/c3lang/win-llvm/releases/download/llvm_16_0_2/llvm-16.0.2-windows-amd64-msvc17-libcmt-dbg.7z
|
||||
URL https://github.com/c3lang/win-llvm/releases/download/llvm_18_1_8_with_rt/llvm-18.1.8-windows-amd64-msvc17-libcmt-dbg.7z
|
||||
)
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
message("Loading Windows LLVM debug libraries, this may take a while...")
|
||||
@@ -130,6 +136,10 @@ message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
|
||||
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
|
||||
message(STATUS "Libraries located in: ${LLVM_LIBRARY_DIRS}")
|
||||
|
||||
if (NOT LLVM_PACKAGE_VERSION VERSION_GREATER_EQUAL 15.0)
|
||||
message(FATAL_ERROR "LLVM version 15.0 or later is required.")
|
||||
endif()
|
||||
|
||||
if(LLVM_ENABLE_RTTI)
|
||||
message(STATUS "LLVM was built with RTTI")
|
||||
else()
|
||||
@@ -195,8 +205,10 @@ else()
|
||||
find_library(LLD_WASM NAMES liblldWasm.so PATHS ${LLVM_LIBRARY_DIRS} NO_DEFAULT_PATH)
|
||||
endif()
|
||||
|
||||
file(REMOVE_RECURSE ${CMAKE_BINARY_DIR}/lib)
|
||||
file(COPY ${CMAKE_SOURCE_DIR}/lib DESTINATION ${CMAKE_BINARY_DIR})
|
||||
if (NOT(${CMAKE_BINARY_DIR} EQUAL ${CMAKE_SOURCE_DIR}))
|
||||
file(REMOVE_RECURSE ${CMAKE_BINARY_DIR}/lib)
|
||||
file(COPY ${CMAKE_SOURCE_DIR}/lib DESTINATION ${CMAKE_BINARY_DIR})
|
||||
endif()
|
||||
|
||||
if (${LLVM_PACKAGE_VERSION} VERSION_GREATER_EQUAL 16)
|
||||
find_library(LLD_LOONG NAMES libLLVMLoongArchCodeGen.lib libLLVMLoongArchAsmParser.lib libLLVMLoongArchCodeGen.a libLLVMLoongArchAsmParser.a PATHS ${LLVM_LIBRARY_DIRS} NO_DEFAULT_PATH)
|
||||
@@ -234,6 +246,7 @@ add_executable(c3c
|
||||
src/build/builder.c
|
||||
src/build/build_options.c
|
||||
src/build/project_creation.c
|
||||
src/build/libraries.c
|
||||
src/compiler/ast.c
|
||||
src/compiler/bigint.c
|
||||
src/compiler/codegen_general.c
|
||||
@@ -246,7 +259,6 @@ add_executable(c3c
|
||||
src/compiler/headers.c
|
||||
src/compiler/json_output.c
|
||||
src/compiler/lexer.c
|
||||
src/compiler/libraries.c
|
||||
src/compiler/linker.c
|
||||
src/compiler/llvm_codegen.c
|
||||
src/compiler/abi/c_abi_aarch64.c
|
||||
@@ -315,7 +327,8 @@ add_executable(c3c
|
||||
src/compiler/expr.c
|
||||
src/utils/time.c
|
||||
src/utils/http.c
|
||||
src/compiler/sema_liveness.c)
|
||||
src/compiler/sema_liveness.c
|
||||
src/build/common_build.c)
|
||||
|
||||
|
||||
if (C3_USE_TB)
|
||||
|
||||
@@ -32,7 +32,7 @@ whole new language.
|
||||
|
||||
### Example code
|
||||
|
||||
The following code shows [generic modules](http://www.c3-lang.org/generics/) (more examples can be found at http://www.c3-lang.org/examples/).
|
||||
The following code shows [generic modules](https://c3-lang.org/references/docs/generics/) (more examples can be found at https://c3-lang.org/references/docs/examples/).
|
||||
|
||||
```c++
|
||||
module stack (<Type>);
|
||||
@@ -122,7 +122,7 @@ fn void main()
|
||||
- No mandatory header files
|
||||
- New semantic macro system
|
||||
- Module based name spacing
|
||||
- Subarrays (slices)
|
||||
- Slices
|
||||
- Compile time reflection
|
||||
- Enhanced compile time execution
|
||||
- Generics based on generic modules
|
||||
@@ -212,6 +212,8 @@ More platforms will be supported in the future.
|
||||
3. Unzip executable and standard lib.
|
||||
4. Run `./c3c`.
|
||||
|
||||
(*Note that there is a known issue with debug symbol generation on MacOS 13, see issue #1086)
|
||||
|
||||
#### Installing on Arch Linux
|
||||
There is an AUR package for the c3c compiler : [c3c-git](https://aur.archlinux.org/packages/c3c-git).
|
||||
|
||||
|
||||
@@ -124,7 +124,7 @@ macro @atomic_exec(#func, data, value, ordering) @local
|
||||
case RELEASE: return #func(data, value, RELEASE);
|
||||
case ACQUIRE_RELEASE: return #func(data, value, ACQUIRE_RELEASE);
|
||||
case SEQ_CONSISTENT: return #func(data, value, SEQ_CONSISTENT);
|
||||
default: assert(false, "Ordering may not be non-atomic or unordered.");
|
||||
default: unreachable("Ordering may not be non-atomic or unordered.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ macro @__atomic_compare_exchange_ordering_failure(ptr, expected, desired, $succe
|
||||
case AtomicOrdering.RELAXED.ordinal: return $$compare_exchange(ptr, expected, desired, false, false, $success, AtomicOrdering.RELAXED.ordinal, $alignment);
|
||||
case AtomicOrdering.ACQUIRE.ordinal: return $$compare_exchange(ptr, expected, desired, false, false, $success, AtomicOrdering.ACQUIRE.ordinal, $alignment);
|
||||
case AtomicOrdering.SEQ_CONSISTENT.ordinal: return $$compare_exchange(ptr, expected, desired, false, false, $success, AtomicOrdering.SEQ_CONSISTENT.ordinal, $alignment);
|
||||
default: assert(false, "Unrecognized failure ordering");
|
||||
default: unreachable("Unrecognized failure ordering");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -23,12 +23,12 @@ macro @__atomic_compare_exchange_ordering_success(ptr, expected, desired, succes
|
||||
case AtomicOrdering.RELEASE.ordinal: return @__atomic_compare_exchange_ordering_failure(ptr, expected, desired, AtomicOrdering.RELEASE.ordinal, failure, $alignment);
|
||||
case AtomicOrdering.ACQUIRE_RELEASE.ordinal: return @__atomic_compare_exchange_ordering_failure(ptr, expected, desired, AtomicOrdering.ACQUIRE_RELEASE.ordinal, failure, $alignment);
|
||||
case AtomicOrdering.SEQ_CONSISTENT.ordinal: return @__atomic_compare_exchange_ordering_failure(ptr, expected, desired, AtomicOrdering.SEQ_CONSISTENT.ordinal, failure, $alignment);
|
||||
default: assert(false, "Unrecognized success ordering");
|
||||
default: unreachable("Unrecognized success ordering");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
fn CInt __atomic_compare_exchange(CInt size, any* ptr, any* expected, any* desired, CInt success, CInt failure) @extern("__atomic_compare_exchange") @export
|
||||
fn CInt __atomic_compare_exchange(CInt size, any ptr, any expected, any desired, CInt success, CInt failure) @extern("__atomic_compare_exchange") @export
|
||||
{
|
||||
switch (size)
|
||||
{
|
||||
@@ -57,7 +57,7 @@ fn CInt __atomic_compare_exchange(CInt size, any* ptr, any* expected, any* desir
|
||||
nextcase;
|
||||
$endif
|
||||
default:
|
||||
assert(false, "Unsuported size (%d) for atomic_compare_exchange", size);
|
||||
unreachable("Unsuported size (%d) for atomic_compare_exchange", size);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -1,18 +1,18 @@
|
||||
// Copyright (c) 2024 Christoffer Lerno. All rights reserved.
|
||||
// Use of self source code is governed by the MIT license
|
||||
// a copy of which can be found in the LICENSE_STDLIB file.
|
||||
module std::collections::generic_list;
|
||||
module std::collections::anylist;
|
||||
import std::io,std::math;
|
||||
|
||||
def GenericPredicate = fn bool(any* value);
|
||||
def GenericTest = fn bool(any* type, any* context);
|
||||
def AnyPredicate = fn bool(any value);
|
||||
def AnyTest = fn bool(any type, any context);
|
||||
|
||||
struct GenericList (Printable)
|
||||
struct AnyList (Printable)
|
||||
{
|
||||
usz size;
|
||||
usz capacity;
|
||||
Allocator* allocator;
|
||||
any** entries;
|
||||
Allocator allocator;
|
||||
any* entries;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,14 +20,14 @@ struct GenericList (Printable)
|
||||
* @param initial_capacity "The initial capacity to reserve"
|
||||
* @param [&inout] allocator "The allocator to use, defaults to the heap allocator"
|
||||
**/
|
||||
fn GenericList* GenericList.new_init(&self, usz initial_capacity = 16, Allocator* allocator = allocator::heap())
|
||||
fn AnyList* AnyList.new_init(&self, usz initial_capacity = 16, Allocator allocator = allocator::heap())
|
||||
{
|
||||
self.allocator = allocator;
|
||||
self.size = 0;
|
||||
if (initial_capacity > 0)
|
||||
{
|
||||
initial_capacity = math::next_power_of_2(initial_capacity);
|
||||
self.entries = allocator::alloc_array(allocator, any*, initial_capacity);
|
||||
self.entries = allocator::alloc_array(allocator, any, initial_capacity);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -37,26 +37,17 @@ fn GenericList* GenericList.new_init(&self, usz initial_capacity = 16, Allocator
|
||||
return self;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param initial_capacity "The initial capacity to reserve"
|
||||
* @param [&inout] allocator "The allocator to use, defaults to the heap allocator"
|
||||
**/
|
||||
fn GenericList* GenericList.init_new(&self, usz initial_capacity = 16, Allocator* allocator = allocator::heap()) @deprecated("Replaced by new_init")
|
||||
{
|
||||
return self.new_init(initial_capacity, allocator) @inline;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the list using the temp allocator.
|
||||
*
|
||||
* @param initial_capacity "The initial capacity to reserve"
|
||||
**/
|
||||
fn GenericList* GenericList.temp_init(&self, usz initial_capacity = 16)
|
||||
fn AnyList* AnyList.temp_init(&self, usz initial_capacity = 16)
|
||||
{
|
||||
return self.new_init(initial_capacity, allocator::temp()) @inline;
|
||||
}
|
||||
|
||||
fn usz! GenericList.to_format(&self, Formatter* formatter) @dynamic
|
||||
fn usz! AnyList.to_format(&self, Formatter* formatter) @dynamic
|
||||
{
|
||||
switch (self.size)
|
||||
{
|
||||
@@ -76,12 +67,12 @@ fn usz! GenericList.to_format(&self, Formatter* formatter) @dynamic
|
||||
}
|
||||
}
|
||||
|
||||
fn String GenericList.to_new_string(&self, Allocator* allocator = allocator::heap()) @dynamic
|
||||
fn String AnyList.to_new_string(&self, Allocator allocator = allocator::heap()) @dynamic
|
||||
{
|
||||
return string::new_format("%s", *self, .allocator = allocator);
|
||||
}
|
||||
|
||||
fn String GenericList.to_tstring(&self)
|
||||
fn String AnyList.to_tstring(&self)
|
||||
{
|
||||
return string::tformat("%s", *self);
|
||||
}
|
||||
@@ -89,13 +80,13 @@ fn String GenericList.to_tstring(&self)
|
||||
/**
|
||||
* Push an element on the list by cloning it.
|
||||
**/
|
||||
macro void GenericList.push(&self, element)
|
||||
macro void AnyList.push(&self, element)
|
||||
{
|
||||
if (!self.allocator) self.allocator = allocator::heap();
|
||||
self.append_internal(allocator::clone(self.allocator, element));
|
||||
}
|
||||
|
||||
fn void GenericList.append_internal(&self, any* element) @local
|
||||
fn void AnyList.append_internal(&self, any element) @local
|
||||
{
|
||||
self.ensure_capacity();
|
||||
self.entries[self.size++] = element;
|
||||
@@ -104,7 +95,7 @@ fn void GenericList.append_internal(&self, any* element) @local
|
||||
/**
|
||||
* Free a retained element removed using *_retained.
|
||||
**/
|
||||
fn void GenericList.free_element(&self, any* element) @inline
|
||||
fn void AnyList.free_element(&self, any element) @inline
|
||||
{
|
||||
allocator::free(self.allocator, element.ptr);
|
||||
}
|
||||
@@ -115,7 +106,7 @@ fn void GenericList.free_element(&self, any* element) @inline
|
||||
*
|
||||
* @return! CastResult.TYPE_MISMATCH, IteratorResult.NO_MORE_ELEMENT
|
||||
**/
|
||||
macro GenericList.pop(&self, $Type)
|
||||
macro AnyList.pop(&self, $Type)
|
||||
{
|
||||
if (!self.size) return IteratorResult.NO_MORE_ELEMENT?;
|
||||
defer self.free_element(self.entries[self.size]);
|
||||
@@ -126,7 +117,7 @@ macro GenericList.pop(&self, $Type)
|
||||
* Pop the last value and allocate the copy using the given allocator.
|
||||
* @return! IteratorResult.NO_MORE_ELEMENT
|
||||
**/
|
||||
fn any*! GenericList.new_pop(&self, Allocator* allocator = allocator::heap())
|
||||
fn any! AnyList.new_pop(&self, Allocator allocator = allocator::heap())
|
||||
{
|
||||
if (!self.size) return IteratorResult.NO_MORE_ELEMENT?;
|
||||
defer self.free_element(self.entries[self.size]);
|
||||
@@ -137,19 +128,19 @@ fn any*! GenericList.new_pop(&self, Allocator* allocator = allocator::heap())
|
||||
* Pop the last value and allocate the copy using the temp allocator
|
||||
* @return! IteratorResult.NO_MORE_ELEMENT
|
||||
**/
|
||||
fn any*! GenericList.temp_pop(&self) => self.new_pop(allocator::temp());
|
||||
fn any! AnyList.temp_pop(&self) => self.new_pop(allocator::temp());
|
||||
|
||||
/**
|
||||
* Pop the last value. It must later be released using list.free_element()
|
||||
* @return! IteratorResult.NO_MORE_ELEMENT
|
||||
**/
|
||||
fn any*! GenericList.pop_retained(&self)
|
||||
fn any! AnyList.pop_retained(&self)
|
||||
{
|
||||
if (!self.size) return IteratorResult.NO_MORE_ELEMENT?;
|
||||
return self.entries[--self.size];
|
||||
}
|
||||
|
||||
fn void GenericList.clear(&self)
|
||||
fn void AnyList.clear(&self)
|
||||
{
|
||||
for (usz i = 0; i < self.size; i++)
|
||||
{
|
||||
@@ -161,7 +152,7 @@ fn void GenericList.clear(&self)
|
||||
/**
|
||||
* Same as pop() but pops the first value instead.
|
||||
**/
|
||||
macro GenericList.pop_first(&self, $Type)
|
||||
macro AnyList.pop_first(&self, $Type)
|
||||
{
|
||||
if (!self.size) return IteratorResult.NO_MORE_ELEMENT?;
|
||||
defer self.remove_at(0);
|
||||
@@ -171,7 +162,7 @@ macro GenericList.pop_first(&self, $Type)
|
||||
/**
|
||||
* Same as pop_retained() but pops the first value instead.
|
||||
**/
|
||||
fn any*! GenericList.pop_first_retained(&self)
|
||||
fn any! AnyList.pop_first_retained(&self)
|
||||
{
|
||||
if (!self.size) return IteratorResult.NO_MORE_ELEMENT?;
|
||||
defer self.remove_at(0);
|
||||
@@ -181,7 +172,7 @@ fn any*! GenericList.pop_first_retained(&self)
|
||||
/**
|
||||
* Same as new_pop() but pops the first value instead.
|
||||
**/
|
||||
fn any*! GenericList.new_pop_first(&self, Allocator* allocator = allocator::heap())
|
||||
fn any! AnyList.new_pop_first(&self, Allocator allocator = allocator::heap())
|
||||
{
|
||||
if (!self.size) return IteratorResult.NO_MORE_ELEMENT?;
|
||||
defer self.free_element(self.entries[self.size]);
|
||||
@@ -192,19 +183,19 @@ fn any*! GenericList.new_pop_first(&self, Allocator* allocator = allocator::heap
|
||||
/**
|
||||
* Same as temp_pop() but pops the first value instead.
|
||||
**/
|
||||
fn any*! GenericList.temp_pop_first(&self) => self.new_pop_first(allocator::temp());
|
||||
fn any! AnyList.temp_pop_first(&self) => self.new_pop_first(allocator::temp());
|
||||
|
||||
/**
|
||||
* @require index < self.size
|
||||
**/
|
||||
fn void GenericList.remove_at(&self, usz index)
|
||||
fn void AnyList.remove_at(&self, usz index)
|
||||
{
|
||||
if (!--self.size || index == self.size) return;
|
||||
self.free_element(self.entries[index]);
|
||||
self.entries[index .. self.size - 1] = self.entries[index + 1 .. self.size];
|
||||
}
|
||||
|
||||
fn void GenericList.add_all(&self, GenericList* other_list)
|
||||
fn void AnyList.add_all(&self, AnyList* other_list)
|
||||
{
|
||||
if (!other_list.size) return;
|
||||
self.reserve(other_list.size);
|
||||
@@ -217,7 +208,7 @@ fn void GenericList.add_all(&self, GenericList* other_list)
|
||||
/**
|
||||
* Reverse the elements in a list.
|
||||
**/
|
||||
fn void GenericList.reverse(&self)
|
||||
fn void AnyList.reverse(&self)
|
||||
{
|
||||
if (self.size < 2) return;
|
||||
usz half = self.size / 2U;
|
||||
@@ -228,7 +219,7 @@ fn void GenericList.reverse(&self)
|
||||
}
|
||||
}
|
||||
|
||||
fn any*[] GenericList.array_view(&self)
|
||||
fn any[] AnyList.array_view(&self)
|
||||
{
|
||||
return self.entries[:self.size];
|
||||
}
|
||||
@@ -236,7 +227,7 @@ fn any*[] GenericList.array_view(&self)
|
||||
/**
|
||||
* Push an element to the front of the list.
|
||||
**/
|
||||
macro void GenericList.push_front(&self, type)
|
||||
macro void AnyList.push_front(&self, type)
|
||||
{
|
||||
self.insert_at(0, type);
|
||||
}
|
||||
@@ -244,16 +235,16 @@ macro void GenericList.push_front(&self, type)
|
||||
/**
|
||||
* @require index < self.size
|
||||
**/
|
||||
macro void GenericList.insert_at(&self, usz index, type) @local
|
||||
macro void AnyList.insert_at(&self, usz index, type) @local
|
||||
{
|
||||
any* value = allocator::copy(self.allocator, type);
|
||||
any value = allocator::copy(self.allocator, type);
|
||||
self.insert_at_internal(self, index, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @require index < self.size
|
||||
**/
|
||||
fn void GenericList.insert_at_internal(&self, usz index, any* value) @local
|
||||
fn void AnyList.insert_at_internal(&self, usz index, any value) @local
|
||||
{
|
||||
self.ensure_capacity();
|
||||
for (usz i = self.size; i > index; i--)
|
||||
@@ -268,7 +259,7 @@ fn void GenericList.insert_at_internal(&self, usz index, any* value) @local
|
||||
/**
|
||||
* @require self.size > 0
|
||||
**/
|
||||
fn void GenericList.remove_last(&self)
|
||||
fn void AnyList.remove_last(&self)
|
||||
{
|
||||
self.free_element(self.entries[--self.size]);
|
||||
}
|
||||
@@ -276,37 +267,37 @@ fn void GenericList.remove_last(&self)
|
||||
/**
|
||||
* @require self.size > 0
|
||||
**/
|
||||
fn void GenericList.remove_first(&self)
|
||||
fn void AnyList.remove_first(&self)
|
||||
{
|
||||
self.remove_at(0);
|
||||
}
|
||||
|
||||
macro GenericList.first(&self, $Type)
|
||||
macro AnyList.first(&self, $Type)
|
||||
{
|
||||
return *anycast(self.first_any(), $Type);
|
||||
}
|
||||
|
||||
fn any*! GenericList.first_any(&self) @inline
|
||||
fn any! AnyList.first_any(&self) @inline
|
||||
{
|
||||
return self.size ? self.entries[0] : IteratorResult.NO_MORE_ELEMENT?;
|
||||
}
|
||||
|
||||
macro GenericList.last(&self, $Type)
|
||||
macro AnyList.last(&self, $Type)
|
||||
{
|
||||
return *anycast(self.last_any(), $Type);
|
||||
}
|
||||
|
||||
fn any*! GenericList.last_any(&self) @inline
|
||||
fn any! AnyList.last_any(&self) @inline
|
||||
{
|
||||
return self.size ? self.entries[self.size - 1] : IteratorResult.NO_MORE_ELEMENT?;
|
||||
}
|
||||
|
||||
fn bool GenericList.is_empty(&self) @inline
|
||||
fn bool AnyList.is_empty(&self) @inline
|
||||
{
|
||||
return !self.size;
|
||||
}
|
||||
|
||||
fn usz GenericList.len(&self) @operator(len) @inline
|
||||
fn usz AnyList.len(&self) @operator(len) @inline
|
||||
{
|
||||
return self.size;
|
||||
}
|
||||
@@ -314,7 +305,7 @@ fn usz GenericList.len(&self) @operator(len) @inline
|
||||
/**
|
||||
* @require index < self.size "Index out of range"
|
||||
**/
|
||||
macro GenericList.get(&self, usz index, $Type)
|
||||
macro AnyList.get(&self, usz index, $Type)
|
||||
{
|
||||
return *anycast(self.entries[index], $Type);
|
||||
}
|
||||
@@ -322,12 +313,12 @@ macro GenericList.get(&self, usz index, $Type)
|
||||
/**
|
||||
* @require index < self.size "Index out of range"
|
||||
**/
|
||||
fn any* GenericList.get_any(&self, usz index) @inline
|
||||
fn any AnyList.get_any(&self, usz index) @inline
|
||||
{
|
||||
return self.entries[index];
|
||||
}
|
||||
|
||||
fn void GenericList.free(&self)
|
||||
fn void AnyList.free(&self)
|
||||
{
|
||||
if (!self.allocator) return;
|
||||
self.clear();
|
||||
@@ -336,9 +327,9 @@ fn void GenericList.free(&self)
|
||||
self.entries = null;
|
||||
}
|
||||
|
||||
fn void GenericList.swap(&self, usz i, usz j)
|
||||
fn void AnyList.swap(&self, usz i, usz j)
|
||||
{
|
||||
any* temp = self.entries[i];
|
||||
any temp = self.entries[i];
|
||||
self.entries[i] = self.entries[j];
|
||||
self.entries[j] = temp;
|
||||
}
|
||||
@@ -347,7 +338,7 @@ fn void GenericList.swap(&self, usz i, usz j)
|
||||
* @param filter "The function to determine if it should be removed or not"
|
||||
* @return "the number of deleted elements"
|
||||
**/
|
||||
fn usz GenericList.remove_if(&self, GenericPredicate filter)
|
||||
fn usz AnyList.remove_if(&self, AnyPredicate filter)
|
||||
{
|
||||
return self._remove_if(filter, false);
|
||||
}
|
||||
@@ -356,12 +347,12 @@ fn usz GenericList.remove_if(&self, GenericPredicate filter)
|
||||
* @param selection "The function to determine if it should be kept or not"
|
||||
* @return "the number of deleted elements"
|
||||
**/
|
||||
fn usz GenericList.retain_if(&self, GenericPredicate selection)
|
||||
fn usz AnyList.retain_if(&self, AnyPredicate selection)
|
||||
{
|
||||
return self._remove_if(selection, true);
|
||||
}
|
||||
|
||||
macro usz GenericList._remove_if(&self, GenericPredicate filter, bool $invert) @local
|
||||
macro usz AnyList._remove_if(&self, AnyPredicate filter, bool $invert) @local
|
||||
{
|
||||
usz size = self.size;
|
||||
for (usz i = size, usz k = size; k > 0; k = i)
|
||||
@@ -387,17 +378,17 @@ macro usz GenericList._remove_if(&self, GenericPredicate filter, bool $invert) @
|
||||
return size - self.size;
|
||||
}
|
||||
|
||||
fn usz GenericList.remove_using_test(&self, GenericTest filter, any* context)
|
||||
fn usz AnyList.remove_using_test(&self, AnyTest filter, any context)
|
||||
{
|
||||
return self._remove_using_test(filter, false, context);
|
||||
}
|
||||
|
||||
fn usz GenericList.retain_using_test(&self, GenericTest filter, any* context)
|
||||
fn usz AnyList.retain_using_test(&self, AnyTest filter, any context)
|
||||
{
|
||||
return self._remove_using_test(filter, true, context);
|
||||
}
|
||||
|
||||
macro usz GenericList._remove_using_test(&self, GenericTest filter, bool $invert, ctx) @local
|
||||
macro usz AnyList._remove_using_test(&self, AnyTest filter, bool $invert, ctx) @local
|
||||
{
|
||||
usz size = self.size;
|
||||
for (usz i = size, usz k = size; k > 0; k = i)
|
||||
@@ -426,17 +417,17 @@ macro usz GenericList._remove_using_test(&self, GenericTest filter, bool $invert
|
||||
/**
|
||||
* Reserve at least min_capacity
|
||||
**/
|
||||
fn void GenericList.reserve(&self, usz min_capacity)
|
||||
fn void AnyList.reserve(&self, usz min_capacity)
|
||||
{
|
||||
if (!min_capacity) return;
|
||||
if (self.capacity >= min_capacity) return;
|
||||
if (!self.allocator) self.allocator = allocator::heap();
|
||||
min_capacity = math::next_power_of_2(min_capacity);
|
||||
self.entries = allocator::realloc(self.allocator, self.entries, any*.sizeof * min_capacity);
|
||||
self.entries = allocator::realloc(self.allocator, self.entries, any.sizeof * min_capacity);
|
||||
self.capacity = min_capacity;
|
||||
}
|
||||
|
||||
macro any* GenericList.@item_at(&self, usz index) @operator([])
|
||||
macro any AnyList.@item_at(&self, usz index) @operator([])
|
||||
{
|
||||
return self.entries[index];
|
||||
}
|
||||
@@ -444,7 +435,7 @@ macro any* GenericList.@item_at(&self, usz index) @operator([])
|
||||
/**
|
||||
* @require index <= self.size "Index out of range"
|
||||
**/
|
||||
macro void GenericList.set(&self, usz index, value)
|
||||
macro void AnyList.set(&self, usz index, value)
|
||||
{
|
||||
if (index == self.size)
|
||||
{
|
||||
@@ -455,7 +446,7 @@ macro void GenericList.set(&self, usz index, value)
|
||||
self.entries[index] = allocator::copy(self.allocator, value);
|
||||
}
|
||||
|
||||
fn void GenericList.ensure_capacity(&self, usz added = 1) @inline @private
|
||||
fn void AnyList.ensure_capacity(&self, usz added = 1) @inline @private
|
||||
{
|
||||
usz new_size = self.size + added;
|
||||
if (self.capacity >= new_size) return;
|
||||
@@ -86,31 +86,17 @@ struct GrowableBitSet
|
||||
* @param initial_capacity
|
||||
* @param [&inout] allocator "The allocator to use, defaults to the heap allocator"
|
||||
**/
|
||||
fn GrowableBitSet* GrowableBitSet.new_init(&self, usz initial_capacity = 1, Allocator* allocator = allocator::heap())
|
||||
fn GrowableBitSet* GrowableBitSet.new_init(&self, usz initial_capacity = 1, Allocator allocator = allocator::heap())
|
||||
{
|
||||
self.data.new_init(initial_capacity, allocator);
|
||||
return self;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param initial_capacity
|
||||
* @param [&inout] allocator "The allocator to use, defaults to the heap allocator"
|
||||
**/
|
||||
fn GrowableBitSet* GrowableBitSet.init_new(&self, usz initial_capacity = 1, Allocator* allocator = allocator::heap()) @deprecated("Replaced by new_init")
|
||||
{
|
||||
return self.new_init(initial_capacity, allocator) @inline;
|
||||
}
|
||||
|
||||
fn GrowableBitSet* GrowableBitSet.temp_init(&self, usz initial_capacity = 1)
|
||||
{
|
||||
return self.new_init(initial_capacity, allocator::temp()) @inline;
|
||||
}
|
||||
|
||||
fn GrowableBitSet* GrowableBitSet.init_temp(&self, usz initial_capacity = 1) @deprecated("Replaced by temp_init")
|
||||
{
|
||||
return self.temp_init(initial_capacity);
|
||||
}
|
||||
|
||||
fn void GrowableBitSet.free(&self)
|
||||
{
|
||||
self.data.free();
|
||||
|
||||
@@ -25,7 +25,7 @@ fn usz! EnumMap.to_format(&self, Formatter* formatter) @dynamic
|
||||
return n;
|
||||
}
|
||||
|
||||
fn String EnumMap.to_new_string(&self, Allocator* allocator = allocator::heap()) @dynamic
|
||||
fn String EnumMap.to_new_string(&self, Allocator allocator = allocator::heap()) @dynamic
|
||||
{
|
||||
return string::new_format("%s", *self, .allocator = allocator);
|
||||
}
|
||||
|
||||
@@ -141,7 +141,7 @@ fn usz! EnumSet.to_format(&set, Formatter* formatter) @dynamic
|
||||
return n;
|
||||
}
|
||||
|
||||
fn String EnumSet.to_new_string(&set, Allocator* allocator = allocator::heap()) @dynamic
|
||||
fn String EnumSet.to_new_string(&set, Allocator allocator = allocator::heap()) @dynamic
|
||||
{
|
||||
return string::new_format("%s", *set, .allocator = allocator);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
// a copy of which can be found in the LICENSE_STDLIB file.
|
||||
module std::collections::linkedlist(<Type>);
|
||||
|
||||
const ELEMENT_IS_EQUATABLE = types::is_equatable_type(Type);
|
||||
|
||||
struct Node @private
|
||||
{
|
||||
Node *next;
|
||||
@@ -12,51 +14,28 @@ struct Node @private
|
||||
|
||||
struct LinkedList
|
||||
{
|
||||
Allocator *allocator;
|
||||
Allocator allocator;
|
||||
usz size;
|
||||
Node *_first;
|
||||
Node *_last;
|
||||
}
|
||||
|
||||
fn void LinkedList.push(&self, Type value)
|
||||
{
|
||||
self.link_first(value);
|
||||
}
|
||||
|
||||
fn void LinkedList.push_last(&self, Type value)
|
||||
{
|
||||
self.link_last(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param [&inout] allocator "The allocator to use, defaults to the heap allocator"
|
||||
* @return "the initialized list"
|
||||
**/
|
||||
fn LinkedList* LinkedList.new_init(&self, Allocator* allocator = allocator::heap())
|
||||
fn LinkedList* LinkedList.new_init(&self, Allocator allocator = allocator::heap())
|
||||
{
|
||||
*self = { .allocator = allocator };
|
||||
return self;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param [&inout] allocator "The allocator to use, defaults to the heap allocator"
|
||||
* @return "the initialized list"
|
||||
**/
|
||||
fn LinkedList* LinkedList.init_new(&self, Allocator* allocator = allocator::heap()) @deprecated("Replaced by new_init")
|
||||
{
|
||||
return self.new_init(allocator);
|
||||
}
|
||||
|
||||
fn LinkedList* LinkedList.temp_init(&self)
|
||||
{
|
||||
return self.new_init(allocator::temp()) @inline;
|
||||
}
|
||||
|
||||
fn LinkedList* LinkedList.init_temp(&self) @deprecated("Replaced by temp_init")
|
||||
{
|
||||
return self.temp_init() @inline;
|
||||
}
|
||||
|
||||
/**
|
||||
* @require self.allocator
|
||||
**/
|
||||
@@ -71,7 +50,7 @@ macro Node* LinkedList.alloc_node(&self) @private
|
||||
return allocator::alloc(self.allocator, Node);
|
||||
}
|
||||
|
||||
fn void LinkedList.link_first(&self, Type value) @private
|
||||
fn void LinkedList.push_front(&self, Type value)
|
||||
{
|
||||
Node *first = self._first;
|
||||
Node *new_node = self.alloc_node();
|
||||
@@ -88,7 +67,7 @@ fn void LinkedList.link_first(&self, Type value) @private
|
||||
self.size++;
|
||||
}
|
||||
|
||||
fn void LinkedList.link_last(&self, Type value) @private
|
||||
fn void LinkedList.push(&self, Type value)
|
||||
{
|
||||
Node *last = self._last;
|
||||
Node *new_node = self.alloc_node();
|
||||
@@ -172,7 +151,7 @@ fn void LinkedList.set(&self, usz index, Type element)
|
||||
/**
|
||||
* @require index < self.size
|
||||
**/
|
||||
fn void LinkedList.remove(&self, usz index)
|
||||
fn void LinkedList.remove_at(&self, usz index)
|
||||
{
|
||||
self.unlink(self.node_at_index(index));
|
||||
}
|
||||
@@ -180,14 +159,14 @@ fn void LinkedList.remove(&self, usz index)
|
||||
/**
|
||||
* @require index <= self.size
|
||||
**/
|
||||
fn void LinkedList.insert(&self, usz index, Type element)
|
||||
fn void LinkedList.insert_at(&self, usz index, Type element)
|
||||
{
|
||||
switch (index)
|
||||
{
|
||||
case 0:
|
||||
self.push(element);
|
||||
self.push_front(element);
|
||||
case self.size:
|
||||
self.push_last(element);
|
||||
self.push(element);
|
||||
default:
|
||||
self.link_before(self.node_at_index(index), element);
|
||||
}
|
||||
@@ -232,7 +211,53 @@ fn void LinkedList.unlink_first(&self) @private
|
||||
self.size--;
|
||||
}
|
||||
|
||||
fn bool LinkedList.remove_value(&self, Type t)
|
||||
fn usz LinkedList.remove(&self, Type t) @if(ELEMENT_IS_EQUATABLE)
|
||||
{
|
||||
usz start = self.size;
|
||||
Node* node = self._first;
|
||||
while (node)
|
||||
{
|
||||
switch
|
||||
{
|
||||
case equals(node.value, t):
|
||||
Node* next = node.next;
|
||||
self.unlink(node);
|
||||
node = next;
|
||||
default:
|
||||
node = node.next;
|
||||
}
|
||||
}
|
||||
return start - self.size;
|
||||
}
|
||||
|
||||
fn Type! LinkedList.pop(&self)
|
||||
{
|
||||
if (!self._last) return IteratorResult.NO_MORE_ELEMENT?;
|
||||
defer self.unlink_last();
|
||||
return self._last.value;
|
||||
}
|
||||
|
||||
fn Type! LinkedList.pop_front(&self)
|
||||
{
|
||||
if (!self._first) return IteratorResult.NO_MORE_ELEMENT?;
|
||||
defer self.unlink_first();
|
||||
return self._first.value;
|
||||
}
|
||||
|
||||
fn void! LinkedList.remove_last(&self) @maydiscard
|
||||
{
|
||||
if (!self._first) return IteratorResult.NO_MORE_ELEMENT?;
|
||||
self.unlink_last();
|
||||
}
|
||||
|
||||
fn void! LinkedList.remove_first(&self) @maydiscard
|
||||
{
|
||||
if (!self._first) return IteratorResult.NO_MORE_ELEMENT?;
|
||||
self.unlink_first();
|
||||
}
|
||||
|
||||
|
||||
fn bool LinkedList.remove_first_match(&self, Type t) @if(ELEMENT_IS_EQUATABLE)
|
||||
{
|
||||
for (Node* node = self._first; node != null; node = node.next)
|
||||
{
|
||||
@@ -245,7 +270,7 @@ fn bool LinkedList.remove_value(&self, Type t)
|
||||
return false;
|
||||
}
|
||||
|
||||
fn bool LinkedList.remove_last_value(&self, Type t)
|
||||
fn bool LinkedList.remove_last_match(&self, Type t) @if(ELEMENT_IS_EQUATABLE)
|
||||
{
|
||||
for (Node* node = self._last; node != null; node = node.prev)
|
||||
{
|
||||
@@ -257,26 +282,6 @@ fn bool LinkedList.remove_last_value(&self, Type t)
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
fn Type! LinkedList.pop(&self)
|
||||
{
|
||||
if (!self._first) return IteratorResult.NO_MORE_ELEMENT?;
|
||||
defer self.unlink_first();
|
||||
return self._first.value;
|
||||
}
|
||||
|
||||
fn void! LinkedList.remove_last(&self)
|
||||
{
|
||||
if (!self._first) return IteratorResult.NO_MORE_ELEMENT?;
|
||||
self.unlink_last();
|
||||
}
|
||||
|
||||
fn void! LinkedList.remove_first(&self)
|
||||
{
|
||||
if (!self._first) return IteratorResult.NO_MORE_ELEMENT?;
|
||||
self.unlink_first();
|
||||
}
|
||||
|
||||
/**
|
||||
* @require self._last
|
||||
**/
|
||||
|
||||
@@ -5,31 +5,36 @@ module std::collections::list(<Type>);
|
||||
import std::io,std::math;
|
||||
|
||||
def ElementPredicate = fn bool(Type *type);
|
||||
def ElementTest = fn bool(Type *type, any* context);
|
||||
def ElementTest = fn bool(Type *type, any context);
|
||||
const ELEMENT_IS_EQUATABLE = types::is_equatable_type(Type);
|
||||
const ELEMENT_IS_POINTER = Type.kindof == POINTER;
|
||||
|
||||
macro type_is_overaligned() => Type.alignof > mem::DEFAULT_MEM_ALIGNMENT;
|
||||
|
||||
struct List (Printable)
|
||||
{
|
||||
usz size;
|
||||
usz capacity;
|
||||
Allocator *allocator;
|
||||
Allocator allocator;
|
||||
Type *entries;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param initial_capacity "The initial capacity to reserve"
|
||||
* @param [&inout] allocator "The allocator to use, defaults to the heap allocator"
|
||||
**/
|
||||
fn List* List.new_init(&self, usz initial_capacity = 16, Allocator* allocator = allocator::heap())
|
||||
fn List* List.new_init(&self, usz initial_capacity = 16, Allocator allocator = allocator::heap())
|
||||
{
|
||||
self.allocator = allocator;
|
||||
self.size = 0;
|
||||
if (initial_capacity > 0)
|
||||
{
|
||||
initial_capacity = math::next_power_of_2(initial_capacity);
|
||||
$if type_is_overaligned():
|
||||
self.entries = allocator::malloc_aligned(allocator, Type.sizeof * initial_capacity, .alignment = Type[1].alignof)!!;
|
||||
$else
|
||||
self.entries = allocator::malloc(allocator, Type.sizeof * initial_capacity);
|
||||
$endif
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -39,15 +44,6 @@ fn List* List.new_init(&self, usz initial_capacity = 16, Allocator* allocator =
|
||||
return self;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param initial_capacity "The initial capacity to reserve"
|
||||
* @param [&inout] allocator "The allocator to use, defaults to the heap allocator"
|
||||
**/
|
||||
fn List* List.init_new(&self, usz initial_capacity = 16, Allocator* allocator = allocator::heap()) @deprecated("Replaced by new_init")
|
||||
{
|
||||
return self.new_init(initial_capacity, allocator) @inline;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the list using the temp allocator.
|
||||
*
|
||||
@@ -59,19 +55,35 @@ fn List* List.temp_init(&self, usz initial_capacity = 16)
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the list using the temp allocator.
|
||||
* Initialize a new list with an array.
|
||||
*
|
||||
* @param initial_capacity "The initial capacity to reserve"
|
||||
* @param [in] values `The values to initialize the list with.`
|
||||
* @require self.size == 0 "The List must be empty"
|
||||
**/
|
||||
fn List* List.init_temp(&self, usz initial_capacity = 16) @deprecated("Replaced by temp_init")
|
||||
fn List* List.new_init_with_array(&self, Type[] values, Allocator allocator = allocator::heap())
|
||||
{
|
||||
return self.temp_init(initial_capacity) @inline;
|
||||
self.new_init(values.len, allocator) @inline;
|
||||
self.add_array(values) @inline;
|
||||
return self;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize a temporary list with an array.
|
||||
*
|
||||
* @param [in] values `The values to initialize the list with.`
|
||||
* @require self.size == 0 "The List must be empty"
|
||||
**/
|
||||
fn List* List.temp_init_with_array(&self, Type[] values)
|
||||
{
|
||||
self.temp_init(values.len) @inline;
|
||||
self.add_array(values) @inline;
|
||||
return self;
|
||||
}
|
||||
|
||||
/**
|
||||
* @require self.size == 0 "The List must be empty"
|
||||
**/
|
||||
fn void List.init_wrapping_array(&self, Type[] types, Allocator* allocator = allocator::heap())
|
||||
fn void List.init_wrapping_array(&self, Type[] types, Allocator allocator = allocator::heap())
|
||||
{
|
||||
self.allocator = allocator;
|
||||
self.size = types.len;
|
||||
@@ -99,7 +111,7 @@ fn usz! List.to_format(&self, Formatter* formatter) @dynamic
|
||||
}
|
||||
}
|
||||
|
||||
fn String List.to_new_string(&self, Allocator* allocator = allocator::heap()) @dynamic
|
||||
fn String List.to_new_string(&self, Allocator allocator = allocator::heap()) @dynamic
|
||||
{
|
||||
return string::new_format("%s", *self, .allocator = allocator);
|
||||
}
|
||||
@@ -110,21 +122,14 @@ fn String List.to_tstring(&self)
|
||||
}
|
||||
|
||||
fn void List.push(&self, Type element) @inline
|
||||
{
|
||||
self.append(element);
|
||||
}
|
||||
|
||||
fn void List.append(&self, Type element)
|
||||
{
|
||||
self.ensure_capacity();
|
||||
self.entries[self.size++] = element;
|
||||
}
|
||||
|
||||
/**
|
||||
* @require self.size > 0
|
||||
**/
|
||||
fn Type List.pop(&self)
|
||||
fn Type! List.pop(&self)
|
||||
{
|
||||
if (!self.size) return IteratorResult.NO_MORE_ELEMENT?;
|
||||
return self.entries[--self.size];
|
||||
}
|
||||
|
||||
@@ -136,11 +141,11 @@ fn void List.clear(&self)
|
||||
/**
|
||||
* @require self.size > 0
|
||||
**/
|
||||
fn Type List.pop_first(&self)
|
||||
fn Type! List.pop_first(&self)
|
||||
{
|
||||
Type value = self.entries[0];
|
||||
self.remove_at(0);
|
||||
return value;
|
||||
if (!self.size) return IteratorResult.NO_MORE_ELEMENT?;
|
||||
defer self.remove_at(0);
|
||||
return self.entries[0];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -163,7 +168,21 @@ fn void List.add_all(&self, List* other_list)
|
||||
}
|
||||
|
||||
|
||||
fn Type[] List.to_new_array(&self, Allocator* allocator = allocator::heap())
|
||||
/**
|
||||
* IMPORTANT The returned array must be freed using free_aligned.
|
||||
**/
|
||||
fn Type[] List.to_new_aligned_array(&self, Allocator allocator = allocator::heap())
|
||||
{
|
||||
if (!self.size) return Type[] {};
|
||||
Type[] result = allocator::alloc_array_aligned(allocator, Type, self.size);
|
||||
result[..] = self.entries[:self.size];
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @require !type_is_overaligned() : "This function is not available on overaligned types"
|
||||
**/
|
||||
macro Type[] List.to_new_array(&self, Allocator allocator = allocator::heap())
|
||||
{
|
||||
if (!self.size) return Type[] {};
|
||||
Type[] result = allocator::alloc_array(allocator, Type, self.size);
|
||||
@@ -173,7 +192,11 @@ fn Type[] List.to_new_array(&self, Allocator* allocator = allocator::heap())
|
||||
|
||||
fn Type[] List.to_tarray(&self)
|
||||
{
|
||||
$if type_is_overaligned():
|
||||
return self.to_new_aligned_array(allocator::temp());
|
||||
$else
|
||||
return self.to_new_array(allocator::temp());
|
||||
$endif;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -195,6 +218,12 @@ fn Type[] List.array_view(&self)
|
||||
return self.entries[:self.size];
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the values of an array to this list.
|
||||
*
|
||||
* @param [in] array
|
||||
* @ensure self.size >= array.len
|
||||
**/
|
||||
fn void List.add_array(&self, Type[] array)
|
||||
{
|
||||
if (!array.len) return;
|
||||
@@ -232,30 +261,28 @@ fn void List.set_at(&self, usz index, Type type)
|
||||
self.entries[index] = type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @require self.size > 0
|
||||
**/
|
||||
fn void List.remove_last(&self)
|
||||
fn void! List.remove_last(&self) @maydiscard
|
||||
{
|
||||
if (!self.size) return IteratorResult.NO_MORE_ELEMENT?;
|
||||
self.size--;
|
||||
}
|
||||
|
||||
/**
|
||||
* @require self.size > 0
|
||||
**/
|
||||
fn void List.remove_first(&self)
|
||||
fn void! List.remove_first(&self) @maydiscard
|
||||
{
|
||||
if (!self.size) return IteratorResult.NO_MORE_ELEMENT?;
|
||||
self.remove_at(0);
|
||||
}
|
||||
|
||||
fn Type* List.first(&self)
|
||||
fn Type! List.first(&self)
|
||||
{
|
||||
return self.size ? &self.entries[0] : null;
|
||||
if (!self.size) return IteratorResult.NO_MORE_ELEMENT?;
|
||||
return self.entries[0];
|
||||
}
|
||||
|
||||
fn Type* List.last(&self)
|
||||
fn Type! List.last(&self)
|
||||
{
|
||||
return self.size ? &self.entries[self.size - 1] : null;
|
||||
if (!self.size) return IteratorResult.NO_MORE_ELEMENT?;
|
||||
return self.entries[self.size - 1];
|
||||
}
|
||||
|
||||
fn bool List.is_empty(&self) @inline
|
||||
@@ -281,7 +308,11 @@ fn Type List.get(&self, usz index) @inline
|
||||
fn void List.free(&self)
|
||||
{
|
||||
if (!self.allocator) return;
|
||||
$if type_is_overaligned():
|
||||
allocator::free_aligned(self.allocator, self.entries);
|
||||
$else
|
||||
allocator::free(self.allocator, self.entries);
|
||||
$endif;
|
||||
self.capacity = 0;
|
||||
self.size = 0;
|
||||
self.entries = null;
|
||||
@@ -335,12 +366,12 @@ macro usz List._remove_if(&self, ElementPredicate filter, bool $invert) @local
|
||||
return size - self.size;
|
||||
}
|
||||
|
||||
fn usz List.remove_using_test(&self, ElementTest filter, any* context)
|
||||
fn usz List.remove_using_test(&self, ElementTest filter, any context)
|
||||
{
|
||||
return self._remove_using_test(filter, false, context);
|
||||
}
|
||||
|
||||
fn usz List.retain_using_test(&self, ElementTest filter, any* context)
|
||||
fn usz List.retain_using_test(&self, ElementTest filter, any context)
|
||||
{
|
||||
return self._remove_using_test(filter, true, context);
|
||||
}
|
||||
@@ -379,7 +410,11 @@ fn void List.reserve(&self, usz min_capacity)
|
||||
if (self.capacity >= min_capacity) return;
|
||||
if (!self.allocator) self.allocator = allocator::heap();
|
||||
min_capacity = math::next_power_of_2(min_capacity);
|
||||
self.entries = allocator::realloc_aligned(self.allocator, self.entries, Type.sizeof * min_capacity, .alignment = Type[1].alignof) ?? null;
|
||||
$if type_is_overaligned():
|
||||
self.entries = allocator::realloc_aligned(self.allocator, self.entries, Type.sizeof * min_capacity, .alignment = Type[1].alignof)!!;
|
||||
$else
|
||||
self.entries = allocator::realloc(self.allocator, self.entries, Type.sizeof * min_capacity);
|
||||
$endif;
|
||||
self.capacity = min_capacity;
|
||||
}
|
||||
|
||||
@@ -456,19 +491,38 @@ fn bool List.contains(&self, Type value) @if(ELEMENT_IS_EQUATABLE)
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param [&inout] self "The list to remove elements from"
|
||||
* @param value "The value to remove"
|
||||
* @return "true if the value was found"
|
||||
**/
|
||||
fn bool List.remove_last_item(&self, Type value) @if(ELEMENT_IS_EQUATABLE)
|
||||
{
|
||||
return @ok(self.remove_at(self.rindex_of(value)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param [&inout] self "The list to remove elements from"
|
||||
* @param value "The value to remove"
|
||||
* @return "true if the value was found"
|
||||
**/
|
||||
fn bool List.remove_first_item(&self, Type value) @if(ELEMENT_IS_EQUATABLE)
|
||||
{
|
||||
return @ok(self.remove_at(self.index_of(value)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param [&inout] self "The list to remove elements from"
|
||||
* @param value "The value to remove"
|
||||
* @return "the number of deleted elements."
|
||||
**/
|
||||
fn usz List.remove(&self, Type value) @if(ELEMENT_IS_EQUATABLE)
|
||||
fn usz List.remove_item(&self, Type value) @if(ELEMENT_IS_EQUATABLE)
|
||||
{
|
||||
usz size = self.size;
|
||||
for (usz i = size; i > 0; i--)
|
||||
{
|
||||
if (!equals(self.entries[i - 1], value)) continue;
|
||||
for (usz j = i; j < size; j++)
|
||||
for (usz j = i; j < self.size; j++)
|
||||
{
|
||||
self.entries[j - 1] = self.entries[j];
|
||||
}
|
||||
@@ -477,10 +531,12 @@ fn usz List.remove(&self, Type value) @if(ELEMENT_IS_EQUATABLE)
|
||||
return size - self.size;
|
||||
}
|
||||
|
||||
fn void List.remove_all(&self, List* other_list) @if(ELEMENT_IS_EQUATABLE)
|
||||
|
||||
|
||||
fn void List.remove_all_from(&self, List* other_list) @if(ELEMENT_IS_EQUATABLE)
|
||||
{
|
||||
if (!other_list.size) return;
|
||||
foreach (v : other_list) self.remove(v);
|
||||
foreach (v : other_list) self.remove_item(v);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -508,3 +564,36 @@ fn usz List.compact(&self) @if(ELEMENT_IS_POINTER)
|
||||
}
|
||||
return size - self.size;
|
||||
}
|
||||
|
||||
// --> Deprecated
|
||||
|
||||
/**
|
||||
* @param [&inout] self "The list to remove elements from"
|
||||
* @param value "The value to remove"
|
||||
* @return "true if the value was found"
|
||||
**/
|
||||
fn bool List.remove_last_match(&self, Type value) @if(ELEMENT_IS_EQUATABLE) @deprecated
|
||||
{
|
||||
return self.remove_last_item(value) @inline;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param [&inout] self "The list to remove elements from"
|
||||
* @param value "The value to remove"
|
||||
* @return "true if the value was found"
|
||||
**/
|
||||
fn bool List.remove_first_match(&self, Type value) @if(ELEMENT_IS_EQUATABLE) @deprecated
|
||||
{
|
||||
return self.remove_first_item(value) @inline;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param [&inout] self "The list to remove elements from"
|
||||
* @param value "The value to remove"
|
||||
* @return "the number of deleted elements."
|
||||
**/
|
||||
fn usz List.remove_all_matches(&self, Type value) @if(ELEMENT_IS_EQUATABLE) @deprecated
|
||||
{
|
||||
return self.remove_item(value) @inline;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ const bool COPY_KEYS = types::implements_copy(Key);
|
||||
struct HashMap
|
||||
{
|
||||
Entry*[] table;
|
||||
Allocator* allocator;
|
||||
Allocator allocator;
|
||||
uint count; // Number of elements
|
||||
uint threshold; // Resize limit
|
||||
float load_factor;
|
||||
@@ -26,7 +26,7 @@ struct HashMap
|
||||
* @require !self.allocator "Map was already initialized"
|
||||
* @require capacity < MAXIMUM_CAPACITY "Capacity cannot exceed maximum"
|
||||
**/
|
||||
fn HashMap* HashMap.new_init(&self, uint capacity = DEFAULT_INITIAL_CAPACITY, float load_factor = DEFAULT_LOAD_FACTOR, Allocator* allocator = allocator::heap())
|
||||
fn HashMap* HashMap.new_init(&self, uint capacity = DEFAULT_INITIAL_CAPACITY, float load_factor = DEFAULT_LOAD_FACTOR, Allocator allocator = allocator::heap())
|
||||
{
|
||||
capacity = math::next_power_of_2(capacity);
|
||||
self.allocator = allocator;
|
||||
@@ -36,18 +36,6 @@ fn HashMap* HashMap.new_init(&self, uint capacity = DEFAULT_INITIAL_CAPACITY, fl
|
||||
return self;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param [&inout] allocator "The allocator to use"
|
||||
* @require capacity > 0 "The capacity must be 1 or higher"
|
||||
* @require load_factor > 0.0 "The load factor must be higher than 0"
|
||||
* @require !map.allocator "Map was already initialized"
|
||||
* @require capacity < MAXIMUM_CAPACITY "Capacity cannot exceed maximum"
|
||||
**/
|
||||
fn HashMap* HashMap.init_new(&map, uint capacity = DEFAULT_INITIAL_CAPACITY, float load_factor = DEFAULT_LOAD_FACTOR, Allocator* allocator = allocator::heap()) @deprecated("Replaced by new_init")
|
||||
{
|
||||
return map.new_init(capacity, load_factor, allocator);
|
||||
}
|
||||
|
||||
/**
|
||||
* @require capacity > 0 "The capacity must be 1 or higher"
|
||||
* @require load_factor > 0.0 "The load factor must be higher than 0"
|
||||
@@ -59,16 +47,6 @@ fn HashMap* HashMap.temp_init(&self, uint capacity = DEFAULT_INITIAL_CAPACITY, f
|
||||
return self.new_init(capacity, load_factor, allocator::temp()) @inline;
|
||||
}
|
||||
|
||||
/**
|
||||
* @require capacity > 0 "The capacity must be 1 or higher"
|
||||
* @require load_factor > 0.0 "The load factor must be higher than 0"
|
||||
* @require !map.allocator "Map was already initialized"
|
||||
* @require capacity < MAXIMUM_CAPACITY "Capacity cannot exceed maximum"
|
||||
**/
|
||||
fn HashMap* HashMap.init_temp(&map, uint capacity = DEFAULT_INITIAL_CAPACITY, float load_factor = DEFAULT_LOAD_FACTOR) @deprecated("Replaced by temp_init")
|
||||
{
|
||||
return map.temp_init(capacity, load_factor) @inline;
|
||||
}
|
||||
|
||||
/**
|
||||
* Has this hash map been initialized yet?
|
||||
@@ -85,23 +63,13 @@ fn bool HashMap.is_initialized(&map)
|
||||
* @param [&inout] allocator "The allocator to use"
|
||||
* @param [&in] other_map "The map to copy from."
|
||||
**/
|
||||
fn HashMap* HashMap.new_init_from_map(&self, HashMap* other_map, Allocator* allocator = allocator::heap())
|
||||
fn HashMap* HashMap.new_init_from_map(&self, HashMap* other_map, Allocator allocator = allocator::heap())
|
||||
{
|
||||
self.new_init(other_map.table.len, other_map.load_factor, allocator);
|
||||
self.put_all_for_create(other_map);
|
||||
return self;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param [&inout] allocator "The allocator to use"
|
||||
* @param [&in] other_map "The map to copy from."
|
||||
**/
|
||||
fn HashMap* HashMap.init_new_from_map(&self, HashMap* other_map, Allocator* allocator = allocator::heap()) @deprecated("Replaced by new_init_from_map")
|
||||
|
||||
{
|
||||
return self.new_init_from_map(other_map, allocator) @inline;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param [&in] other_map "The map to copy from."
|
||||
**/
|
||||
@@ -110,15 +78,6 @@ fn HashMap* HashMap.temp_init_from_map(&map, HashMap* other_map)
|
||||
return map.new_init_from_map(other_map, allocator::temp()) @inline;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param [&in] other_map "The map to copy from."
|
||||
**/
|
||||
fn HashMap* HashMap.init_temp_from_map(&map, HashMap* other_map) @deprecated("Replaced by temp_init_from_map")
|
||||
{
|
||||
return map.temp_init_from_map(other_map) @inline;
|
||||
}
|
||||
|
||||
|
||||
fn bool HashMap.is_empty(&map) @inline
|
||||
{
|
||||
return !map.count;
|
||||
@@ -243,7 +202,7 @@ fn Key[] HashMap.key_tlist(&map)
|
||||
return map.key_new_list(allocator::temp()) @inline;
|
||||
}
|
||||
|
||||
fn Key[] HashMap.key_new_list(&map, Allocator* allocator = allocator::heap())
|
||||
fn Key[] HashMap.key_new_list(&map, Allocator allocator = allocator::heap())
|
||||
{
|
||||
if (!map.count) return {};
|
||||
|
||||
@@ -287,7 +246,7 @@ fn Value[] HashMap.value_tlist(&map)
|
||||
return map.value_new_list(allocator::temp()) @inline;
|
||||
}
|
||||
|
||||
fn Value[] HashMap.value_new_list(&map, Allocator* allocator = allocator::heap())
|
||||
fn Value[] HashMap.value_new_list(&map, Allocator allocator = allocator::heap())
|
||||
{
|
||||
if (!map.count) return {};
|
||||
Value[] list = allocator::alloc_array(allocator, Value, map.count);
|
||||
|
||||
@@ -11,7 +11,7 @@ const Object NULL_OBJECT = { .type = void*.typeid };
|
||||
struct Object (Printable)
|
||||
{
|
||||
typeid type;
|
||||
Allocator* allocator;
|
||||
Allocator allocator;
|
||||
union
|
||||
{
|
||||
uint128 i;
|
||||
@@ -76,7 +76,7 @@ fn usz! Object.to_format(&self, Formatter* formatter) @dynamic
|
||||
}
|
||||
}
|
||||
|
||||
fn Object* new_obj(Allocator* allocator)
|
||||
fn Object* new_obj(Allocator allocator)
|
||||
{
|
||||
return allocator::new(allocator, Object, { .allocator = allocator, .type = void.typeid });
|
||||
}
|
||||
@@ -86,22 +86,22 @@ fn Object* new_null()
|
||||
return &NULL_OBJECT;
|
||||
}
|
||||
|
||||
fn Object* new_int(int128 i, Allocator* allocator)
|
||||
fn Object* new_int(int128 i, Allocator allocator)
|
||||
{
|
||||
return allocator::new(allocator, Object, { .i = i, .allocator = allocator, .type = int128.typeid });
|
||||
}
|
||||
|
||||
macro Object* new_enum(e, Allocator* allocator)
|
||||
macro Object* new_enum(e, Allocator allocator)
|
||||
{
|
||||
return allocator::new(allocator, Object, { .i = (int128)e, .allocator = allocator, .type = @typeid(e) });
|
||||
}
|
||||
|
||||
fn Object* new_float(double f, Allocator* allocator)
|
||||
fn Object* new_float(double f, Allocator allocator)
|
||||
{
|
||||
return allocator::new(allocator, Object, { .f = f, .allocator = allocator, .type = double.typeid });
|
||||
}
|
||||
|
||||
fn Object* new_string(String s, Allocator* allocator)
|
||||
fn Object* new_string(String s, Allocator allocator)
|
||||
{
|
||||
return allocator::new(allocator, Object, { .s = s.copy(allocator), .allocator = allocator, .type = String.typeid });
|
||||
}
|
||||
@@ -234,10 +234,10 @@ macro Object* Object.set_at(&self, usz index, String key, value)
|
||||
* @require self.is_indexable()
|
||||
* @ensure return != null
|
||||
**/
|
||||
macro Object* Object.append(&self, value)
|
||||
macro Object* Object.push(&self, value)
|
||||
{
|
||||
Object* val = self.object_from_value(value);
|
||||
self.append_object(val);
|
||||
self.push_object(val);
|
||||
return val;
|
||||
}
|
||||
|
||||
@@ -268,10 +268,10 @@ fn usz Object.get_len(&self)
|
||||
/**
|
||||
* @require self.is_indexable()
|
||||
**/
|
||||
fn void Object.append_object(&self, Object* to_append)
|
||||
fn void Object.push_object(&self, Object* to_append)
|
||||
{
|
||||
self.init_array_if_needed();
|
||||
self.array.append(to_append);
|
||||
self.array.push(to_append);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -282,11 +282,11 @@ fn void Object.set_object_at(&self, usz index, Object* to_set)
|
||||
self.init_array_if_needed();
|
||||
while (self.array.len() < index)
|
||||
{
|
||||
self.array.append(&NULL_OBJECT);
|
||||
self.array.push(&NULL_OBJECT);
|
||||
}
|
||||
if (self.array.len() == index)
|
||||
{
|
||||
self.array.append(to_set);
|
||||
self.array.push(to_set);
|
||||
return;
|
||||
}
|
||||
self.array.get(index).free();
|
||||
|
||||
@@ -36,12 +36,7 @@ struct PrivatePriorityQueue (Printable)
|
||||
Heap heap;
|
||||
}
|
||||
|
||||
fn void PrivatePriorityQueue.init_new(&self, usz initial_capacity = 16, Allocator* allocator = allocator::heap()) @inline @deprecated("Replaced by new_init")
|
||||
{
|
||||
return self.new_init(initial_capacity, allocator);
|
||||
}
|
||||
|
||||
fn void PrivatePriorityQueue.new_init(&self, usz initial_capacity = 16, Allocator* allocator = allocator::heap()) @inline
|
||||
fn void PrivatePriorityQueue.new_init(&self, usz initial_capacity = 16, Allocator allocator = allocator::heap()) @inline
|
||||
{
|
||||
self.heap.new_init(initial_capacity, allocator);
|
||||
}
|
||||
@@ -51,11 +46,6 @@ fn void PrivatePriorityQueue.temp_init(&self, usz initial_capacity = 16) @inline
|
||||
self.heap.new_init(initial_capacity, allocator::temp()) @inline;
|
||||
}
|
||||
|
||||
fn void PrivatePriorityQueue.init_temp(&self, usz initial_capacity = 16) @inline @deprecated("Replaced by temp_init")
|
||||
{
|
||||
return self.temp_init(initial_capacity) @inline;
|
||||
}
|
||||
|
||||
fn void PrivatePriorityQueue.push(&self, Type element)
|
||||
{
|
||||
self.heap.push(element);
|
||||
@@ -117,7 +107,7 @@ fn Type! PrivatePriorityQueue.pop(&self)
|
||||
return self.heap.pop();
|
||||
}
|
||||
|
||||
fn Type! PrivatePriorityQueue.peek(&self)
|
||||
fn Type! PrivatePriorityQueue.first(&self)
|
||||
{
|
||||
if (!self.len()) return IteratorResult.NO_MORE_ELEMENT?;
|
||||
return self.heap.get(0);
|
||||
@@ -141,7 +131,7 @@ fn bool PrivatePriorityQueue.is_empty(&self)
|
||||
/**
|
||||
* @require index < self.len()
|
||||
*/
|
||||
fn Type PrivatePriorityQueue.peek_at(&self, usz index) @operator([])
|
||||
fn Type PrivatePriorityQueue.get(&self, usz index) @operator([])
|
||||
{
|
||||
return self.heap[index];
|
||||
}
|
||||
@@ -151,7 +141,7 @@ fn usz! PrivatePriorityQueue.to_format(&self, Formatter* formatter) @dynamic
|
||||
return self.heap.to_format(formatter);
|
||||
}
|
||||
|
||||
fn String PrivatePriorityQueue.to_new_string(&self, Allocator* allocator = allocator::heap()) @dynamic
|
||||
fn String PrivatePriorityQueue.to_new_string(&self, Allocator allocator = allocator::heap()) @dynamic
|
||||
{
|
||||
return self.heap.to_new_string(allocator);
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ fn Type Range.get(&self, usz index) @operator([])
|
||||
return (Type)(self.start + (usz)index);
|
||||
}
|
||||
|
||||
fn String Range.to_new_string(&self, Allocator* allocator = allocator::heap()) @dynamic
|
||||
fn String Range.to_new_string(&self, Allocator allocator = allocator::heap()) @dynamic
|
||||
{
|
||||
return string::new_format("[%s..%s]", self.start, self.end, .allocator = allocator);
|
||||
}
|
||||
@@ -66,7 +66,7 @@ fn usz! ExclusiveRange.to_format(&self, Formatter* formatter) @dynamic
|
||||
return formatter.printf("[%s..<%s]", self.start, self.end)!;
|
||||
}
|
||||
|
||||
fn String ExclusiveRange.to_new_string(&self, Allocator* allocator = allocator::heap()) @dynamic
|
||||
fn String ExclusiveRange.to_new_string(&self, Allocator allocator = allocator::heap()) @dynamic
|
||||
{
|
||||
return string::new_format("[%s..<%s]", self.start, self.end, .allocator = allocator);
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ fn void RingBuffer.init(&self) @inline
|
||||
*self = {};
|
||||
}
|
||||
|
||||
fn void RingBuffer.putc(&self, Type c)
|
||||
fn void RingBuffer.push(&self, Type c)
|
||||
{
|
||||
if (self.written < SIZE)
|
||||
{
|
||||
@@ -26,7 +26,7 @@ fn void RingBuffer.putc(&self, Type c)
|
||||
}
|
||||
}
|
||||
|
||||
fn Type RingBuffer.getc(&self, usz index)
|
||||
fn Type RingBuffer.get(&self, usz index) @operator([])
|
||||
{
|
||||
index %= SIZE;
|
||||
usz avail = SIZE - self.head;
|
||||
@@ -37,7 +37,7 @@ fn Type RingBuffer.getc(&self, usz index)
|
||||
return self.buf[index - avail];
|
||||
}
|
||||
|
||||
fn Type! RingBuffer.popc(&self)
|
||||
fn Type! RingBuffer.pop(&self)
|
||||
{
|
||||
switch
|
||||
{
|
||||
@@ -52,7 +52,7 @@ fn Type! RingBuffer.popc(&self)
|
||||
}
|
||||
}
|
||||
|
||||
fn usz RingBuffer.get(&self, usz index, Type[] buffer)
|
||||
fn usz RingBuffer.read(&self, usz index, Type[] buffer)
|
||||
{
|
||||
index %= SIZE;
|
||||
if (self.written < SIZE)
|
||||
@@ -87,7 +87,7 @@ fn usz RingBuffer.get(&self, usz index, Type[] buffer)
|
||||
return n1 + n2;
|
||||
}
|
||||
|
||||
fn void RingBuffer.push(&self, Type[] buffer)
|
||||
fn void RingBuffer.write(&self, Type[] buffer)
|
||||
{
|
||||
usz i;
|
||||
while (self.written < SIZE && i < buffer.len)
|
||||
|
||||
@@ -30,9 +30,11 @@ struct ArenaAllocatorHeader @local
|
||||
char[*] data;
|
||||
}
|
||||
|
||||
/*
|
||||
* @require ptr != null
|
||||
**/
|
||||
fn void ArenaAllocator.release(&self, void* ptr, bool) @dynamic
|
||||
{
|
||||
if (!ptr) return;
|
||||
assert((uptr)ptr >= (uptr)self.data.ptr, "Pointer originates from a different allocator.");
|
||||
ArenaAllocatorHeader* header = ptr - ArenaAllocatorHeader.sizeof;
|
||||
// Reclaim memory if it's the last element.
|
||||
@@ -41,53 +43,40 @@ fn void ArenaAllocator.release(&self, void* ptr, bool) @dynamic
|
||||
self.used -= header.size + ArenaAllocatorHeader.sizeof;
|
||||
}
|
||||
}
|
||||
|
||||
fn usz ArenaAllocator.mark(&self) @dynamic => self.used;
|
||||
fn void ArenaAllocator.reset(&self, usz mark) @dynamic => self.used = mark;
|
||||
|
||||
/**
|
||||
* @require !alignment || math::is_power_of_2(alignment)
|
||||
* @require alignment <= mem::MAX_MEMORY_ALIGNMENT `alignment too big`
|
||||
* @require offset <= mem::MAX_MEMORY_ALIGNMENT `offset too big`
|
||||
* @require offset <= size && offset >= 0
|
||||
* @require mem::aligned_offset(offset, ArenaAllocatorHeader.alignof) == offset
|
||||
* @require size > 0
|
||||
**/
|
||||
fn void*! ArenaAllocator.acquire(&self, usz size, bool clear, usz alignment, usz offset) @dynamic
|
||||
fn void*! ArenaAllocator.acquire(&self, usz size, AllocInitType init_type, usz alignment) @dynamic
|
||||
{
|
||||
if (!size) return null;
|
||||
alignment = alignment_for_allocation(alignment);
|
||||
usz total_len = self.data.len;
|
||||
if (size > total_len) return AllocationFailure.CHUNK_TOO_LARGE?;
|
||||
void* start_mem = self.data.ptr;
|
||||
void* unaligned_pointer_to_offset = start_mem + self.used + ArenaAllocatorHeader.sizeof + offset;
|
||||
void* aligned_pointer_to_offset = mem::aligned_pointer(unaligned_pointer_to_offset, alignment);
|
||||
usz end = (usz)(aligned_pointer_to_offset - self.data.ptr) + size - offset;
|
||||
void* unaligned_pointer_to_offset = start_mem + self.used + ArenaAllocatorHeader.sizeof;
|
||||
void* mem = mem::aligned_pointer(unaligned_pointer_to_offset, alignment);
|
||||
usz end = (usz)(mem - self.data.ptr) + size;
|
||||
if (end > total_len) return AllocationFailure.OUT_OF_MEMORY?;
|
||||
self.used = end;
|
||||
void* mem = aligned_pointer_to_offset - offset;
|
||||
ArenaAllocatorHeader* header = mem - ArenaAllocatorHeader.sizeof;
|
||||
header.size = size;
|
||||
if (clear) mem::clear(mem, size, mem::DEFAULT_MEM_ALIGNMENT);
|
||||
if (init_type == ZERO) mem::clear(mem, size, mem::DEFAULT_MEM_ALIGNMENT);
|
||||
return mem;
|
||||
}
|
||||
|
||||
/**
|
||||
* @require !alignment || math::is_power_of_2(alignment)
|
||||
* @require alignment <= mem::MAX_MEMORY_ALIGNMENT `alignment too big`
|
||||
* @require offset <= mem::MAX_MEMORY_ALIGNMENT `offset too big`
|
||||
* @require offset <= size && offset >= 0
|
||||
* @require mem::aligned_offset(offset, ArenaAllocatorHeader.alignof) == offset
|
||||
* @require old_pointer != null
|
||||
* @require size > 0
|
||||
**/
|
||||
fn void*! ArenaAllocator.resize(&self, void *old_pointer, usz size, usz alignment, usz offset) @dynamic
|
||||
fn void*! ArenaAllocator.resize(&self, void *old_pointer, usz size, usz alignment) @dynamic
|
||||
{
|
||||
if (!size)
|
||||
{
|
||||
self.release(old_pointer, alignment > 0);
|
||||
return null;
|
||||
}
|
||||
if (!old_pointer)
|
||||
{
|
||||
return self.acquire(size, true, alignment, offset);
|
||||
}
|
||||
alignment = alignment_for_allocation(alignment);
|
||||
assert(old_pointer >= self.data.ptr, "Pointer originates from a different allocator.");
|
||||
usz total_len = self.data.len;
|
||||
@@ -95,7 +84,7 @@ fn void*! ArenaAllocator.resize(&self, void *old_pointer, usz size, usz alignmen
|
||||
ArenaAllocatorHeader* header = old_pointer - ArenaAllocatorHeader.sizeof;
|
||||
usz old_size = header.size;
|
||||
// Do last allocation and alignment match?
|
||||
if (&self.data[self.used] == old_pointer + old_size && mem::ptr_is_aligned(old_pointer + offset, alignment))
|
||||
if (&self.data[self.used] == old_pointer + old_size && mem::ptr_is_aligned(old_pointer, alignment))
|
||||
{
|
||||
if (old_size >= size)
|
||||
{
|
||||
@@ -111,7 +100,7 @@ fn void*! ArenaAllocator.resize(&self, void *old_pointer, usz size, usz alignmen
|
||||
return old_pointer;
|
||||
}
|
||||
// Otherwise just allocate new memory.
|
||||
void* mem = self.acquire(size, false, alignment, offset)!;
|
||||
void* mem = self.acquire(size, NO_ZERO, alignment)!;
|
||||
mem::copy(mem, old_pointer, old_size, mem::DEFAULT_MEM_ALIGNMENT, mem::DEFAULT_MEM_ALIGNMENT);
|
||||
return mem;
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import std::math;
|
||||
|
||||
struct DynamicArenaAllocator (Allocator)
|
||||
{
|
||||
Allocator* backing_allocator;
|
||||
Allocator backing_allocator;
|
||||
DynamicArenaPage* page;
|
||||
DynamicArenaPage* unused_page;
|
||||
usz page_size;
|
||||
@@ -16,7 +16,7 @@ struct DynamicArenaAllocator (Allocator)
|
||||
* @param [&inout] allocator
|
||||
* @require page_size >= 128
|
||||
**/
|
||||
fn void DynamicArenaAllocator.init(&self, usz page_size, Allocator* allocator)
|
||||
fn void DynamicArenaAllocator.init(&self, usz page_size, Allocator allocator)
|
||||
{
|
||||
self.page = null;
|
||||
self.unused_page = null;
|
||||
@@ -59,11 +59,11 @@ struct DynamicArenaChunk @local
|
||||
}
|
||||
|
||||
/**
|
||||
* @require ptr
|
||||
* @require self.page `tried to free pointer on invalid allocator`
|
||||
*/
|
||||
fn void DynamicArenaAllocator.release(&self, void* ptr, bool) @dynamic
|
||||
{
|
||||
if (!ptr) return;
|
||||
DynamicArenaPage* current_page = self.page;
|
||||
if (ptr == current_page.current_stack_ptr)
|
||||
{
|
||||
@@ -73,19 +73,12 @@ fn void DynamicArenaAllocator.release(&self, void* ptr, bool) @dynamic
|
||||
}
|
||||
|
||||
/**
|
||||
* @require size > 0 `Resize doesn't support zeroing`
|
||||
* @require old_pointer != null `Resize doesn't handle null pointers`
|
||||
* @require self.page `tried to realloc pointer on invalid allocator`
|
||||
*/
|
||||
fn void*! DynamicArenaAllocator.resize(&self, void* old_pointer, usz size, usz alignment, usz offset) @dynamic
|
||||
fn void*! DynamicArenaAllocator.resize(&self, void* old_pointer, usz size, usz alignment) @dynamic
|
||||
{
|
||||
if (!size)
|
||||
{
|
||||
self.release(old_pointer, alignment > 0);
|
||||
return null;
|
||||
}
|
||||
if (!old_pointer)
|
||||
{
|
||||
return self.acquire(size, true, alignment, offset);
|
||||
}
|
||||
DynamicArenaPage* current_page = self.page;
|
||||
alignment = alignment_for_allocation(alignment);
|
||||
usz* old_size_ptr = old_pointer - DEFAULT_SIZE_PREFIX;
|
||||
@@ -109,7 +102,7 @@ fn void*! DynamicArenaAllocator.resize(&self, void* old_pointer, usz size, usz a
|
||||
current_page.used += add_size;
|
||||
return old_pointer;
|
||||
}
|
||||
void* new_mem = self.acquire(size, false, alignment, offset)!;
|
||||
void* new_mem = self.acquire(size, NO_ZERO, alignment)!;
|
||||
mem::copy(new_mem, old_pointer, old_size, mem::DEFAULT_MEM_ALIGNMENT);
|
||||
return new_mem;
|
||||
}
|
||||
@@ -135,10 +128,10 @@ fn void DynamicArenaAllocator.reset(&self, usz mark = 0) @dynamic
|
||||
* @require math::is_power_of_2(alignment)
|
||||
* @require size > 0
|
||||
*/
|
||||
fn void*! DynamicArenaAllocator._alloc_new(&self, usz size, usz alignment, usz offset) @local
|
||||
fn void*! DynamicArenaAllocator._alloc_new(&self, usz size, usz alignment) @local
|
||||
{
|
||||
// First, make sure that we can align it, extending the page size if needed.
|
||||
usz page_size = max(self.page_size, mem::aligned_offset(size + DynamicArenaChunk.sizeof + offset, alignment) - offset);
|
||||
usz page_size = max(self.page_size, mem::aligned_offset(size + DynamicArenaChunk.sizeof, alignment));
|
||||
|
||||
// Grab the page without alignment (we do it ourselves)
|
||||
void* mem = allocator::malloc_try(self.backing_allocator, page_size)!;
|
||||
@@ -149,7 +142,7 @@ fn void*! DynamicArenaAllocator._alloc_new(&self, usz size, usz alignment, usz o
|
||||
return err?;
|
||||
}
|
||||
page.memory = mem;
|
||||
void* mem_start = mem::aligned_pointer(mem + offset + DynamicArenaChunk.sizeof, alignment) - offset;
|
||||
void* mem_start = mem::aligned_pointer(mem + DynamicArenaChunk.sizeof, alignment);
|
||||
assert(mem_start + size < mem + page_size);
|
||||
DynamicArenaChunk* chunk = (DynamicArenaChunk*)mem_start - 1;
|
||||
chunk.size = size;
|
||||
@@ -162,11 +155,11 @@ fn void*! DynamicArenaAllocator._alloc_new(&self, usz size, usz alignment, usz o
|
||||
}
|
||||
|
||||
/**
|
||||
* @require size > 0 `acquire expects size > 0`
|
||||
* @require !alignment || math::is_power_of_2(alignment)
|
||||
*/
|
||||
fn void*! DynamicArenaAllocator.acquire(&self, usz size, bool clear, usz alignment, usz offset) @dynamic
|
||||
fn void*! DynamicArenaAllocator.acquire(&self, usz size, AllocInitType init_type, usz alignment) @dynamic
|
||||
{
|
||||
if (!size) return null;
|
||||
alignment = alignment_for_allocation(alignment);
|
||||
DynamicArenaPage* page = self.page;
|
||||
void* ptr = {|
|
||||
@@ -176,14 +169,14 @@ fn void*! DynamicArenaAllocator.acquire(&self, usz size, bool clear, usz alignme
|
||||
self.unused_page = page.prev_arena;
|
||||
page.prev_arena = null;
|
||||
}
|
||||
if (!page) return self._alloc_new(size, alignment, offset);
|
||||
void* start = mem::aligned_pointer(page.memory + page.used + DynamicArenaChunk.sizeof + offset, alignment) - offset;
|
||||
if (!page) return self._alloc_new(size, alignment);
|
||||
void* start = mem::aligned_pointer(page.memory + page.used + DynamicArenaChunk.sizeof, alignment);
|
||||
usz new_used = start - page.memory + size;
|
||||
if ALLOCATE_NEW: (new_used > page.total)
|
||||
{
|
||||
if ((page = self.unused_page))
|
||||
{
|
||||
start = mem::aligned_pointer(page.memory + page.used + DynamicArenaChunk.sizeof + offset, alignment) - offset;
|
||||
start = mem::aligned_pointer(page.memory + page.used + DynamicArenaChunk.sizeof, alignment);
|
||||
new_used = start + size - page.memory;
|
||||
if (page.total >= new_used)
|
||||
{
|
||||
@@ -193,7 +186,7 @@ fn void*! DynamicArenaAllocator.acquire(&self, usz size, bool clear, usz alignme
|
||||
break ALLOCATE_NEW;
|
||||
}
|
||||
}
|
||||
return self._alloc_new(size, alignment, offset);
|
||||
return self._alloc_new(size, alignment);
|
||||
}
|
||||
page.used = new_used;
|
||||
assert(start + size == page.memory + page.used);
|
||||
@@ -202,6 +195,6 @@ fn void*! DynamicArenaAllocator.acquire(&self, usz size, bool clear, usz alignme
|
||||
chunk.size = size;
|
||||
return mem;
|
||||
|}!;
|
||||
if (clear) mem::clear(ptr, size, mem::DEFAULT_MEM_ALIGNMENT);
|
||||
if (init_type == ZERO) mem::clear(ptr, size, mem::DEFAULT_MEM_ALIGNMENT);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
@@ -21,29 +21,19 @@ fn void SimpleHeapAllocator.init(&self, MemoryAllocFn allocator)
|
||||
self.free_list = null;
|
||||
}
|
||||
|
||||
fn void*! SimpleHeapAllocator.acquire(&self, usz size, bool clear, usz alignment, usz offset) @dynamic
|
||||
fn void*! SimpleHeapAllocator.acquire(&self, usz size, AllocInitType init_type, usz alignment) @dynamic
|
||||
{
|
||||
if (!size) return null;
|
||||
if (clear)
|
||||
if (init_type == ZERO)
|
||||
{
|
||||
return alignment > 0 ? @aligned_calloc(self._calloc, size, alignment, offset) : self._calloc(size);
|
||||
return alignment > 0 ? @aligned_alloc(self._calloc, size, alignment) : self._calloc(size);
|
||||
}
|
||||
return alignment > 0 ? @aligned_alloc(self._alloc, size, alignment, offset) : self._alloc(size);
|
||||
return alignment > 0 ? @aligned_alloc(self._alloc, size, alignment) : self._alloc(size);
|
||||
}
|
||||
|
||||
fn void*! SimpleHeapAllocator.resize(&self, void* old_pointer, usz size, usz alignment, usz offset) @dynamic
|
||||
fn void*! SimpleHeapAllocator.resize(&self, void* old_pointer, usz size, usz alignment) @dynamic
|
||||
{
|
||||
if (!size)
|
||||
{
|
||||
self.release(old_pointer, alignment > 0);
|
||||
return null;
|
||||
}
|
||||
if (!old_pointer)
|
||||
{
|
||||
return self.acquire(size, true, alignment, offset);
|
||||
}
|
||||
return alignment > 0
|
||||
? @aligned_realloc(self._calloc, self._free, old_pointer, size, alignment, offset)
|
||||
? @aligned_realloc(self._calloc, self._free, old_pointer, size, alignment)
|
||||
: self._realloc(old_pointer, size);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,21 +6,123 @@ module std::core::mem::allocator;
|
||||
import libc;
|
||||
|
||||
const LibcAllocator LIBC_ALLOCATOR = {};
|
||||
|
||||
|
||||
distinct LibcAllocator (Allocator) = uptr;
|
||||
|
||||
fn void*! LibcAllocator.acquire(&self, usz bytes, bool clear, usz alignment, usz offset) @dynamic
|
||||
module std::core::mem::allocator @if(env::POSIX);
|
||||
import std::os;
|
||||
import libc;
|
||||
|
||||
fn void*! LibcAllocator.acquire(&self, usz bytes, AllocInitType init_type, usz alignment) @dynamic
|
||||
{
|
||||
assert(alignment != 0 || offset == 0);
|
||||
if (clear)
|
||||
if (init_type == ZERO)
|
||||
{
|
||||
void* data = alignment ? @aligned_calloc(fn void*(usz bytes) => libc::calloc(bytes, 1), bytes, alignment, offset)!! : libc::calloc(bytes, 1);
|
||||
void* data @noinit;
|
||||
if (alignment > mem::DEFAULT_MEM_ALIGNMENT)
|
||||
{
|
||||
if (posix::posix_memalign(&data, alignment, bytes)) return AllocationFailure.OUT_OF_MEMORY?;
|
||||
mem::clear(data, bytes, mem::DEFAULT_MEM_ALIGNMENT);
|
||||
return data;
|
||||
}
|
||||
return libc::calloc(1, bytes) ?: AllocationFailure.OUT_OF_MEMORY?;
|
||||
}
|
||||
else
|
||||
{
|
||||
void* data @noinit;
|
||||
if (alignment > mem::DEFAULT_MEM_ALIGNMENT)
|
||||
{
|
||||
if (posix::posix_memalign(&data, alignment, bytes)) return AllocationFailure.OUT_OF_MEMORY?;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!(data = libc::malloc(bytes))) return AllocationFailure.OUT_OF_MEMORY?;
|
||||
}
|
||||
$if env::TESTING:
|
||||
for (usz i = 0; i < bytes; i++) ((char*)data)[i] = 0xAA;
|
||||
$endif
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
fn void*! LibcAllocator.resize(&self, void* old_ptr, usz new_bytes, usz alignment) @dynamic
|
||||
{
|
||||
if (alignment <= mem::DEFAULT_MEM_ALIGNMENT) return libc::realloc(old_ptr, new_bytes) ?: AllocationFailure.OUT_OF_MEMORY?;
|
||||
void* new_ptr;
|
||||
if (posix::posix_memalign(&new_ptr, alignment, new_bytes)) return AllocationFailure.OUT_OF_MEMORY?;
|
||||
|
||||
$switch
|
||||
$case env::DARWIN:
|
||||
usz old_usable_size = darwin::malloc_size(old_ptr);
|
||||
$case env::LINUX:
|
||||
usz old_usable_size = linux::malloc_usable_size(old_ptr);
|
||||
$default:
|
||||
usz old_usable_size = new_bytes;
|
||||
$endswitch
|
||||
|
||||
usz copy_size = new_bytes < old_usable_size ? new_bytes : old_usable_size;
|
||||
mem::copy(new_ptr, old_ptr, copy_size, mem::DEFAULT_MEM_ALIGNMENT, mem::DEFAULT_MEM_ALIGNMENT);
|
||||
libc::free(old_ptr);
|
||||
return new_ptr;
|
||||
}
|
||||
|
||||
fn void LibcAllocator.release(&self, void* old_ptr, bool aligned) @dynamic
|
||||
{
|
||||
libc::free(old_ptr);
|
||||
}
|
||||
|
||||
module std::core::mem::allocator @if(env::WIN32);
|
||||
import std::os::win32;
|
||||
import libc;
|
||||
|
||||
fn void*! LibcAllocator.acquire(&self, usz bytes, AllocInitType init_type, usz alignment) @dynamic
|
||||
{
|
||||
if (init_type == ZERO)
|
||||
{
|
||||
if (alignment > 0)
|
||||
{
|
||||
return win32::_aligned_recalloc(null, bytes, alignment) ?: AllocationFailure.OUT_OF_MEMORY?;
|
||||
}
|
||||
return libc::calloc(1, bytes) ?: AllocationFailure.OUT_OF_MEMORY?;
|
||||
}
|
||||
void* data = alignment > 0 ? win32::_aligned_malloc(bytes, alignment) : libc::malloc(bytes);
|
||||
if (!data) return AllocationFailure.OUT_OF_MEMORY?;
|
||||
$if env::TESTING:
|
||||
for (usz i = 0; i < bytes; i++) ((char*)data)[i] = 0xAA;
|
||||
$endif
|
||||
return data;
|
||||
}
|
||||
|
||||
fn void*! LibcAllocator.resize(&self, void* old_ptr, usz new_bytes, usz alignment) @dynamic
|
||||
{
|
||||
if (alignment)
|
||||
{
|
||||
return win32::_aligned_realloc(old_ptr, new_bytes, alignment) ?: AllocationFailure.OUT_OF_MEMORY?;
|
||||
}
|
||||
return libc::realloc(old_ptr, new_bytes) ?: AllocationFailure.OUT_OF_MEMORY?;
|
||||
}
|
||||
|
||||
fn void LibcAllocator.release(&self, void* old_ptr, bool aligned) @dynamic
|
||||
{
|
||||
if (aligned)
|
||||
{
|
||||
win32::_aligned_free(old_ptr);
|
||||
return;
|
||||
}
|
||||
libc::free(old_ptr);
|
||||
}
|
||||
|
||||
module std::core::mem::allocator @if(!env::WIN32 && !env::POSIX);
|
||||
import libc;
|
||||
|
||||
fn void*! LibcAllocator.acquire(&self, usz bytes, AllocInitType init_type, usz alignment) @dynamic
|
||||
{
|
||||
if (init_type == ZERO)
|
||||
{
|
||||
void* data = alignment ? @aligned_alloc(fn void*(usz bytes) => libc::calloc(bytes, 1), bytes, alignment)!! : libc::calloc(bytes, 1);
|
||||
return data ?: AllocationFailure.OUT_OF_MEMORY?;
|
||||
}
|
||||
else
|
||||
{
|
||||
void* data = alignment ? @aligned_alloc(libc::malloc, bytes, alignment, offset)!! : libc::malloc(bytes);
|
||||
void* data = alignment ? @aligned_alloc(libc::malloc, bytes, alignment)!! : libc::malloc(bytes);
|
||||
if (!data) return AllocationFailure.OUT_OF_MEMORY?;
|
||||
$if env::TESTING:
|
||||
for (usz i = 0; i < bytes; i++) ((char*)data)[i] = 0xAA;
|
||||
@@ -29,21 +131,12 @@ fn void*! LibcAllocator.acquire(&self, usz bytes, bool clear, usz alignment, usz
|
||||
}
|
||||
}
|
||||
|
||||
fn void*! LibcAllocator.resize(&self, void* old_ptr, usz new_bytes, usz alignment, usz offset) @dynamic
|
||||
|
||||
fn void*! LibcAllocator.resize(&self, void* old_ptr, usz new_bytes, usz alignment) @dynamic
|
||||
{
|
||||
assert(alignment != 0 || offset == 0);
|
||||
if (!new_bytes)
|
||||
{
|
||||
self.release(old_ptr, alignment > 0);
|
||||
return null;
|
||||
}
|
||||
if (!old_ptr)
|
||||
{
|
||||
return self.acquire(new_bytes, true, alignment, offset);
|
||||
}
|
||||
if (alignment)
|
||||
{
|
||||
void* data = @aligned_realloc(fn void*(usz bytes) => libc::calloc(bytes, 1), libc::free, old_ptr, new_bytes, alignment, offset)!!;
|
||||
void* data = @aligned_realloc(fn void*(usz bytes) => libc::malloc(bytes), libc::free, old_ptr, new_bytes, alignment)!!;
|
||||
return data ?: AllocationFailure.OUT_OF_MEMORY?;
|
||||
}
|
||||
return libc::realloc(old_ptr, new_bytes) ?: AllocationFailure.OUT_OF_MEMORY?;
|
||||
|
||||
@@ -2,7 +2,7 @@ module std::core::mem::allocator;
|
||||
|
||||
struct OnStackAllocator (Allocator)
|
||||
{
|
||||
Allocator* backing_allocator;
|
||||
Allocator backing_allocator;
|
||||
char[] data;
|
||||
usz used;
|
||||
OnStackAllocatorExtraChunk* chunk;
|
||||
@@ -20,7 +20,7 @@ struct OnStackAllocatorExtraChunk @local
|
||||
* @param [&inout] allocator
|
||||
* Initialize a memory arena for use using the provided bytes.
|
||||
**/
|
||||
fn void OnStackAllocator.init(&self, char[] data, Allocator* allocator)
|
||||
fn void OnStackAllocator.init(&self, char[] data, Allocator allocator)
|
||||
{
|
||||
self.data = data;
|
||||
self.backing_allocator = allocator;
|
||||
@@ -54,9 +54,11 @@ struct OnStackAllocatorHeader
|
||||
char[*] data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @require old_pointer
|
||||
**/
|
||||
fn void OnStackAllocator.release(&self, void* old_pointer, bool aligned) @dynamic
|
||||
{
|
||||
if (!old_pointer) return;
|
||||
if (allocation_in_stack_mem(self, old_pointer)) return;
|
||||
on_stack_allocator_remove_chunk(self, old_pointer);
|
||||
self.release(old_pointer, aligned);
|
||||
@@ -98,45 +100,39 @@ fn OnStackAllocatorExtraChunk* on_stack_allocator_find_chunk(OnStackAllocator* a
|
||||
|
||||
/**
|
||||
* @require size > 0
|
||||
* @require old_pointer != null
|
||||
* @require alignment <= mem::MAX_MEMORY_ALIGNMENT `alignment too big`
|
||||
* @require offset <= mem::MAX_MEMORY_ALIGNMENT `offset too big`
|
||||
* @require offset <= size && offset >= 0
|
||||
* @require mem::aligned_offset(offset, OnStackAllocatorExtraChunk.alignof) == offset
|
||||
**/
|
||||
fn void*! OnStackAllocator.resize(&self, void* old_pointer, usz size, usz alignment, usz offset) @dynamic
|
||||
fn void*! OnStackAllocator.resize(&self, void* old_pointer, usz size, usz alignment) @dynamic
|
||||
{
|
||||
if (!allocation_in_stack_mem(self, old_pointer))
|
||||
{
|
||||
OnStackAllocatorExtraChunk* chunk = on_stack_allocator_find_chunk(self, old_pointer);
|
||||
assert(chunk, "Tried to realloc pointer not belonging to the allocator");
|
||||
return chunk.data = self.backing_allocator.resize(old_pointer, size, alignment, offset)!;
|
||||
return chunk.data = self.backing_allocator.resize(old_pointer, size, alignment)!;
|
||||
}
|
||||
|
||||
OnStackAllocatorHeader* header = old_pointer - OnStackAllocatorHeader.sizeof;
|
||||
usz old_size = header.size;
|
||||
void* mem = self.acquire(size, true, alignment, offset)!;
|
||||
void* mem = self.acquire(size, NO_ZERO, alignment)!;
|
||||
mem::copy(mem, old_pointer, old_size, mem::DEFAULT_MEM_ALIGNMENT, mem::DEFAULT_MEM_ALIGNMENT);
|
||||
return mem;
|
||||
}
|
||||
|
||||
/**
|
||||
* @require alignment <= mem::MAX_MEMORY_ALIGNMENT `alignment too big`
|
||||
* @require offset <= mem::MAX_MEMORY_ALIGNMENT `offset too big`
|
||||
* @require offset <= size && offset >= 0
|
||||
* @require offset == 0 || alignment > 0
|
||||
* @require mem::aligned_offset(offset, OnStackAllocatorHeader.alignof) == offset
|
||||
* @require size > 0
|
||||
**/
|
||||
fn void*! OnStackAllocator.acquire(&self, usz size, bool clear, usz alignment, usz offset) @dynamic
|
||||
fn void*! OnStackAllocator.acquire(&self, usz size, AllocInitType init_type, usz alignment) @dynamic
|
||||
{
|
||||
if (size == 0) return null;
|
||||
bool aligned = alignment > 0;
|
||||
alignment = alignment_for_allocation(alignment);
|
||||
usz total_len = self.data.len;
|
||||
void* start_mem = self.data.ptr;
|
||||
void* unaligned_pointer_to_offset = start_mem + self.used + OnStackAllocatorHeader.sizeof + offset;
|
||||
void* aligned_pointer_to_offset = mem::aligned_pointer(unaligned_pointer_to_offset, alignment);
|
||||
usz end = (usz)(aligned_pointer_to_offset - self.data.ptr) + size - offset;
|
||||
Allocator* backing_allocator = self.backing_allocator;
|
||||
void* unaligned_pointer_to_offset = start_mem + self.used + OnStackAllocatorHeader.sizeof ;
|
||||
void* mem = mem::aligned_pointer(unaligned_pointer_to_offset, alignment);
|
||||
usz end = (usz)(mem - self.data.ptr) + size;
|
||||
Allocator backing_allocator = self.backing_allocator;
|
||||
|
||||
if (end > total_len)
|
||||
{
|
||||
@@ -144,10 +140,9 @@ fn void*! OnStackAllocator.acquire(&self, usz size, bool clear, usz alignment, u
|
||||
defer catch allocator::free(backing_allocator, chunk);
|
||||
defer try self.chunk = chunk;
|
||||
*chunk = { .prev = self.chunk, .is_aligned = aligned };
|
||||
return chunk.data = backing_allocator.acquire(size, clear, aligned ? alignment : 0, offset)!;
|
||||
return chunk.data = backing_allocator.acquire(size, init_type, aligned ? alignment : 0)!;
|
||||
}
|
||||
self.used = end;
|
||||
void *mem = aligned_pointer_to_offset - offset;
|
||||
OnStackAllocatorHeader* header = mem - OnStackAllocatorHeader.sizeof;
|
||||
header.size = size;
|
||||
return mem;
|
||||
|
||||
@@ -9,7 +9,7 @@ struct TempAllocatorChunk @local
|
||||
|
||||
struct TempAllocator (Allocator)
|
||||
{
|
||||
Allocator* backing_allocator;
|
||||
Allocator backing_allocator;
|
||||
TempAllocatorPage* last_page;
|
||||
usz used;
|
||||
usz capacity;
|
||||
@@ -35,7 +35,7 @@ macro bool TempAllocatorPage.is_aligned(&self) => self.size & PAGE_IS_ALIGNED ==
|
||||
/**
|
||||
* @require size >= 16
|
||||
**/
|
||||
fn TempAllocator*! new_temp_allocator(usz size, Allocator* allocator)
|
||||
fn TempAllocator*! new_temp_allocator(usz size, Allocator allocator)
|
||||
{
|
||||
TempAllocator* temp = allocator::alloc_with_padding(allocator, TempAllocator, size)!;
|
||||
temp.last_page = null;
|
||||
@@ -45,11 +45,6 @@ fn TempAllocator*! new_temp_allocator(usz size, Allocator* allocator)
|
||||
return temp;
|
||||
}
|
||||
|
||||
fn TempAllocator*! new_temp(usz size, Allocator* allocator) @deprecated("Use new_temp_allocator")
|
||||
{
|
||||
return new_temp_allocator(size, allocator);
|
||||
}
|
||||
|
||||
fn usz TempAllocator.mark(&self) @dynamic => self.used;
|
||||
|
||||
fn void TempAllocator.release(&self, void* old_pointer, bool) @dynamic
|
||||
@@ -79,7 +74,7 @@ fn void! TempAllocator._free_page(&self, TempAllocatorPage* page) @inline @local
|
||||
return self.backing_allocator.release(mem, page.is_aligned());
|
||||
}
|
||||
|
||||
fn void*! TempAllocator._realloc_page(&self, TempAllocatorPage* page, usz size, usz alignment, usz offset) @inline @local
|
||||
fn void*! TempAllocator._realloc_page(&self, TempAllocatorPage* page, usz size, usz alignment) @inline @local
|
||||
{
|
||||
// Then the actual start pointer:
|
||||
void* real_pointer = page.start;
|
||||
@@ -94,46 +89,36 @@ fn void*! TempAllocator._realloc_page(&self, TempAllocatorPage* page, usz size,
|
||||
*pointer_to_prev = page.prev_page;
|
||||
usz page_size = page.pagesize();
|
||||
// Clear on size > original size.
|
||||
void* data = self.acquire(size, size > page_size, alignment, offset)!;
|
||||
void* data = self.acquire(size, NO_ZERO, alignment)!;
|
||||
mem::copy(data, &page.data[0], page_size, mem::DEFAULT_MEM_ALIGNMENT, mem::DEFAULT_MEM_ALIGNMENT);
|
||||
self.backing_allocator.release(real_pointer, page.is_aligned());
|
||||
return data;
|
||||
}
|
||||
|
||||
fn void*! TempAllocator.resize(&self, void* pointer, usz size, usz alignment, usz offset) @dynamic
|
||||
fn void*! TempAllocator.resize(&self, void* pointer, usz size, usz alignment) @dynamic
|
||||
{
|
||||
if (!size)
|
||||
{
|
||||
self.release(pointer, alignment > 0);
|
||||
return null;
|
||||
}
|
||||
if (!pointer)
|
||||
{
|
||||
return self.acquire(size, true, alignment, offset);
|
||||
}
|
||||
TempAllocatorChunk *chunk = pointer - TempAllocatorChunk.sizeof;
|
||||
if (chunk.size == (usz)-1)
|
||||
{
|
||||
assert(self.last_page, "Realloc of non temp pointer");
|
||||
// First grab the page
|
||||
TempAllocatorPage *page = pointer - TempAllocatorPage.sizeof;
|
||||
return self._realloc_page(page, size, alignment, offset);
|
||||
return self._realloc_page(page, size, alignment);
|
||||
}
|
||||
|
||||
// TODO optimize last allocation
|
||||
TempAllocatorChunk* data = self.acquire(size, size > chunk.size, alignment, offset)!;
|
||||
TempAllocatorChunk* data = self.acquire(size, NO_ZERO, alignment)!;
|
||||
mem::copy(data, pointer, chunk.size, mem::DEFAULT_MEM_ALIGNMENT, mem::DEFAULT_MEM_ALIGNMENT);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @require size > 0
|
||||
* @require !alignment || math::is_power_of_2(alignment)
|
||||
* @require alignment <= mem::MAX_MEMORY_ALIGNMENT `alignment too big`
|
||||
**/
|
||||
fn void*! TempAllocator.acquire(&self, usz size, bool clear, usz alignment, usz offset) @dynamic
|
||||
fn void*! TempAllocator.acquire(&self, usz size, AllocInitType init_type, usz alignment) @dynamic
|
||||
{
|
||||
if (!size) return null;
|
||||
alignment = alignment_for_allocation(alignment);
|
||||
void* start_mem = &self.data;
|
||||
void* starting_ptr = start_mem + self.used;
|
||||
@@ -141,7 +126,7 @@ fn void*! TempAllocator.acquire(&self, usz size, bool clear, usz alignment, usz
|
||||
void* mem = aligned_header_start + TempAllocatorChunk.sizeof;
|
||||
if (alignment > TempAllocatorChunk.alignof)
|
||||
{
|
||||
mem = mem::aligned_pointer(mem + offset, alignment) - offset;
|
||||
mem = mem::aligned_pointer(mem, alignment);
|
||||
}
|
||||
usz new_usage = (usz)(mem - start_mem) + size;
|
||||
|
||||
@@ -151,7 +136,7 @@ fn void*! TempAllocator.acquire(&self, usz size, bool clear, usz alignment, usz
|
||||
TempAllocatorChunk* chunk_start = mem - TempAllocatorChunk.sizeof;
|
||||
chunk_start.size = size;
|
||||
self.used = new_usage;
|
||||
if (clear) mem::clear(mem, size, mem::DEFAULT_MEM_ALIGNMENT);
|
||||
if (init_type == ZERO) mem::clear(mem, size, mem::DEFAULT_MEM_ALIGNMENT);
|
||||
return mem;
|
||||
}
|
||||
|
||||
@@ -159,19 +144,20 @@ fn void*! TempAllocator.acquire(&self, usz size, bool clear, usz alignment, usz
|
||||
TempAllocatorPage* page;
|
||||
|
||||
// We have something we need to align.
|
||||
if (alignment > mem::DEFAULT_MEM_ALIGNMENT || offset)
|
||||
if (alignment > mem::DEFAULT_MEM_ALIGNMENT)
|
||||
{
|
||||
// This is actually simpler, since it will create the offset for us.
|
||||
usz total_alloc_size = TempAllocatorPage.sizeof + size;
|
||||
if (clear)
|
||||
usz total_alloc_size = mem::aligned_offset(TempAllocatorPage.sizeof + size, alignment);
|
||||
if (init_type == ZERO)
|
||||
{
|
||||
page = allocator::calloc_aligned(self.backing_allocator, total_alloc_size, alignment, TempAllocatorPage.sizeof + offset)!;
|
||||
mem = allocator::calloc_aligned(self.backing_allocator, total_alloc_size, alignment)!;
|
||||
}
|
||||
else
|
||||
{
|
||||
page = allocator::malloc_aligned(self.backing_allocator, total_alloc_size, alignment, TempAllocatorPage.sizeof + offset)!;
|
||||
mem = allocator::malloc_aligned(self.backing_allocator, total_alloc_size, alignment)!;
|
||||
}
|
||||
page.start = page;
|
||||
page = (TempAllocatorPage*)mem - 1;
|
||||
page.start = mem;
|
||||
page.size = size | PAGE_IS_ALIGNED;
|
||||
}
|
||||
else
|
||||
@@ -179,7 +165,7 @@ fn void*! TempAllocator.acquire(&self, usz size, bool clear, usz alignment, usz
|
||||
// Here we might need to pad
|
||||
usz padded_header_size = mem::aligned_offset(TempAllocatorPage.sizeof, mem::DEFAULT_MEM_ALIGNMENT);
|
||||
usz total_alloc_size = padded_header_size + size;
|
||||
void* alloc = self.backing_allocator.acquire(total_alloc_size, clear, 0, 0)!;
|
||||
void* alloc = self.backing_allocator.acquire(total_alloc_size, init_type, 0)!;
|
||||
|
||||
// Find the page.
|
||||
page = alloc + padded_header_size - TempAllocatorPage.sizeof;
|
||||
|
||||
@@ -20,7 +20,7 @@ def AllocMap = HashMap(<uptr, Allocation>);
|
||||
// is not compatible with allocators that uses mark()
|
||||
struct TrackingAllocator (Allocator)
|
||||
{
|
||||
Allocator* inner_allocator;
|
||||
Allocator inner_allocator;
|
||||
AllocMap map;
|
||||
usz mem_total;
|
||||
usz allocs_total;
|
||||
@@ -31,7 +31,7 @@ struct TrackingAllocator (Allocator)
|
||||
*
|
||||
* @param [&inout] allocator "The allocator to track"
|
||||
**/
|
||||
fn void TrackingAllocator.init(&self, Allocator* allocator)
|
||||
fn void TrackingAllocator.init(&self, Allocator allocator)
|
||||
{
|
||||
*self = { .inner_allocator = allocator };
|
||||
self.map.new_init(.allocator = allocator);
|
||||
@@ -69,7 +69,7 @@ fn usz TrackingAllocator.total_allocated(&self) => self.mem_total;
|
||||
**/
|
||||
fn usz TrackingAllocator.total_allocation_count(&self) => self.allocs_total;
|
||||
|
||||
fn Allocation[] TrackingAllocator.allocations_tlist(&self, Allocator* allocator)
|
||||
fn Allocation[] TrackingAllocator.allocations_tlist(&self, Allocator allocator)
|
||||
{
|
||||
return self.map.value_tlist();
|
||||
}
|
||||
@@ -79,48 +79,36 @@ fn Allocation[] TrackingAllocator.allocations_tlist(&self, Allocator* allocator)
|
||||
**/
|
||||
fn usz TrackingAllocator.allocation_count(&self) => self.map.count;
|
||||
|
||||
fn void*! TrackingAllocator.acquire(&self, usz size, bool clear, usz alignment, usz offset) @dynamic
|
||||
fn void*! TrackingAllocator.acquire(&self, usz size, AllocInitType init_type, usz alignment) @dynamic
|
||||
{
|
||||
void* data = self.inner_allocator.acquire(size, clear, alignment, offset)!;
|
||||
void* data = self.inner_allocator.acquire(size, init_type, alignment)!;
|
||||
self.allocs_total++;
|
||||
void*[MAX_BACKTRACE] bt;
|
||||
backtrace::capture_current(&bt);
|
||||
self.map.set((uptr)data, { data, size, bt });
|
||||
self.mem_total += size;
|
||||
self.allocs_total++;
|
||||
if (data)
|
||||
{
|
||||
void*[MAX_BACKTRACE] bt;
|
||||
backtrace::capture_current(&bt);
|
||||
self.map.set((uptr)data, { data, size, bt });
|
||||
self.mem_total += size;
|
||||
self.allocs_total++;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
fn void*! TrackingAllocator.resize(&self, void* old_pointer, usz size, usz alignment, usz offset) @dynamic
|
||||
fn void*! TrackingAllocator.resize(&self, void* old_pointer, usz size, usz alignment) @dynamic
|
||||
{
|
||||
void* data = self.inner_allocator.resize(old_pointer, size, alignment, offset)!;
|
||||
if (old_pointer)
|
||||
{
|
||||
self.map.remove((uptr)old_pointer);
|
||||
}
|
||||
if (data)
|
||||
{
|
||||
void*[MAX_BACKTRACE] bt;
|
||||
backtrace::capture_current(&bt);
|
||||
self.map.set((uptr)data, { data, size, bt });
|
||||
self.mem_total += size;
|
||||
self.allocs_total++;
|
||||
}
|
||||
void* data = self.inner_allocator.resize(old_pointer, size, alignment)!;
|
||||
self.map.remove((uptr)old_pointer);
|
||||
void*[MAX_BACKTRACE] bt;
|
||||
backtrace::capture_current(&bt);
|
||||
self.map.set((uptr)data, { data, size, bt });
|
||||
self.mem_total += size;
|
||||
self.allocs_total++;
|
||||
return data;
|
||||
}
|
||||
|
||||
fn void TrackingAllocator.release(&self, void* old_pointer, bool is_aligned) @dynamic
|
||||
{
|
||||
if (old_pointer)
|
||||
{
|
||||
if (catch self.map.remove((uptr)old_pointer))
|
||||
{
|
||||
assert(false, "Attempt to release untracked pointer %p, this is likely a bug.", old_pointer);
|
||||
}
|
||||
}
|
||||
if (catch self.map.remove((uptr)old_pointer))
|
||||
{
|
||||
unreachable("Attempt to release untracked pointer %p, this is likely a bug.", old_pointer);
|
||||
}
|
||||
self.inner_allocator.release(old_pointer, is_aligned);
|
||||
}
|
||||
|
||||
@@ -131,7 +119,7 @@ fn void TrackingAllocator.clear(&self)
|
||||
|
||||
fn void TrackingAllocator.print_report(&self) => self.fprint_report(io::stdout())!!;
|
||||
|
||||
fn void! TrackingAllocator.fprint_report(&self, OutStream* out)
|
||||
fn void! TrackingAllocator.fprint_report(&self, OutStream out)
|
||||
{
|
||||
|
||||
usz total = 0;
|
||||
|
||||
@@ -45,17 +45,17 @@ macro rindex_of(array, element)
|
||||
}
|
||||
|
||||
/**
|
||||
* Concatenate two arrays or subarrays, returning a subarray containing the concatenation of them.
|
||||
* Concatenate two arrays or slices, returning a slice containing the concatenation of them.
|
||||
*
|
||||
* @param [in] arr1
|
||||
* @param [in] arr2
|
||||
* @param [&inout] allocator "The allocator to use, default is the heap allocator"
|
||||
* @require @typekind(arr1) == SUBARRAY || @typekind(arr1) == ARRAY
|
||||
* @require @typekind(arr2) == SUBARRAY || @typekind(arr2) == ARRAY
|
||||
* @require @typekind(arr1) == SLICE || @typekind(arr1) == ARRAY
|
||||
* @require @typekind(arr2) == SLICE || @typekind(arr2) == ARRAY
|
||||
* @require @typeis(arr1[0], $typeof(arr2[0])) "Arrays must have the same type"
|
||||
* @ensure result.len == arr1.len + arr2.len
|
||||
**/
|
||||
macro concat_new(arr1, arr2, Allocator* allocator = allocator::heap())
|
||||
macro concat_new(arr1, arr2, Allocator allocator = allocator::heap())
|
||||
{
|
||||
var $Type = $typeof(arr1[0]);
|
||||
$Type[] result = allocator::alloc_array(allocator, $Type, arr1.len + arr2.len);
|
||||
@@ -71,13 +71,13 @@ macro concat_new(arr1, arr2, Allocator* allocator = allocator::heap())
|
||||
}
|
||||
|
||||
/**
|
||||
* Concatenate two arrays or subarrays, returning a subarray containing the concatenation of them,
|
||||
* Concatenate two arrays or slices, returning a slice containing the concatenation of them,
|
||||
* allocated using the temp allocator.
|
||||
*
|
||||
* @param [in] arr1
|
||||
* @param [in] arr2
|
||||
* @require @typekind(arr1) == SUBARRAY || @typekind(arr1) == ARRAY
|
||||
* @require @typekind(arr2) == SUBARRAY || @typekind(arr2) == ARRAY
|
||||
* @require @typekind(arr1) == SLICE || @typekind(arr1) == ARRAY
|
||||
* @require @typekind(arr2) == SLICE || @typekind(arr2) == ARRAY
|
||||
* @require @typeis(arr1[0], $typeof(arr2[0])) "Arrays must have the same type"
|
||||
* @ensure result.len == arr1.len + arr2.len
|
||||
**/
|
||||
|
||||
@@ -88,7 +88,7 @@ bitstruct UInt128LE : uint128 @littleendian
|
||||
}
|
||||
|
||||
/**
|
||||
* @require is_array_or_sub_of_char(bytes) "argument must be an array, a pointer to an array or a subarray of char"
|
||||
* @require is_array_or_slice_of_char(bytes) "argument must be an array, a pointer to an array or a slice of char"
|
||||
* @require is_bitorder($Type) "type must be a bitorder integer"
|
||||
**/
|
||||
macro read(bytes, $Type)
|
||||
@@ -104,7 +104,7 @@ macro read(bytes, $Type)
|
||||
}
|
||||
|
||||
/**
|
||||
* @require is_arrayptr_or_sub_of_char(bytes) "argument must be a pointer to an array or a subarray of char"
|
||||
* @require is_arrayptr_or_slice_of_char(bytes) "argument must be a pointer to an array or a slice of char"
|
||||
* @require is_bitorder($Type) "type must be a bitorder integer"
|
||||
**/
|
||||
macro write(x, bytes, $Type)
|
||||
@@ -144,7 +144,7 @@ macro is_bitorder($Type)
|
||||
$endswitch
|
||||
}
|
||||
|
||||
macro bool is_array_or_sub_of_char(bytes)
|
||||
macro bool is_array_or_slice_of_char(bytes)
|
||||
{
|
||||
$switch (@typekind(bytes))
|
||||
$case POINTER:
|
||||
@@ -154,7 +154,7 @@ macro bool is_array_or_sub_of_char(bytes)
|
||||
return $Inner2.typeid == char.typeid;
|
||||
$endif
|
||||
$case ARRAY:
|
||||
$case SUBARRAY:
|
||||
$case SLICE:
|
||||
var $Inner = $typefrom($typeof(bytes).inner);
|
||||
return $Inner.typeid == char.typeid;
|
||||
$default:
|
||||
@@ -162,7 +162,7 @@ macro bool is_array_or_sub_of_char(bytes)
|
||||
$endswitch
|
||||
}
|
||||
|
||||
macro bool is_arrayptr_or_sub_of_char(bytes)
|
||||
macro bool is_arrayptr_or_slice_of_char(bytes)
|
||||
{
|
||||
$switch (@typekind(bytes))
|
||||
$case POINTER:
|
||||
@@ -171,7 +171,7 @@ macro bool is_arrayptr_or_sub_of_char(bytes)
|
||||
var $Inner2 = $typefrom($Inner.inner);
|
||||
return $Inner2.typeid == char.typeid;
|
||||
$endif
|
||||
$case SUBARRAY:
|
||||
$case SLICE:
|
||||
var $Inner = $typefrom($typeof(bytes).inner);
|
||||
return $Inner.typeid == char.typeid;
|
||||
$default:
|
||||
|
||||
@@ -61,7 +61,7 @@ macro void @swap(&a, &b) @builtin
|
||||
* @ensure @typeis(return, $Type*)
|
||||
* @return! CastResult.TYPE_MISMATCH
|
||||
**/
|
||||
macro anycast(any* v, $Type) @builtin
|
||||
macro anycast(any v, $Type) @builtin
|
||||
{
|
||||
if (v.type != $Type.typeid) return CastResult.TYPE_MISMATCH?;
|
||||
return ($Type*)v.ptr;
|
||||
@@ -83,17 +83,18 @@ fn bool print_backtrace(String message, int backtraces_to_ignore) @if(env::NATIV
|
||||
foreach (i, &trace : backtrace)
|
||||
{
|
||||
if (i < backtraces_to_ignore) continue;
|
||||
String inline_suffix = trace.is_inline ? " [inline]" : "";
|
||||
if (trace.is_unknown())
|
||||
{
|
||||
io::eprintn(" in ???");
|
||||
io::eprintfn(" in ???%s", inline_suffix);
|
||||
continue;
|
||||
}
|
||||
if (trace.has_file())
|
||||
{
|
||||
io::eprintfn(" in %s (%s:%d) [%s]", trace.function, trace.file, trace.line, trace.object_file);
|
||||
io::eprintfn(" in %s (%s:%d) [%s]%s", trace.function, trace.file, trace.line, trace.object_file, inline_suffix);
|
||||
continue;
|
||||
}
|
||||
io::eprintfn(" in %s (source unavailable) [%s]", trace.function, trace.object_file);
|
||||
io::eprintfn(" in %s (source unavailable) [%s]%s", trace.function, trace.object_file, inline_suffix);
|
||||
}
|
||||
return true;
|
||||
};
|
||||
@@ -126,7 +127,7 @@ PanicFn panic = &default_panic;
|
||||
|
||||
fn void panicf(String fmt, String file, String function, uint line, args...)
|
||||
{
|
||||
@stack_mem(512; Allocator* allocator)
|
||||
@stack_mem(512; Allocator allocator)
|
||||
{
|
||||
DString s;
|
||||
s.new_init(.allocator = allocator);
|
||||
@@ -142,7 +143,9 @@ fn void panicf(String fmt, String file, String function, uint line, args...)
|
||||
**/
|
||||
macro void unreachable(String string = "Unreachable statement reached.", ...) @builtin @noreturn
|
||||
{
|
||||
$if env::COMPILER_SAFE_MODE:
|
||||
panicf(string, $$FILE, $$FUNC, $$LINE, $vasplat());
|
||||
$endif;
|
||||
$$unreachable();
|
||||
}
|
||||
|
||||
@@ -156,6 +159,14 @@ macro void unsupported(String string = "Unsupported function invoked") @builtin
|
||||
$$unreachable();
|
||||
}
|
||||
|
||||
/**
|
||||
* Unconditionally break into an attached debugger when reached.
|
||||
**/
|
||||
macro void breakpoint() @builtin
|
||||
{
|
||||
$$breakpoint();
|
||||
}
|
||||
|
||||
macro any_make(void* ptr, typeid type) @builtin
|
||||
{
|
||||
return $$any_make(ptr, type);
|
||||
@@ -314,6 +325,11 @@ macro char[] @as_char_view(&value) @builtin
|
||||
return ((char*)value)[:$sizeof(*value)];
|
||||
}
|
||||
|
||||
macro isz @str_find(String $string, String $needle) @builtin => $$str_find($string, $needle);
|
||||
macro String @str_upper(String $str) @builtin => $$str_upper($str);
|
||||
macro String @str_lower(String $str) @builtin => $$str_lower($str);
|
||||
macro uint @str_hash(String $str) @builtin => $$str_hash($str);
|
||||
|
||||
macro uint int.hash(int i) => i;
|
||||
macro uint uint.hash(uint i) => i;
|
||||
macro uint short.hash(short s) => s;
|
||||
@@ -330,6 +346,11 @@ macro uint String.hash(String c) => (uint)fnv32a::encode(c);
|
||||
macro uint char[].hash(char[] c) => (uint)fnv32a::encode(c);
|
||||
macro uint void*.hash(void* ptr) => ((ulong)(uptr)ptr).hash();
|
||||
|
||||
distinct EmptySlot = void*;
|
||||
const EmptySlot EMPTY_MACRO_SLOT @builtin = null;
|
||||
macro @is_empty_macro_slot(#arg) @builtin => @typeis(#arg, EmptySlot);
|
||||
macro @is_valid_macro_slot(#arg) @builtin => !@typeis(#arg, EmptySlot);
|
||||
|
||||
const MAX_FRAMEADDRESS = 128;
|
||||
/**
|
||||
* @require n >= 0
|
||||
@@ -614,7 +635,7 @@ macro void* get_returnaddress(int n)
|
||||
}
|
||||
|
||||
module std::core::builtin @if((env::LINUX || env::DARWIN) && env::COMPILER_SAFE_MODE && env::DEBUG_SYMBOLS);
|
||||
import libc;
|
||||
import libc, std::io;
|
||||
|
||||
fn void sig_panic(String message)
|
||||
{
|
||||
|
||||
@@ -108,14 +108,14 @@ fn usz char32_to_utf8_unsafe(Char32 c, char** output)
|
||||
{
|
||||
switch
|
||||
{
|
||||
case c < 0x7f:
|
||||
case c <= 0x7f:
|
||||
(*output)++[0] = (char)c;
|
||||
return 1;
|
||||
case c < 0x7ff:
|
||||
case c <= 0x7ff:
|
||||
(*output)++[0] = (char)(0xC0 | c >> 6);
|
||||
(*output)++[0] = (char)(0x80 | (c & 0x3F));
|
||||
return 2;
|
||||
case c < 0xffff:
|
||||
case c <= 0xffff:
|
||||
(*output)++[0] = (char)(0xE0 | c >> 12);
|
||||
(*output)++[0] = (char)(0x80 | (c >> 6 & 0x3F));
|
||||
(*output)++[0] = (char)(0x80 | (c & 0x3F));
|
||||
@@ -210,11 +210,11 @@ fn usz utf8len_for_utf32(Char32[] utf32)
|
||||
{
|
||||
switch (true)
|
||||
{
|
||||
case uc < 0x7f:
|
||||
case uc <= 0x7f:
|
||||
len++;
|
||||
case uc < 0x7ff:
|
||||
case uc <= 0x7ff:
|
||||
len += 2;
|
||||
case uc < 0xffff:
|
||||
case uc <= 0xffff:
|
||||
len += 3;
|
||||
default:
|
||||
len += 4;
|
||||
@@ -237,12 +237,12 @@ fn usz utf8len_for_utf16(Char16[] utf16)
|
||||
Char16 c = utf16[i];
|
||||
if (c & UTF16_SURROGATE_GENERIC_MASK != UTF16_SURROGATE_GENERIC_VALUE)
|
||||
{
|
||||
if (c < 0x7f)
|
||||
if (c <= 0x7f)
|
||||
{
|
||||
len++;
|
||||
continue;
|
||||
}
|
||||
if (c < 0x7ff)
|
||||
if (c <= 0x7ff)
|
||||
{
|
||||
len += 2;
|
||||
continue;
|
||||
|
||||
@@ -8,7 +8,7 @@ const usz MIN_CAPACITY @private = 16;
|
||||
/**
|
||||
* @require !self.data() "String already initialized"
|
||||
**/
|
||||
fn DString DString.new_init(&self, usz capacity = MIN_CAPACITY, Allocator* allocator = allocator::heap())
|
||||
fn DString DString.new_init(&self, usz capacity = MIN_CAPACITY, Allocator allocator = allocator::heap())
|
||||
{
|
||||
if (capacity < MIN_CAPACITY) capacity = MIN_CAPACITY;
|
||||
StringData* data = allocator::alloc_with_padding(allocator, StringData, capacity)!!;
|
||||
@@ -18,14 +18,6 @@ fn DString DString.new_init(&self, usz capacity = MIN_CAPACITY, Allocator* alloc
|
||||
return *self = (DString)data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @require !self.data() "String already initialized"
|
||||
**/
|
||||
fn DString DString.init_new(&self, usz capacity = MIN_CAPACITY, Allocator* allocator = allocator::heap()) @deprecated("Replaced by new_init")
|
||||
{
|
||||
return self.new_init(capacity, allocator) @inline;
|
||||
}
|
||||
|
||||
/**
|
||||
* @require !self.data() "String already initialized"
|
||||
**/
|
||||
@@ -35,22 +27,14 @@ fn DString DString.temp_init(&self, usz capacity = MIN_CAPACITY)
|
||||
return *self;
|
||||
}
|
||||
|
||||
/**
|
||||
* @require !self.data() "String already initialized"
|
||||
**/
|
||||
fn DString DString.init_temp(&self, usz capacity = MIN_CAPACITY) @deprecated("Replaced by temp_init")
|
||||
{
|
||||
return self.temp_init(capacity) @inline;
|
||||
}
|
||||
|
||||
fn DString new_with_capacity(usz capacity, Allocator* allocator = allocator::heap())
|
||||
fn DString new_with_capacity(usz capacity, Allocator allocator = allocator::heap())
|
||||
{
|
||||
return DString{}.new_init(capacity, allocator);
|
||||
}
|
||||
|
||||
fn DString temp_with_capacity(usz capacity) => new_with_capacity(capacity, allocator::temp()) @inline;
|
||||
|
||||
fn DString new(String c = "", Allocator* allocator = allocator::heap())
|
||||
fn DString new(String c = "", Allocator allocator = allocator::heap())
|
||||
{
|
||||
usz len = c.len;
|
||||
StringData* data = (StringData*)new_with_capacity(len, allocator);
|
||||
@@ -64,7 +48,7 @@ fn DString new(String c = "", Allocator* allocator = allocator::heap())
|
||||
|
||||
fn DString temp_new(String s = "") => new(s, allocator::temp()) @inline;
|
||||
|
||||
fn DString DString.new_concat(self, DString b, Allocator* allocator = allocator::heap())
|
||||
fn DString DString.new_concat(self, DString b, Allocator allocator = allocator::heap())
|
||||
{
|
||||
DString string;
|
||||
string.new_init(self.len() + b.len(), allocator);
|
||||
@@ -75,8 +59,6 @@ fn DString DString.new_concat(self, DString b, Allocator* allocator = allocator:
|
||||
|
||||
fn DString DString.temp_concat(self, DString b) => self.new_concat(b, allocator::temp());
|
||||
|
||||
fn DString DString.new_tconcat(self, DString b) @deprecated("Replaced by temp_concat") => self.new_concat(b, allocator::temp());
|
||||
|
||||
fn ZString DString.zstr_view(&self)
|
||||
{
|
||||
StringData* data = self.data();
|
||||
@@ -166,7 +148,7 @@ fn void DString.append_char32(&self, Char32 c)
|
||||
|
||||
fn DString DString.tcopy(&self) => self.copy(allocator::temp());
|
||||
|
||||
fn DString DString.copy(self, Allocator* allocator = null)
|
||||
fn DString DString.copy(self, Allocator allocator = null)
|
||||
{
|
||||
if (!self)
|
||||
{
|
||||
@@ -180,7 +162,7 @@ fn DString DString.copy(self, Allocator* allocator = null)
|
||||
return new_string;
|
||||
}
|
||||
|
||||
fn ZString DString.copy_zstr(self, Allocator* allocator = allocator::heap())
|
||||
fn ZString DString.copy_zstr(self, Allocator allocator = allocator::heap())
|
||||
{
|
||||
usz str_len = self.len();
|
||||
if (!str_len)
|
||||
@@ -194,7 +176,7 @@ fn ZString DString.copy_zstr(self, Allocator* allocator = allocator::heap())
|
||||
return (ZString)zstr;
|
||||
}
|
||||
|
||||
fn String DString.copy_str(self, Allocator* allocator = allocator::heap())
|
||||
fn String DString.copy_str(self, Allocator allocator = allocator::heap())
|
||||
{
|
||||
return (String)self.copy_zstr(allocator)[:self.len()];
|
||||
}
|
||||
@@ -258,7 +240,7 @@ fn void DString.append_chars(&self, String str)
|
||||
data.len += other_len;
|
||||
}
|
||||
|
||||
fn Char32[] DString.copy_utf32(&self, Allocator* allocator = allocator::heap())
|
||||
fn Char32[] DString.copy_utf32(&self, Allocator allocator = allocator::heap())
|
||||
{
|
||||
return self.str_view().to_new_utf32(allocator) @inline!!;
|
||||
}
|
||||
@@ -403,7 +385,7 @@ fn usz! DString.appendfn(&self, String format, args...) @maydiscard
|
||||
return len + 1;
|
||||
}
|
||||
|
||||
fn DString new_join(String[] s, String joiner, Allocator* allocator = allocator::heap())
|
||||
fn DString new_join(String[] s, String joiner, Allocator allocator = allocator::heap())
|
||||
{
|
||||
if (!s.len) return (DString)null;
|
||||
usz total_size = joiner.len * s.len;
|
||||
@@ -450,7 +432,7 @@ fn void DString.reserve(&self, usz addition)
|
||||
*self = (DString)allocator::realloc(data.allocator, data, StringData.sizeof + new_capacity);
|
||||
}
|
||||
|
||||
fn usz! DString.read_from_stream(&self, InStream* reader)
|
||||
fn usz! DString.read_from_stream(&self, InStream reader)
|
||||
{
|
||||
if (&reader.available)
|
||||
{
|
||||
@@ -482,7 +464,7 @@ fn usz! DString.read_from_stream(&self, InStream* reader)
|
||||
|
||||
struct StringData @private
|
||||
{
|
||||
Allocator* allocator;
|
||||
Allocator allocator;
|
||||
usz len;
|
||||
usz capacity;
|
||||
char[*] chars;
|
||||
|
||||
@@ -116,6 +116,8 @@ enum ArchType
|
||||
|
||||
const OsType OS_TYPE = (OsType)$$OS_TYPE;
|
||||
const ArchType ARCH_TYPE = (ArchType)$$ARCH_TYPE;
|
||||
const bool ARCH_32_BIT = $$REGISTER_SIZE == 32;
|
||||
const bool ARCH_64_BIT = $$REGISTER_SIZE == 64;
|
||||
const bool LIBC = $$COMPILER_LIBC_AVAILABLE;
|
||||
const bool NO_LIBC = !$$COMPILER_LIBC_AVAILABLE;
|
||||
const CompilerOptLevel COMPILER_OPT_LEVEL = (CompilerOptLevel)$$COMPILER_OPT_LEVEL;
|
||||
@@ -123,6 +125,7 @@ const bool BIG_ENDIAN = $$PLATFORM_BIG_ENDIAN;
|
||||
const bool I128_NATIVE_SUPPORT = $$PLATFORM_I128_SUPPORTED;
|
||||
const bool F16_SUPPORT = $$PLATFORM_F16_SUPPORTED;
|
||||
const bool F128_SUPPORT = $$PLATFORM_F128_SUPPORTED;
|
||||
const REGISTER_SIZE = $$REGISTER_SIZE;
|
||||
const bool COMPILER_SAFE_MODE = $$COMPILER_SAFE_MODE;
|
||||
const bool DEBUG_SYMBOLS = $$DEBUG_SYMBOLS;
|
||||
const usz LLVM_VERSION = $$LLVM_VERSION;
|
||||
|
||||
@@ -8,6 +8,10 @@ import std::math;
|
||||
const MAX_MEMORY_ALIGNMENT = 0x1000_0000;
|
||||
const DEFAULT_MEM_ALIGNMENT = (void*.alignof) * 2;
|
||||
|
||||
macro bool @constant_is_power_of_2($x) @private
|
||||
{
|
||||
return $x != 0 && ($x & ($x - 1)) == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a vector from memory according to a mask assuming default alignment.
|
||||
@@ -37,7 +41,7 @@ macro masked_load(ptr, bool[<*>] mask, passthru)
|
||||
* @require $assignable(&&passthru, $typeof(ptr)) : "Pointer and passthru must match"
|
||||
* @require @typekind(passthru) == VECTOR : "Expected passthru to be a vector"
|
||||
* @require passthru.len == mask.len : "Mask and passthru must have the same length"
|
||||
* @require math::is_power_of_2($alignment) : "The alignment must be a power of two"
|
||||
* @require @constant_is_power_of_2($alignment) : "The alignment must be a power of two"
|
||||
*
|
||||
* @return "A vector with the loaded values where the mask is true, passthru where the mask is false"
|
||||
**/
|
||||
@@ -80,7 +84,7 @@ macro gather(ptrvec, bool[<*>] mask, passthru)
|
||||
* @require $assignable(&&passthru[0], $typeof(ptrvec[0])) : "Pointer and passthru must match"
|
||||
* @require passthru.len == mask.len : "Mask and passthru must have the same length"
|
||||
* @require mask.len == ptrvec.len : "Mask and ptrvec must have the same length"
|
||||
* @require math::is_power_of_2($alignment) : "The alignment must be a power of two"
|
||||
* @require @constant_is_power_of_2($alignment) : "The alignment must be a power of two"
|
||||
*
|
||||
* @return "A vector with the loaded values where the mask is true, passthru where the mask is false"
|
||||
**/
|
||||
@@ -115,7 +119,7 @@ macro masked_store(ptr, value, bool[<*>] mask)
|
||||
* @require $assignable(&&value, $typeof(ptr)) : "Pointer and value must match"
|
||||
* @require @typekind(value) == VECTOR : "Expected value to be a vector"
|
||||
* @require value.len == mask.len : "Mask and value must have the same length"
|
||||
* @require math::is_power_of_2($alignment) : "The alignment must be a power of two"
|
||||
* @require @constant_is_power_of_2($alignment) : "The alignment must be a power of two"
|
||||
*
|
||||
**/
|
||||
macro @masked_store_aligned(ptr, value, bool[<*>] mask, usz $alignment)
|
||||
@@ -150,13 +154,39 @@ macro scatter(ptrvec, value, bool[<*>] mask)
|
||||
* @require $assignable(&&value[0], $typeof(ptrvec[0])) : "Pointer and value must match"
|
||||
* @require value.len == mask.len : "Mask and value must have the same length"
|
||||
* @require mask.len == ptrvec.len : "Mask and ptrvec must have the same length"
|
||||
* @require math::is_power_of_2($alignment) : "The alignment must be a power of two"
|
||||
* @require @constant_is_power_of_2($alignment) : "The alignment must be a power of two"
|
||||
**/
|
||||
macro @scatter_aligned(ptrvec, value, bool[<*>] mask, usz $alignment)
|
||||
{
|
||||
return $$scatter(ptrvec, value, mask, $alignment);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param [in] x "The variable or dereferenced pointer to load."
|
||||
* @param $alignment "The alignment to assume for the load"
|
||||
* @return "The value of x"
|
||||
*
|
||||
* @require @constant_is_power_of_2($alignment) : "The alignment must be a power of two"
|
||||
**/
|
||||
macro @unaligned_load(&x, usz $alignment) @builtin
|
||||
{
|
||||
return $$unaligned_load(x, $alignment);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param [out] x "The variable or dereferenced pointer to store to."
|
||||
* @param value "The value to store."
|
||||
* @param $alignment "The alignment to assume for the store"
|
||||
* @return "The value of x"
|
||||
*
|
||||
* @require $assignable(value, $typeof(*x)) : "The value doesn't match the variable"
|
||||
* @require @constant_is_power_of_2($alignment) : "The alignment must be a power of two"
|
||||
**/
|
||||
macro @unaligned_store(&x, value, usz $alignment) @builtin
|
||||
{
|
||||
return $$unaligned_store(x, ($typeof(*x))value, $alignment);
|
||||
}
|
||||
|
||||
macro @volatile_load(&x) @builtin
|
||||
{
|
||||
return $$volatile_load(x);
|
||||
@@ -253,11 +283,12 @@ fn bool ptr_is_aligned(void* ptr, usz alignment) @inline
|
||||
|
||||
macro void clear(void* dst, usz len, usz $dst_align = 0, bool $is_volatile = false, bool $inlined = false)
|
||||
{
|
||||
$if $inlined:
|
||||
$$memset_inline(dst, (char)0, len, $is_volatile, $dst_align);
|
||||
$else
|
||||
$$memset(dst, (char)0, len, $is_volatile, $dst_align);
|
||||
$endif
|
||||
$$memset(dst, (char)0, len, $is_volatile, $dst_align);
|
||||
}
|
||||
|
||||
macro void clear_inline(void* dst, usz $len, usz $dst_align = 0, bool $is_volatile = false)
|
||||
{
|
||||
$$memset_inline(dst, (char)0, $len, $is_volatile, $dst_align);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -269,17 +300,30 @@ macro void clear(void* dst, usz len, usz $dst_align = 0, bool $is_volatile = fal
|
||||
* @param $dst_align "the alignment of the destination if different from the default, 0 assumes the default"
|
||||
* @param $src_align "the alignment of the destination if different from the default, 0 assumes the default"
|
||||
* @param $is_volatile "True if this copy should be treated as volatile, i.e. it can't be optimized away."
|
||||
* @param $inlined "True if this copy should never call the OS memcpy."
|
||||
*
|
||||
* @require len == 0 || dst + len <= src || src + len <= dst : "Ranges may not overlap"
|
||||
**/
|
||||
macro void copy(void* dst, void* src, usz len, usz $dst_align = 0, usz $src_align = 0, bool $is_volatile = false, bool $inlined = false)
|
||||
{
|
||||
$if $inlined:
|
||||
$$memcpy_inline(dst, src, len, $is_volatile, $dst_align, $src_align);
|
||||
$else
|
||||
$$memcpy(dst, src, len, $is_volatile, $dst_align, $src_align);
|
||||
$endif
|
||||
$$memcpy(dst, src, len, $is_volatile, $dst_align, $src_align);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy memory from src to dst efficiently, assuming the memory ranges do not overlap, it
|
||||
* will always be inlined and never call memcopy
|
||||
*
|
||||
* @param [&out] dst "The destination to copy to"
|
||||
* @param [&in] src "The source to copy from"
|
||||
* @param $len "The number of bytes to copy"
|
||||
* @param $dst_align "the alignment of the destination if different from the default, 0 assumes the default"
|
||||
* @param $src_align "the alignment of the destination if different from the default, 0 assumes the default"
|
||||
* @param $is_volatile "True if this copy should be treated as volatile, i.e. it can't be optimized away."
|
||||
*
|
||||
* @require $len == 0 || dst + $len <= src || src + $len <= dst : "Ranges may not overlap"
|
||||
**/
|
||||
macro void copy_inline(void* dst, void* src, usz $len, usz $dst_align = 0, usz $src_align = 0, bool $is_volatile = false)
|
||||
{
|
||||
$$memcpy_inline(dst, src, $len, $is_volatile, $dst_align, $src_align);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -305,23 +349,33 @@ macro void move(void* dst, void* src, usz len, usz $dst_align = 0, usz $src_alig
|
||||
* @param len "The number of bytes to copy"
|
||||
* @param $dst_align "the alignment of the destination if different from the default, 0 assumes the default"
|
||||
* @param $is_volatile "True if this copy should be treated as volatile, i.e. it can't be optimized away."
|
||||
* @param $inlined "True if this copy should never call the OS memset."
|
||||
*
|
||||
* @ensure !len || (dst[0] == val && dst[len - 1] == val)
|
||||
**/
|
||||
macro void set(void* dst, char val, usz len, usz $dst_align = 0, bool $is_volatile = false, bool $inlined = false)
|
||||
macro void set(void* dst, char val, usz len, usz $dst_align = 0, bool $is_volatile = false)
|
||||
{
|
||||
$if $inlined:
|
||||
$$memset_inline(dst, val, len, $is_volatile, $dst_align);
|
||||
$else
|
||||
$$memset(dst, val, len, $is_volatile, $dst_align);
|
||||
$endif
|
||||
$$memset(dst, val, len, $is_volatile, $dst_align);
|
||||
}
|
||||
|
||||
/**
|
||||
* @require values::@inner_kind(a) == TypeKind.SUBARRAY || values::@inner_kind(a) == TypeKind.POINTER
|
||||
* @require values::@inner_kind(b) == TypeKind.SUBARRAY || values::@inner_kind(b) == TypeKind.POINTER
|
||||
* @require values::@inner_kind(a) != TypeKind.SUBARRAY || len == -1
|
||||
* Sets all memory in a region to that of the provided byte. Never calls OS memset.
|
||||
*
|
||||
* @param [&out] dst "The destination to copy to"
|
||||
* @param val "The value to copy into memory"
|
||||
* @param $len "The number of bytes to copy"
|
||||
* @param $dst_align "the alignment of the destination if different from the default, 0 assumes the default"
|
||||
* @param $is_volatile "True if this copy should be treated as volatile, i.e. it can't be optimized away."
|
||||
*
|
||||
* @ensure !$len || (dst[0] == val && dst[$len - 1] == val)
|
||||
**/
|
||||
macro void set_inline(void* dst, char val, usz $len, usz $dst_align = 0, bool $is_volatile = false)
|
||||
{
|
||||
$$memset_inline(dst, val, $len, $is_volatile, $dst_align);
|
||||
}
|
||||
/**
|
||||
* @require values::@inner_kind(a) == TypeKind.SLICE || values::@inner_kind(a) == TypeKind.POINTER
|
||||
* @require values::@inner_kind(b) == TypeKind.SLICE || values::@inner_kind(b) == TypeKind.POINTER
|
||||
* @require values::@inner_kind(a) != TypeKind.SLICE || len == -1
|
||||
* @require values::@inner_kind(a) != TypeKind.POINTER || len > -1
|
||||
* @require values::@assign_to(a, b) && values::@assign_to(b, a)
|
||||
**/
|
||||
@@ -332,7 +386,7 @@ macro bool equals(a, b, isz len = -1, usz $align = 0)
|
||||
$endif
|
||||
void* x @noinit;
|
||||
void* y @noinit;
|
||||
$if values::@inner_kind(a) == TypeKind.SUBARRAY:
|
||||
$if values::@inner_kind(a) == TypeKind.SLICE:
|
||||
len = a.len;
|
||||
if (len != b.len) return false;
|
||||
x = a.ptr;
|
||||
@@ -379,9 +433,9 @@ macro type_alloc_must_be_aligned($Type)
|
||||
/**
|
||||
* Run with a specific allocator inside of the macro body.
|
||||
**/
|
||||
macro void @scoped(Allocator* allocator; @body())
|
||||
macro void @scoped(Allocator allocator; @body())
|
||||
{
|
||||
Allocator* old_allocator = allocator::thread_allocator;
|
||||
Allocator old_allocator = allocator::thread_allocator;
|
||||
allocator::thread_allocator = allocator;
|
||||
defer allocator::thread_allocator = old_allocator;
|
||||
@body();
|
||||
@@ -390,8 +444,8 @@ macro void @scoped(Allocator* allocator; @body())
|
||||
macro void @report_heap_allocs_in_scope(;@body())
|
||||
{
|
||||
TrackingAllocator tracker;
|
||||
tracker.init(thread_allocator);
|
||||
Allocator* old_allocator = allocator::thread_allocator;
|
||||
tracker.init(allocator::thread_allocator);
|
||||
Allocator old_allocator = allocator::thread_allocator;
|
||||
allocator::thread_allocator = &tracker;
|
||||
defer
|
||||
{
|
||||
@@ -402,7 +456,7 @@ macro void @report_heap_allocs_in_scope(;@body())
|
||||
@body();
|
||||
}
|
||||
|
||||
macro void @stack_mem(usz $size; @body(Allocator* mem)) @builtin
|
||||
macro void @stack_mem(usz $size; @body(Allocator mem)) @builtin
|
||||
{
|
||||
char[$size] buffer;
|
||||
OnStackAllocator allocator;
|
||||
@@ -480,11 +534,6 @@ macro void @pool(TempAllocator* #other_temp = null; @body) @builtin
|
||||
import libc;
|
||||
|
||||
|
||||
|
||||
macro TempAllocator* temp() @deprecated("Use allocator::temp()") => allocator::temp();
|
||||
macro Allocator* current_allocator() @deprecated("Use allocator::heap()") => allocator::heap();
|
||||
macro Allocator* heap() @deprecated("Use allocator::heap()") => allocator::heap();
|
||||
|
||||
module std::core::mem @if(WASM_NOLIBC);
|
||||
|
||||
SimpleHeapAllocator wasm_allocator @private;
|
||||
@@ -498,7 +547,7 @@ fn void initialize_wasm_mem() @init(1) @private
|
||||
if (start > mem::DEFAULT_MEM_ALIGNMENT) allocator::wasm_memory.use = start;
|
||||
wasm_allocator.init(fn (x) => allocator::wasm_memory.allocate_block(x));
|
||||
temp_base_allocator = &wasm_allocator;
|
||||
thread_allocator = &wasm_allocator;
|
||||
allocator::thread_allocator = &wasm_allocator;
|
||||
}
|
||||
|
||||
module std::core::mem;
|
||||
@@ -528,14 +577,25 @@ fn void* malloc(usz size) @builtin @inline @nodiscard
|
||||
return allocator::malloc(allocator::heap(), size);
|
||||
}
|
||||
|
||||
fn void* tmalloc(usz size, usz alignment = 0, usz offset = 0) @builtin @inline @nodiscard
|
||||
/**
|
||||
* Allocate using an aligned allocation. This is necessary for types with a default memory alignment
|
||||
* exceeding DEFAULT_MEM_ALIGNMENT. IMPORTANT! It must be freed using free_aligned.
|
||||
**/
|
||||
fn void* malloc_aligned(usz size, usz alignment) @builtin @inline @nodiscard
|
||||
{
|
||||
return allocator::temp().acquire(size, false, alignment, offset)!!;
|
||||
return allocator::malloc_aligned(allocator::heap(), size, alignment)!!;
|
||||
}
|
||||
|
||||
fn void* tmalloc(usz size, usz alignment = 0) @builtin @inline @nodiscard
|
||||
{
|
||||
if (!size) return null;
|
||||
return allocator::temp().acquire(size, NO_ZERO, alignment)!!;
|
||||
}
|
||||
|
||||
/**
|
||||
* @require $vacount < 2 : "Too many arguments."
|
||||
* @require $or($vacount == 0, $assignable($vaexpr(0), $Type)) : "The second argument must be an initializer for the type"
|
||||
* @require $Type.alignof <= DEFAULT_MEM_ALIGNMENT : "Types with alignment exceeding the default must use 'alloc_aligned' instead"
|
||||
**/
|
||||
macro new($Type, ...) @nodiscard
|
||||
{
|
||||
@@ -548,19 +608,38 @@ macro new($Type, ...) @nodiscard
|
||||
$endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Allocate using an aligned allocation. This is necessary for types with a default memory alignment
|
||||
* exceeding DEFAULT_MEM_ALIGNMENT. IMPORTANT! It must be freed using free_aligned.
|
||||
* @require $vacount < 2 : "Too many arguments."
|
||||
* @require $or($vacount == 0, $assignable($vaexpr(0), $Type)) : "The second argument must be an initializer for the type"
|
||||
**/
|
||||
macro new_aligned($Type, ...) @nodiscard
|
||||
{
|
||||
$if $vacount == 0:
|
||||
return ($Type*)calloc_aligned($Type.sizeof, $Type.alignof);
|
||||
$else
|
||||
$Type* val = malloc_aligned($Type.sizeof, $Type.alignof);
|
||||
*val = $vaexpr(0);
|
||||
return val;
|
||||
$endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @require $Type.alignof <= DEFAULT_MEM_ALIGNMENT : "Types with alignment exceeding the default must use 'alloc_aligned' instead"
|
||||
**/
|
||||
macro alloc($Type) @nodiscard
|
||||
{
|
||||
return ($Type*)malloc($Type.sizeof);
|
||||
}
|
||||
|
||||
macro new_clear($Type) @deprecated("Use mem::new")
|
||||
/**
|
||||
* Allocate using an aligned allocation. This is necessary for types with a default memory alignment
|
||||
* exceeding DEFAULT_MEM_ALIGNMENT. IMPORTANT! It must be freed using free_aligned.
|
||||
**/
|
||||
macro alloc_aligned($Type) @nodiscard
|
||||
{
|
||||
return new($Type);
|
||||
}
|
||||
|
||||
macro new_temp($Type) @deprecated("Use mem::temp_alloc or mem::temp_new")
|
||||
{
|
||||
return tmalloc($Type.sizeof);
|
||||
return ($Type*)malloc_aligned($Type.sizeof, $Type.alignof);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -583,24 +662,39 @@ macro temp_alloc($Type) @nodiscard
|
||||
return tmalloc($Type.sizeof);
|
||||
}
|
||||
|
||||
macro new_temp_clear($Type) @deprecated("use mem::temp_new")
|
||||
{
|
||||
return tcalloc($Type.sizeof);
|
||||
}
|
||||
|
||||
/**
|
||||
* @require $Type.alignof <= DEFAULT_MEM_ALIGNMENT : "Types with alignment exceeding the default must use 'new_array_aligned' instead"
|
||||
**/
|
||||
macro new_array($Type, usz elements) @nodiscard
|
||||
{
|
||||
return allocator::new_array(allocator::heap(), $Type, elements);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allocate using an aligned allocation. This is necessary for types with a default memory alignment
|
||||
* exceeding DEFAULT_MEM_ALIGNMENT. IMPORTANT! It must be freed using free_aligned.
|
||||
**/
|
||||
macro new_array_aligned($Type, usz elements) @nodiscard
|
||||
{
|
||||
return allocator::new_array_aligned(allocator::heap(), $Type, elements);
|
||||
}
|
||||
|
||||
/**
|
||||
* @require $Type.alignof <= DEFAULT_MEM_ALIGNMENT : "Types with alignment exceeding the default must use 'alloc_array_aligned' instead"
|
||||
**/
|
||||
macro alloc_array($Type, usz elements) @nodiscard
|
||||
{
|
||||
return allocator::alloc_array(allocator::heap(), $Type, elements);
|
||||
}
|
||||
|
||||
macro talloc_array($Type, usz elements) @nodiscard @deprecated("use mem::temp_alloc_array")
|
||||
/**
|
||||
* Allocate using an aligned allocation. This is necessary for types with a default memory alignment
|
||||
* exceeding DEFAULT_MEM_ALIGNMENT. IMPORTANT! It must be freed using free_aligned.
|
||||
**/
|
||||
macro alloc_array_aligned($Type, usz elements) @nodiscard
|
||||
{
|
||||
return temp_alloc_array($Type, elements);
|
||||
return allocator::alloc_array_aligned(allocator::heap(), $Type, elements);
|
||||
}
|
||||
|
||||
macro temp_alloc_array($Type, usz elements) @nodiscard
|
||||
@@ -608,34 +702,29 @@ macro temp_alloc_array($Type, usz elements) @nodiscard
|
||||
return (($Type*)tmalloc($Type.sizeof * elements, $Type.alignof))[:elements];
|
||||
}
|
||||
|
||||
macro temp_array($Type, usz elements) @nodiscard @deprecated("use mem::temp_alloc_array")
|
||||
{
|
||||
return temp_alloc_array($Type, elements);
|
||||
}
|
||||
|
||||
macro temp_new_array($Type, usz elements) @nodiscard
|
||||
{
|
||||
return (($Type*)tcalloc($Type.sizeof * elements, $Type.alignof))[:elements];
|
||||
}
|
||||
|
||||
macro new_zero_array($Type, usz elements) @deprecated("Use new_array")
|
||||
{
|
||||
return new_array($Type, elements);
|
||||
}
|
||||
|
||||
macro temp_zero_array($Type, usz elements) @deprecated("Use temp_new_array")
|
||||
{
|
||||
return temp_new_array($Type, elements);
|
||||
}
|
||||
|
||||
fn void* calloc(usz size) @builtin @inline @nodiscard
|
||||
{
|
||||
return allocator::calloc(allocator::heap(), size);
|
||||
}
|
||||
|
||||
fn void* tcalloc(usz size, usz alignment = 0, usz offset = 0) @builtin @inline @nodiscard
|
||||
/**
|
||||
* Allocate using an aligned allocation. This is necessary for types with a default memory alignment
|
||||
* exceeding DEFAULT_MEM_ALIGNMENT. IMPORTANT! It must be freed using free_aligned.
|
||||
**/
|
||||
fn void* calloc_aligned(usz size, usz alignment) @builtin @inline @nodiscard
|
||||
{
|
||||
return allocator::temp().acquire(size, false, alignment, offset)!!;
|
||||
return allocator::calloc_aligned(allocator::heap(), size, alignment)!!;
|
||||
}
|
||||
|
||||
fn void* tcalloc(usz size, usz alignment = 0) @builtin @inline @nodiscard
|
||||
{
|
||||
if (!size) return null;
|
||||
return allocator::temp().acquire(size, ZERO, alignment)!!;
|
||||
}
|
||||
|
||||
fn void* realloc(void *ptr, usz new_size) @builtin @inline @nodiscard
|
||||
@@ -643,13 +732,25 @@ fn void* realloc(void *ptr, usz new_size) @builtin @inline @nodiscard
|
||||
return allocator::realloc(allocator::heap(), ptr, new_size);
|
||||
}
|
||||
|
||||
fn void* realloc_aligned(void *ptr, usz new_size, usz alignment) @builtin @inline @nodiscard
|
||||
{
|
||||
return allocator::realloc_aligned(allocator::heap(), ptr, new_size, alignment)!!;
|
||||
}
|
||||
|
||||
fn void free(void* ptr) @builtin @inline
|
||||
{
|
||||
return allocator::free(allocator::heap(), ptr);
|
||||
}
|
||||
|
||||
fn void* trealloc(void* ptr, usz size, usz alignment = mem::DEFAULT_MEM_ALIGNMENT) @builtin @inline @nodiscard
|
||||
fn void free_aligned(void* ptr) @builtin @inline
|
||||
{
|
||||
return allocator::temp().resize(ptr, size, alignment, 0)!!;
|
||||
return allocator::free_aligned(allocator::heap(), ptr);
|
||||
}
|
||||
|
||||
fn void* trealloc(void* ptr, usz size, usz alignment = mem::DEFAULT_MEM_ALIGNMENT) @builtin @inline @nodiscard
|
||||
{
|
||||
if (!size) return null;
|
||||
if (!ptr) return tmalloc(size, alignment);
|
||||
return allocator::temp().resize(ptr, size, alignment)!!;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,13 +10,32 @@ struct TrackingEnv
|
||||
uint line;
|
||||
}
|
||||
|
||||
enum AllocInitType
|
||||
{
|
||||
NO_ZERO,
|
||||
ZERO
|
||||
}
|
||||
|
||||
interface Allocator
|
||||
{
|
||||
fn void reset(usz mark) @optional;
|
||||
fn usz mark() @optional;
|
||||
fn void*! acquire(usz size, bool clear, usz alignment, usz offset);
|
||||
fn void*! resize(void* ptr, usz new_size, usz alignment, usz offset);
|
||||
/**
|
||||
* @require !alignment || math::is_power_of_2(alignment)
|
||||
* @require alignment <= mem::MAX_MEMORY_ALIGNMENT `alignment too big`
|
||||
* @require size > 0
|
||||
**/
|
||||
fn void*! acquire(usz size, AllocInitType init_type, usz alignment = 0);
|
||||
/**
|
||||
* @require !alignment || math::is_power_of_2(alignment)
|
||||
* @require alignment <= mem::MAX_MEMORY_ALIGNMENT `alignment too big`
|
||||
* @require ptr != null
|
||||
* @require new_size > 0
|
||||
**/
|
||||
fn void*! resize(void* ptr, usz new_size, usz alignment = 0);
|
||||
/**
|
||||
* @require ptr != null
|
||||
**/
|
||||
fn void release(void* ptr, bool aligned);
|
||||
}
|
||||
|
||||
@@ -30,87 +49,109 @@ fault AllocationFailure
|
||||
|
||||
fn usz alignment_for_allocation(usz alignment) @inline @private
|
||||
{
|
||||
return alignment < mem::DEFAULT_MEM_ALIGNMENT ? alignment = mem::DEFAULT_MEM_ALIGNMENT : alignment;
|
||||
return alignment < mem::DEFAULT_MEM_ALIGNMENT ? mem::DEFAULT_MEM_ALIGNMENT : alignment;
|
||||
}
|
||||
|
||||
macro void* malloc(Allocator* allocator, usz size) @nodiscard
|
||||
macro void* malloc(Allocator allocator, usz size) @nodiscard
|
||||
{
|
||||
return malloc_try(allocator, size)!!;
|
||||
}
|
||||
|
||||
macro void*! malloc_try(Allocator* allocator, usz size) @nodiscard
|
||||
macro void*! malloc_try(Allocator allocator, usz size) @nodiscard
|
||||
{
|
||||
if (!size) return null;
|
||||
$if env::TESTING:
|
||||
char* data = allocator.acquire(size, false, 0, 0)!;
|
||||
char* data = allocator.acquire(size, NO_ZERO)!;
|
||||
mem::set(data, 0xAA, size, mem::DEFAULT_MEM_ALIGNMENT);
|
||||
return data;
|
||||
$else
|
||||
return allocator.acquire(size, false, 0, 0);
|
||||
return allocator.acquire(size, NO_ZERO);
|
||||
$endif
|
||||
}
|
||||
|
||||
macro void* calloc(Allocator* allocator, usz size) @nodiscard
|
||||
macro void* calloc(Allocator allocator, usz size) @nodiscard
|
||||
{
|
||||
return calloc_try(allocator, size)!!;
|
||||
}
|
||||
|
||||
macro void*! calloc_try(Allocator* allocator, usz size) @nodiscard
|
||||
macro void*! calloc_try(Allocator allocator, usz size) @nodiscard
|
||||
{
|
||||
return allocator.acquire(size, true, 0, 0);
|
||||
if (!size) return null;
|
||||
return allocator.acquire(size, ZERO);
|
||||
}
|
||||
|
||||
macro void* realloc(Allocator* allocator, void* ptr, usz new_size) @nodiscard
|
||||
macro void* realloc(Allocator allocator, void* ptr, usz new_size) @nodiscard
|
||||
{
|
||||
return realloc_try(allocator, ptr, new_size)!!;
|
||||
}
|
||||
|
||||
macro void*! realloc_try(Allocator* allocator, void* ptr, usz new_size) @nodiscard
|
||||
macro void*! realloc_try(Allocator allocator, void* ptr, usz new_size) @nodiscard
|
||||
{
|
||||
return allocator.resize(ptr, new_size, 0, 0);
|
||||
if (!new_size)
|
||||
{
|
||||
free(allocator, ptr);
|
||||
return null;
|
||||
}
|
||||
if (!ptr) return allocator.acquire(new_size, NO_ZERO);
|
||||
return allocator.resize(ptr, new_size);
|
||||
}
|
||||
|
||||
macro void free(Allocator* allocator, void* ptr)
|
||||
macro void free(Allocator allocator, void* ptr)
|
||||
{
|
||||
if (!ptr) return;
|
||||
$if env::TESTING:
|
||||
if (ptr) ((char*)ptr)[0] = 0xBA;
|
||||
((char*)ptr)[0] = 0xBA;
|
||||
$endif
|
||||
allocator.release(ptr, false);
|
||||
}
|
||||
|
||||
macro void*! malloc_aligned(Allocator* allocator, usz size, usz alignment, usz offset = 0) @nodiscard
|
||||
macro void*! malloc_aligned(Allocator allocator, usz size, usz alignment) @nodiscard
|
||||
{
|
||||
if (!size) return null;
|
||||
$if env::TESTING:
|
||||
char* data = allocator.acquire(size, false, alignment, offset)!;
|
||||
char* data = allocator.acquire(size, NO_ZERO, alignment)!;
|
||||
mem::set(data, 0xAA, size, mem::DEFAULT_MEM_ALIGNMENT);
|
||||
return data;
|
||||
$else
|
||||
return allocator.acquire(size, false, alignment, offset);
|
||||
return allocator.acquire(size, NO_ZERO, alignment);
|
||||
$endif
|
||||
}
|
||||
|
||||
macro void*! calloc_aligned(Allocator* allocator, usz size, usz alignment, usz offset = 0) @nodiscard
|
||||
macro void*! calloc_aligned(Allocator allocator, usz size, usz alignment) @nodiscard
|
||||
{
|
||||
return allocator.acquire(size, true, alignment, offset);
|
||||
if (!size) return null;
|
||||
return allocator.acquire(size, ZERO, alignment);
|
||||
}
|
||||
|
||||
macro void*! realloc_aligned(Allocator* allocator, void* ptr, usz new_size, usz alignment, usz offset = 0) @nodiscard
|
||||
macro void*! realloc_aligned(Allocator allocator, void* ptr, usz new_size, usz alignment) @nodiscard
|
||||
{
|
||||
return allocator.resize(ptr, new_size, alignment, offset);
|
||||
if (!new_size)
|
||||
{
|
||||
free_aligned(allocator, ptr);
|
||||
return null;
|
||||
}
|
||||
if (!ptr)
|
||||
{
|
||||
return malloc_aligned(allocator, new_size, alignment);
|
||||
}
|
||||
return allocator.resize(ptr, new_size, alignment);
|
||||
}
|
||||
|
||||
macro void free_aligned(Allocator* allocator, void* ptr)
|
||||
macro void free_aligned(Allocator allocator, void* ptr)
|
||||
{
|
||||
if (!ptr) return;
|
||||
$if env::TESTING:
|
||||
if (ptr) ((char*)ptr)[0] = 0xBA;
|
||||
((char*)ptr)[0] = 0xBA;
|
||||
$endif
|
||||
allocator.release(ptr, true);
|
||||
allocator.release(ptr, .aligned = true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @require $Type.alignof <= mem::DEFAULT_MEM_ALIGNMENT : "Types with alignment exceeding the default must use 'new_aligned' instead"
|
||||
* @require $vacount < 2 : "Too many arguments."
|
||||
* @require $or($vacount == 0, $assignable($vaexpr(0), $Type)) : "The second argument must be an initializer for the type"
|
||||
**/
|
||||
macro new(Allocator* allocator, $Type, ...) @nodiscard
|
||||
macro new(Allocator allocator, $Type, ...) @nodiscard
|
||||
{
|
||||
$if $vacount == 0:
|
||||
return ($Type*)calloc(allocator, $Type.sizeof);
|
||||
@@ -122,10 +163,11 @@ macro new(Allocator* allocator, $Type, ...) @nodiscard
|
||||
}
|
||||
|
||||
/**
|
||||
* @require $Type.alignof <= mem::DEFAULT_MEM_ALIGNMENT : "Types with alignment exceeding the default must use 'new_aligned' instead"
|
||||
* @require $vacount < 2 : "Too many arguments."
|
||||
* @require $or($vacount == 0, $assignable($vaexpr(0), $Type)) : "The second argument must be an initializer for the type"
|
||||
**/
|
||||
macro new_try(Allocator* allocator, $Type, ...) @nodiscard
|
||||
macro new_try(Allocator allocator, $Type, ...) @nodiscard
|
||||
{
|
||||
$if $vacount == 0:
|
||||
return ($Type*)calloc_try(allocator, $Type.sizeof);
|
||||
@@ -136,52 +178,120 @@ macro new_try(Allocator* allocator, $Type, ...) @nodiscard
|
||||
$endif
|
||||
}
|
||||
|
||||
macro new_with_padding(Allocator* allocator, $Type, usz padding) @nodiscard
|
||||
/**
|
||||
* Allocate using an aligned allocation. This is necessary for types with a default memory alignment
|
||||
* exceeding DEFAULT_MEM_ALIGNMENT. IMPORTANT! It must be freed using free_aligned.
|
||||
* @require $vacount < 2 : "Too many arguments."
|
||||
* @require $or($vacount == 0, $assignable($vaexpr(0), $Type)) : "The second argument must be an initializer for the type"
|
||||
**/
|
||||
macro new_aligned($Type, ...) @nodiscard
|
||||
{
|
||||
$if $vacount == 0:
|
||||
return ($Type*)calloc_aligned(allocator, $Type.sizeof, $Type.alignof);
|
||||
$else
|
||||
$Type* val = malloc_aligned(allocator, $Type.sizeof, $Type.alignof);
|
||||
*val = $vaexpr(0);
|
||||
return val;
|
||||
$endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @require $Type.alignof <= mem::DEFAULT_MEM_ALIGNMENT
|
||||
**/
|
||||
macro new_with_padding(Allocator allocator, $Type, usz padding) @nodiscard
|
||||
{
|
||||
return ($Type*)calloc_try(allocator, $Type.sizeof + padding);
|
||||
}
|
||||
|
||||
macro alloc(Allocator* allocator, $Type) @nodiscard
|
||||
/**
|
||||
* @require $Type.alignof <= mem::DEFAULT_MEM_ALIGNMENT : "Types with alignment exceeding the default must use 'alloc_aligned' instead"
|
||||
**/
|
||||
macro alloc(Allocator allocator, $Type) @nodiscard
|
||||
{
|
||||
return ($Type*)malloc(allocator, $Type.sizeof);
|
||||
}
|
||||
|
||||
macro alloc_try(Allocator* allocator, $Type) @nodiscard
|
||||
/**
|
||||
* @require $Type.alignof <= mem::DEFAULT_MEM_ALIGNMENT : "Types with alignment exceeding the default must use 'alloc_aligned' instead"
|
||||
**/
|
||||
macro alloc_try(Allocator allocator, $Type) @nodiscard
|
||||
{
|
||||
return ($Type*)malloc_try(allocator, $Type.sizeof);
|
||||
}
|
||||
|
||||
macro alloc_with_padding(Allocator* allocator, $Type, usz padding) @nodiscard
|
||||
/**
|
||||
* Allocate using an aligned allocation. This is necessary for types with a default memory alignment
|
||||
* exceeding DEFAULT_MEM_ALIGNMENT. IMPORTANT! It must be freed using free_aligned.
|
||||
**/
|
||||
macro alloc_aligned(Allocator allocator, $Type) @nodiscard
|
||||
{
|
||||
return ($Type*)malloc_aligned(allocator, $Type.sizeof, $Type.alignof);
|
||||
}
|
||||
|
||||
/**
|
||||
* @require $Type.alignof <= mem::DEFAULT_MEM_ALIGNMENT
|
||||
**/
|
||||
macro alloc_with_padding(Allocator allocator, $Type, usz padding) @nodiscard
|
||||
{
|
||||
return ($Type*)malloc_try(allocator, $Type.sizeof + padding);
|
||||
}
|
||||
|
||||
macro new_array(Allocator* allocator, $Type, usz elements) @nodiscard
|
||||
/**
|
||||
* @require $Type.alignof <= mem::DEFAULT_MEM_ALIGNMENT : "Types with alignment exceeding the default must use 'new_array_aligned' instead"
|
||||
**/
|
||||
macro new_array(Allocator allocator, $Type, usz elements) @nodiscard
|
||||
{
|
||||
return new_array_try(allocator, $Type, elements)!!;
|
||||
}
|
||||
|
||||
macro new_array_try(Allocator* allocator, $Type, usz elements) @nodiscard
|
||||
/**
|
||||
* @require $Type.alignof <= mem::DEFAULT_MEM_ALIGNMENT : "Types with alignment exceeding the default must use 'new_array_aligned' instead"
|
||||
**/
|
||||
macro new_array_try(Allocator allocator, $Type, usz elements) @nodiscard
|
||||
{
|
||||
return (($Type*)calloc_try(allocator, $Type.sizeof * elements))[:elements];
|
||||
}
|
||||
|
||||
macro alloc_array(Allocator* allocator, $Type, usz elements) @nodiscard
|
||||
/**
|
||||
* Allocate using an aligned allocation. This is necessary for types with a default memory alignment
|
||||
* exceeding DEFAULT_MEM_ALIGNMENT. IMPORTANT! It must be freed using free_aligned.
|
||||
**/
|
||||
macro new_array_aligned(Allocator allocator, $Type, usz elements) @nodiscard
|
||||
{
|
||||
return (($Type*)calloc_aligned(allocator, $Type.sizeof * elements, $Type.alignof))[:elements]!!;
|
||||
}
|
||||
|
||||
/**
|
||||
* @require $Type.alignof <= mem::DEFAULT_MEM_ALIGNMENT : "Types with alignment exceeding the default must use 'alloc_array_aligned' instead"
|
||||
**/
|
||||
macro alloc_array(Allocator allocator, $Type, usz elements) @nodiscard
|
||||
{
|
||||
return alloc_array_try(allocator, $Type, elements)!!;
|
||||
}
|
||||
|
||||
macro alloc_array_try(Allocator* allocator, $Type, usz elements) @nodiscard
|
||||
/**
|
||||
* Allocate using an aligned allocation. This is necessary for types with a default memory alignment
|
||||
* exceeding DEFAULT_MEM_ALIGNMENT. IMPORTANT! It must be freed using free_aligned.
|
||||
**/
|
||||
macro alloc_array_aligned(Allocator allocator, $Type, usz elements) @nodiscard
|
||||
{
|
||||
return (($Type*)malloc_aligned(allocator, $Type.sizeof * elements, $Type.alignof))[:elements]!!;
|
||||
}
|
||||
|
||||
/**
|
||||
* @require $Type.alignof <= mem::DEFAULT_MEM_ALIGNMENT : "Types with alignment exceeding the default must use 'alloc_array_aligned' instead"
|
||||
**/
|
||||
macro alloc_array_try(Allocator allocator, $Type, usz elements) @nodiscard
|
||||
{
|
||||
return (($Type*)malloc_try(allocator, $Type.sizeof * elements))[:elements];
|
||||
}
|
||||
|
||||
macro clone(Allocator* allocator, value) @nodiscard
|
||||
macro clone(Allocator allocator, value) @nodiscard
|
||||
{
|
||||
return new(allocator, $typeof(value), value);
|
||||
}
|
||||
|
||||
fn any* clone_any(Allocator* allocator, any* value) @nodiscard
|
||||
fn any clone_any(Allocator allocator, any value) @nodiscard
|
||||
{
|
||||
usz size = value.type.sizeof;
|
||||
void* data = malloc(allocator, size);
|
||||
@@ -189,155 +299,23 @@ fn any* clone_any(Allocator* allocator, any* value) @nodiscard
|
||||
return any_make(data, value.type);
|
||||
}
|
||||
|
||||
// Allocator "functions"
|
||||
|
||||
macro void*! Allocator.alloc_checked(&self, usz size) @deprecated("Use allocator::malloc_try")
|
||||
{
|
||||
$if env::TESTING:
|
||||
char* data = self.acquire(size, false, 0, 0)!;
|
||||
mem::set(data, 0xAA, size, mem::DEFAULT_MEM_ALIGNMENT);
|
||||
return data;
|
||||
$else
|
||||
return self.acquire(size, false, 0, 0);
|
||||
$endif
|
||||
}
|
||||
|
||||
macro void*! Allocator.calloc_checked(&self, usz size) @deprecated("Use allocator::calloc_try")
|
||||
{
|
||||
return self.acquire(size, true, 0, 0);
|
||||
}
|
||||
|
||||
macro void*! Allocator.realloc_checked(&self, void* ptr, usz new_size) @deprecated("Use allocator::realloc_try")
|
||||
{
|
||||
return self.resize(ptr, new_size, 0, 0);
|
||||
}
|
||||
|
||||
macro Allocator.new_array(&self, $Type, usz size, usz end_padding = 0) @deprecated("Use allocator::alloc_array")
|
||||
{
|
||||
return (($Type*)self.alloc_checked($Type.sizeof * size + end_padding))[:size]!!;
|
||||
}
|
||||
|
||||
macro Allocator.new_array_checked(&self, $Type, usz size, usz end_padding = 0) @deprecated("Use allocator::alloc_array_try")
|
||||
{
|
||||
return (($Type*)self.alloc_checked($Type.sizeof * size + end_padding))[:size];
|
||||
}
|
||||
|
||||
macro Allocator.new_zero_array(&self, $Type, usz size, usz end_padding = 0) @deprecated("Use allocator::new_array")
|
||||
{
|
||||
return (($Type*)self.calloc_checked($Type.sizeof * size + end_padding))[:size]!!;
|
||||
}
|
||||
|
||||
macro Allocator.new_zero_array_checked(&self, $Type, usz size, usz end_padding = 0) @deprecated("Use allocator::new_array_try")
|
||||
{
|
||||
return (($Type*)self.calloc_checked($Type.sizeof * size + end_padding))[:size];
|
||||
}
|
||||
|
||||
macro Allocator.new(&self, $Type, usz end_padding = 0) @nodiscard @deprecated("Use allocator::alloc")
|
||||
{
|
||||
return ($Type*)self.alloc_checked($Type.sizeof + end_padding)!!;
|
||||
}
|
||||
|
||||
macro Allocator.new_checked(&self, $Type, usz end_padding = 0) @nodiscard @deprecated("Use allocator::alloc_try")
|
||||
{
|
||||
return ($Type*)self.alloc_checked($Type.sizeof + end_padding);
|
||||
}
|
||||
|
||||
macro Allocator.new_clear(&self, $Type, usz end_padding = 0) @nodiscard @deprecated("Use allocator::new")
|
||||
{
|
||||
return ($Type*)self.calloc_checked($Type.sizeof + end_padding)!!;
|
||||
}
|
||||
|
||||
macro Allocator.new_clear_checked(&self, $Type, usz end_padding = 0) @nodiscard @deprecated("Use allocator::new_try")
|
||||
{
|
||||
return ($Type*)self.calloc_checked($Type.sizeof + end_padding);
|
||||
}
|
||||
macro Allocator.clone(&self, value) @deprecated("Use allocator::clone")
|
||||
{
|
||||
var x = self.alloc($typeof(value));
|
||||
*x = value;
|
||||
return x;
|
||||
}
|
||||
|
||||
macro void* Allocator.alloc(&self, usz size) @nodiscard @deprecated("Use allocator::malloc")
|
||||
{
|
||||
return self.alloc_checked(size)!!;
|
||||
}
|
||||
macro void* Allocator.calloc(&self, usz size) @nodiscard @deprecated("Use allocator::calloc")
|
||||
{
|
||||
return self.acquire(size, true, 0, 0)!!;
|
||||
}
|
||||
macro void* Allocator.realloc(&self, void* ptr, usz new_size) @nodiscard @deprecated("Use allocator::realloc")
|
||||
{
|
||||
return self.resize(ptr, new_size, 0, 0)!!;
|
||||
}
|
||||
|
||||
macro void*! Allocator.alloc_aligned(&self, usz size, usz alignment, usz offset = 0) @deprecated("Use allocator::alloc_aligned")
|
||||
{
|
||||
$if env::TESTING:
|
||||
char* data = self.acquire(size, false, alignment, offset)!;
|
||||
mem::set(data, 0xAA, size, mem::DEFAULT_MEM_ALIGNMENT);
|
||||
return data;
|
||||
$else
|
||||
return self.acquire(size, false, alignment, offset);
|
||||
$endif
|
||||
}
|
||||
macro void*! Allocator.calloc_aligned(&self, usz size, usz alignment, usz offset = 0) @deprecated("Use allocator::calloc_aligned")
|
||||
{
|
||||
return self.acquire(size, true, alignment, offset);
|
||||
}
|
||||
macro void*! Allocator.realloc_aligned(&self, void* ptr, usz new_size, usz alignment = 0, usz offset = 0) @deprecated("Use allocator::realloc_aligned")
|
||||
{
|
||||
return self.resize(ptr, new_size, alignment, offset);
|
||||
}
|
||||
|
||||
macro void Allocator.free(&self, void* ptr) @deprecated("Use allocator::free")
|
||||
{
|
||||
$if env::TESTING:
|
||||
if (ptr) ((char*)ptr)[0] = 0xBA;
|
||||
$endif
|
||||
self.release(ptr, false);
|
||||
}
|
||||
macro void Allocator.free_aligned(&self, void* ptr) @deprecated("Use allocator::free_aligned")
|
||||
{
|
||||
$if env::TESTING:
|
||||
if (ptr) ((char*)ptr)[0] = 0xBA;
|
||||
$endif
|
||||
self.release(ptr, true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @require bytes > 0
|
||||
* @require alignment > 0
|
||||
* @require bytes <= isz.max
|
||||
**/
|
||||
macro void*! @aligned_alloc(#alloc_fn, usz bytes, usz alignment, usz offset)
|
||||
macro void*! @aligned_alloc(#alloc_fn, usz bytes, usz alignment)
|
||||
{
|
||||
usz header = mem::aligned_offset(AlignedBlock.sizeof + offset, alignment) - offset;
|
||||
if (alignment < void*.alignof) alignment = void*.alignof;
|
||||
usz header = AlignedBlock.sizeof + alignment;
|
||||
usz alignsize = bytes + header;
|
||||
$if @typekind(#alloc_fn(bytes)) == OPTIONAL:
|
||||
void* data = #alloc_fn(header + bytes)!;
|
||||
void* data = #alloc_fn(alignsize)!;
|
||||
$else
|
||||
void* data = #alloc_fn(header + bytes);
|
||||
void* data = #alloc_fn(alignsize);
|
||||
$endif
|
||||
void* mem = mem::aligned_pointer(data + header + offset, alignment) - offset;
|
||||
assert(mem > data);
|
||||
AlignedBlock* desc = (AlignedBlock*)mem - 1;
|
||||
*desc = { bytes, data };
|
||||
return mem;
|
||||
}
|
||||
|
||||
/**
|
||||
* @require bytes > 0
|
||||
* @require alignment > 0
|
||||
**/
|
||||
macro void*! @aligned_calloc(#calloc_fn, usz bytes, usz alignment, usz offset)
|
||||
{
|
||||
usz header = mem::aligned_offset(AlignedBlock.sizeof + offset, alignment) - offset;
|
||||
$if @typekind(#calloc_fn(bytes)) == OPTIONAL:
|
||||
void* data = #calloc_fn(header + bytes)!;
|
||||
$else
|
||||
void* data = #calloc_fn(header + bytes);
|
||||
$endif
|
||||
void* mem = mem::aligned_pointer(data + header + offset, alignment) - offset;
|
||||
void* mem = mem::aligned_pointer(data + AlignedBlock.sizeof, alignment);
|
||||
AlignedBlock* desc = (AlignedBlock*)mem - 1;
|
||||
assert(mem > data);
|
||||
*desc = { bytes, data };
|
||||
@@ -364,12 +342,12 @@ macro void! @aligned_free(#free_fn, void* old_pointer)
|
||||
* @require bytes > 0
|
||||
* @require alignment > 0
|
||||
**/
|
||||
macro void*! @aligned_realloc(#calloc_fn, #free_fn, void* old_pointer, usz bytes, usz alignment, usz offset)
|
||||
macro void*! @aligned_realloc(#calloc_fn, #free_fn, void* old_pointer, usz bytes, usz alignment)
|
||||
{
|
||||
AlignedBlock* desc = (AlignedBlock*)old_pointer - 1;
|
||||
void* data_start = desc.start;
|
||||
void* new_data = @aligned_calloc(#calloc_fn, bytes, alignment, offset)!;
|
||||
mem::copy(new_data, old_pointer, desc.len < bytes ? desc.len : bytes, mem::DEFAULT_MEM_ALIGNMENT, mem::DEFAULT_MEM_ALIGNMENT);
|
||||
void* new_data = @aligned_alloc(#calloc_fn, bytes, alignment)!;
|
||||
mem::copy(new_data, old_pointer, desc.len < bytes ? desc.len : bytes, 1, 1);
|
||||
$if @typekind(#free_fn(data_start)) == OPTIONAL:
|
||||
#free_fn(data_start)!;
|
||||
$else
|
||||
@@ -380,12 +358,12 @@ macro void*! @aligned_realloc(#calloc_fn, #free_fn, void* old_pointer, usz bytes
|
||||
|
||||
|
||||
// All allocators
|
||||
tlocal Allocator* thread_allocator @private = &allocator::LIBC_ALLOCATOR;
|
||||
tlocal Allocator thread_allocator @private = &allocator::LIBC_ALLOCATOR;
|
||||
tlocal TempAllocator* thread_temp_allocator @private = null;
|
||||
tlocal TempAllocator*[2] temp_allocator_pair @private;
|
||||
Allocator* temp_base_allocator @private = &allocator::LIBC_ALLOCATOR;
|
||||
Allocator temp_base_allocator @private = &allocator::LIBC_ALLOCATOR;
|
||||
|
||||
macro TempAllocator* create_default_sized_temp_allocator(Allocator* allocator) @local
|
||||
macro TempAllocator* create_default_sized_temp_allocator(Allocator allocator) @local
|
||||
{
|
||||
$switch (env::MEMORY_ENV)
|
||||
$case NORMAL:
|
||||
@@ -399,7 +377,7 @@ macro TempAllocator* create_default_sized_temp_allocator(Allocator* allocator) @
|
||||
$endswitch
|
||||
}
|
||||
|
||||
macro Allocator* heap() => thread_allocator;
|
||||
macro Allocator heap() => thread_allocator;
|
||||
|
||||
macro TempAllocator* temp()
|
||||
{
|
||||
@@ -417,7 +395,7 @@ fn void init_default_temp_allocators() @private
|
||||
thread_temp_allocator = temp_allocator_pair[0];
|
||||
}
|
||||
|
||||
fn TempAllocator *temp_allocator_next() @private
|
||||
fn TempAllocator* temp_allocator_next() @private
|
||||
{
|
||||
if (!thread_temp_allocator)
|
||||
{
|
||||
@@ -426,4 +404,4 @@ fn TempAllocator *temp_allocator_next() @private
|
||||
}
|
||||
usz index = thread_temp_allocator == temp_allocator_pair[0] ? 1 : 0;
|
||||
return thread_temp_allocator = temp_allocator_pair[index];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,7 +175,7 @@ fn void runtime_startup() @public @export("__c3_runtime_startup")
|
||||
}
|
||||
assert(runtime_state == RUN_CTORS);
|
||||
runtime_state = READ_DYLIB;
|
||||
ctor = null;
|
||||
ctor_first = null;
|
||||
}
|
||||
|
||||
fn void runtime_finalize() @public @export("__c3_runtime_finalize")
|
||||
|
||||
@@ -4,13 +4,13 @@
|
||||
module std::core::runtime;
|
||||
import libc, std::time, std::io, std::sort;
|
||||
|
||||
struct AnyStruct
|
||||
struct AnyRaw
|
||||
{
|
||||
void* ptr;
|
||||
typeid type;
|
||||
}
|
||||
|
||||
struct SubArrayStruct
|
||||
struct SliceRaw
|
||||
{
|
||||
void* ptr;
|
||||
usz len;
|
||||
@@ -24,7 +24,7 @@ struct BenchmarkUnit
|
||||
BenchmarkFn func;
|
||||
}
|
||||
|
||||
fn BenchmarkUnit[] benchmark_collection_create(Allocator* allocator = allocator::heap())
|
||||
fn BenchmarkUnit[] benchmark_collection_create(Allocator allocator = allocator::heap())
|
||||
{
|
||||
BenchmarkFn[] fns = $$BENCHMARK_FNS;
|
||||
String[] names = $$BENCHMARK_NAMES;
|
||||
@@ -142,7 +142,7 @@ struct TestUnit
|
||||
TestFn func;
|
||||
}
|
||||
|
||||
fn TestUnit[] test_collection_create(Allocator* allocator = allocator::heap())
|
||||
fn TestUnit[] test_collection_create(Allocator allocator = allocator::heap())
|
||||
{
|
||||
TestFn[] fns = $$TEST_FNS;
|
||||
String[] names = $$TEST_NAMES;
|
||||
|
||||
@@ -31,6 +31,11 @@ fault NumberConversion
|
||||
FLOAT_OUT_OF_RANGE,
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a temporary String created using the formatting function.
|
||||
*
|
||||
* @param [in] fmt `The formatting string`
|
||||
**/
|
||||
macro String tformat(String fmt, ...)
|
||||
{
|
||||
DString str = dstring::temp_with_capacity(fmt.len + $vacount * 8);
|
||||
@@ -38,7 +43,25 @@ macro String tformat(String fmt, ...)
|
||||
return str.str_view();
|
||||
}
|
||||
|
||||
macro String new_format(String fmt, ..., Allocator* allocator = allocator::heap())
|
||||
/**
|
||||
* Return a temporary ZString created using the formatting function.
|
||||
*
|
||||
* @param [in] fmt `The formatting string`
|
||||
**/
|
||||
macro ZString tformat_zstr(String fmt, ...)
|
||||
{
|
||||
DString str = dstring::temp_with_capacity(fmt.len + $vacount * 8);
|
||||
str.appendf(fmt, $vasplat());
|
||||
return str.zstr_view();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new String created using the formatting function.
|
||||
*
|
||||
* @param [in] fmt `The formatting string`
|
||||
* @param [inout] allocator `The allocator to use`
|
||||
**/
|
||||
macro String new_format(String fmt, ..., Allocator allocator = allocator::heap())
|
||||
{
|
||||
@pool(allocator)
|
||||
{
|
||||
@@ -48,14 +71,37 @@ macro String new_format(String fmt, ..., Allocator* allocator = allocator::heap(
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new ZString created using the formatting function.
|
||||
*
|
||||
* @param [in] fmt `The formatting string`
|
||||
* @param [inout] allocator `The allocator to use`
|
||||
**/
|
||||
macro ZString new_format_zstr(String fmt, ..., Allocator allocator = allocator::heap())
|
||||
{
|
||||
@pool(allocator)
|
||||
{
|
||||
DString str = dstring::temp_with_capacity(fmt.len + $vacount * 8);
|
||||
str.appendf(fmt, $vasplat());
|
||||
return str.copy_zstr(allocator);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a character is in a set.
|
||||
*
|
||||
* @param c `the character to check`
|
||||
* @param [in] set `The formatting string`
|
||||
* @pure
|
||||
* @return `True if a character is in the set`
|
||||
**/
|
||||
macro bool char_in_set(char c, String set)
|
||||
{
|
||||
foreach (ch : set) if (ch == c) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
fn String join_new(String[] s, String joiner, Allocator* allocator = allocator::heap())
|
||||
fn String join_new(String[] s, String joiner, Allocator allocator = allocator::heap())
|
||||
{
|
||||
if (!s)
|
||||
{
|
||||
@@ -81,8 +127,12 @@ fn String join_new(String[] s, String joiner, Allocator* allocator = allocator::
|
||||
}
|
||||
|
||||
/**
|
||||
* @param [in] string
|
||||
* @param [in] to_trim
|
||||
* Remove characters from the front and end of a string.
|
||||
*
|
||||
* @param [in] string `The string to trim`
|
||||
* @param [in] to_trim `The set of characters to trim, defaults to whitespace`
|
||||
* @pure
|
||||
* @return `a substring of the string passed in`
|
||||
**/
|
||||
fn String String.trim(string, String to_trim = "\t\n\r ")
|
||||
{
|
||||
@@ -96,8 +146,12 @@ fn String String.trim(string, String to_trim = "\t\n\r ")
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the String starts with the needle.
|
||||
*
|
||||
* @param [in] string
|
||||
* @param [in] needle
|
||||
* @pure
|
||||
* @return `'true' if the string starts with the needle`
|
||||
**/
|
||||
fn bool String.starts_with(string, String needle)
|
||||
{
|
||||
@@ -107,8 +161,12 @@ fn bool String.starts_with(string, String needle)
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the String ends with the needle.
|
||||
*
|
||||
* @param [in] string
|
||||
* @param [in] needle
|
||||
* @pure
|
||||
* @return `'true' if the string ends with the needle`
|
||||
**/
|
||||
fn bool String.ends_with(string, String needle)
|
||||
{
|
||||
@@ -122,6 +180,8 @@ fn bool String.ends_with(string, String needle)
|
||||
*
|
||||
* @param [in] string
|
||||
* @param [in] needle
|
||||
* @pure
|
||||
* @return `the substring with the prefix removed`
|
||||
**/
|
||||
fn String String.strip(string, String needle)
|
||||
{
|
||||
@@ -134,6 +194,8 @@ fn String String.strip(string, String needle)
|
||||
*
|
||||
* @param [in] string
|
||||
* @param [in] needle
|
||||
* @pure
|
||||
* @return `the substring with the suffix removed`
|
||||
**/
|
||||
fn String String.strip_end(string, String needle)
|
||||
{
|
||||
@@ -153,7 +215,7 @@ fn String String.strip_end(string, String needle)
|
||||
* @require needle.len > 0 "The needle must be at least 1 character long"
|
||||
* @ensure return.len > 0
|
||||
**/
|
||||
fn String[] String.split(s, String needle, usz max = 0, Allocator* allocator = allocator::heap())
|
||||
fn String[] String.split(s, String needle, usz max = 0, Allocator allocator = allocator::heap())
|
||||
{
|
||||
usz capacity = 16;
|
||||
usz i = 0;
|
||||
@@ -196,6 +258,14 @@ fn String[] String.tsplit(s, String needle, usz max = 0)
|
||||
return s.split(needle, max, allocator::temp()) @inline;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a substring is found in the string.
|
||||
|
||||
* @param [in] s
|
||||
* @param [in] needle "The string to look for."
|
||||
* @pure
|
||||
* @return "true if the string contains the substring, false otherwise"
|
||||
**/
|
||||
fn bool String.contains(s, String needle)
|
||||
{
|
||||
return @ok(s.index_of(needle));
|
||||
@@ -205,6 +275,7 @@ fn bool String.contains(s, String needle)
|
||||
* Find the index of the first incidence of a string.
|
||||
*
|
||||
* @param [in] s
|
||||
* @param needle "The character to look for"
|
||||
* @pure
|
||||
* @ensure return < s.len
|
||||
* @return "the index of the needle"
|
||||
@@ -220,9 +291,32 @@ fn usz! String.index_of_char(s, char needle)
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the index of the first incidence of a string.
|
||||
* Find the index of the first incidence of a character.
|
||||
*
|
||||
* @param [in] s
|
||||
* @param needle "The character to look for"
|
||||
* @param start_index "The index to start with, may exceed max index."
|
||||
* @pure
|
||||
* @ensure return < s.len
|
||||
* @return "the index of the needle"
|
||||
* @return! SearchResult.MISSING "if the needle cannot be found starting from the start_index"
|
||||
**/
|
||||
fn usz! String.index_of_char_from(s, char needle, usz start_index)
|
||||
{
|
||||
usz len = s.len;
|
||||
if (len <= start_index) return SearchResult.MISSING?;
|
||||
for (usz i = start_index; i < len; i++)
|
||||
{
|
||||
if (s[i] == needle) return i;
|
||||
}
|
||||
return SearchResult.MISSING?;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the index of the first incidence of a character starting from the end.
|
||||
*
|
||||
* @param [in] s
|
||||
* @param needle "the character to find"
|
||||
* @pure
|
||||
* @ensure return < s.len
|
||||
* @return "the index of the needle"
|
||||
@@ -312,7 +406,7 @@ fn usz ZString.len(str)
|
||||
}
|
||||
|
||||
|
||||
fn ZString String.zstr_copy(s, Allocator* allocator = allocator::heap())
|
||||
fn ZString String.zstr_copy(s, Allocator allocator = allocator::heap())
|
||||
{
|
||||
usz len = s.len;
|
||||
char* str = allocator::malloc(allocator, len + 1);
|
||||
@@ -321,7 +415,7 @@ fn ZString String.zstr_copy(s, Allocator* allocator = allocator::heap())
|
||||
return (ZString)str;
|
||||
}
|
||||
|
||||
fn String String.concat(s1, String s2, Allocator* allocator = allocator::heap())
|
||||
fn String String.concat(s1, String s2, Allocator allocator = allocator::heap())
|
||||
{
|
||||
usz full_len = s1.len + s2.len;
|
||||
char* str = allocator::malloc(allocator, full_len + 1);
|
||||
@@ -337,7 +431,7 @@ fn String String.tconcat(s1, String s2) => s1.concat(s2, allocator::temp());
|
||||
|
||||
fn ZString String.zstr_tcopy(s) => s.zstr_copy(allocator::temp()) @inline;
|
||||
|
||||
fn String String.copy(s, Allocator* allocator = allocator::heap())
|
||||
fn String String.copy(s, Allocator allocator = allocator::heap())
|
||||
{
|
||||
usz len = s.len;
|
||||
char* str = allocator::malloc(allocator, len + 1);
|
||||
@@ -346,7 +440,7 @@ fn String String.copy(s, Allocator* allocator = allocator::heap())
|
||||
return (String)str[:len];
|
||||
}
|
||||
|
||||
fn void String.free(&s, Allocator* allocator = allocator::heap())
|
||||
fn void String.free(&s, Allocator allocator = allocator::heap())
|
||||
{
|
||||
if (!s.len) return;
|
||||
allocator::free(allocator, s.ptr);
|
||||
@@ -355,7 +449,7 @@ fn void String.free(&s, Allocator* allocator = allocator::heap())
|
||||
|
||||
fn String String.tcopy(s) => s.copy(allocator::temp()) @inline;
|
||||
|
||||
fn String ZString.copy(z, Allocator* allocator = allocator::temp())
|
||||
fn String ZString.copy(z, Allocator allocator = allocator::temp())
|
||||
{
|
||||
return z.str_view().copy(allocator) @inline;
|
||||
}
|
||||
@@ -371,7 +465,7 @@ fn String ZString.tcopy(z)
|
||||
* @return! UnicodeResult.INVALID_UTF8 "If the string contained an invalid UTF-8 sequence"
|
||||
* @return! AllocationFailure "If allocation of the string fails"
|
||||
**/
|
||||
fn Char16[]! String.to_new_utf16(s, Allocator* allocator = allocator::heap())
|
||||
fn Char16[]! String.to_new_utf16(s, Allocator allocator = allocator::heap())
|
||||
{
|
||||
usz len16 = conv::utf16len_for_utf8(s);
|
||||
Char16* data = allocator::alloc_array_try(allocator, Char16, len16 + 1)!;
|
||||
@@ -391,7 +485,7 @@ fn Char16[]! String.to_temp_utf16(s)
|
||||
return s.to_new_utf16(allocator::temp());
|
||||
}
|
||||
|
||||
fn WString! String.to_new_wstring(s, Allocator* allocator = allocator::heap())
|
||||
fn WString! String.to_new_wstring(s, Allocator allocator = allocator::heap())
|
||||
{
|
||||
return (WString)s.to_new_utf16(allocator).ptr;
|
||||
}
|
||||
@@ -401,7 +495,7 @@ fn WString! String.to_temp_wstring(s)
|
||||
return (WString)s.to_temp_utf16().ptr;
|
||||
}
|
||||
|
||||
fn Char32[]! String.to_new_utf32(s, Allocator* allocator = allocator::heap())
|
||||
fn Char32[]! String.to_new_utf32(s, Allocator allocator = allocator::heap())
|
||||
{
|
||||
usz codepoints = conv::utf8_codepoints(s);
|
||||
Char32* data = allocator::alloc_array_try(allocator, Char32, codepoints + 1)!;
|
||||
@@ -415,29 +509,49 @@ fn Char32[]! String.to_temp_utf32(s)
|
||||
return s.to_new_utf32(allocator::temp());
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a string to ASCII lower case.
|
||||
*
|
||||
* @param [inout] s
|
||||
* @pure
|
||||
**/
|
||||
fn void String.convert_ascii_to_lower(s)
|
||||
{
|
||||
foreach (&c : s) if (c.is_upper()) *c += 'a' - 'A';
|
||||
foreach (&c : s) if (c.is_upper() @pure) *c += 'a' - 'A';
|
||||
}
|
||||
|
||||
fn String String.new_ascii_to_lower(s, Allocator* allocator = allocator::heap())
|
||||
fn String String.new_ascii_to_lower(s, Allocator allocator = allocator::heap())
|
||||
{
|
||||
String copy = s.copy(allocator);
|
||||
copy.convert_ascii_to_lower();
|
||||
return copy;
|
||||
}
|
||||
|
||||
fn String String.temp_ascii_to_lower(s, Allocator* allocator = allocator::heap())
|
||||
fn String String.temp_ascii_to_lower(s, Allocator allocator = allocator::heap())
|
||||
{
|
||||
return s.new_ascii_to_lower(allocator::temp());
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a string to ASCII upper case.
|
||||
*
|
||||
* @param [inout] s
|
||||
* @pure
|
||||
**/
|
||||
fn void String.convert_ascii_to_upper(s)
|
||||
{
|
||||
foreach (&c : s) if (c.is_lower()) *c -= 'a' - 'A';
|
||||
foreach (&c : s) if (c.is_lower() @pure) *c -= 'a' - 'A';
|
||||
}
|
||||
|
||||
fn String String.new_ascii_to_upper(s, Allocator* allocator = allocator::heap())
|
||||
/**
|
||||
* Returns a string converted to ASCII upper case.
|
||||
*
|
||||
* @param [in] s
|
||||
* @param [inout] allocator
|
||||
*
|
||||
* @return `a new String converted to ASCII upper case.`
|
||||
**/
|
||||
fn String String.new_ascii_to_upper(s, Allocator allocator = allocator::heap())
|
||||
{
|
||||
String copy = s.copy(allocator);
|
||||
copy.convert_ascii_to_upper();
|
||||
@@ -449,12 +563,16 @@ fn StringIterator String.iterator(s)
|
||||
return { s, 0 };
|
||||
}
|
||||
|
||||
/**
|
||||
* @param [in] s
|
||||
* @return `a temporary String converted to ASCII upper case.`
|
||||
**/
|
||||
fn String String.temp_ascii_to_upper(s)
|
||||
{
|
||||
return s.new_ascii_to_upper(allocator::temp());
|
||||
}
|
||||
|
||||
fn String! new_from_utf32(Char32[] utf32, Allocator* allocator = allocator::heap())
|
||||
fn String! new_from_utf32(Char32[] utf32, Allocator allocator = allocator::heap())
|
||||
{
|
||||
usz len = conv::utf8len_for_utf32(utf32);
|
||||
char* data = allocator::malloc_try(allocator, len + 1)!;
|
||||
@@ -464,7 +582,7 @@ fn String! new_from_utf32(Char32[] utf32, Allocator* allocator = allocator::heap
|
||||
return (String)data[:len];
|
||||
}
|
||||
|
||||
fn String! new_from_utf16(Char16[] utf16, Allocator* allocator = allocator::heap())
|
||||
fn String! new_from_utf16(Char16[] utf16, Allocator allocator = allocator::heap())
|
||||
{
|
||||
usz len = conv::utf8len_for_utf16(utf16);
|
||||
char* data = allocator::malloc_try(allocator, len + 1)!;
|
||||
@@ -474,7 +592,7 @@ fn String! new_from_utf16(Char16[] utf16, Allocator* allocator = allocator::heap
|
||||
return (String)data[:len];
|
||||
}
|
||||
|
||||
fn String! new_from_wstring(WString wstring, Allocator* allocator = allocator::heap())
|
||||
fn String! new_from_wstring(WString wstring, Allocator allocator = allocator::heap())
|
||||
{
|
||||
usz utf16_len;
|
||||
while (wstring[utf16_len] != 0) utf16_len++;
|
||||
|
||||
@@ -11,7 +11,7 @@ fault ConversionResult
|
||||
/**
|
||||
* @require $Type.kindof.is_int() || $Type.kindof == TypeKind.ENUM "Argument was not an integer"
|
||||
**/
|
||||
macro any_to_int(any* v, $Type)
|
||||
macro any_to_int(any v, $Type)
|
||||
{
|
||||
typeid any_type = v.type;
|
||||
TypeKind kind = any_type.kindof;
|
||||
@@ -108,10 +108,10 @@ fn bool TypeKind.is_int(kind) @inline
|
||||
return kind == TypeKind.SIGNED_INT || kind == TypeKind.UNSIGNED_INT;
|
||||
}
|
||||
|
||||
macro bool is_subarray_convertable($Type)
|
||||
macro bool is_slice_convertable($Type)
|
||||
{
|
||||
$switch ($Type.kindof)
|
||||
$case SUBARRAY:
|
||||
$case SLICE:
|
||||
return true;
|
||||
$case POINTER:
|
||||
return $Type.inner.kindof == TypeKind.ARRAY;
|
||||
@@ -123,6 +123,16 @@ macro bool is_subarray_convertable($Type)
|
||||
macro bool is_bool($Type) => $Type.kindof == TypeKind.BOOL;
|
||||
macro bool is_int($Type) => $Type.kindof == TypeKind.SIGNED_INT || $Type.kindof == TypeKind.UNSIGNED_INT;
|
||||
|
||||
macro bool is_indexable($Type)
|
||||
{
|
||||
return $defined($Type{}[0]);
|
||||
}
|
||||
|
||||
macro bool is_ref_indexable($Type)
|
||||
{
|
||||
return $defined(&$Type{}[0]);
|
||||
}
|
||||
|
||||
macro bool is_intlike($Type)
|
||||
{
|
||||
$switch ($Type.kindof)
|
||||
@@ -298,10 +308,11 @@ enum TypeKind : char
|
||||
FUNC,
|
||||
OPTIONAL,
|
||||
ARRAY,
|
||||
SUBARRAY,
|
||||
SLICE,
|
||||
VECTOR,
|
||||
DISTINCT,
|
||||
POINTER,
|
||||
INTERFACE,
|
||||
}
|
||||
|
||||
struct TypeEnum
|
||||
|
||||
@@ -26,5 +26,21 @@ macro promote_int(x)
|
||||
$endif
|
||||
}
|
||||
|
||||
macro promote_int_same(x, y)
|
||||
{
|
||||
$if @is_int(x):
|
||||
$switch
|
||||
$case $and(@is_vector(y), $typeof(y).inner == float.typeid):
|
||||
return (float)x;
|
||||
$case $typeof(y).typeid == float.typeid:
|
||||
return (float)x;
|
||||
$default:
|
||||
return (double)x;
|
||||
$endswitch
|
||||
$else
|
||||
return x;
|
||||
$endif
|
||||
}
|
||||
|
||||
macro TypeKind @inner_kind(#value) => types::inner_kind($typeof(#value));
|
||||
|
||||
|
||||
@@ -28,6 +28,18 @@ fn void Rc4.init(&self, char[] key)
|
||||
self.j = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a single pass of en/decryption using a particular key.
|
||||
* @param [in] key
|
||||
* @param [inout] data
|
||||
**/
|
||||
fn void crypt(char[] key, char[] data)
|
||||
{
|
||||
Rc4 rc4;
|
||||
rc4.init(key);
|
||||
rc4.crypt(data, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encrypt or decrypt a sequence of bytes.
|
||||
*
|
||||
|
||||
@@ -3,22 +3,22 @@ import std::io;
|
||||
|
||||
struct CsvReader
|
||||
{
|
||||
InStream* stream;
|
||||
InStream stream;
|
||||
String separator;
|
||||
}
|
||||
|
||||
fn void CsvReader.init(&self, InStream* stream, String separator = ",")
|
||||
fn void CsvReader.init(&self, InStream stream, String separator = ",")
|
||||
{
|
||||
self.stream = stream;
|
||||
self.separator = separator;
|
||||
}
|
||||
|
||||
fn String[]! CsvReader.read_new_row(self, Allocator* allocator = allocator::heap())
|
||||
fn String[]! CsvReader.read_new_row(self, Allocator allocator = allocator::heap())
|
||||
{
|
||||
return self.read_new_row_with_allocator(allocator::temp()) @inline;
|
||||
}
|
||||
|
||||
fn String[]! CsvReader.read_new_row_with_allocator(self, Allocator* allocator = allocator::heap())
|
||||
fn String[]! CsvReader.read_new_row_with_allocator(self, Allocator allocator = allocator::heap())
|
||||
{
|
||||
@pool(allocator)
|
||||
{
|
||||
@@ -45,7 +45,7 @@ macro CsvReader.@each_row(self, int rows = int.max; @body(String[] row))
|
||||
String sep = self.separator;
|
||||
while (rows--)
|
||||
{
|
||||
@stack_mem(512; Allocator* mem)
|
||||
@stack_mem(512; Allocator mem)
|
||||
{
|
||||
String[] parts;
|
||||
@pool()
|
||||
|
||||
@@ -15,7 +15,7 @@ fault JsonParsingError
|
||||
INVALID_NUMBER,
|
||||
}
|
||||
|
||||
fn Object*! parse(InStream* s, Allocator* allocator = allocator::heap())
|
||||
fn Object*! parse(InStream s, Allocator allocator = allocator::heap())
|
||||
{
|
||||
JsonContext context = { .last_string = dstring::new_with_capacity(64, allocator), .stream = s, .allocator = allocator };
|
||||
defer context.last_string.free();
|
||||
@@ -44,8 +44,8 @@ enum JsonTokenType @local
|
||||
struct JsonContext @local
|
||||
{
|
||||
uint line;
|
||||
InStream* stream;
|
||||
Allocator* allocator;
|
||||
InStream stream;
|
||||
Allocator allocator;
|
||||
JsonTokenType token;
|
||||
DString last_string;
|
||||
double last_number;
|
||||
@@ -170,7 +170,7 @@ fn Object*! parse_array(JsonContext* context) @local
|
||||
while (token != JsonTokenType.RBRACKET)
|
||||
{
|
||||
Object* element = parse_from_token(context, token)!;
|
||||
list.append(element);
|
||||
list.push(element);
|
||||
token = advance(context)!;
|
||||
if (token == JsonTokenType.COMMA)
|
||||
{
|
||||
|
||||
@@ -2,12 +2,12 @@ module std::io;
|
||||
|
||||
struct BitReader
|
||||
{
|
||||
InStream* reader;
|
||||
InStream reader;
|
||||
uint bits;
|
||||
uint len;
|
||||
}
|
||||
|
||||
fn void BitReader.init(&self, InStream* byte_reader)
|
||||
fn void BitReader.init(&self, InStream byte_reader)
|
||||
{
|
||||
*self = { .reader = byte_reader };
|
||||
}
|
||||
@@ -40,12 +40,12 @@ fn char! BitReader.read_bits(&self, uint nbits)
|
||||
|
||||
struct BitWriter
|
||||
{
|
||||
OutStream* writer;
|
||||
OutStream writer;
|
||||
uint bits;
|
||||
uint len;
|
||||
}
|
||||
|
||||
fn void BitWriter.init(&self, OutStream* byte_writer)
|
||||
fn void BitWriter.init(&self, OutStream byte_writer)
|
||||
{
|
||||
*self = { .writer = byte_writer };
|
||||
}
|
||||
|
||||
@@ -161,7 +161,7 @@ fn char[]! load_buffer(String filename, char[] buffer)
|
||||
|
||||
}
|
||||
|
||||
fn char[]! load_new(String filename, Allocator* allocator = allocator::heap())
|
||||
fn char[]! load_new(String filename, Allocator allocator = allocator::heap())
|
||||
{
|
||||
File file = open(filename, "rb")!;
|
||||
defer (void)file.close();
|
||||
|
||||
@@ -6,7 +6,7 @@ const int PRINTF_NTOA_BUFFER_SIZE = 256;
|
||||
|
||||
interface Printable
|
||||
{
|
||||
fn String to_new_string(Allocator *allocator) @optional;
|
||||
fn String to_new_string(Allocator allocator) @optional;
|
||||
fn usz! to_format(Formatter* formatter) @optional;
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ fn usz! Formatter.out(&self, char c) @private
|
||||
return 1;
|
||||
}
|
||||
|
||||
fn usz! Formatter.print_with_function(&self, Printable* arg)
|
||||
fn usz! Formatter.print_with_function(&self, Printable arg)
|
||||
{
|
||||
if (&arg.to_format)
|
||||
{
|
||||
@@ -98,7 +98,7 @@ fn usz! Formatter.print_with_function(&self, Printable* arg)
|
||||
self.width = old_width;
|
||||
self.prec = old_prec;
|
||||
}
|
||||
@stack_mem(1024; Allocator* mem)
|
||||
@stack_mem(1024; Allocator mem)
|
||||
{
|
||||
return self.out_substr(arg.to_new_string(mem));
|
||||
};
|
||||
@@ -107,7 +107,7 @@ fn usz! Formatter.print_with_function(&self, Printable* arg)
|
||||
}
|
||||
|
||||
|
||||
fn usz! Formatter.out_str(&self, any* arg) @private
|
||||
fn usz! Formatter.out_str(&self, any arg) @private
|
||||
{
|
||||
switch (arg.type.kindof)
|
||||
{
|
||||
@@ -119,7 +119,7 @@ fn usz! Formatter.out_str(&self, any* arg) @private
|
||||
case FAULT:
|
||||
return self.out_substr((*(anyfault*)arg.ptr).nameof);
|
||||
case ANY:
|
||||
return self.out_str(*(any**)arg);
|
||||
return self.out_str(*(any*)arg);
|
||||
case OPTIONAL:
|
||||
unreachable();
|
||||
case SIGNED_INT:
|
||||
@@ -149,7 +149,7 @@ fn usz! Formatter.out_str(&self, any* arg) @private
|
||||
return self.out_substr(*(bool*)arg.ptr ? "true" : "false");
|
||||
default:
|
||||
}
|
||||
usz! n = self.print_with_function((Printable*)arg);
|
||||
usz! n = self.print_with_function((Printable)arg);
|
||||
if (catch err = n)
|
||||
{
|
||||
case SearchResult.MISSING:
|
||||
@@ -178,14 +178,18 @@ fn usz! Formatter.out_str(&self, any* arg) @private
|
||||
case DISTINCT:
|
||||
if (arg.type == ZString.typeid)
|
||||
{
|
||||
return self.out_substr(((ZString*)arg).str_view());
|
||||
return self.out_substr(*(ZString*)arg ? ((ZString*)arg).str_view() : "(null)");
|
||||
}
|
||||
if (arg.type == DString.typeid)
|
||||
{
|
||||
return self.out_substr(((DString*)arg).str_view());
|
||||
return self.out_substr(*(DString*)arg ? ((DString*)arg).str_view() : "(null)");
|
||||
}
|
||||
return self.out_str(arg.as_inner());
|
||||
case POINTER:
|
||||
if (*(void**)arg == null)
|
||||
{
|
||||
return self.out_substr("(null)");
|
||||
}
|
||||
PrintFlags flags = self.flags;
|
||||
uint width = self.width;
|
||||
defer
|
||||
@@ -246,7 +250,7 @@ fn usz! Formatter.out_str(&self, any* arg) @private
|
||||
}
|
||||
len += self.out_substr(">]")!;
|
||||
return len;
|
||||
case SUBARRAY:
|
||||
case SLICE:
|
||||
// this is SomeType[] so grab the "SomeType"
|
||||
typeid inner = arg.type.inner;
|
||||
if (inner == char.typeid)
|
||||
@@ -291,7 +295,7 @@ fn void! out_null_fn(void* data @unused, char c @unused) @private
|
||||
|
||||
|
||||
|
||||
fn usz! Formatter.vprintf(&self, String format, any*[] anys)
|
||||
fn usz! Formatter.vprintf(&self, String format, any[] anys)
|
||||
{
|
||||
if (!self.out_fn)
|
||||
{
|
||||
@@ -358,7 +362,7 @@ fn usz! Formatter.vprintf(&self, String format, any*[] anys)
|
||||
// evaluate specifier
|
||||
uint base = 0;
|
||||
if (variant_index >= anys.len) return PrintFault.MISSING_ARG?;
|
||||
any* current = anys[variant_index++];
|
||||
any current = anys[variant_index++];
|
||||
switch (c)
|
||||
{
|
||||
case 'd':
|
||||
|
||||
@@ -10,7 +10,7 @@ fn usz! Formatter.adjust(&self, usz len) @local
|
||||
return self.pad(' ', self.width, len);
|
||||
}
|
||||
|
||||
fn uint128! int_from_any(any* arg, bool *is_neg) @private
|
||||
fn uint128! int_from_any(any arg, bool *is_neg) @private
|
||||
{
|
||||
switch (arg.type.kindof)
|
||||
{
|
||||
@@ -63,14 +63,11 @@ fn uint128! int_from_any(any* arg, bool *is_neg) @private
|
||||
}
|
||||
}
|
||||
|
||||
fn FloatType! float_from_any(any* arg) @private
|
||||
fn FloatType! float_from_any(any arg) @private
|
||||
{
|
||||
$if env::F128_SUPPORT:
|
||||
if (arg.type == float128.typeid) return (FloatType)*((float128*)arg.ptr);
|
||||
$endif
|
||||
$if env::F16_SUPPORT:
|
||||
if (arg.type == float16.typeid) return *((float16*)arg.ptr);
|
||||
$endif
|
||||
if (arg.type.kindof == TypeKind.DISTINCT)
|
||||
{
|
||||
return float_from_any(arg.as_inner());
|
||||
@@ -588,14 +585,14 @@ fn usz! Formatter.ntoa_format(&self, String buf, usz len, bool negative, uint ba
|
||||
}
|
||||
|
||||
|
||||
fn usz! Formatter.ntoa_any(&self, any* arg, uint base) @private
|
||||
fn usz! Formatter.ntoa_any(&self, any arg, uint base) @private
|
||||
{
|
||||
bool is_neg;
|
||||
uint128 val = int_from_any(arg, &is_neg)!!;
|
||||
return self.ntoa(val, is_neg, base) @inline;
|
||||
}
|
||||
|
||||
fn usz! Formatter.out_char(&self, any* arg) @private
|
||||
fn usz! Formatter.out_char(&self, any arg) @private
|
||||
{
|
||||
usz len = 1;
|
||||
uint l = 1;
|
||||
@@ -649,21 +646,21 @@ fn void! printf_advance_format(usz format_len, usz *index_ptr) @inline @private
|
||||
if (val >= format_len) return FormattingFault.UNTERMINATED_FORMAT?;
|
||||
}
|
||||
|
||||
fn any*! next_any(any** args_ptr, usz args_len, usz* arg_index_ptr) @inline @private
|
||||
fn any! next_any(any* args_ptr, usz args_len, usz* arg_index_ptr) @inline @private
|
||||
{
|
||||
if (*arg_index_ptr >= args_len) return FormattingFault.MISSING_ARG?;
|
||||
return args_ptr[(*arg_index_ptr)++];
|
||||
}
|
||||
|
||||
fn int! printf_parse_format_field(
|
||||
any** args_ptr, usz args_len, usz* args_index_ptr,
|
||||
any* args_ptr, usz args_len, usz* args_index_ptr,
|
||||
char* format_ptr, usz format_len, usz* index_ptr) @inline @private
|
||||
{
|
||||
char c = format_ptr[*index_ptr];
|
||||
if (c.is_digit()) return simple_atoi(format_ptr, format_len, index_ptr);
|
||||
if (c != '*') return 0;
|
||||
printf_advance_format(format_len, index_ptr)!;
|
||||
any* val = next_any(args_ptr, args_len, args_index_ptr)!;
|
||||
any val = next_any(args_ptr, args_len, args_index_ptr)!;
|
||||
if (!val.type.kindof.is_int()) return FormattingFault.INVALID_WIDTH_ARG?;
|
||||
uint! intval = types::any_to_int(val, int);
|
||||
return intval ?? FormattingFault.INVALID_WIDTH_ARG?;
|
||||
|
||||
167
lib/std/io/io.c3
167
lib/std/io/io.c3
@@ -46,12 +46,18 @@ fault IoError
|
||||
|
||||
|
||||
/**
|
||||
* @param stream
|
||||
* @require @is_instream(stream)
|
||||
* Read from a stream (default is stdin) to the next "\n"
|
||||
* or to the end of the stream, whatever comes first.
|
||||
* "\r" will be filtered from the String.
|
||||
*
|
||||
* @param stream `The stream to read from.`
|
||||
* @require @is_instream(stream) `The stream must implement InStream.`
|
||||
* @param [inout] allocator `the allocator to use.`
|
||||
* @return `The string containing the data read.`
|
||||
**/
|
||||
macro String! readline(stream = io::stdin(), Allocator* allocator = allocator::heap())
|
||||
macro String! readline(stream = io::stdin(), Allocator allocator = allocator::heap())
|
||||
{
|
||||
bool $is_stream = @typeid(stream) == InStream*.typeid;
|
||||
bool $is_stream = @typeis(stream, InStream);
|
||||
$if $is_stream:
|
||||
$typeof(&stream.read_byte) func = &stream.read_byte;
|
||||
char val = func((void*)stream)!;
|
||||
@@ -84,38 +90,67 @@ macro String! readline(stream = io::stdin(), Allocator* allocator = allocator::h
|
||||
};
|
||||
}
|
||||
|
||||
macro String! treadline(stream = io::stdin()) => readline(stream, allocator::temp()) @inline;
|
||||
/**
|
||||
* Reads a string, see `readline`, except the it is allocated
|
||||
* on the temporary allocator and does not need to be freed.
|
||||
*
|
||||
* @param stream `The stream to read from.`
|
||||
* @require @is_instream(stream) `The stream must implement InStream.`
|
||||
* @return `The temporary string containing the data read.`
|
||||
**/
|
||||
macro String! treadline(stream = io::stdin())
|
||||
{
|
||||
return readline(stream, allocator::temp()) @inline;
|
||||
}
|
||||
|
||||
/**
|
||||
* @require @is_outstream(out) "The output must implement OutStream"
|
||||
* Print a value to a stream.
|
||||
*
|
||||
* @param out `the stream to print to`
|
||||
* @param x `the value to print`
|
||||
* @require @is_outstream(out) `The output must implement OutStream.`
|
||||
* @return `the number of bytes printed.`
|
||||
*/
|
||||
macro usz! fprint(out, x)
|
||||
{
|
||||
var $Type = $typeof(x);
|
||||
$switch ($Type)
|
||||
$case String:
|
||||
return out.write(x);
|
||||
$case ZString:
|
||||
return out.write(x.str_view());
|
||||
$case DString:
|
||||
return out.write(x.str_view());
|
||||
$default:
|
||||
$if $assignable(x, String):
|
||||
return out.write((String)x);
|
||||
$else
|
||||
return fprintf(out, "%s", x);
|
||||
$endif
|
||||
$case String: return out.write(x);
|
||||
$case ZString: return out.write(x.str_view());
|
||||
$case DString: return out.write(x.str_view());
|
||||
$default:
|
||||
$if $assignable(x, String):
|
||||
return out.write((String)x);
|
||||
$else
|
||||
return fprintf(out, "%s", x);
|
||||
$endif
|
||||
$endswitch
|
||||
}
|
||||
|
||||
fn usz! fprintf(OutStream* out, String format, args...)
|
||||
/**
|
||||
* Prints using a 'printf'-style formatting string.
|
||||
* See `printf` for details on formatting.
|
||||
*
|
||||
* @param [inout] out `The OutStream to print to`
|
||||
* @param [in] format `The printf-style format string`
|
||||
* @return `the number of characters printed`
|
||||
**/
|
||||
fn usz! fprintf(OutStream out, String format, args...)
|
||||
{
|
||||
Formatter formatter;
|
||||
formatter.init(&out_putstream_fn, &out);
|
||||
return formatter.vprintf(format, args);
|
||||
}
|
||||
|
||||
fn usz! fprintfn(OutStream* out, String format, args...) @maydiscard
|
||||
/**
|
||||
* Prints using a 'printf'-style formatting string,
|
||||
* appending '\n' at the end. See `printf`.
|
||||
*
|
||||
* @param [inout] out `The OutStream to print to`
|
||||
* @param [in] format `The printf-style format string`
|
||||
* @return `the number of characters printed`
|
||||
**/
|
||||
fn usz! fprintfn(OutStream out, String format, args...) @maydiscard
|
||||
{
|
||||
Formatter formatter;
|
||||
formatter.init(&out_putstream_fn, &out);
|
||||
@@ -133,7 +168,7 @@ macro usz! fprintn(out, x = "")
|
||||
usz len = fprint(out, x)!;
|
||||
out.write_byte('\n')!;
|
||||
$switch
|
||||
$case @typeid(out) == OutStream*.typeid:
|
||||
$case @typeid(out) == OutStream.typeid:
|
||||
if (&out.flush) out.flush()!;
|
||||
$case $defined(out.flush):
|
||||
out.flush()!;
|
||||
@@ -141,21 +176,37 @@ macro usz! fprintn(out, x = "")
|
||||
return len + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print any value to stdout.
|
||||
**/
|
||||
macro void print(x)
|
||||
{
|
||||
(void)fprint(io::stdout(), x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print any value to stdout, appending an '\n’ after.
|
||||
*
|
||||
* @param x "The value to print"
|
||||
**/
|
||||
macro void printn(x = "")
|
||||
{
|
||||
(void)fprintn(io::stdout(), x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print any value to stderr.
|
||||
**/
|
||||
macro void eprint(x)
|
||||
{
|
||||
(void)fprint(io::stderr(), x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print any value to stderr, appending an '\n’ after.
|
||||
*
|
||||
* @param x "The value to print"
|
||||
**/
|
||||
macro void eprintn(x)
|
||||
{
|
||||
(void)fprintn(io::stderr(), x);
|
||||
@@ -164,7 +215,7 @@ macro void eprintn(x)
|
||||
|
||||
fn void! out_putstream_fn(void* data, char c) @private
|
||||
{
|
||||
OutStream** stream = data;
|
||||
OutStream* stream = data;
|
||||
return (*stream).write_byte(c);
|
||||
}
|
||||
|
||||
@@ -173,6 +224,20 @@ fn void! out_putchar_fn(void* data @unused, char c) @private
|
||||
libc::putchar(c);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints using a 'printf'-style formatting string.
|
||||
* To print integer numbers, use "%d" or "%x"/"%X,
|
||||
* the latter gives the hexadecimal representation.
|
||||
*
|
||||
* All types can be printed using "%s" which gives
|
||||
* the default representation of the value.
|
||||
*
|
||||
* To create a custom output for a type, implement
|
||||
* the Printable interface.
|
||||
*
|
||||
* @param [in] format `The printf-style format string`
|
||||
* @return `the number of characters printed`
|
||||
**/
|
||||
fn usz! printf(String format, args...) @maydiscard
|
||||
{
|
||||
Formatter formatter;
|
||||
@@ -180,6 +245,13 @@ fn usz! printf(String format, args...) @maydiscard
|
||||
return formatter.vprintf(format, args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints using a 'printf'-style formatting string,
|
||||
* appending '\n' at the end. See `printf`.
|
||||
*
|
||||
* @param [in] format `The printf-style format string`
|
||||
* @return `the number of characters printed`
|
||||
**/
|
||||
fn usz! printfn(String format, args...) @maydiscard
|
||||
{
|
||||
Formatter formatter;
|
||||
@@ -190,19 +262,33 @@ fn usz! printfn(String format, args...) @maydiscard
|
||||
return len + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints using a 'printf'-style formatting string
|
||||
* to stderr.
|
||||
*
|
||||
* @param [in] format `The printf-style format string`
|
||||
* @return `the number of characters printed`
|
||||
**/
|
||||
fn usz! eprintf(String format, args...) @maydiscard
|
||||
{
|
||||
Formatter formatter;
|
||||
OutStream* stream = stderr();
|
||||
OutStream stream = stderr();
|
||||
formatter.init(&out_putstream_fn, &stream);
|
||||
return formatter.vprintf(format, args);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Prints using a 'printf'-style formatting string,
|
||||
* to stderr appending '\n' at the end. See `printf`.
|
||||
*
|
||||
* @param [in] format `The printf-style format string`
|
||||
* @return `the number of characters printed`
|
||||
**/
|
||||
fn usz! eprintfn(String format, args...) @maydiscard
|
||||
{
|
||||
Formatter formatter;
|
||||
OutStream* stream = stderr();
|
||||
OutStream stream = stderr();
|
||||
formatter.init(&out_putstream_fn, &stream);
|
||||
usz len = formatter.vprintf(format, args)! + 1;
|
||||
stderr().write_byte('\n')!;
|
||||
@@ -210,6 +296,14 @@ fn usz! eprintfn(String format, args...) @maydiscard
|
||||
return len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints using a 'printf'-style formatting string,
|
||||
* to a string buffer. See `printf`.
|
||||
*
|
||||
* @param [inout] buffer `The buffer to print to`
|
||||
* @param [in] format `The printf-style format string`
|
||||
* @return `a slice formed from the "buffer" with the resulting length.`
|
||||
**/
|
||||
fn char[]! bprintf(char[] buffer, String format, args...) @maydiscard
|
||||
{
|
||||
Formatter formatter;
|
||||
@@ -219,6 +313,7 @@ fn char[]! bprintf(char[] buffer, String format, args...) @maydiscard
|
||||
return buffer[:data.written];
|
||||
}
|
||||
|
||||
// Used to print to a buffer.
|
||||
fn void! out_buffer_fn(void *data, char c) @private
|
||||
{
|
||||
BufferData *buffer_data = data;
|
||||
@@ -226,22 +321,30 @@ fn void! out_buffer_fn(void *data, char c) @private
|
||||
buffer_data.buffer[buffer_data.written++] = c;
|
||||
}
|
||||
|
||||
|
||||
// Used for buffer printing
|
||||
struct BufferData @private
|
||||
{
|
||||
char[] buffer;
|
||||
usz written;
|
||||
}
|
||||
|
||||
|
||||
// Only available with LIBC
|
||||
module std::io @if (env::LIBC);
|
||||
import libc;
|
||||
|
||||
/**
|
||||
* Libc `putchar`, prints a single character to stdout.
|
||||
**/
|
||||
fn void putchar(char c) @inline
|
||||
{
|
||||
libc::putchar(c);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get standard out.
|
||||
*
|
||||
* @return `stdout as a File`
|
||||
**/
|
||||
fn File* stdout()
|
||||
{
|
||||
static File file;
|
||||
@@ -249,6 +352,11 @@ fn File* stdout()
|
||||
return &file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get standard err.
|
||||
*
|
||||
* @return `stderr as a File`
|
||||
**/
|
||||
fn File* stderr()
|
||||
{
|
||||
static File file;
|
||||
@@ -256,6 +364,11 @@ fn File* stderr()
|
||||
return &file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get standard in.
|
||||
*
|
||||
* @return `stdin as a File`
|
||||
**/
|
||||
fn File* stdin()
|
||||
{
|
||||
static File file;
|
||||
@@ -271,7 +384,7 @@ File stderr_file;
|
||||
|
||||
fn void putchar(char c) @inline
|
||||
{
|
||||
(void)stdout_file.putc(c);
|
||||
(void)stdout_file.write_byte(c);
|
||||
}
|
||||
|
||||
fn File* stdout()
|
||||
|
||||
@@ -95,7 +95,7 @@ fn bool native_is_file(String path)
|
||||
$case env::DARWIN:
|
||||
$case env::LINUX:
|
||||
Stat stat;
|
||||
return @ok(native_stat(&stat, path)) && stat.st_mode & libc::S_IFREG;
|
||||
return @ok(native_stat(&stat, path)) && libc_S_ISTYPE(stat.st_mode, libc::S_IFREG);
|
||||
$default:
|
||||
File! f = file::open(path, "r");
|
||||
defer (void)f.close();
|
||||
@@ -107,7 +107,7 @@ fn bool native_is_dir(String path)
|
||||
{
|
||||
$if env::DARWIN || env::LINUX:
|
||||
Stat stat;
|
||||
return @ok(native_stat(&stat, path)) && stat.st_mode & libc::S_IFDIR;
|
||||
return @ok(native_stat(&stat, path)) && libc_S_ISTYPE(stat.st_mode, libc::S_IFDIR);
|
||||
$else
|
||||
return native_file_or_dir_exists(path) && !native_is_file(path);
|
||||
$endif
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
module std::io::os;
|
||||
import libc, std::os;
|
||||
|
||||
macro String! getcwd(Allocator* allocator = allocator::heap())
|
||||
macro String! getcwd(Allocator allocator = allocator::heap())
|
||||
{
|
||||
$switch
|
||||
$case env::WIN32:
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
module std::io::os @if(env::POSIX);
|
||||
import std::io, std::os;
|
||||
|
||||
fn PathList! native_ls(Path dir, bool no_dirs, bool no_symlinks, String mask, Allocator* allocator)
|
||||
fn PathList! native_ls(Path dir, bool no_dirs, bool no_symlinks, String mask, Allocator allocator)
|
||||
{
|
||||
PathList list;
|
||||
list.new_init(.allocator = allocator);
|
||||
@@ -16,7 +16,7 @@ fn PathList! native_ls(Path dir, bool no_dirs, bool no_symlinks, String mask, Al
|
||||
if (entry.d_type == posix::DT_LNK && no_symlinks) continue;
|
||||
if (entry.d_type == posix::DT_DIR && no_dirs) continue;
|
||||
Path path = path::new(name, allocator)!!;
|
||||
list.append(path);
|
||||
list.push(path);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
@@ -24,7 +24,7 @@ fn PathList! native_ls(Path dir, bool no_dirs, bool no_symlinks, String mask, Al
|
||||
module std::io::os @if(env::WIN32);
|
||||
import std::time, std::os, std::io;
|
||||
|
||||
fn PathList! native_ls(Path dir, bool no_dirs, bool no_symlinks, String mask, Allocator* allocator)
|
||||
fn PathList! native_ls(Path dir, bool no_dirs, bool no_symlinks, String mask, Allocator allocator)
|
||||
{
|
||||
PathList list;
|
||||
list.new_init(.allocator = allocator);
|
||||
@@ -43,7 +43,7 @@ fn PathList! native_ls(Path dir, bool no_dirs, bool no_symlinks, String mask, Al
|
||||
{
|
||||
String filename = string::temp_from_wstring((WString)&find_data.cFileName)!;
|
||||
if (filename == ".." || filename == ".") continue;
|
||||
list.append(path::new(filename, allocator)!);
|
||||
list.push(path::new(filename, allocator)!);
|
||||
};
|
||||
} while (win32::findNextFileW(find, &find_data));
|
||||
return list;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
module std::io::os @if(env::LIBC);
|
||||
import std::io::path, std::os;
|
||||
|
||||
fn Path! native_temp_directory(Allocator* allocator = allocator::heap()) @if(!env::WIN32)
|
||||
fn Path! native_temp_directory(Allocator allocator = allocator::heap()) @if(!env::WIN32)
|
||||
{
|
||||
foreach (String env : { "TMPDIR", "TMP", "TEMP", "TEMPDIR" })
|
||||
{
|
||||
@@ -11,7 +11,7 @@ fn Path! native_temp_directory(Allocator* allocator = allocator::heap()) @if(!en
|
||||
return path::new("/tmp", allocator);
|
||||
}
|
||||
|
||||
fn Path! native_temp_directory(Allocator* allocator = allocator::heap()) @if(env::WIN32)
|
||||
fn Path! native_temp_directory(Allocator allocator = allocator::heap()) @if(env::WIN32)
|
||||
{
|
||||
@pool(allocator)
|
||||
{
|
||||
@@ -24,8 +24,9 @@ fn Path! native_temp_directory(Allocator* allocator = allocator::heap()) @if(env
|
||||
}
|
||||
|
||||
module std::io::os @if(env::NO_LIBC);
|
||||
import std::io::path;
|
||||
|
||||
macro Path! native_temp_directory(Allocator* allocator = allocator::heap())
|
||||
macro Path! native_temp_directory(Allocator allocator = allocator::heap())
|
||||
{
|
||||
return IoError.UNSUPPORTED_OPERATION?;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
module std::io::path;
|
||||
import std::collections::list, std::io::os;
|
||||
import std::os::win32;
|
||||
|
||||
const PathEnv DEFAULT_PATH_ENV = env::WIN32 ? PathEnv.WIN32 : PathEnv.POSIX;
|
||||
const char PREFERRED_SEPARATOR_WIN32 = '\\';
|
||||
@@ -26,7 +27,7 @@ enum PathEnv
|
||||
POSIX
|
||||
}
|
||||
|
||||
fn Path! getcwd(Allocator* allocator = allocator::heap())
|
||||
fn Path! getcwd(Allocator allocator = allocator::heap())
|
||||
{
|
||||
@pool(allocator)
|
||||
{
|
||||
@@ -40,7 +41,7 @@ fn usz! file_size(Path path) => os::native_file_size(path.str_view());
|
||||
fn bool exists(Path path) => os::native_file_or_dir_exists(path.str_view());
|
||||
fn Path! tgetcwd() => getcwd(allocator::temp()) @inline;
|
||||
fn void! chdir(Path path) => os::native_chdir(path) @inline;
|
||||
fn Path! temp_directory(Allocator* allocator = allocator::heap()) => os::native_temp_directory(allocator);
|
||||
fn Path! temp_directory(Allocator allocator = allocator::heap()) => os::native_temp_directory(allocator);
|
||||
fn void! delete(Path path) => os::native_remove(path.str_view()) @inline;
|
||||
|
||||
macro bool is_separator(char c, PathEnv path_env = DEFAULT_PATH_ENV)
|
||||
@@ -58,7 +59,7 @@ macro bool is_win32_separator(char c)
|
||||
return c == '/' || c == '\\';
|
||||
}
|
||||
|
||||
fn PathList! ls(Path dir, bool no_dirs = false, bool no_symlinks = false, String mask = "", Allocator* allocator = allocator::heap())
|
||||
fn PathList! ls(Path dir, bool no_dirs = false, bool no_symlinks = false, String mask = "", Allocator allocator = allocator::heap())
|
||||
{
|
||||
$if $defined(os::native_ls):
|
||||
return os::native_ls(dir, no_dirs, no_symlinks, mask, allocator);
|
||||
@@ -105,7 +106,7 @@ fn void! rmtree(Path path)
|
||||
$endif
|
||||
}
|
||||
|
||||
fn Path! new(String path, Allocator* allocator = allocator::heap(), PathEnv path_env = DEFAULT_PATH_ENV)
|
||||
fn Path! new(String path, Allocator allocator = allocator::heap(), PathEnv path_env = DEFAULT_PATH_ENV)
|
||||
{
|
||||
return { normalize(path.copy(allocator), path_env), path_env };
|
||||
}
|
||||
@@ -115,7 +116,7 @@ fn Path! temp_new(String path, PathEnv path_env = DEFAULT_PATH_ENV)
|
||||
return new(path, allocator::temp(), path_env);
|
||||
}
|
||||
|
||||
fn Path! new_win32_wstring(WString path, Allocator* allocator = allocator::heap())
|
||||
fn Path! new_win32_wstring(WString path, Allocator allocator = allocator::heap())
|
||||
{
|
||||
@pool(allocator)
|
||||
{
|
||||
@@ -123,12 +124,12 @@ fn Path! new_win32_wstring(WString path, Allocator* allocator = allocator::heap(
|
||||
};
|
||||
}
|
||||
|
||||
fn Path! new_windows(String path, Allocator* allocator = allocator::heap())
|
||||
fn Path! new_windows(String path, Allocator allocator = allocator::heap())
|
||||
{
|
||||
return new(path, allocator, WIN32);
|
||||
}
|
||||
|
||||
fn Path! new_posix(String path, Allocator* allocator = allocator::heap())
|
||||
fn Path! new_posix(String path, Allocator allocator = allocator::heap())
|
||||
{
|
||||
return new(path, allocator, POSIX);
|
||||
}
|
||||
@@ -143,7 +144,7 @@ fn bool Path.equals(self, Path p2)
|
||||
*
|
||||
* @param [in] filename
|
||||
**/
|
||||
fn Path! Path.append(self, String filename, Allocator* allocator = allocator::heap())
|
||||
fn Path! Path.append(self, String filename, Allocator allocator = allocator::heap())
|
||||
{
|
||||
if (!self.path_string.len) return new(filename, allocator, self.env)!;
|
||||
assert(!is_separator(self.path_string[^1], self.env));
|
||||
@@ -166,7 +167,19 @@ fn usz Path.start_of_base_name(self) @local
|
||||
if (!path_str.len) return 0;
|
||||
if (self.env == PathEnv.WIN32)
|
||||
{
|
||||
return path_str.rindex_of_char('\\') + 1 ?? volume_name_len(path_str, self.env)!!;
|
||||
if (try index = path_str.rindex_of_char('\\'))
|
||||
{
|
||||
// c:\ style path, we're done!
|
||||
if (path_str[0] != '\\') return index + 1;
|
||||
// Handle \\server\foo
|
||||
// Find the \ before "foo"
|
||||
usz last_index = 2 + path_str[2..].index_of_char('\\')!!;
|
||||
// If they don't match, we're done
|
||||
assert(last_index <= index, "Invalid normalized, path %d vs %s in %s", last_index, index, path_str);
|
||||
if (last_index != index) return index + 1;
|
||||
// Otherwise just default to the volume length.
|
||||
}
|
||||
return volume_name_len(path_str, self.env)!!;
|
||||
}
|
||||
return path_str.rindex_of_char('/') + 1 ?? 0;
|
||||
}
|
||||
@@ -176,28 +189,39 @@ fn bool! Path.is_absolute(self)
|
||||
String path_str = self.str_view();
|
||||
if (!path_str.len) return false;
|
||||
usz path_start = volume_name_len(path_str, self.env)!;
|
||||
if (path_start > 0 && path_str[0] == '\\') return true;
|
||||
return path_start < path_str.len && is_separator(path_str[path_start], self.env);
|
||||
}
|
||||
|
||||
fn Path! Path.absolute(self, Allocator* allocator = allocator::heap())
|
||||
/**
|
||||
* @require self.env == DEFAULT_PATH_ENV : "This method is only available on native paths"
|
||||
**/
|
||||
fn Path! Path.absolute(self, Allocator allocator = allocator::heap())
|
||||
{
|
||||
String path_str = self.str_view();
|
||||
if (!path_str.len) path_str = ".";
|
||||
if (!path_str.len) return PathResult.INVALID_PATH?;
|
||||
if (self.is_absolute()!) return new(path_str, allocator, self.env);
|
||||
if (path_str == ".")
|
||||
{
|
||||
@pool(allocator)
|
||||
{
|
||||
String cwd = os::getcwd(allocator::temp())!;
|
||||
return new(cwd, allocator, self.env);
|
||||
};
|
||||
}
|
||||
$if DEFAULT_PATH_ENV == WIN32:
|
||||
@pool(allocator)
|
||||
{
|
||||
const usz BUFFER_LEN = 4096;
|
||||
WString buffer = (WString)mem::temp_alloc_array(Char16, BUFFER_LEN);
|
||||
buffer = win32::_wfullpath(buffer, path_str.to_temp_wstring()!, BUFFER_LEN);
|
||||
if (!buffer) return PathResult.INVALID_PATH?;
|
||||
return { string::new_from_wstring(buffer, allocator), WIN32 };
|
||||
};
|
||||
$else
|
||||
String cwd = os::getcwd(allocator::temp())!;
|
||||
return new(cwd, allocator, self.env);
|
||||
}
|
||||
switch (self.env)
|
||||
{
|
||||
case WIN32:
|
||||
usz path_start = volume_name_len(path_str, self.env)!;
|
||||
if (path_start > 0) return self;
|
||||
case POSIX:
|
||||
if (path_str[0] == PREFERRED_SEPARATOR_POSIX) return self;
|
||||
}
|
||||
String cwd = os::getcwd(allocator::temp())!;
|
||||
return Path{ cwd, self.env }.append(path_str, allocator)!;
|
||||
return Path { cwd, self.env }.append(path_str, allocator)!;
|
||||
$endif
|
||||
}
|
||||
|
||||
fn String Path.basename(self)
|
||||
@@ -208,13 +232,21 @@ fn String Path.basename(self)
|
||||
return path_str[basename_start..];
|
||||
}
|
||||
|
||||
|
||||
fn String Path.dirname(self)
|
||||
{
|
||||
usz basename_start = self.start_of_base_name();
|
||||
String path_str = self.path_string;
|
||||
if (basename_start == 0) return "";
|
||||
if (basename_start == 0) return ".";
|
||||
usz start = volume_name_len(path_str, self.env)!!;
|
||||
if (basename_start <= start + 1) return path_str[:basename_start];
|
||||
if (basename_start <= start + 1)
|
||||
{
|
||||
if (self.env == WIN32 && basename_start > start && path_str[0..1] == `\\`)
|
||||
{
|
||||
return path_str[:basename_start - 1];
|
||||
}
|
||||
return path_str[:basename_start];
|
||||
}
|
||||
return path_str[:basename_start - 1];
|
||||
}
|
||||
|
||||
@@ -248,13 +280,20 @@ fn usz! volume_name_len(String path, PathEnv path_env) @local
|
||||
while (count < len && path[count] == '\\') count++;
|
||||
// Not 2 => folded paths
|
||||
if (count != 2) return 0;
|
||||
// Check that we have a name followed by '/'
|
||||
// Check that we have a name followed by '\'
|
||||
isz base_found = 0;
|
||||
for (usz i = 2; i < len; i++)
|
||||
{
|
||||
char c = path[i];
|
||||
if (is_win32_separator(c)) return i;
|
||||
if (is_win32_separator(c))
|
||||
{
|
||||
if (base_found) return i;
|
||||
base_found = i;
|
||||
continue;
|
||||
}
|
||||
if (is_reserved_win32_path_char(c)) return PathResult.INVALID_PATH?;
|
||||
}
|
||||
if (base_found > 0 && base_found + 1 < len) return len;
|
||||
return PathResult.INVALID_PATH?;
|
||||
case 'A'..'Z':
|
||||
case 'a'..'z':
|
||||
@@ -281,6 +320,10 @@ fn String! normalize(String path_str, PathEnv path_env = DEFAULT_PATH_ENV)
|
||||
{
|
||||
if (!path_str.len) return "";
|
||||
usz path_start = volume_name_len(path_str, path_env)!;
|
||||
if (path_start > 0 && path_env == PathEnv.WIN32)
|
||||
{
|
||||
for (usz i = 0; i < path_start; i++) if (path_str[i] == '/') path_str[i] = '\\';
|
||||
}
|
||||
usz path_len = path_str.len;
|
||||
if (path_start == path_len) return path_str;
|
||||
char path_separator = path_env == PathEnv.WIN32 ? PREFERRED_SEPARATOR_WIN32 : PREFERRED_SEPARATOR_POSIX;
|
||||
@@ -384,7 +427,9 @@ fn String! normalize(String path_str, PathEnv path_env = DEFAULT_PATH_ENV)
|
||||
len++;
|
||||
}
|
||||
if (len > path_start + 1 && is_separator(path_str[len - 1], path_env)) len--;
|
||||
path_str.ptr[len] = 0;
|
||||
if (path_str.len > len) path_str.ptr[len] = 0;
|
||||
// Empty path after normalization -> "."
|
||||
if (!len) return ".";
|
||||
return path_str[:len];
|
||||
}
|
||||
|
||||
@@ -395,6 +440,7 @@ fn String Path.root_directory(self)
|
||||
String path_str = self.str_view();
|
||||
usz len = path_str.len;
|
||||
if (!len) return "";
|
||||
if (path_str == ".") return ".";
|
||||
if (self.env == PathEnv.WIN32)
|
||||
{
|
||||
usz root_len = volume_name_len(path_str, self.env)!!;
|
||||
@@ -417,11 +463,12 @@ def PathWalker = fn bool! (Path, bool is_dir, void*);
|
||||
/*
|
||||
* Walk the path recursively. PathWalker is run on every file and
|
||||
* directory found. Return true to abort the walk.
|
||||
* @require self.env == DEFAULT_PATH_ENV : "This method is only available on native paths"
|
||||
*/
|
||||
fn bool! Path.walk(self, PathWalker w, void* data)
|
||||
{
|
||||
const PATH_MAX = 512;
|
||||
@stack_mem(PATH_MAX; Allocator* allocator)
|
||||
@stack_mem(PATH_MAX; Allocator allocator)
|
||||
{
|
||||
Path abs = self.absolute(allocator)!;
|
||||
PathList files = ls(abs, .allocator = allocator)!;
|
||||
@@ -448,6 +495,11 @@ fn bool Path.has_suffix(self, String str)
|
||||
return self.str_view().ends_with(str);
|
||||
}
|
||||
|
||||
fn void Path.free_with_allocator(self, Allocator allocator)
|
||||
{
|
||||
allocator::free(allocator, self.path_string.ptr);
|
||||
}
|
||||
|
||||
fn void Path.free(self)
|
||||
{
|
||||
free(self.path_string.ptr);
|
||||
@@ -459,7 +511,7 @@ fn usz! Path.to_format(&self, Formatter* formatter) @dynamic
|
||||
return formatter.print(self.str_view());
|
||||
}
|
||||
|
||||
fn String Path.to_new_string(&self, Allocator* allocator = allocator::heap()) @dynamic
|
||||
fn String Path.to_new_string(&self, Allocator allocator = allocator::heap()) @dynamic
|
||||
{
|
||||
return self.str_view().copy(allocator);
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ interface InStream
|
||||
fn usz! available() @optional;
|
||||
fn usz! read(char[] buffer);
|
||||
fn char! read_byte();
|
||||
fn usz! write_to(OutStream* out) @optional;
|
||||
fn usz! write_to(OutStream out) @optional;
|
||||
fn void! pushback_byte() @optional;
|
||||
}
|
||||
|
||||
@@ -21,10 +21,10 @@ interface OutStream
|
||||
fn void! flush() @optional;
|
||||
fn usz! write(char[] bytes);
|
||||
fn void! write_byte(char c);
|
||||
fn usz! read_to(InStream* in) @optional;
|
||||
fn usz! read_to(InStream in) @optional;
|
||||
}
|
||||
|
||||
fn usz! available(InStream* s)
|
||||
fn usz! available(InStream s)
|
||||
{
|
||||
if (&s.available) return s.available();
|
||||
if (&s.seek)
|
||||
@@ -39,19 +39,19 @@ fn usz! available(InStream* s)
|
||||
|
||||
macro bool @is_instream(#expr)
|
||||
{
|
||||
return $assignable(#expr, InStream*);
|
||||
return $assignable(#expr, InStream);
|
||||
}
|
||||
|
||||
macro bool @is_outstream(#expr)
|
||||
{
|
||||
return $assignable(#expr, OutStream*);
|
||||
return $assignable(#expr, OutStream);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param [&out] ref
|
||||
* @require @is_instream(stream)
|
||||
**/
|
||||
macro usz! read_any(stream, any* ref)
|
||||
macro usz! read_any(stream, any ref)
|
||||
{
|
||||
return read_all(stream, ((char*)ref)[:ref.type.sizeof]);
|
||||
}
|
||||
@@ -61,7 +61,7 @@ macro usz! read_any(stream, any* ref)
|
||||
* @require @is_outstream(stream)
|
||||
* @ensure return == ref.type.sizeof
|
||||
*/
|
||||
macro usz! write_any(stream, any* ref)
|
||||
macro usz! write_any(stream, any ref)
|
||||
{
|
||||
return write_all(stream, ((char*)ref)[:ref.type.sizeof]);
|
||||
}
|
||||
@@ -134,7 +134,7 @@ macro void! @pushback_using_seek(&s)
|
||||
s.seek(-1, CURSOR)!;
|
||||
}
|
||||
|
||||
fn usz! copy_to(InStream* in, OutStream* dst, char[] buffer = {})
|
||||
fn usz! copy_to(InStream in, OutStream dst, char[] buffer = {})
|
||||
{
|
||||
if (buffer.len) return copy_through_buffer(in, dst, buffer);
|
||||
if (&in.write_to) return in.write_to(dst);
|
||||
@@ -156,7 +156,7 @@ fn usz! copy_to(InStream* in, OutStream* dst, char[] buffer = {})
|
||||
$endswitch
|
||||
}
|
||||
|
||||
macro usz! copy_through_buffer(InStream *in, OutStream* dst, char[] buffer) @local
|
||||
macro usz! copy_through_buffer(InStream in, OutStream dst, char[] buffer) @local
|
||||
{
|
||||
usz total_copied;
|
||||
while (true)
|
||||
|
||||
@@ -2,7 +2,7 @@ module std::io;
|
||||
|
||||
struct ReadBuffer (InStream)
|
||||
{
|
||||
InStream* wrapped_stream;
|
||||
InStream wrapped_stream;
|
||||
char[] bytes;
|
||||
usz read_idx;
|
||||
usz write_idx;
|
||||
@@ -14,7 +14,7 @@ struct ReadBuffer (InStream)
|
||||
* @require bytes.len > 0
|
||||
* @require self.bytes.len == 0 "Init may not run on already initialized data"
|
||||
**/
|
||||
fn ReadBuffer* ReadBuffer.init(&self, InStream* wrapped_stream, char[] bytes)
|
||||
fn ReadBuffer* ReadBuffer.init(&self, InStream wrapped_stream, char[] bytes)
|
||||
{
|
||||
*self = { .wrapped_stream = wrapped_stream, .bytes = bytes };
|
||||
return self;
|
||||
@@ -63,7 +63,7 @@ fn void! ReadBuffer.refill(&self) @local @inline
|
||||
|
||||
struct WriteBuffer (OutStream)
|
||||
{
|
||||
OutStream* wrapped_stream;
|
||||
OutStream wrapped_stream;
|
||||
char[] bytes;
|
||||
usz index;
|
||||
}
|
||||
@@ -74,7 +74,7 @@ struct WriteBuffer (OutStream)
|
||||
* @require bytes.len > 0 "Non-empty buffer required"
|
||||
* @require self.bytes.len == 0 "Init may not run on already initialized data"
|
||||
**/
|
||||
fn WriteBuffer* WriteBuffer.init(&self, OutStream* wrapped_stream, char[] bytes)
|
||||
fn WriteBuffer* WriteBuffer.init(&self, OutStream wrapped_stream, char[] bytes)
|
||||
{
|
||||
*self = { .wrapped_stream = wrapped_stream, .bytes = bytes };
|
||||
return self;
|
||||
|
||||
@@ -3,7 +3,7 @@ import std::math;
|
||||
|
||||
struct ByteBuffer (InStream, OutStream)
|
||||
{
|
||||
Allocator* allocator;
|
||||
Allocator allocator;
|
||||
usz max_read;
|
||||
char[] bytes;
|
||||
usz read_idx;
|
||||
@@ -16,17 +16,7 @@ struct ByteBuffer (InStream, OutStream)
|
||||
* max_read defines how many bytes might be kept before its internal buffer is shrinked.
|
||||
* @require self.bytes.len == 0 "Buffer already initialized."
|
||||
**/
|
||||
fn ByteBuffer*! ByteBuffer.init_new(&self, usz max_read, usz initial_capacity = 16, Allocator* allocator = allocator::heap()) @deprecated("Replaced by new_init")
|
||||
{
|
||||
return self.new_init(max_read, initial_capacity, allocator) @inline;
|
||||
}
|
||||
|
||||
/**
|
||||
* ByteBuffer provides a streamable read/write buffer.
|
||||
* max_read defines how many bytes might be kept before its internal buffer is shrinked.
|
||||
* @require self.bytes.len == 0 "Buffer already initialized."
|
||||
**/
|
||||
fn ByteBuffer*! ByteBuffer.new_init(&self, usz max_read, usz initial_capacity = 16, Allocator* allocator = allocator::heap())
|
||||
fn ByteBuffer*! ByteBuffer.new_init(&self, usz max_read, usz initial_capacity = 16, Allocator allocator = allocator::heap())
|
||||
{
|
||||
*self = { .allocator = allocator, .max_read = max_read };
|
||||
initial_capacity = max(initial_capacity, 16);
|
||||
@@ -34,11 +24,6 @@ fn ByteBuffer*! ByteBuffer.new_init(&self, usz max_read, usz initial_capacity =
|
||||
return self;
|
||||
}
|
||||
|
||||
fn ByteBuffer*! ByteBuffer.init_temp(&self, usz max_read, usz initial_capacity = 16) @deprecated("Replaced by temp_init")
|
||||
{
|
||||
return self.temp_init(max_read, initial_capacity) @inline;
|
||||
}
|
||||
|
||||
fn ByteBuffer*! ByteBuffer.temp_init(&self, usz max_read, usz initial_capacity = 16)
|
||||
{
|
||||
return self.new_init(max_read, initial_capacity, allocator::temp());
|
||||
|
||||
@@ -53,7 +53,7 @@ fn usz! ByteReader.seek(&self, isz offset, Seek seek) @dynamic
|
||||
return new_index;
|
||||
}
|
||||
|
||||
fn usz! ByteReader.write_to(&self, OutStream* writer) @dynamic
|
||||
fn usz! ByteReader.write_to(&self, OutStream writer) @dynamic
|
||||
{
|
||||
if (self.index >= self.bytes.len) return 0;
|
||||
usz written = writer.write(self.bytes[self.index..])!;
|
||||
|
||||
@@ -5,7 +5,7 @@ struct ByteWriter (OutStream)
|
||||
{
|
||||
char[] bytes;
|
||||
usz index;
|
||||
Allocator* allocator;
|
||||
Allocator allocator;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -14,23 +14,12 @@ struct ByteWriter (OutStream)
|
||||
* @require self.bytes.len == 0 "Init may not run on on already initialized data"
|
||||
* @ensure (bool)allocator, self.index == 0
|
||||
**/
|
||||
fn ByteWriter* ByteWriter.new_init(&self, Allocator* allocator = allocator::heap())
|
||||
fn ByteWriter* ByteWriter.new_init(&self, Allocator allocator = allocator::heap())
|
||||
{
|
||||
*self = { .bytes = {}, .allocator = allocator };
|
||||
return self;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param [&inout] self
|
||||
* @param [&inout] allocator
|
||||
* @require self.bytes.len == 0 "Init may not run on on already initialized data"
|
||||
* @ensure (bool)allocator, self.index == 0
|
||||
**/
|
||||
fn ByteWriter* ByteWriter.init_new(&self, Allocator* allocator = allocator::heap()) @deprecated("Replaced by new_init")
|
||||
{
|
||||
return self.new_init(allocator) @inline;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param [&inout] self
|
||||
* @require self.bytes.len == 0 "Init may not run on on already initialized data"
|
||||
@@ -41,16 +30,6 @@ fn ByteWriter* ByteWriter.temp_init(&self)
|
||||
return self.new_init(allocator::temp()) @inline;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param [&inout] self
|
||||
* @require self.bytes.len == 0 "Init may not run on on already initialized data"
|
||||
* @ensure self.index == 0
|
||||
**/
|
||||
fn ByteWriter* ByteWriter.init_temp(&self) @deprecated("Replaced by temp_init")
|
||||
{
|
||||
return self.temp_init() @inline;
|
||||
}
|
||||
|
||||
fn ByteWriter* ByteWriter.init_with_buffer(&self, char[] data)
|
||||
{
|
||||
*self = { .bytes = data, .allocator = null };
|
||||
@@ -97,7 +76,7 @@ fn void! ByteWriter.write_byte(&self, char c) @dynamic
|
||||
* @param [&inout] self
|
||||
* @param reader
|
||||
**/
|
||||
fn usz! ByteWriter.read_from(&self, InStream* reader) @dynamic
|
||||
fn usz! ByteWriter.read_from(&self, InStream reader) @dynamic
|
||||
{
|
||||
usz start_index = self.index;
|
||||
if (&reader.available)
|
||||
|
||||
@@ -2,7 +2,7 @@ module std::io;
|
||||
|
||||
struct LimitReader (InStream)
|
||||
{
|
||||
InStream* wrapped_stream;
|
||||
InStream wrapped_stream;
|
||||
usz limit;
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ struct LimitReader (InStream)
|
||||
* @param [&inout] wrapped_stream "The stream to read from"
|
||||
* @param limit "The max limit to read"
|
||||
**/
|
||||
fn LimitReader* LimitReader.init(&self, InStream* wrapped_stream, usz limit)
|
||||
fn LimitReader* LimitReader.init(&self, InStream wrapped_stream, usz limit)
|
||||
{
|
||||
*self = { .wrapped_stream = wrapped_stream, .limit = limit };
|
||||
return self;
|
||||
|
||||
@@ -2,7 +2,7 @@ module std::io;
|
||||
|
||||
struct Scanner (InStream)
|
||||
{
|
||||
InStream* wrapped_stream;
|
||||
InStream wrapped_stream;
|
||||
char[] buf;
|
||||
usz pattern_idx;
|
||||
usz read_idx;
|
||||
@@ -16,7 +16,7 @@ struct Scanner (InStream)
|
||||
* @param [&in] stream "The stream to read data from."
|
||||
* @require buffer.len > 0 "Non-empty buffer required."
|
||||
**/
|
||||
fn void Scanner.init(&self, InStream* stream, char[] buffer)
|
||||
fn void Scanner.init(&self, InStream stream, char[] buffer)
|
||||
{
|
||||
*self = { .wrapped_stream = stream, .buf = buffer };
|
||||
}
|
||||
|
||||
@@ -183,8 +183,8 @@ extern fn isz write(Fd fd, void* buffer, usz count) @if(!env::WIN32);
|
||||
|
||||
extern fn CFile fmemopen(void* ptr, usz size, ZString mode);
|
||||
extern fn isz getline(char** linep, usz* linecapp, CFile stream);
|
||||
extern fn int timespec_get(TimeSpec* ts, int base);
|
||||
extern fn int nanosleep(TimeSpec* req, TimeSpec* remaining);
|
||||
extern fn CInt timespec_get(TimeSpec* ts, CInt base);
|
||||
extern fn CInt nanosleep(TimeSpec* req, TimeSpec* remaining);
|
||||
extern fn ZString ctime(Time_t *timer);
|
||||
extern fn Time_t time(Time_t *timer);
|
||||
|
||||
@@ -360,6 +360,7 @@ const int EOF = -1;
|
||||
const int FOPEN_MAX = 20;
|
||||
const int FILENAME_MAX = 1024;
|
||||
|
||||
macro bool libc_S_ISTYPE(value, mask) @builtin => (value & S_IFMT) == mask;
|
||||
const S_IFMT = 0o170000; // type of file mask
|
||||
const S_IFIFO = 0o010000; // named pipe (fifo)
|
||||
const S_IFCHR = 0o020000; // character special
|
||||
|
||||
@@ -92,13 +92,11 @@ fault MatrixError
|
||||
|
||||
def Complexf = Complex(<float>);
|
||||
def Complex = Complex(<double>);
|
||||
def complexf_identity = complex::identity(<float>);
|
||||
def complex_identity = complex::identity(<double>);
|
||||
def COMPLEX_IDENTITY = complex::IDENTITY(<double>);
|
||||
def COMPLEXF_IDENTITY = complex::IDENTITY(<float>);
|
||||
|
||||
def Quaternionf = Quaternion(<float>);
|
||||
def Quaternion = Quaternion(<double>);
|
||||
def quaternionf_identity = quaternion::identity(<float>);
|
||||
def quaternion_identity = quaternion::identity(<double>);
|
||||
def QUATERNION_IDENTITY = quaternion::IDENTITY(<double>);
|
||||
def QUATERNIONF_IDENTITY = quaternion::IDENTITY(<float>);
|
||||
|
||||
@@ -120,6 +118,14 @@ def MATRIX3F_IDENTITY = matrix::IDENTITY3(<float>);
|
||||
def MATRIX4_IDENTITY = matrix::IDENTITY4(<double>);
|
||||
def MATRIX4F_IDENTITY = matrix::IDENTITY4(<float>);
|
||||
|
||||
|
||||
/**
|
||||
* @require types::is_numerical($typeof(x)) `The input must be a numerical value or numerical vector`
|
||||
**/
|
||||
macro deg_to_rad(x) {
|
||||
return x * PI / 180;
|
||||
}
|
||||
|
||||
/**
|
||||
* @require types::is_numerical($typeof(x)) `The input must be a numerical value or numerical vector`
|
||||
**/
|
||||
@@ -260,7 +266,7 @@ macro clamp(x, lower, upper) => $$max(($typeof(x))lower, $$min(x, ($typeof(x))up
|
||||
* @require values::@is_promotable_to_floatlike(mag) `The input must be a number value or float vector`
|
||||
* @require values::@assign_to(sgn, values::promote_int(mag))
|
||||
**/
|
||||
macro copysign(mag, sgn) => $$copysign(values::promote_int(mag), ($typeof(values::promote_int(mag)))sgn);
|
||||
macro copysign(mag, sgn) => $$copysign(values::promote_int_same(mag, sgn), ($typeof(values::promote_int_same(mag, sgn)))sgn);
|
||||
|
||||
/**
|
||||
* @require values::@is_promotable_to_floatlike(x) `The input must be a number value or float vector`
|
||||
@@ -333,7 +339,10 @@ macro ln(x) => $$log(values::promote_int(x));
|
||||
* @require values::@is_promotable_to_floatlike(x) `The input must be a number or a float vector`
|
||||
* @require values::@is_promotable_to_floatlike(base) `The base must be a number or a float vector`
|
||||
**/
|
||||
macro log(x, base) => $$log(values::promote_int(x)) / $$log(values::promote_int(base));
|
||||
macro log(x, base)
|
||||
{
|
||||
return $$log(values::promote_int_same(x, base)) / $$log(values::promote_int_same(base, x));
|
||||
}
|
||||
|
||||
/**
|
||||
* @require values::@is_promotable_to_floatlike(x) `The input must be a number or a float vector`
|
||||
@@ -392,14 +401,14 @@ macro nearbyint(x) => $$nearbyint(x);
|
||||
|
||||
/**
|
||||
* @require values::@is_promotable_to_floatlike(x) `The input must be a number or a float vector`
|
||||
* @require $assignable(exp, $typeof(x)) || values::@is_int(exp) `The input must be an integer, castable to the type of x`
|
||||
* @require $assignable(exp, $typeof(values::promote_int(x))) || values::@is_int(exp) `The input must be an integer, castable to the type of x`
|
||||
**/
|
||||
macro pow(x, exp)
|
||||
{
|
||||
$if types::is_floatlike($typeof(exp)):
|
||||
return $$pow(x, ($typeof(x))exp);
|
||||
return $$pow(values::promote_int_same(x, exp), ($typeof(values::promote_int_same(x, exp)))exp);
|
||||
$else
|
||||
return $$pow_int(x, exp);
|
||||
return $$pow_int(values::promote_int(x), exp);
|
||||
$endif
|
||||
}
|
||||
|
||||
@@ -441,6 +450,16 @@ macro rint(x) => $$rint(x);
|
||||
**/
|
||||
macro round(x) => $$round(x);
|
||||
|
||||
|
||||
/**
|
||||
* @require values::@is_floatlike(x) `The input must be a floating point value or float vector`
|
||||
**/
|
||||
macro round_to_decimals(x, int decimal_places)
|
||||
{
|
||||
var div = $$pow_int(($typeof(x))10, decimal_places);
|
||||
return round(div * x) / div;
|
||||
}
|
||||
|
||||
/**
|
||||
* @require values::@is_floatlike(x) `The input must be a floating point value or float vector`
|
||||
**/
|
||||
@@ -674,6 +693,7 @@ macro ichar ichar[<*>].or(ichar[<*>] x) => $$reduce_or(x);
|
||||
macro ichar ichar[<*>].xor(ichar[<*>] x) => $$reduce_xor(x);
|
||||
macro ichar ichar[<*>].max(ichar[<*>] x) => $$reduce_max(x);
|
||||
macro ichar ichar[<*>].min(ichar[<*>] x) => $$reduce_min(x);
|
||||
macro ichar ichar[<*>].dot(ichar[<*>] x, ichar[<*>] y) => (x * y).sum();
|
||||
|
||||
macro bool[<*>] short[<*>].comp_lt(short[<*>] x, short[<*>] y) => $$veccomplt(x, y);
|
||||
macro bool[<*>] short[<*>].comp_le(short[<*>] x, short[<*>] y) => $$veccomple(x, y);
|
||||
@@ -689,6 +709,7 @@ macro short short[<*>].or(short[<*>] x) => $$reduce_or(x);
|
||||
macro short short[<*>].xor(short[<*>] x) => $$reduce_xor(x);
|
||||
macro short short[<*>].max(short[<*>] x) => $$reduce_max(x);
|
||||
macro short short[<*>].min(short[<*>] x) => $$reduce_min(x);
|
||||
macro short short[<*>].dot(short[<*>] x, short[<*>] y) => (x * y).sum();
|
||||
|
||||
macro bool[<*>] int[<*>].comp_lt(int[<*>] x, int[<*>] y) => $$veccomplt(x, y);
|
||||
macro bool[<*>] int[<*>].comp_le(int[<*>] x, int[<*>] y) => $$veccomple(x, y);
|
||||
@@ -704,6 +725,7 @@ macro int int[<*>].or(int[<*>] x) => $$reduce_or(x);
|
||||
macro int int[<*>].xor(int[<*>] x) => $$reduce_xor(x);
|
||||
macro int int[<*>].max(int[<*>] x) => $$reduce_max(x);
|
||||
macro int int[<*>].min(int[<*>] x) => $$reduce_min(x);
|
||||
macro int int[<*>].dot(int[<*>] x, int[<*>] y) => (x * y).sum();
|
||||
|
||||
macro bool[<*>] long[<*>].comp_lt(long[<*>] x, long[<*>] y) => $$veccomplt(x, y);
|
||||
macro bool[<*>] long[<*>].comp_le(long[<*>] x, long[<*>] y) => $$veccomple(x, y);
|
||||
@@ -718,6 +740,7 @@ macro long long[<*>].or(long[<*>] x) => $$reduce_or(x);
|
||||
macro long long[<*>].xor(long[<*>] x) => $$reduce_xor(x);
|
||||
macro long long[<*>].max(long[<*>] x) => $$reduce_max(x);
|
||||
macro long long[<*>].min(long[<*>] x) => $$reduce_min(x);
|
||||
macro long long[<*>].dot(long[<*>] x, long[<*>] y) => (x * y).sum();
|
||||
|
||||
macro bool[<*>] int128[<*>].comp_lt(int128[<*>] x, int128[<*>] y) => $$veccomplt(x, y);
|
||||
macro bool[<*>] int128[<*>].comp_le(int128[<*>] x, int128[<*>] y) => $$veccomple(x, y);
|
||||
@@ -732,6 +755,7 @@ macro int128 int128[<*>].or(int128[<*>] x) => $$reduce_or(x);
|
||||
macro int128 int128[<*>].xor(int128[<*>] x) => $$reduce_xor(x);
|
||||
macro int128 int128[<*>].max(int128[<*>] x) => $$reduce_max(x);
|
||||
macro int128 int128[<*>].min(int128[<*>] x) => $$reduce_min(x);
|
||||
macro int128 int128[<*>].dot(int128[<*>] x, int128[<*>] y) => (x * y).sum();
|
||||
|
||||
macro bool[<*>] bool[<*>].comp_lt(bool[<*>] x, bool[<*>] y) => $$veccomplt(x, y);
|
||||
macro bool[<*>] bool[<*>].comp_le(bool[<*>] x, bool[<*>] y) => $$veccomple(x, y);
|
||||
@@ -762,6 +786,7 @@ macro char char[<*>].or(char[<*>] x) => $$reduce_or(x);
|
||||
macro char char[<*>].xor(char[<*>] x) => $$reduce_xor(x);
|
||||
macro char char[<*>].max(char[<*>] x) => $$reduce_max(x);
|
||||
macro char char[<*>].min(char[<*>] x) => $$reduce_min(x);
|
||||
macro char char[<*>].dot(char[<*>] x, char[<*>] y) => (x * y).sum();
|
||||
|
||||
macro bool[<*>] ushort[<*>].comp_lt(ushort[<*>] x, ushort[<*>] y) => $$veccomplt(x, y);
|
||||
macro bool[<*>] ushort[<*>].comp_le(ushort[<*>] x, ushort[<*>] y) => $$veccomple(x, y);
|
||||
@@ -777,6 +802,7 @@ macro ushort ushort[<*>].or(ushort[<*>] x) => $$reduce_or(x);
|
||||
macro ushort ushort[<*>].xor(ushort[<*>] x) => $$reduce_xor(x);
|
||||
macro ushort ushort[<*>].max(ushort[<*>] x) => $$reduce_max(x);
|
||||
macro ushort ushort[<*>].min(ushort[<*>] x) => $$reduce_min(x);
|
||||
macro ushort ushort[<*>].dot(ushort[<*>] x, ushort[<*>] y) => (x * y).sum();
|
||||
|
||||
macro bool[<*>] uint[<*>].comp_lt(uint[<*>] x, uint[<*>] y) => $$veccomplt(x, y);
|
||||
macro bool[<*>] uint[<*>].comp_le(uint[<*>] x, uint[<*>] y) => $$veccomple(x, y);
|
||||
@@ -792,6 +818,7 @@ macro uint uint[<*>].or(uint[<*>] x) => $$reduce_or(x);
|
||||
macro uint uint[<*>].xor(uint[<*>] x) => $$reduce_xor(x);
|
||||
macro uint uint[<*>].max(uint[<*>] x) => $$reduce_max(x);
|
||||
macro uint uint[<*>].min(uint[<*>] x) => $$reduce_min(x);
|
||||
macro uint uint[<*>].dot(uint[<*>] x, uint[<*>] y) => (x * y).sum();
|
||||
|
||||
macro bool[<*>] ulong[<*>].comp_lt(ulong[<*>] x, ulong[<*>] y) => $$veccomplt(x, y);
|
||||
macro bool[<*>] ulong[<*>].comp_le(ulong[<*>] x, ulong[<*>] y) => $$veccomple(x, y);
|
||||
@@ -807,6 +834,7 @@ macro ulong ulong[<*>].or(ulong[<*>] x) => $$reduce_or(x);
|
||||
macro ulong ulong[<*>].xor(ulong[<*>] x) => $$reduce_xor(x);
|
||||
macro ulong ulong[<*>].max(ulong[<*>] x) => $$reduce_max(x);
|
||||
macro ulong ulong[<*>].min(ulong[<*>] x) => $$reduce_min(x);
|
||||
macro ulong ulong[<*>].dot(ulong[<*>] x, ulong[<*>] y) => (x * y).sum();
|
||||
|
||||
macro bool[<*>] uint128[<*>].comp_lt(uint128[<*>] x, uint128[<*>] y) => $$veccomplt(x, y);
|
||||
macro bool[<*>] uint128[<*>].comp_le(uint128[<*>] x, uint128[<*>] y) => $$veccomple(x, y);
|
||||
@@ -822,6 +850,7 @@ macro uint128 uint128[<*>].or(uint128[<*>] x) => $$reduce_or(x);
|
||||
macro uint128 uint128[<*>].xor(uint128[<*>] x) => $$reduce_xor(x);
|
||||
macro uint128 uint128[<*>].max(uint128[<*>] x) => $$reduce_max(x);
|
||||
macro uint128 uint128[<*>].min(uint128[<*>] x) => $$reduce_min(x);
|
||||
macro uint128 uint128[<*>].dot(uint128[<*>] x, uint128[<*>] y) => (x * y).sum();
|
||||
|
||||
macro char char.sat_add(char x, char y) => $$sat_add(x, y);
|
||||
macro char char.sat_sub(char x, char y) => $$sat_sub(x, y);
|
||||
|
||||
@@ -10,7 +10,7 @@ union Complex
|
||||
}
|
||||
|
||||
|
||||
macro Complex identity() => { 1, 0 };
|
||||
const Complex IDENTITY = { 1, 0 };
|
||||
macro Complex Complex.add(self, Complex b) => Complex { .v = self.v + b.v };
|
||||
macro Complex Complex.add_each(self, Real b) => Complex { .v = self.v + b };
|
||||
macro Complex Complex.sub(self, Complex b) => Complex { .v = self.v - b.v };
|
||||
|
||||
@@ -60,7 +60,7 @@ fn double __tan(double x, double y, int odd) @extern("__tan") @weak @nostrip
|
||||
w = x + r;
|
||||
if (big)
|
||||
{
|
||||
s = 1 - 2 * odd;
|
||||
s = (double)(1 - 2 * odd);
|
||||
v = s - 2.0 * (x + (r - w*w/(w + s)));
|
||||
return sign ? -v : v;
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ fn int __rem_pio2f(float x, double *y)
|
||||
if (ix >= 0x7f800000)
|
||||
{
|
||||
// x is inf or NaN */
|
||||
*y = x-x;
|
||||
*y = x - (double)x;
|
||||
return 0;
|
||||
}
|
||||
/* scale x into [2^23, 2^24-1] */
|
||||
|
||||
@@ -9,8 +9,8 @@ union Quaternion
|
||||
Real[<4>] v;
|
||||
}
|
||||
|
||||
macro Quaternion identity() => { 0, 0, 0, 1 };
|
||||
const Quaternion IDENTITY = { 0, 0, 0, 1 };
|
||||
|
||||
macro Quaternion Quaternion.add(Quaternion a, Quaternion b) => Quaternion { .v = a.v + b.v };
|
||||
macro Quaternion Quaternion.add_each(Quaternion a, Real b) => Quaternion { .v = a.v + b };
|
||||
macro Quaternion Quaternion.sub(Quaternion a, Quaternion b) => Quaternion { .v = a.v - b.v };
|
||||
@@ -70,33 +70,16 @@ fn Quaternion Quaternion.mul(a, Quaternion b)
|
||||
|
||||
macro into_matrix(Quaternion* q, $Type) @private
|
||||
{
|
||||
Quaternion q_norm = q.normalize();
|
||||
Quaternion rotation = q.normalize();
|
||||
var x = rotation.i;
|
||||
var y = rotation.j;
|
||||
var z = rotation.k;
|
||||
var w = rotation.l;
|
||||
|
||||
var x = q_norm.i;
|
||||
var y = q_norm.j;
|
||||
var z = q_norm.k;
|
||||
var w = q_norm.l;
|
||||
|
||||
var xx = x + x;
|
||||
var yy = y + y;
|
||||
var zz = z + z;
|
||||
|
||||
var xxx = xx * x;
|
||||
var xxy = xx * y;
|
||||
var xxz = xx * z;
|
||||
|
||||
var yyy = yy * y;
|
||||
var yyz = yy * z;
|
||||
var zzz = zz * z;
|
||||
|
||||
var yyw = yy * w;
|
||||
var zzw = zz * w;
|
||||
var xxw = xx * w;
|
||||
|
||||
return $Type {
|
||||
1 - yyy - zzz, xxy - zzw, xxz + yyw, 0,
|
||||
xxy + zzw, 1 - xxx - zzz, yyz - xxw, 0,
|
||||
xxz - yyw, yyz + zzw, 1.0 - xxx - yyy, 0,
|
||||
0, 0, 0, 1,
|
||||
};
|
||||
return $Type {
|
||||
1 - 2*y*y - 2*z*z, 2*x*y - 2*z*w, 2*x*z + 2*y*w, 0,
|
||||
2*x*y + 2*z*w, 1 - 2*x*x - 2*z*z, 2*y*z - 2*x*w, 0,
|
||||
2*x*z - 2*y*w, 2*y*z + 2*x*w , 1 - 2*x*x - 2*y*y, 0,
|
||||
0.0, 0.0, 0.0, 1.0,
|
||||
};
|
||||
}
|
||||
@@ -48,7 +48,7 @@ fn int rand(int max) @builtin
|
||||
/**
|
||||
* @require is_random(random)
|
||||
**/
|
||||
macro void next_bool(random)
|
||||
macro bool next_bool(random)
|
||||
{
|
||||
return random.next_byte() & 1;
|
||||
}
|
||||
@@ -94,4 +94,4 @@ macro @random_value_to_bytes(#function, char[] bytes)
|
||||
bytes = bytes[$byte_size..];
|
||||
}
|
||||
unreachable();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,7 +206,7 @@ macro matrix_look_at($Type, eye, target, up) @private
|
||||
vx[0], vx[1], vx[2], - vx.dot(eye),
|
||||
vy[0], vy[1], vy[2], - vy.dot(eye),
|
||||
vz[0], vz[1], vz[2], - vz.dot(eye),
|
||||
0, 0, 0, 1
|
||||
0.0, 0.0, 0.0, 1
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -4,9 +4,9 @@ import std::ascii;
|
||||
|
||||
enum IpProtocol : char (AIFamily ai_family)
|
||||
{
|
||||
UNSPECIFIED (os::AF_UNSPEC),
|
||||
IPV4 (os::AF_INET),
|
||||
IPV6 (os::AF_INET6),
|
||||
UNSPECIFIED = os::AF_UNSPEC,
|
||||
IPV4 = os::AF_INET,
|
||||
IPV6 = os::AF_INET6,
|
||||
}
|
||||
|
||||
struct InetAddress (Printable)
|
||||
@@ -56,7 +56,7 @@ fn usz! InetAddress.to_format(InetAddress* addr, Formatter* formatter) @dynamic
|
||||
return formatter.printf("%d.%d.%d.%d", addr.ipv4.a, addr.ipv4.b, addr.ipv4.c, addr.ipv4.d)!;
|
||||
}
|
||||
|
||||
fn String InetAddress.to_new_string(InetAddress* addr, Allocator* allocator = allocator::heap()) @dynamic
|
||||
fn String InetAddress.to_new_string(InetAddress* addr, Allocator allocator = allocator::heap()) @dynamic
|
||||
{
|
||||
if (addr.is_ipv6)
|
||||
{
|
||||
|
||||
@@ -57,7 +57,7 @@ fn uint! ipv4toint(String s)
|
||||
return out;
|
||||
}
|
||||
|
||||
fn String! int_to_new_ipv4(uint val, Allocator* allocator = allocator::heap())
|
||||
fn String! int_to_new_ipv4(uint val, Allocator allocator = allocator::heap())
|
||||
{
|
||||
char[3 * 4 + 3 + 1] buffer;
|
||||
String res = (String)io::bprintf(&buffer, "%d.%d.%d.%d", val >> 24, (val >> 16) & 0xFF, (val >> 8) & 0xFF, val & 0xFF)!;
|
||||
|
||||
@@ -83,12 +83,12 @@ macro Socket new_socket(fd, ai)
|
||||
|
||||
enum SocketOption : char (CInt value)
|
||||
{
|
||||
REUSEADDR (os::SO_REUSEADDR),
|
||||
REUSEPORT (os::SO_REUSEPORT) @if(!env::WIN32),
|
||||
KEEPALIVE (os::SO_KEEPALIVE),
|
||||
BROADCAST (os::SO_BROADCAST),
|
||||
OOBINLINE (os::SO_OOBINLINE),
|
||||
DONTROUTE (os::SO_DONTROUTE),
|
||||
REUSEADDR = os::SO_REUSEADDR,
|
||||
REUSEPORT @if(!env::WIN32) = os::SO_REUSEPORT,
|
||||
KEEPALIVE = os::SO_KEEPALIVE,
|
||||
BROADCAST = os::SO_BROADCAST,
|
||||
OOBINLINE = os::SO_OOBINLINE,
|
||||
DONTROUTE = os::SO_DONTROUTE,
|
||||
}
|
||||
|
||||
fn bool! Socket.get_broadcast(&self) => self.get_option(BROADCAST);
|
||||
|
||||
@@ -10,7 +10,7 @@ fault BacktraceFault
|
||||
RESOLUTION_FAILED,
|
||||
}
|
||||
|
||||
const Backtrace BACKTRACE_UNKNOWN = { 0, "", "", "", 0, null };
|
||||
const Backtrace BACKTRACE_UNKNOWN = { 0, "", "", "", 0, null, false };
|
||||
|
||||
struct Backtrace (Printable)
|
||||
{
|
||||
@@ -19,7 +19,8 @@ struct Backtrace (Printable)
|
||||
String object_file;
|
||||
String file;
|
||||
uint line;
|
||||
Allocator* allocator;
|
||||
Allocator allocator;
|
||||
bool is_inline;
|
||||
}
|
||||
|
||||
|
||||
@@ -35,15 +36,16 @@ fn bool Backtrace.is_unknown(&self)
|
||||
|
||||
fn usz! Backtrace.to_format(&self, Formatter* formatter) @dynamic
|
||||
{
|
||||
String inline_suffix = self.is_inline ? " [inline]" : "";
|
||||
if (self.has_file())
|
||||
{
|
||||
return formatter.printf("%s (in %s) (%s:%d)", self.function, self.object_file, self.file, self.line);
|
||||
return formatter.printf("%s (in %s) (%s:%d)%s", self.function, self.object_file, self.file, self.line, inline_suffix);
|
||||
}
|
||||
if (self.is_unknown())
|
||||
{
|
||||
return formatter.printf("??? (in unknown)");
|
||||
return formatter.printf("??? (in unknown)%s", inline_suffix);
|
||||
}
|
||||
return formatter.printf("%s (in %s) (source unavailable)", self.function, self.object_file);
|
||||
return formatter.printf("%s (in %s) (source unavailable)%s", self.function, self.object_file, inline_suffix);
|
||||
}
|
||||
fn void Backtrace.free(&self)
|
||||
{
|
||||
@@ -53,7 +55,7 @@ fn void Backtrace.free(&self)
|
||||
allocator::free(self.allocator, self.file);
|
||||
}
|
||||
|
||||
fn Backtrace* Backtrace.init(&self, uptr offset, String function, String object_file, String file = "", uint line = 0, Allocator* allocator)
|
||||
fn Backtrace* Backtrace.init(&self, uptr offset, String function, String object_file, String file = "", uint line = 0, Allocator allocator)
|
||||
{
|
||||
if (!allocator)
|
||||
{
|
||||
@@ -95,7 +97,7 @@ def symbolize_backtrace = linux::symbolize_backtrace @if(env::LINUX);
|
||||
def symbolize_backtrace = win32::symbolize_backtrace @if(env::WIN32);
|
||||
def symbolize_backtrace = darwin::symbolize_backtrace @if(env::DARWIN);
|
||||
|
||||
fn BacktraceList! symbolize_backtrace(void*[] backtrace, Allocator* allocator) @if(!env::NATIVE_STACKTRACE)
|
||||
fn BacktraceList! symbolize_backtrace(void*[] backtrace, Allocator allocator) @if(!env::NATIVE_STACKTRACE)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
@@ -9,7 +9,7 @@ import std::io::path, libc, std::os;
|
||||
* @require name.len > 0
|
||||
* @return! SearchResult.MISSING
|
||||
**/
|
||||
fn String! get_var(String name, Allocator* allocator = allocator::heap())
|
||||
fn String! get_var(String name, Allocator allocator = allocator::heap())
|
||||
{
|
||||
@pool(allocator)
|
||||
{
|
||||
@@ -72,7 +72,7 @@ fn bool set_var(String name, String value, bool overwrite = true)
|
||||
/**
|
||||
* Returns the current user's home directory.
|
||||
**/
|
||||
fn String! get_home_dir(Allocator* using = allocator::heap())
|
||||
fn String! get_home_dir(Allocator using = allocator::heap())
|
||||
{
|
||||
String home;
|
||||
$if !env::WIN32:
|
||||
@@ -86,7 +86,7 @@ fn String! get_home_dir(Allocator* using = allocator::heap())
|
||||
/**
|
||||
* Returns the current user's config directory.
|
||||
**/
|
||||
fn Path! get_config_dir(Allocator* allocator = allocator::heap())
|
||||
fn Path! get_config_dir(Allocator allocator = allocator::heap())
|
||||
{
|
||||
@pool(allocator)
|
||||
{
|
||||
@@ -126,7 +126,7 @@ fn bool clear_var(String name)
|
||||
};
|
||||
}
|
||||
|
||||
fn String! executable_path(Allocator *allocator = allocator::heap())
|
||||
fn String! executable_path(Allocator allocator = allocator::heap())
|
||||
{
|
||||
$if env::DARWIN:
|
||||
return darwin::executable_path(allocator);
|
||||
|
||||
2
lib/std/os/linux/heap.c3
Normal file
2
lib/std/os/linux/heap.c3
Normal file
@@ -0,0 +1,2 @@
|
||||
module std::os::linux @if(env::LINUX);
|
||||
extern fn usz malloc_usable_size(void* ptr);
|
||||
@@ -128,17 +128,17 @@ fn ulong! elf_module_image_base(String path) @local
|
||||
return 0;
|
||||
}
|
||||
|
||||
fn Backtrace! backtrace_load_from_exec(void* addr, Allocator* allocator) @local
|
||||
fn void! backtrace_add_from_exec(BacktraceList* list, void* addr, Allocator allocator) @local
|
||||
{
|
||||
char[] buf = mem::temp_alloc_array(char, 1024);
|
||||
|
||||
String exec_path = process::execute_stdout_to_buffer(buf, {"realpath", "-e", string::tformat("/proc/%d/exe", posix::getpid())})!;
|
||||
String obj_name = exec_path.copy(allocator);
|
||||
String addr2line = process::execute_stdout_to_buffer(buf, {"addr2line", "-p", "-i", "-C", "-f", "-e", exec_path, string::tformat("0x%x", addr)})!;
|
||||
return backtrace_from_addr2line(addr, addr2line, obj_name, "???", allocator);
|
||||
return backtrace_add_addr2line(list, addr, addr2line, obj_name, "???", allocator);
|
||||
}
|
||||
|
||||
fn Backtrace! backtrace_load_from_dlinfo(void* addr, Linux_Dl_info* info, Allocator* allocator) @local
|
||||
fn void! backtrace_add_from_dlinfo(BacktraceList* list, void* addr, Linux_Dl_info* info, Allocator allocator) @local
|
||||
{
|
||||
char[] buf = mem::temp_alloc_array(char, 1024);
|
||||
|
||||
@@ -146,30 +146,19 @@ fn Backtrace! backtrace_load_from_dlinfo(void* addr, Linux_Dl_info* info, Alloca
|
||||
ZString obj_path = info.dli_fname;
|
||||
String sname = info.dli_sname ? info.dli_sname.str_view() : "???";
|
||||
String addr2line = process::execute_stdout_to_buffer(buf, {"addr2line", "-p", "-i", "-C", "-f", "-e", obj_path.str_view(), string::tformat("0x%x", obj_addr - 1)})!;
|
||||
return backtrace_from_addr2line(addr, addr2line, info.dli_fname.str_view(), sname, allocator);
|
||||
return backtrace_add_addr2line(list, addr, addr2line, info.dli_fname.str_view(), sname, allocator);
|
||||
}
|
||||
|
||||
fn Backtrace! backtrace_from_addr2line(void* addr, String addr2line, String obj_name, String func_name, Allocator* allocator) @local
|
||||
fn Backtrace! backtrace_line_parse(String string, String obj_name, String func_name, bool is_inlined, Allocator allocator)
|
||||
{
|
||||
String[] parts = addr2line.tsplit(" at ");
|
||||
if (parts.len != 2)
|
||||
{
|
||||
return {
|
||||
.function = func_name.copy(allocator),
|
||||
.object_file = obj_name.copy(allocator),
|
||||
.offset = (uptr)addr,
|
||||
.file = "".copy(allocator),
|
||||
.line = 0,
|
||||
.allocator = allocator
|
||||
};
|
||||
}
|
||||
String[] parts = string.trim().tsplit(" at ");
|
||||
if (parts.len != 2) return SearchResult.MISSING?;
|
||||
|
||||
uint line = 0;
|
||||
uint line = 0;
|
||||
String source = "";
|
||||
if (!parts[1].contains("?") && parts[1].contains(":"))
|
||||
{
|
||||
usz index = parts[1].rindex_of_char(':')!;
|
||||
|
||||
source = parts[1][:index];
|
||||
line = parts[1][index + 1..].to_uint()!;
|
||||
}
|
||||
@@ -179,25 +168,53 @@ fn Backtrace! backtrace_from_addr2line(void* addr, String addr2line, String obj_
|
||||
.file = source.copy(allocator),
|
||||
.line = line,
|
||||
.allocator = allocator,
|
||||
.is_inline = is_inlined
|
||||
};
|
||||
}
|
||||
|
||||
fn Backtrace! backtrace_load_element(void* addr, Allocator* allocator = allocator::heap()) @local
|
||||
fn void! backtrace_add_addr2line(BacktraceList* list, void* addr, String addr2line, String obj_name, String func_name, Allocator allocator) @local
|
||||
{
|
||||
if (!addr) return backtrace::BACKTRACE_UNKNOWN;
|
||||
String[] inline_parts = addr2line.tsplit("(inlined by)");
|
||||
usz last = inline_parts.len - 1;
|
||||
foreach (i, part : inline_parts)
|
||||
{
|
||||
bool is_inline = i != last;
|
||||
Backtrace! trace = backtrace_line_parse(part, obj_name, func_name, is_inline, allocator);
|
||||
if (catch trace)
|
||||
{
|
||||
list.push({
|
||||
.function = func_name.copy(allocator),
|
||||
.object_file = obj_name.copy(allocator),
|
||||
.offset = (uptr)addr,
|
||||
.file = "".copy(allocator),
|
||||
.line = 0,
|
||||
.allocator = allocator,
|
||||
.is_inline = is_inline
|
||||
});
|
||||
continue;
|
||||
}
|
||||
list.push(trace);
|
||||
}
|
||||
}
|
||||
|
||||
fn void! backtrace_add_element(BacktraceList *list, void* addr, Allocator allocator = allocator::heap()) @local
|
||||
{
|
||||
if (!addr)
|
||||
{
|
||||
list.push(backtrace::BACKTRACE_UNKNOWN);
|
||||
return;
|
||||
}
|
||||
|
||||
@pool(allocator) {
|
||||
Linux_Dl_info info;
|
||||
|
||||
if (dladdr(addr, &info) == 0)
|
||||
{
|
||||
return backtrace_load_from_exec(addr, allocator);
|
||||
return backtrace_add_from_exec(list, addr, allocator);
|
||||
}
|
||||
return backtrace_load_from_dlinfo(addr, &info, allocator);
|
||||
return backtrace_add_from_dlinfo(list, addr, &info, allocator);
|
||||
};
|
||||
}
|
||||
|
||||
fn BacktraceList! symbolize_backtrace(void*[] backtrace, Allocator* allocator)
|
||||
fn BacktraceList! symbolize_backtrace(void*[] backtrace, Allocator allocator)
|
||||
{
|
||||
BacktraceList list;
|
||||
list.new_init(backtrace.len, allocator);
|
||||
@@ -213,8 +230,7 @@ fn BacktraceList! symbolize_backtrace(void*[] backtrace, Allocator* allocator)
|
||||
{
|
||||
foreach (addr : backtrace)
|
||||
{
|
||||
Backtrace trace = backtrace_load_element(addr, allocator)!;
|
||||
list.append(trace);
|
||||
backtrace_add_element(&list, addr, allocator)!;
|
||||
}
|
||||
};
|
||||
return list;
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
module std::os::macos::cf @if(env::DARWIN);
|
||||
module std::os::macos::cf @if(env::DARWIN) @link(env::DARWIN, "CoreFoundation.framework");
|
||||
|
||||
distinct CFAllocatorRef = void*;
|
||||
distinct CFAllocatorContextRef = void*;
|
||||
def CFOptionFlags = usz;
|
||||
|
||||
macro CFAllocatorRef default_allocator() => _macos_CFAllocatorGetDefault();
|
||||
macro void CFAllocatorRef.dealloc(CFAllocatorRef allocator, void* ptr) => _macos_CFAllocatorDeallocate(allocator, ptr);
|
||||
macro void* CFAllocatorRef.alloc(CFAllocatorRef allocator, usz size) => _macos_CFAllocatorAllocate(allocator, size, 0);
|
||||
macro usz CFAllocatorRef.get_preferred_size(CFAllocatorRef allocator, usz req_size) => _macos_CFAllocatorGetPreferredSizeForSize(allocator, req_size, 0);
|
||||
macro void CFAllocatorRef.set_default(CFAllocatorRef allocator) => _macos_CFAllocatorSetDefault(allocator);
|
||||
macro CFAllocatorRef default_allocator() => macos_CFAllocatorGetDefault();
|
||||
macro void CFAllocatorRef.dealloc(CFAllocatorRef allocator, void* ptr) => macos_CFAllocatorDeallocate(allocator, ptr);
|
||||
macro void* CFAllocatorRef.alloc(CFAllocatorRef allocator, usz size) => macos_CFAllocatorAllocate(allocator, size, 0);
|
||||
macro usz CFAllocatorRef.get_preferred_size(CFAllocatorRef allocator, usz req_size) => macos_CFAllocatorGetPreferredSizeForSize(allocator, req_size, 0);
|
||||
macro void CFAllocatorRef.set_default(CFAllocatorRef allocator) => macos_CFAllocatorSetDefault(allocator);
|
||||
|
||||
extern fn CFAllocatorRef _macos_CFAllocatorCreate(CFAllocatorRef allocator, CFAllocatorContextRef context) @extern("CFAllocatorCreate");
|
||||
extern fn void _macos_CFAllocatorDeallocate(CFAllocatorRef allocator, void* ptr) @extern("CFAllocatorDeallocate");
|
||||
extern fn CFAllocatorRef _macos_CFAllocatorGetDefault() @extern("CFAllocatorGetDefault");
|
||||
extern fn void _macos_CFAllocatorSetDefault(CFAllocatorRef allocator) @extern("CFAllocatorSetDefault");
|
||||
extern fn void* _macos_CFAllocatorAllocate(CFAllocatorRef allocator, CFIndex size, CFOptionFlags hint) @extern("CFAllocatorAllocate");
|
||||
extern fn CFIndex _macos_CFAllocatorGetPreferredSizeForSize(CFAllocatorRef allocator, CFIndex size, CFOptionFlags hint) @extern("CFAllocatorGetPreferredSizeForSize");
|
||||
extern fn CFAllocatorRef macos_CFAllocatorCreate(CFAllocatorRef allocator, CFAllocatorContextRef context) @extern("CFAllocatorCreate") @builtin;
|
||||
extern fn void macos_CFAllocatorDeallocate(CFAllocatorRef allocator, void* ptr) @extern("CFAllocatorDeallocate") @builtin;
|
||||
extern fn CFAllocatorRef macos_CFAllocatorGetDefault() @extern("CFAllocatorGetDefault") @builtin;
|
||||
extern fn void macos_CFAllocatorSetDefault(CFAllocatorRef allocator) @extern("CFAllocatorSetDefault") @builtin;
|
||||
extern fn void* macos_CFAllocatorAllocate(CFAllocatorRef allocator, CFIndex size, CFOptionFlags hint) @extern("CFAllocatorAllocate") @builtin;
|
||||
extern fn CFIndex macos_CFAllocatorGetPreferredSizeForSize(CFAllocatorRef allocator, CFIndex size, CFOptionFlags hint) @extern("CFAllocatorGetPreferredSizeForSize") @builtin;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
module std::os::macos::cf @if(env::DARWIN);
|
||||
module std::os::macos::cf @if(env::DARWIN) @link(env::DARWIN, "CoreFoundation.framework");
|
||||
|
||||
distinct CFArrayRef = void*;
|
||||
distinct CFArrayCallBacksRef = void*;
|
||||
distinct CFMutableArrayRef = void*;
|
||||
extern fn CFArrayRef _macos_CFArrayCreate(CFAllocatorRef allocator, void** values, CFIndex num_values, CFArrayCallBacksRef callBacks) @extern("CFArrayCreate");
|
||||
extern fn CFArrayRef _macos_CFArrayCopy(CFAllocatorRef allocator, CFArrayRef array) @extern("CFArrayCopy");
|
||||
extern fn CFIndex _macos_CFArrayGetCount(CFArrayRef array) @extern("CFArrayGetCount");
|
||||
extern fn void _macos_CFArrayAppendArray(CFMutableArrayRef theArray, CFArrayRef otherArray, CFRange otherRange) @extern("CFArrayAppendArray");
|
||||
extern fn CFMutableArrayRef _macos_CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, CFArrayCallBacksRef callBacks) @extern("CFArrayCreateMutable");
|
||||
extern fn void _macos_CFArrayAppendValue(CFMutableArrayRef theArray, void *value) @extern("CFArrayAppendValue");
|
||||
extern fn CFArrayRef macos_CFArrayCreate(CFAllocatorRef allocator, void** values, CFIndex num_values, CFArrayCallBacksRef callBacks) @extern("CFArrayCreate") @builtin;
|
||||
extern fn CFArrayRef macos_CFArrayCopy(CFAllocatorRef allocator, CFArrayRef array) @extern("CFArrayCopy") @builtin;
|
||||
extern fn CFIndex macos_CFArrayGetCount(CFArrayRef array) @extern("CFArrayGetCount") @builtin;
|
||||
extern fn void macos_CFArrayAppendArray(CFMutableArrayRef theArray, CFArrayRef otherArray, CFRange otherRange) @extern("CFArrayAppendArray") @builtin;
|
||||
extern fn CFMutableArrayRef macos_CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, CFArrayCallBacksRef callBacks) @extern("CFArrayCreateMutable") @builtin;
|
||||
extern fn void macos_CFArrayAppendValue(CFMutableArrayRef theArray, void *value) @extern("CFArrayAppendValue") @builtin;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
module std::os::macos::cf @if(env::DARWIN);
|
||||
module std::os::macos::cf @if(env::DARWIN) @link(env::DARWIN, "CoreFoundation.framework");
|
||||
|
||||
distinct CFTypeRef = void*;
|
||||
def CFIndex = isz;
|
||||
@@ -8,5 +8,5 @@ struct CFRange
|
||||
CFIndex length;
|
||||
}
|
||||
|
||||
extern fn CFTypeRef _macos_CFRetain(CFTypeRef cf) @extern("CFRetain");
|
||||
extern fn void _macos_CFRelease(CFTypeRef cf) @extern("CFRelease");
|
||||
extern fn CFTypeRef macos_CFRetain(CFTypeRef cf) @extern("CFRetain") @builtin;
|
||||
extern fn void macos_CFRelease(CFTypeRef cf) @extern("CFRelease") @builtin;
|
||||
@@ -68,7 +68,7 @@ struct Darwin_segment_command_64
|
||||
}
|
||||
|
||||
|
||||
fn String! executable_path(Allocator *allocator)
|
||||
fn String! executable_path(Allocator allocator)
|
||||
{
|
||||
char[4096] path;
|
||||
uint len = path.len;
|
||||
@@ -93,7 +93,7 @@ fn uptr! load_address() @local
|
||||
}
|
||||
|
||||
|
||||
fn Backtrace! backtrace_load_element(String execpath, void* buffer, void* load_address, Allocator* allocator = allocator::heap()) @local
|
||||
fn Backtrace! backtrace_load_element(String execpath, void* buffer, void* load_address, Allocator allocator = allocator::heap()) @local
|
||||
{
|
||||
@pool(allocator)
|
||||
{
|
||||
@@ -132,7 +132,7 @@ fn Backtrace! backtrace_load_element(String execpath, void* buffer, void* load_a
|
||||
};
|
||||
}
|
||||
|
||||
fn BacktraceList! symbolize_backtrace(void*[] backtrace, Allocator* allocator)
|
||||
fn BacktraceList! symbolize_backtrace(void*[] backtrace, Allocator allocator)
|
||||
{
|
||||
void *load_addr = (void *)load_address()!;
|
||||
BacktraceList list;
|
||||
@@ -150,7 +150,7 @@ fn BacktraceList! symbolize_backtrace(void*[] backtrace, Allocator* allocator)
|
||||
String execpath = executable_path(allocator::temp())!;
|
||||
foreach (addr : backtrace)
|
||||
{
|
||||
list.append(backtrace_load_element(execpath, addr, load_addr, allocator) ?? backtrace::BACKTRACE_UNKNOWN);
|
||||
list.push(backtrace_load_element(execpath, addr, load_addr, allocator) ?? backtrace::BACKTRACE_UNKNOWN);
|
||||
}
|
||||
};
|
||||
return list;
|
||||
|
||||
3
lib/std/os/macos/heap.c3
Normal file
3
lib/std/os/macos/heap.c3
Normal file
@@ -0,0 +1,3 @@
|
||||
module std::os::darwin @if(env::DARWIN);
|
||||
|
||||
extern fn usz malloc_size(void* ptr);
|
||||
@@ -1,4 +1,4 @@
|
||||
module std::os::macos::objc @if(env::DARWIN);
|
||||
module std::os::macos::objc @if(env::DARWIN) @link(env::DARWIN, "CoreFoundation.framework");
|
||||
|
||||
distinct Class = void*;
|
||||
distinct Method = void*;
|
||||
@@ -10,38 +10,36 @@ fault ObjcFailure
|
||||
CLASS_NOT_FOUND
|
||||
}
|
||||
|
||||
macro char* Class.name(Class cls) => _macos_class_getName(cls);
|
||||
macro Class Class.superclass(Class cls) => _macos_class_getSuperclass(cls);
|
||||
macro bool Class.responds_to(Class cls, Selector sel) => _macos_class_respondsToSelector(cls, sel);
|
||||
macro Method Class.method(Class cls, Selector name) => _macos_class_getClassMethod(cls, name);
|
||||
macro ZString Class.name(Class cls) => macos_class_getName(cls);
|
||||
macro Class Class.superclass(Class cls) => macos_class_getSuperclass(cls);
|
||||
macro bool Class.responds_to(Class cls, Selector sel) => macos_class_respondsToSelector(cls, sel);
|
||||
macro Method Class.method(Class cls, Selector name) => macos_class_getClassMethod(cls, name);
|
||||
|
||||
macro bool Selector.equals(Selector a, Selector b) => a == b;
|
||||
macro bool Class.equals(Class a, Class b) => a == b;
|
||||
|
||||
macro Selector selector_register(char* c) => _macos_sel_registerName(c);
|
||||
macro Class! class_by_name(char* c)
|
||||
macro Class! class_by_name(ZString c)
|
||||
{
|
||||
Class cls = _macos_objc_lookUpClass(c);
|
||||
if (!cls) return ObjcFailure.CLASS_NOT_FOUND?;
|
||||
return cls;
|
||||
Class cls = macos_objc_lookUpClass(c);
|
||||
return cls ?: ObjcFailure.CLASS_NOT_FOUND?;
|
||||
}
|
||||
|
||||
macro Class[] class_get_list(Allocator *allocator = allocator::heap())
|
||||
macro Class[] class_get_list(Allocator allocator = allocator::heap())
|
||||
{
|
||||
int num_classes = _macos_objc_getClassList(null, 0);
|
||||
int num_classes = macos_objc_getClassList(null, 0);
|
||||
if (!num_classes) return {};
|
||||
Class[] entries = allocator.new_array(Class, num_classes);
|
||||
_macos_objc_getClassList(entries.ptr, entries.len);
|
||||
macos_objc_getClassList(entries.ptr, entries.len);
|
||||
return entries;
|
||||
}
|
||||
|
||||
extern fn Class _macos_objc_getClass(char* name) @extern("objc_getClass");
|
||||
extern fn int _macos_objc_getClassList(Class* buffer, int buffer_count) @extern("objc_getClassList");
|
||||
extern fn char* _macos_class_getName(Class cls) @extern("class_getName");
|
||||
extern fn Class _macos_class_getSuperclass(Class cls) @extern("class_getSuperclass");
|
||||
extern fn Method _macos_class_getClassMethod(Class cls, Selector name) @extern("class_getClassMethod");
|
||||
extern fn bool _macos_class_respondsToSelector(Class cls, Selector name) @extern("class_respondsToSelector");
|
||||
extern fn Selector _macos_sel_registerName(char* str) @extern("sel_registerName");
|
||||
extern fn Class _macos_objc_lookUpClass(char* name) @extern("objc_lookUpClass");
|
||||
extern fn Class macos_objc_getClass(ZString name) @extern("objc_getClass") @builtin;
|
||||
extern fn int macos_objc_getClassList(Class* buffer, int buffer_count) @extern("objc_getClassList") @builtin;
|
||||
extern fn ZString macos_class_getName(Class cls) @extern("class_getName") @builtin;
|
||||
extern fn Class macos_class_getSuperclass(Class cls) @extern("class_getSuperclass") @builtin;
|
||||
extern fn Method macos_class_getClassMethod(Class cls, Selector name) @extern("class_getClassMethod") @builtin;
|
||||
extern fn bool macos_class_respondsToSelector(Class cls, Selector name) @extern("class_respondsToSelector") @builtin;
|
||||
extern fn Selector macos_sel_registerName(ZString str) @extern("sel_registerName") @builtin;
|
||||
extern fn Class macos_objc_lookUpClass(ZString name) @extern("objc_lookUpClass") @builtin;
|
||||
|
||||
|
||||
|
||||
3
lib/std/os/posix/heap.c3
Normal file
3
lib/std/os/posix/heap.c3
Normal file
@@ -0,0 +1,3 @@
|
||||
module std::os::posix @if(env::POSIX);
|
||||
|
||||
extern fn CInt posix_memalign(void **memptr, usz alignment, usz size);
|
||||
@@ -6,7 +6,6 @@ enum Win32_GET_FILEEX_INFO_LEVELS
|
||||
MAX,
|
||||
}
|
||||
|
||||
|
||||
struct Win32_FILE_ATTRIBUTE_DATA
|
||||
{
|
||||
Win32_DWORD dwFileAttributes;
|
||||
@@ -17,7 +16,6 @@ struct Win32_FILE_ATTRIBUTE_DATA
|
||||
Win32_DWORD nFileSizeLow;
|
||||
}
|
||||
|
||||
|
||||
const MAX_PATH = 260;
|
||||
|
||||
struct Win32_WIN32_FIND_DATAW
|
||||
@@ -99,6 +97,8 @@ extern fn CFile _fdopen(int fd, ZString mode);
|
||||
extern fn CInt _access(ZString path, CInt mode);
|
||||
extern fn CInt _waccess(WString path, CInt mode);
|
||||
|
||||
extern fn WString _wfullpath(WString absPath, WString relPath, usz maxLength);
|
||||
|
||||
/*
|
||||
extern ulong _win32_GetCurrentDirectoryW(ulong, Char16* buffer) @extern("GetCurrentDirectoryW");
|
||||
extern bool _win32_CreateSymbolicLinkW(WString symlink_file, WString target_file, ulong flags) @extern("CreateSymbolicLinkW");
|
||||
|
||||
6
lib/std/os/win32/gdi.c3
Normal file
6
lib/std/os/win32/gdi.c3
Normal file
@@ -0,0 +1,6 @@
|
||||
module std::os::win32 @if(env::WIN32);
|
||||
|
||||
extern fn Win32_HBRUSH createSolidBrush(Win32_COLORREF) @extern("CreateSolidBrush");
|
||||
extern fn Win32_COLORREF setTextColor(Win32_HDC, Win32_COLORREF) @extern("SetTextColor");
|
||||
extern fn CInt setBkMode(Win32_HDC, CInt) @extern("SetBkMode");
|
||||
extern fn Win32_BOOL textOut(Win32_HDC, CInt, CInt, Win32_LPCWSTR, CInt) @extern("TextOutW");
|
||||
11
lib/std/os/win32/heap.c3
Normal file
11
lib/std/os/win32/heap.c3
Normal file
@@ -0,0 +1,11 @@
|
||||
module std::os::win32 @if(env::WIN32);
|
||||
|
||||
extern fn void* _aligned_malloc(usz size, usz alignment);
|
||||
extern fn void* _aligned_realloc(void* memblock, usz size, usz alignment);
|
||||
extern fn void* _aligned_recalloc(void* memblock, usz size, usz alignment);
|
||||
extern fn void _aligned_free(void* memblock);
|
||||
extern fn void _aligned_msize(void* memblock, usz alignment, usz offset);
|
||||
extern fn void* _aligned_offset_malloc(usz size, usz alignment, usz offset);
|
||||
extern fn void* _aligned_offset_realloc(void* memblock, usz size, usz alignment, usz offset);
|
||||
extern fn void* _aligned_offset_recalloc(void* memblock, usz size, usz alignment, usz offset);
|
||||
extern fn usz _msize(void* memblock);
|
||||
@@ -48,20 +48,22 @@ const UNDNAME_COMPLETE = 0x0000;
|
||||
|
||||
extern fn void initializeCriticalSection(Win32_CRITICAL_SECTION* section) @extern("InitializeCriticalSection");
|
||||
extern fn void deleteCriticalSection(Win32_CRITICAL_SECTION* section) @extern("DeleteCriticalSection");
|
||||
extern fn Win32_HANDLE createMutex(void*, bool, void*) @extern("CreateMutexA");
|
||||
extern fn Win32_HANDLE createMutex(void*, Win32_BOOL, void*) @extern("CreateMutexA");
|
||||
extern fn Win32_BOOL releaseMutex(Win32_HANDLE) @extern("ReleaseMutex");
|
||||
extern fn void enterCriticalSection(Win32_CRITICAL_SECTION* section) @extern("EnterCriticalSection");
|
||||
extern fn void leaveCriticalSection(Win32_CRITICAL_SECTION* section) @extern("LeaveCriticalSection");
|
||||
extern fn Win32_BOOL tryEnterCriticalSection(Win32_CRITICAL_SECTION* section) @extern("TryEnterCriticalSection");
|
||||
extern fn uint waitForSingleObject(Win32_HANDLE, uint milliseconds) @extern("WaitForSingleObject");
|
||||
extern fn Win32_DWORD waitForSingleObject(Win32_HANDLE hHandle, Win32_DWORD dwMilliseconds) @extern("WaitForSingleObject");
|
||||
extern fn Win32_DWORD waitForSingleObjectEx(Win32_HANDLE hHandle, Win32_DWORD dwMilliseconds, Win32_BOOL bAlertable) @extern("WaitForSingleObjectEx");
|
||||
extern fn Win32_DWORD waitForMultipleObjects(Win32_DWORD nCount, Win32_HANDLE* lpHandles, Win32_BOOL bWaitAll, Win32_DWORD dwMilliseconds) @extern("WaitForMultipleObjects");
|
||||
extern fn Win32_DWORD waitForMultipleObjectsEx(Win32_DWORD nCount, Win32_HANDLE* lpHandles, Win32_BOOL bWaitAll, Win32_DWORD dwMilliseconds, Win32_BOOL bAlertable) @extern("WaitForMultipleObjectsEx");
|
||||
extern fn void sleep(uint ms) @extern("Sleep");
|
||||
extern fn uint waitForMultipleObjects(uint count, Win32_HANDLE* handles, bool wait_all, uint ms) @extern("WaitForMultipleObjects");
|
||||
extern fn Win32_BOOL resetEvent(Win32_HANDLE event) @extern("ResetEvent");
|
||||
extern fn Win32_BOOL setEvent(Win32_HANDLE handle) @extern("SetEvent");
|
||||
extern fn long interlockedCompareExchange(int* dest, int exchange, int comperand) @extern("InterlockedCompareExchange");
|
||||
extern fn Win32_DWORD sleepEx(Win32_DWORD ms, Win32_BOOL alertable) @extern("SleepEx");
|
||||
extern fn Win32_HANDLE createThread(void* attributes, usz stack, ThreadFn func, void* arg, uint flags, uint* thread_id) @extern("CreateThread");
|
||||
extern fn Win32_BOOL getExitCodeThread(Win32_HANDLE handle, uint* exit_code) @extern("GetExitCodeThread");
|
||||
extern fn Win32_HANDLE createThread(void* attributes, usz stack, ThreadFn func, Win32_LPVOID arg, Win32_DWORD flags, Win32_LPDWORD thread_id) @extern("CreateThread");
|
||||
extern fn Win32_BOOL getExitCodeThread(Win32_HANDLE handle, Win32_LPDWORD exit_code) @extern("GetExitCodeThread");
|
||||
extern fn Win32_BOOL getExitCodeProcess(Win32_HANDLE hProcess, Win32_LPDWORD lpExitCode) @extern("GetExitCodeProcess");
|
||||
extern fn Win32_DWORD getThreadId(Win32_HANDLE) @extern("GetThreadId");
|
||||
extern fn void exitThread(Win32_DWORD dwExitCode) @noreturn @extern("ExitThread");
|
||||
@@ -103,6 +105,8 @@ extern fn Win32_BOOL symFromAddr(Win32_HANDLE hProcess, Win32_DWORD64 address, W
|
||||
extern fn Win32_BOOL symGetLineFromAddr64(Win32_HANDLE hProcess, Win32_DWORD64 dwAddr, Win32_PDWORD pdwDisplacement, Win32_PIMAGEHLP_LINE64 line) @extern("SymGetLineFromAddr64");
|
||||
extern fn Win32_WORD rtlCaptureStackBackTrace(Win32_DWORD framesToSkip, Win32_DWORD framesToCapture, Win32_PVOID *backTrace, Win32_PDWORD backTraceHash) @extern("RtlCaptureStackBackTrace");
|
||||
extern fn Win32_BOOL symGetModuleInfo64(Win32_HANDLE hProcess, Win32_DWORD64 qwAddr, Win32_PIMAGEHLP_MODULE64 moduleInfo) @extern("SymGetModuleInfo64");
|
||||
extern fn Win32_HANDLE getModuleHandleA(Win32_LPCSTR lpModuleName) @extern("GetModuleHandleA");
|
||||
extern fn Win32_HANDLE getModuleHandleW(Win32_LPCWSTR lpModuleName) @extern("GetModuleHandleW");
|
||||
|
||||
fn Win32_DWORD! load_modules()
|
||||
{
|
||||
@@ -150,7 +154,7 @@ struct Symbol
|
||||
|
||||
Win32_DWORD64 displacement;
|
||||
|
||||
fn BacktraceList! symbolize_backtrace(void*[] backtrace, Allocator* allocator)
|
||||
fn BacktraceList! symbolize_backtrace(void*[] backtrace, Allocator allocator)
|
||||
{
|
||||
BacktraceList list;
|
||||
list.new_init(backtrace.len, allocator);
|
||||
@@ -159,12 +163,12 @@ fn BacktraceList! symbolize_backtrace(void*[] backtrace, Allocator* allocator)
|
||||
defer symCleanup(process);
|
||||
foreach (addr : backtrace)
|
||||
{
|
||||
list.append(resolve_backtrace(addr, process, allocator) ?? backtrace::BACKTRACE_UNKNOWN);
|
||||
list.push(resolve_backtrace(addr, process, allocator) ?? backtrace::BACKTRACE_UNKNOWN);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
fn Backtrace! resolve_backtrace(void* addr, Win32_HANDLE process, Allocator* allocator)
|
||||
fn Backtrace! resolve_backtrace(void* addr, Win32_HANDLE process, Allocator allocator)
|
||||
{
|
||||
Symbol symbol;
|
||||
//Win32_DWORD image_type = load_modules()!;
|
||||
|
||||
33
lib/std/os/win32/windef.c3
Normal file
33
lib/std/os/win32/windef.c3
Normal file
@@ -0,0 +1,33 @@
|
||||
module std::os::win32 @if(env::WIN32);
|
||||
|
||||
struct Win32_RECT
|
||||
{
|
||||
Win32_LONG left;
|
||||
Win32_LONG top;
|
||||
Win32_LONG right;
|
||||
Win32_LONG bottom;
|
||||
}
|
||||
|
||||
struct Win32_POINT
|
||||
{
|
||||
Win32_LONG x;
|
||||
Win32_LONG y;
|
||||
}
|
||||
|
||||
struct Win32_SIZE
|
||||
{
|
||||
Win32_LONG cx;
|
||||
Win32_LONG cy;
|
||||
}
|
||||
|
||||
def Win32_PSIZE = Win32_SIZE*;
|
||||
def Win32_NPSIZE = Win32_SIZE*;
|
||||
def Win32_LPSIZE = Win32_SIZE*;
|
||||
|
||||
def Win32_PPOINT = Win32_POINT*;
|
||||
def Win32_NPOINT = Win32_POINT*;
|
||||
def Win32_LPOINT = Win32_POINT*;
|
||||
|
||||
def Win32_PRECT = Win32_RECT*;
|
||||
def Win32_NPRECT = Win32_RECT*;
|
||||
def Win32_LPRECT = Win32_RECT*;
|
||||
150
lib/std/os/win32/winuser.c3
Normal file
150
lib/std/os/win32/winuser.c3
Normal file
@@ -0,0 +1,150 @@
|
||||
module std::os::win32 @if(env::WIN32);
|
||||
|
||||
def Win32_WNDPROC = fn Win32_LRESULT(Win32_HWND, Win32_UINT, Win32_WPARAM, Win32_LPARAM);
|
||||
|
||||
struct Win32_WNDCLASSEXW
|
||||
{
|
||||
Win32_UINT cbSize;
|
||||
Win32_UINT style;
|
||||
Win32_WNDPROC lpfnWndProc;
|
||||
CInt cbClsExtra;
|
||||
CInt cbWndExtra;
|
||||
Win32_HINSTANCE hInstance;
|
||||
Win32_HICON hIcon;
|
||||
Win32_HCURSOR hCursor;
|
||||
Win32_HBRUSH hbrBackground;
|
||||
Win32_LPCWSTR lpszMenuName;
|
||||
Win32_LPCWSTR lpszClassName;
|
||||
Win32_HICON hIconSm;
|
||||
}
|
||||
|
||||
struct Win32_MSG
|
||||
{
|
||||
Win32_HWND hwnd;
|
||||
Win32_UINT message;
|
||||
Win32_WPARAM wParam;
|
||||
Win32_LPARAM lParam;
|
||||
Win32_DWORD time;
|
||||
Win32_POINT pt;
|
||||
Win32_DWORD lPrivate;
|
||||
}
|
||||
|
||||
struct Win32_PAINTSTRUCT
|
||||
{
|
||||
Win32_HDC hdc;
|
||||
Win32_BOOL fErase;
|
||||
Win32_RECT rcPaint;
|
||||
Win32_BOOL fRestore;
|
||||
Win32_BOOL fIncUpdate;
|
||||
Win32_BYTE[32] rgbReserved;
|
||||
}
|
||||
def Win32_PWNDCLASSEXW = Win32_WNDCLASSEXW*;
|
||||
def Win32_LPWNDCLASSEXW = Win32_WNDCLASSEXW*;
|
||||
def Win32_NPWNDCLASSEXW = Win32_WNDCLASSEXW*;
|
||||
|
||||
def Win32_PPAINTSTRUCT = Win32_PAINTSTRUCT*;
|
||||
def Win32_LPPAINTSTRUCT = Win32_PAINTSTRUCT*;
|
||||
def Win32_NPPAINTSTRUCT = Win32_PAINTSTRUCT*;
|
||||
|
||||
def Win32_PMSG = Win32_MSG*;
|
||||
def Win32_LPMSG = Win32_MSG*;
|
||||
def Win32_NPMSG = Win32_MSG*;
|
||||
|
||||
def Win32_ATOM = ushort;
|
||||
|
||||
const Win32_DWORD WS_BORDER = 0x00800000L;
|
||||
const Win32_DWORD WS_CAPTION = 0x00C00000L;
|
||||
const Win32_DWORD WS_CHILD = 0x40000000L;
|
||||
const Win32_DWORD WS_CHILDWINDOW = 0x40000000L;
|
||||
const Win32_DWORD WS_CLIPCHILDREN = 0x02000000L;
|
||||
const Win32_DWORD WS_CLIPSIBLINGS = 0x04000000L;
|
||||
const Win32_DWORD WS_DISABLED = 0x08000000L;
|
||||
const Win32_DWORD WS_DLGFRAME = 0x00400000L;
|
||||
const Win32_DWORD WS_GROUP = 0x00020000L;
|
||||
const Win32_DWORD WS_HSCROLL = 0x00100000L;
|
||||
const Win32_DWORD WS_ICONIC = 0x20000000L;
|
||||
const Win32_DWORD WS_MAXIMIZE = 0x01000000L;
|
||||
const Win32_DWORD WS_MAXIMIZEBOX = 0x00010000L;
|
||||
const Win32_DWORD WS_MINIMIZE = 0x20000000L;
|
||||
const Win32_DWORD WS_MINIMIZEBOX = 0x00020000L;
|
||||
const Win32_DWORD WS_OVERLAPPED = 0x00000000L;
|
||||
const Win32_DWORD WS_OVERLAPPEDWINDOW = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
|
||||
const Win32_DWORD WS_POPUP = 0x80000000L;
|
||||
const Win32_DWORD WS_POPUPWINDOW = WS_POPUP | WS_BORDER | WS_SYSMENU;
|
||||
const Win32_DWORD WS_SIZEBOX = 0x00040000L;
|
||||
const Win32_DWORD WS_SYSMENU = 0x00080000L;
|
||||
const Win32_DWORD WS_TABSTOP = 0x00010000L;
|
||||
const Win32_DWORD WS_THICKFRAME = 0x00040000L;
|
||||
const Win32_DWORD WS_TILED = 0x00000000L;
|
||||
const Win32_DWORD WS_TILEDWINDOW = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
|
||||
const Win32_DWORD WS_VISIBLE = 0x10000000L;
|
||||
const Win32_DWORD WS_VSCROLL = 0x00200000L;
|
||||
|
||||
const Win32_UINT MB_OK = 0x00000000;
|
||||
const Win32_UINT MB_OKCANCEL = 0x00000001;
|
||||
const Win32_UINT MB_ABORTRETRYIGNORE = 0x00000002;
|
||||
const Win32_UINT MB_YESNOCANCEL = 0x00000003;
|
||||
const Win32_UINT MB_YESNO = 0x00000004;
|
||||
const Win32_UINT MB_RETRYCANCEL = 0x00000005;
|
||||
const Win32_UINT MB_CANCELTRYCONTINUE = 0x00000006;
|
||||
const Win32_UINT MB_ICONHAND = 0x00000010;
|
||||
const Win32_UINT MB_ICONQUESTION = 0x00000020;
|
||||
const Win32_UINT MB_ICONEXCLAMATION = 0x00000030;
|
||||
const Win32_UINT MB_ICONASTERISK = 0x00000040;
|
||||
const Win32_UINT MB_USERICON = 0x00000080;
|
||||
const Win32_UINT MB_ICONWARNING = MB_ICONEXCLAMATION;
|
||||
const Win32_UINT MB_ICONERROR = MB_ICONHAND;
|
||||
const Win32_UINT MB_ICONINFORMATION = MB_ICONASTERISK;
|
||||
const Win32_UINT MB_ICONSTOP = MB_ICONHAND;
|
||||
|
||||
const GWL_WNDPROC @if(env::ARCH_32_BIT) = -4;
|
||||
const GWL_HINSTANCE @if(env::ARCH_32_BIT) = -6;
|
||||
const GWL_HWNDPARENT @if(env::ARCH_32_BIT) = -8;
|
||||
const GWL_STYLE = -16;
|
||||
const GWL_EXSTYLE = -20;
|
||||
const GWL_USERDATA @if(env::ARCH_32_BIT) = -21;
|
||||
const GWL_ID = -12;
|
||||
const GWLP_WNDPROC = -4;
|
||||
const GWLP_HINSTANCE = -6;
|
||||
const GWLP_HWNDPARENT = -8;
|
||||
const GWLP_USERDATA = -21;
|
||||
const GWLP_ID = -12;
|
||||
|
||||
extern fn Win32_HDC beginPaint(Win32_HWND, Win32_LPPAINTSTRUCT) @extern("BeginPaint");
|
||||
extern fn Win32_LRESULT callWindowProcW(Win32_WNDPROC lpPrevWndFunc, Win32_HWND hWnd, Win32_UINT msg, Win32_WPARAM wParam, Win32_LPARAM lParam) @extern("CallWindowProcW");
|
||||
extern fn Win32_HWND createWindowExW(Win32_DWORD, Win32_LPCWSTR, Win32_LPCWSTR, Win32_DWORD, CInt, CInt, CInt, CInt, Win32_HWND, Win32_HMENU, Win32_HINSTANCE, Win32_LPVOID) @extern("CreateWindowExW");
|
||||
extern fn Win32_LRESULT defWindowProcW(Win32_HWND, Win32_UINT, Win32_WPARAM, Win32_LPARAM) @extern("DefWindowProcW");
|
||||
extern fn Win32_BOOL dispatchMessage(Win32_MSG* lpMsg) @extern("DispatchMessageW");
|
||||
extern fn Win32_BOOL endPaint(Win32_HWND, Win32_LPPAINTSTRUCT) @extern("EndPaint");
|
||||
extern fn Win32_BOOL getMessageW(Win32_LPMSG, Win32_HWND, Win32_UINT, Win32_UINT) @extern("GetMessageW");
|
||||
extern fn Win32_BOOL getUpdateRect(Win32_HWND hWnd, Win32_LPRECT lpRect, Win32_BOOL bErase) @extern("GetUpdateRect");
|
||||
extern fn Win32_LONG_PTR getWindowLongPtrW(Win32_HWND hWnd, CInt nIndex) @extern("GetWindowLongPtrW");
|
||||
extern fn Win32_LONG getWindowLongW(Win32_HWND hWnd, CInt nIndex) @extern("GetWindowLongW");
|
||||
extern fn Win32_HCURSOR loadCursorW(Win32_HINSTANCE instance, Win32_LPCWSTR cursorName) @extern("LoadCursorW");
|
||||
extern fn Win32_HICON loadIconW(Win32_HINSTANCE instance, Win32_LPCWSTR iconName) @extern("LoadIconW");
|
||||
extern fn int messageBoxW(Win32_HWND hWnd, Win32_LPCWSTR lpText, Win32_LPCWSTR lpCaption, Win32_UINT uType) @extern("MessageBoxW");
|
||||
extern fn void postQuitMessage(CInt) @extern("PostQuitMessage");
|
||||
extern fn Win32_ATOM registerClassExW(Win32_WNDCLASSEXW*) @extern("RegisterClassExW");
|
||||
extern fn Win32_LONG_PTR setWindowLongPtrW(Win32_HWND hWnd, CInt nIndex, Win32_LONG_PTR dwNewLong) @extern("SetWindowLongPtrW");
|
||||
extern fn Win32_LONG setWindowLongW(Win32_HWND hWnd, CInt nIndex, Win32_LONG dwNewLong) @extern("SetWindowLongW");
|
||||
extern fn Win32_BOOL showWindow(Win32_HWND, CInt) @extern("ShowWindow");
|
||||
extern fn Win32_BOOL translateMessage(Win32_MSG* lpMsg) @extern("TranslateMessage");
|
||||
extern fn Win32_BOOL updateWindow(Win32_HWND) @extern("UpdateWindow");
|
||||
|
||||
macro getWindowLongPtr(Win32_HWND hWnd, CInt nIndex)
|
||||
{
|
||||
$if env::ARCH_64_BIT:
|
||||
return getWindowLongPtrW(hWnd, nIndex);
|
||||
$else
|
||||
return getWindowLongW(hWnd, nIndex);
|
||||
$endif
|
||||
}
|
||||
|
||||
macro setWindowLongPtr(Win32_HWND hWnd, CInt nIndex, dwNewLong)
|
||||
{
|
||||
$if env::ARCH_64_BIT:
|
||||
return setWindowLongPtrW(hWnd, nIndex, dwNewLong);
|
||||
$else
|
||||
return setWindowLongW(hWnd, nIndex, dwNewLong);
|
||||
$endif
|
||||
}
|
||||
@@ -3,17 +3,20 @@ module std::sort;
|
||||
/**
|
||||
* Perform a binary search over the sorted array and return the index
|
||||
* in [0, array.len) where x would be inserted or cmp(i) is true and cmp(j) is true for j in [i, array.len).
|
||||
* @require $defined(list[0]) && $defined(list.len) "The list must be indexable"
|
||||
* @require $or(@typeid(cmp) == void*.typeid, @is_comparer(cmp, list)) "Expected a comparison function which compares values"
|
||||
* @require @is_sortable(list) "The list must be sortable"
|
||||
* @require @is_valid_cmp_fn(cmp, list, context) "Expected a comparison function which compares values"
|
||||
* @require @is_valid_context(cmp, context) "Expected a valid context"
|
||||
**/
|
||||
macro usz binarysearch(list, x, cmp = null) @builtin
|
||||
macro usz binarysearch(list, x, cmp = EMPTY_MACRO_SLOT, context = EMPTY_MACRO_SLOT) @builtin
|
||||
{
|
||||
usz i;
|
||||
usz len = @len_from_list(list);
|
||||
var $no_cmp = @is_empty_macro_slot(cmp);
|
||||
var $has_context = @is_valid_macro_slot(context);
|
||||
for (usz j = len; i < j;)
|
||||
{
|
||||
usz half = i + (j - i) / 2;
|
||||
$if @typeid(cmp) == void*.typeid:
|
||||
$if $no_cmp:
|
||||
switch
|
||||
{
|
||||
case greater(list[half], x): j = half;
|
||||
@@ -21,11 +24,20 @@ macro usz binarysearch(list, x, cmp = null) @builtin
|
||||
default: return half;
|
||||
}
|
||||
$else
|
||||
|
||||
$switch
|
||||
$case $typeof(cmp).params[0] == @typeid(list[0]):
|
||||
$case $defined(cmp(list[0], list[0], context)):
|
||||
int res = cmp(list[half], x, context);
|
||||
$case $defined(cmp(list[0], list[0])):
|
||||
assert(!$has_context);
|
||||
int res = cmp(list[half], x);
|
||||
$default:
|
||||
$case $defined(cmp(&list[0], &list[0], context)):
|
||||
int res = cmp(&list[half], &x, context);
|
||||
$case $defined(cmp(&list[0], &list[0])):
|
||||
assert(!$has_context);
|
||||
int res = cmp(&list[half], &x);
|
||||
$default:
|
||||
assert(false, "Invalid comparison function");
|
||||
$endswitch
|
||||
switch
|
||||
{
|
||||
|
||||
181
lib/std/sort/countingsort.c3
Normal file
181
lib/std/sort/countingsort.c3
Normal file
@@ -0,0 +1,181 @@
|
||||
module std::sort;
|
||||
import std::sort::is;
|
||||
import std::sort::cs;
|
||||
import std::sort::qs;
|
||||
|
||||
/**
|
||||
* Sort list using the counting sort algorithm.
|
||||
* @require @is_sortable(list) "The list must be indexable and support .len or .len()"
|
||||
* @require @is_cmp_key_fn(key_fn, list) "Expected a transformation function which returns an unsigned integer."
|
||||
**/
|
||||
macro countingsort(list, key_fn = EMPTY_MACRO_SLOT) @builtin
|
||||
{
|
||||
usz len = sort::@len_from_list(list);
|
||||
cs::csort(<$typeof(list), $typeof(key_fn)>)(list, 0, len, key_fn, ~((uint)0));
|
||||
}
|
||||
|
||||
macro insertionsort_indexed(list, start, end, cmp = EMPTY_MACRO_SLOT, context = EMPTY_MACRO_SLOT) @builtin
|
||||
{
|
||||
is::isort(<$typeof(list), $typeof(cmp), $typeof(context)>)(list, (usz)start, (usz)end, cmp, context);
|
||||
}
|
||||
|
||||
macro quicksort_indexed(list, start, end, cmp = EMPTY_MACRO_SLOT, context = EMPTY_MACRO_SLOT) @builtin
|
||||
{
|
||||
qs::qsort(<$typeof(list), $typeof(cmp), $typeof(context)>)(list, (isz)start, (isz)(end-1), cmp, context);
|
||||
}
|
||||
|
||||
module std::sort::cs(<Type, KeyFn>);
|
||||
|
||||
def Counts = usz[256] @private;
|
||||
def Ranges = usz[257] @private;
|
||||
def Indexs = char[256] @private;
|
||||
def ElementType = $typeof(Type{}[0]);
|
||||
|
||||
const bool NO_KEY_FN @private = types::is_same(KeyFn, EmptySlot);
|
||||
const bool KEY_BY_VALUE @private = $or(NO_KEY_FN, $assignable(Type{}[0], $typefrom(KeyFn.params[0])));
|
||||
const bool LIST_HAS_REF @private = $defined(&Type{}[0]);
|
||||
|
||||
def KeyFnReturnType = $typefrom(KeyFn.returns) @if(!NO_KEY_FN);
|
||||
def KeyFnReturnType = ElementType @if(NO_KEY_FN);
|
||||
def CmpCallback = fn int(ElementType, ElementType) @if(KEY_BY_VALUE && NO_KEY_FN);
|
||||
def CmpCallback = fn int(ElementType*, ElementType*) @if(!KEY_BY_VALUE && NO_KEY_FN);
|
||||
def CmpCallback = fn int(ElementType, ElementType, KeyFn) @if(KEY_BY_VALUE && !NO_KEY_FN);
|
||||
def CmpCallback = fn int(ElementType*, ElementType*, KeyFn) @if(!KEY_BY_VALUE && !NO_KEY_FN);
|
||||
|
||||
fn void csort(Type list, usz low, usz high, KeyFn key_fn, uint byte_idx)
|
||||
{
|
||||
if (high <= low) return;
|
||||
$if NO_KEY_FN:
|
||||
CmpCallback compare_fn = fn (lhs, rhs) => compare_to(lhs, rhs);
|
||||
$else
|
||||
CmpCallback compare_fn = fn (lhs, rhs, key_fn) => compare_to(key_fn(lhs), key_fn(rhs));
|
||||
$endif;
|
||||
|
||||
byte_idx = byte_idx >= KeyFnReturnType.sizeof ? KeyFnReturnType.sizeof - 1 : byte_idx;
|
||||
|
||||
Counts counts;
|
||||
Ranges ranges;
|
||||
Indexs indexs;
|
||||
|
||||
KeyFnReturnType mn = ~(KeyFnReturnType)0;
|
||||
KeyFnReturnType mx = 0;
|
||||
|
||||
char last_key = 0;
|
||||
char keys_ordered = 1;
|
||||
|
||||
for (usz i = low; i < high; i++)
|
||||
{
|
||||
$switch
|
||||
$case NO_KEY_FN:
|
||||
KeyFnReturnType k = list[i];
|
||||
$case KEY_BY_VALUE:
|
||||
KeyFnReturnType k = key_fn(list[i]);
|
||||
$case LIST_HAS_REF:
|
||||
KeyFnReturnType k = key_fn(&list[i]);
|
||||
$default:
|
||||
KeyFnReturnType k = key_fn(&&list[i]);
|
||||
$endswitch;
|
||||
|
||||
char key_byte = (char)((k >> (byte_idx * 8)) & 0xff);
|
||||
++counts[key_byte];
|
||||
|
||||
mn = k < mn ? k : mn;
|
||||
mx = k > mx ? k : mx;
|
||||
|
||||
keys_ordered = keys_ordered & (char)(key_byte >= last_key);
|
||||
last_key = key_byte;
|
||||
}
|
||||
|
||||
KeyFnReturnType diff = mx - mn;
|
||||
if (diff == 0) return;
|
||||
|
||||
ushort fallback0_count = 0;
|
||||
ushort fallback1_count = 0;
|
||||
ushort recursion_count = 0;
|
||||
|
||||
usz total = 0;
|
||||
foreach (char i, count : counts)
|
||||
{
|
||||
indexs[fallback0_count] = i;
|
||||
indexs[255 - recursion_count] = i;
|
||||
|
||||
fallback0_count += (ushort)(count > 1 && count <= 32);
|
||||
recursion_count += (ushort)(count > 128);
|
||||
|
||||
counts[i] = total;
|
||||
ranges[i] = total;
|
||||
total += count;
|
||||
}
|
||||
ranges[256] = total;
|
||||
|
||||
ushort remaining_indexs = 256 - (fallback0_count + recursion_count);
|
||||
for(ushort i = 0; (i < 256) && remaining_indexs; i++) {
|
||||
indexs[fallback0_count + fallback1_count] = (char)i;
|
||||
usz count = ranges[i + 1] - ranges[i];
|
||||
ushort within_fallback1_range = (ushort)(count > 32 && count <= 128);
|
||||
fallback1_count += within_fallback1_range;
|
||||
remaining_indexs -= within_fallback1_range;
|
||||
}
|
||||
|
||||
if (!keys_ordered)
|
||||
{
|
||||
usz sorted_count = 0;
|
||||
|
||||
do
|
||||
{
|
||||
foreach (x, s : counts)
|
||||
{
|
||||
usz e = ranges[x + 1];
|
||||
sorted_count += (e - s);
|
||||
for (; s < e; s++)
|
||||
{
|
||||
$switch
|
||||
$case NO_KEY_FN:
|
||||
KeyFnReturnType k = list[low + s];
|
||||
$case KEY_BY_VALUE:
|
||||
KeyFnReturnType k = key_fn(list[low + s]);
|
||||
$case LIST_HAS_REF:
|
||||
KeyFnReturnType k = key_fn(&list[low + s]);
|
||||
$default:
|
||||
KeyFnReturnType k = key_fn(&&list[low + s]);
|
||||
$endswitch;
|
||||
char k_idx = (char)(k >> (byte_idx * 8));
|
||||
usz target_idx = counts[k_idx];
|
||||
@swap(list[low + s], list[low + target_idx]);
|
||||
counts[k_idx]++;
|
||||
}
|
||||
}
|
||||
} while (sorted_count < ranges[256]);
|
||||
}
|
||||
|
||||
if (byte_idx)
|
||||
{
|
||||
for (usz p = 0; p < fallback0_count; p++) {
|
||||
usz i = indexs[p];
|
||||
|
||||
usz start_offset = ranges[i];
|
||||
usz end_offset = ranges[i + 1];
|
||||
|
||||
insertionsort_indexed(list, low + start_offset, low + end_offset, compare_fn, key_fn);
|
||||
}
|
||||
|
||||
for (usz p = 0; p < fallback1_count; p++) {
|
||||
usz i = indexs[fallback0_count + p];
|
||||
|
||||
usz start_offset = ranges[i];
|
||||
usz end_offset = ranges[i + 1];
|
||||
|
||||
quicksort_indexed(list, low + start_offset, low + end_offset, compare_fn, key_fn);
|
||||
}
|
||||
|
||||
for (usz p = 0; p < recursion_count; p++)
|
||||
{
|
||||
usz i = indexs[255 - p];
|
||||
|
||||
usz start_offset = ranges[i];
|
||||
usz end_offset = ranges[i + 1];
|
||||
|
||||
csort(list, low + start_offset, low + end_offset, key_fn, byte_idx - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
65
lib/std/sort/insertionsort.c3
Normal file
65
lib/std/sort/insertionsort.c3
Normal file
@@ -0,0 +1,65 @@
|
||||
module std::sort;
|
||||
import std::sort::is;
|
||||
|
||||
/**
|
||||
* Sort list using the quick sort algorithm.
|
||||
* @require @is_sortable(list) "The list must be indexable and support .len or .len()"
|
||||
* @require @is_valid_cmp_fn(cmp, list, context) "Expected a comparison function which compares values"
|
||||
**/
|
||||
macro insertionsort(list, cmp = EMPTY_MACRO_SLOT, context = EMPTY_MACRO_SLOT) @builtin
|
||||
{
|
||||
usz len = sort::@len_from_list(list);
|
||||
is::isort(<$typeof(list), $typeof(cmp), $typeof(context)>)(list, 0, (isz)len, cmp, context);
|
||||
}
|
||||
|
||||
module std::sort::is(<Type, CmpFn, Context>);
|
||||
|
||||
def ElementType = $typeof(Type{}[0]);
|
||||
|
||||
fn void isort(Type list, usz low, usz high, CmpFn comp, Context context)
|
||||
{
|
||||
var $has_cmp = @is_valid_macro_slot(comp);
|
||||
var $has_context = @is_valid_macro_slot(context);
|
||||
var $cmp_by_value = $and($has_cmp, $assignable(list[0], $typefrom(CmpFn.params[0])));
|
||||
var $has_get_ref = $defined(&list[0]);
|
||||
for (usz i = low; i < high; ++i)
|
||||
{
|
||||
usz j = i;
|
||||
for (;j > low;)
|
||||
{
|
||||
$if $has_get_ref:
|
||||
ElementType *rhs = &list[j];
|
||||
ElementType *lhs = &list[--j];
|
||||
$switch
|
||||
$case $cmp_by_value && $has_context:
|
||||
if (comp(*rhs, *lhs, context) >= 0) break;
|
||||
$case $cmp_by_value:
|
||||
if (comp(*rhs, *lhs) >= 0) break;
|
||||
$case $has_cmp && $has_context:
|
||||
if (comp(rhs, lhs, context) >= 0) break;
|
||||
$case $has_cmp:
|
||||
if (comp(rhs, lhs) >= 0) break;
|
||||
$default:
|
||||
if (!less(*rhs, *lhs)) break;
|
||||
$endswitch
|
||||
@swap(*rhs, *lhs);
|
||||
$else
|
||||
usz r = j;
|
||||
--j;
|
||||
$switch
|
||||
$case $cmp_by_value && $has_context:
|
||||
if (comp(list[r], list[j], context) >= 0) break;
|
||||
$case $cmp_by_value:
|
||||
if (comp(list[r], list[j]) >= 0) break;
|
||||
$case $has_cmp && $has_context:
|
||||
if (comp(&list[r], &list[j], context) >= 0) break;
|
||||
$case $has_cmp:
|
||||
if (comp(&list[r], &list[j]) >= 0) break;
|
||||
$default:
|
||||
if (!less(list[r], list[j])) break;
|
||||
$endswitch
|
||||
@swap(list[r], list[j]);
|
||||
$endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,18 +3,17 @@ import std::sort::qs;
|
||||
|
||||
/**
|
||||
* Sort list using the quick sort algorithm.
|
||||
* @require $defined(list[0]) && $defined(list.len) "The list must be indexable and support .len or .len()"
|
||||
* @require $or(@typeid(cmp) == void*.typeid, @is_comparer(cmp, list)) "Expected a comparison function which compares values"
|
||||
* @require @is_sortable(list) "The list must be indexable and support .len or .len()"
|
||||
* @require @is_valid_cmp_fn(cmp, list, context) "Expected a comparison function which compares values"
|
||||
* @require @is_valid_context(cmp, context) "Expected a valid context"
|
||||
**/
|
||||
macro quicksort(list, cmp = null) @builtin
|
||||
macro quicksort(list, cmp = EMPTY_MACRO_SLOT, context = EMPTY_MACRO_SLOT) @builtin
|
||||
{
|
||||
var $Type = $typeof(list);
|
||||
var $CmpType = $typeof(cmp);
|
||||
usz len = sort::@len_from_list(list);
|
||||
qs::qsort(<$Type, $CmpType>)(list, 0, (isz)len - 1, cmp);
|
||||
qs::qsort(<$typeof(list), $typeof(cmp), $typeof(context)>)(list, 0, (isz)len - 1, cmp, context);
|
||||
}
|
||||
|
||||
module std::sort::qs(<Type, Comparer>);
|
||||
module std::sort::qs(<Type, CmpFn, Context>);
|
||||
|
||||
def ElementType = $typeof(Type{}[0]);
|
||||
|
||||
@@ -28,10 +27,11 @@ def Stack = StackElementItem[64] @private;
|
||||
|
||||
// Based on https://alienryderflex.com/quicksort by Darel Rex Finley, Public Domain.
|
||||
|
||||
fn void qsort(Type list, isz low, isz high, Comparer cmp)
|
||||
fn void qsort(Type list, isz low, isz high, CmpFn cmp, Context context)
|
||||
{
|
||||
var $no_cmp = Comparer.typeid == void*.typeid;
|
||||
var $cmp_by_value = $and(!$no_cmp, Comparer.params[0] == @typeid(list[0]));
|
||||
var $has_cmp = @is_valid_macro_slot(cmp);
|
||||
var $has_context = @is_valid_macro_slot(context);
|
||||
var $cmp_by_value = $and($has_cmp, $assignable(list[0], $typefrom(CmpFn.params[0])));
|
||||
if (low >= 0 && high >= 0 && low < high)
|
||||
{
|
||||
Stack stack;
|
||||
@@ -51,20 +51,25 @@ fn void qsort(Type list, isz low, isz high, Comparer cmp)
|
||||
while (l < h)
|
||||
{
|
||||
$switch
|
||||
$case $cmp_by_value && $has_context:
|
||||
while (cmp(list[h], pivot, context) >= 0 && l < h) h--;
|
||||
if (l < h) list[l++] = list[h];
|
||||
while (cmp(list[l], pivot, context) <= 0 && l < h) l++;
|
||||
$case $cmp_by_value:
|
||||
while (cmp(list[h], pivot) >= 0 && l < h) h--;
|
||||
$case !$no_cmp:
|
||||
while (cmp(&list[h], &pivot) >= 0 && l < h) h--;
|
||||
$default:
|
||||
while (greater_eq(list[h], pivot) && l < h) h--;
|
||||
$endswitch
|
||||
if (l < h) list[l++] = list[h];
|
||||
$switch
|
||||
$case $cmp_by_value:
|
||||
if (l < h) list[l++] = list[h];
|
||||
while (cmp(list[l], pivot) <= 0 && l < h) l++;
|
||||
$case !$no_cmp:
|
||||
$case $has_cmp && $has_context:
|
||||
while (cmp(&list[h], &pivot, context) >= 0 && l < h) h--;
|
||||
if (l < h) list[l++] = list[h];
|
||||
while (cmp(&list[l], &pivot, context) <= 0 && l < h) l++;
|
||||
$case $has_cmp:
|
||||
while (cmp(&list[h], &pivot) >= 0 && l < h) h--;
|
||||
if (l < h) list[l++] = list[h];
|
||||
while (cmp(&list[l], &pivot) <= 0 && l < h) l++;
|
||||
$default:
|
||||
while (greater_eq(list[h], pivot) && l < h) h--;
|
||||
if (l < h) list[l++] = list[h];
|
||||
while (less_eq(list[l], pivot) && l < h) l++;
|
||||
$endswitch
|
||||
if (l < h) list[h--] = list[l];
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
module std::sort;
|
||||
|
||||
|
||||
macro usz @len_from_list(&list)
|
||||
{
|
||||
$if $defined(list.len()):
|
||||
@@ -10,24 +9,48 @@ macro usz @len_from_list(&list)
|
||||
$endif
|
||||
}
|
||||
|
||||
macro bool @is_comparer(#cmp, #list)
|
||||
macro bool @is_sortable(#list)
|
||||
{
|
||||
var $params = $typeof(#cmp).params;
|
||||
$if $params.len != 2:
|
||||
return false;
|
||||
$else
|
||||
$if $params[0] != $params[1]:
|
||||
$switch
|
||||
$case !$defined(#list[0]):
|
||||
return false;
|
||||
$else
|
||||
var $element = @typeid(#list[0]);
|
||||
$switch
|
||||
$case $element == $params[0]:
|
||||
return true;
|
||||
$case $and($params[0].kindof == POINTER, $params[0].inner == $element):
|
||||
return true;
|
||||
$default:
|
||||
return false;
|
||||
$endswitch
|
||||
$endif
|
||||
$endif
|
||||
$case !$defined(#list.len):
|
||||
return false;
|
||||
$case $and($defined(&#list[0]) && !types::is_same($typeof(&#list[0]), $typeof(#list[0])*)):
|
||||
return false;
|
||||
$default:
|
||||
return true;
|
||||
$endswitch;
|
||||
}
|
||||
|
||||
macro bool @is_valid_context(#cmp, #context)
|
||||
{
|
||||
return @is_valid_macro_slot(#cmp) || @is_empty_macro_slot(#context);
|
||||
}
|
||||
|
||||
macro bool @is_valid_cmp_fn(#cmp, #list, #context)
|
||||
{
|
||||
var $Type = $typeof(#cmp);
|
||||
var $no_context = @is_empty_macro_slot(#context);
|
||||
$switch
|
||||
$case @is_empty_macro_slot(#cmp): return true;
|
||||
$case $or($Type.kindof != FUNC, $Type.returns.kindof != SIGNED_INT): return false;
|
||||
$case $defined(#cmp(#list[0], #list[0], #context)): return true;
|
||||
$case $defined(#cmp(#list[0], #list[0])): return $no_context;
|
||||
$case $defined(#cmp(&#list[0], &#list[0], #context)): return true;
|
||||
$case $defined(#cmp(&#list[0], &#list[0])): return $no_context;
|
||||
$default: return false;
|
||||
$endswitch
|
||||
}
|
||||
|
||||
macro bool @is_cmp_key_fn(#key_fn, #list)
|
||||
{
|
||||
$switch
|
||||
$case @is_empty_macro_slot(#key_fn): return true;
|
||||
$case $typeof(#key_fn).kindof != FUNC: return false;
|
||||
$case $typeof(#key_fn).returns.kindof != UNSIGNED_INT: return false;
|
||||
$case $defined(#key_fn(#list[0])): return true;
|
||||
$case $defined(#key_fn(&&(#list[0]))): return true;
|
||||
$default: return false;
|
||||
$endswitch
|
||||
}
|
||||
@@ -62,7 +62,7 @@ fn void! NativeMutex.lock_timeout(&self, ulong ms)
|
||||
{
|
||||
if (!ms) break;
|
||||
ulong sleep = min(5, ms);
|
||||
if (!libc::nanosleep(&& TimeSpec { .s = 0, .ns = (CLong)sleep * 1000_000 }, null)) return ThreadFault.LOCK_FAILED?;
|
||||
if (!libc::nanosleep(&&time::ms(ms).to_timespec(), null)) return ThreadFault.LOCK_FAILED?;
|
||||
ms -= sleep;
|
||||
}
|
||||
switch (result)
|
||||
@@ -128,8 +128,9 @@ fn void! NativeConditionVariable.wait_timeout(&cond, NativeMutex* mtx, ulong ms)
|
||||
{
|
||||
TimeSpec now;
|
||||
if (libc::timespec_get(&now, libc::TIME_UTC) != libc::TIME_UTC) return ThreadFault.WAIT_FAILED?;
|
||||
now.s += (Time_t)(ms / 1000);
|
||||
now.ns += (CLong)((ms % 1000) * 1000_000);
|
||||
now.s += (Time_t)(ms / 1000 + now.ns / 1000_000_000);
|
||||
now.ns = now.ns % 1000_000_000;
|
||||
switch (posix::pthread_cond_timedwait(cond, &mtx.mutex, &now))
|
||||
{
|
||||
case errno::ETIMEDOUT:
|
||||
|
||||
@@ -48,7 +48,7 @@ fn void! NativeMutex.init(&mtx, MutexType type)
|
||||
win32::initializeCriticalSection(&(mtx.critical_section));
|
||||
return;
|
||||
}
|
||||
if (!(mtx.handle = win32::createMutex(null, false, null))) return ThreadFault.INIT_FAILED?;
|
||||
if (!(mtx.handle = win32::createMutex(null, 0, null))) return ThreadFault.INIT_FAILED?;
|
||||
}
|
||||
|
||||
fn void! NativeMutex.destroy(&mtx)
|
||||
@@ -90,8 +90,9 @@ fn void! NativeMutex.lock(&mtx)
|
||||
/**
|
||||
* @require mtx.timed "Only available for timed locks"
|
||||
**/
|
||||
fn void! NativeMutex.lock_timeout(&mtx, usz ms)
|
||||
fn void! NativeMutex.lock_timeout(&mtx, ulong ms)
|
||||
{
|
||||
if (ms > uint.max) ms = uint.max;
|
||||
switch (win32::waitForSingleObject(mtx.handle, (uint)ms))
|
||||
{
|
||||
case win32::WAIT_OBJECT_0:
|
||||
@@ -208,7 +209,7 @@ fn void! timedwait(NativeConditionVariable* cond, NativeMutex* mtx, uint timeout
|
||||
|
||||
mtx.unlock()!;
|
||||
|
||||
uint result = win32::waitForMultipleObjects(2, &cond.events, false, timeout);
|
||||
uint result = win32::waitForMultipleObjects(2, &cond.events, 0, timeout);
|
||||
switch (result)
|
||||
{
|
||||
case win32::WAIT_TIMEOUT:
|
||||
@@ -244,9 +245,10 @@ fn void! NativeConditionVariable.wait(&cond, NativeMutex* mtx) @inline
|
||||
return timedwait(cond, mtx, win32::INFINITE) @inline;
|
||||
}
|
||||
|
||||
fn void! NativeConditionVariable.wait_timeout(&cond, NativeMutex* mtx, uint time) @inline
|
||||
fn void! NativeConditionVariable.wait_timeout(&cond, NativeMutex* mtx, ulong ms) @inline
|
||||
{
|
||||
return timedwait(cond, mtx, time) @inline;
|
||||
if (ms > uint.max) ms = uint.max;
|
||||
return timedwait(cond, mtx, (uint)ms) @inline;
|
||||
}
|
||||
|
||||
fn void! NativeThread.create(&thread, ThreadFn func, void* args)
|
||||
|
||||
@@ -53,9 +53,9 @@ macro void! ConditionVariable.wait(&cond, Mutex* mutex)
|
||||
{
|
||||
return NativeConditionVariable.wait((NativeConditionVariable*)cond, (NativeMutex*)mutex);
|
||||
}
|
||||
macro void! ConditionVariable.wait_timeout(&cond, Mutex* mutex, ulong timeout)
|
||||
macro void! ConditionVariable.wait_timeout(&cond, Mutex* mutex, ulong ms)
|
||||
{
|
||||
return NativeConditionVariable.wait_timeout((NativeConditionVariable*)cond, (NativeMutex*)mutex, timeout);
|
||||
return NativeConditionVariable.wait_timeout((NativeConditionVariable*)cond, (NativeMutex*)mutex, ms);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ distinct Duration = long;
|
||||
distinct Clock = ulong;
|
||||
distinct NanoDuration (Printable) = long;
|
||||
|
||||
const Duration US = 1;
|
||||
const Duration MS = 1_000;
|
||||
const Duration SEC = 1_000_000;
|
||||
const Duration MIN = 60 * SEC;
|
||||
@@ -15,6 +16,7 @@ const Duration WEEK = 7 * DAY;
|
||||
const Duration MONTH = 30 * WEEK;
|
||||
const Duration YEAR = 36525 * DAY / 100;
|
||||
|
||||
fn Duration us(long l) @inline => (Duration)l * US;
|
||||
fn Duration ms(long l) @inline => (Duration)l * MS;
|
||||
fn Duration sec(long l) @inline => (Duration)l * SEC;
|
||||
fn Duration min(long l) @inline => (Duration)l * MIN;
|
||||
@@ -78,7 +80,7 @@ fn Time now()
|
||||
$endif
|
||||
}
|
||||
|
||||
fn Time Time.add_seconds(time, long seconds) => time + (Time)(seconds * (long)MS);
|
||||
fn Time Time.add_seconds(time, long seconds) => time + (Time)(seconds * (long)SEC);
|
||||
fn Time Time.add_minutes(time, long minutes) => time + (Time)(minutes * (long)MIN);
|
||||
fn Time Time.add_hours(time, long hours) => time + (Time)(hours * (long)HOUR);
|
||||
fn Time Time.add_days(time, long days) => time + (Time)(days * (long)DAY);
|
||||
|
||||
@@ -10,15 +10,26 @@ import json
|
||||
import shutil
|
||||
import hashlib
|
||||
import zipfile
|
||||
import tempfile
|
||||
import argparse
|
||||
import subprocess
|
||||
import urllib.request
|
||||
import os
|
||||
import ssl
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
OUTPUT = Path("msvc_temp") # output folder
|
||||
if (platform.system() == "Windows"):
|
||||
print("Creating msvc_sdk for compilation without VS.")
|
||||
else:
|
||||
print("Creating msvc_sdk for cross platform compilation to Windows.")
|
||||
|
||||
OUTPUT = Path(tempfile.mkdtemp()) # output folder
|
||||
SDK_OUTPUT = Path("msvc_sdk")
|
||||
|
||||
if (not os.environ.get('PYTHONHTTPSVERIFY', '') and
|
||||
getattr(ssl, '_create_unverified_context', None)):
|
||||
ssl._create_default_https_context = ssl._create_unverified_context
|
||||
|
||||
MANIFEST_URL = "https://aka.ms/vs/17/release/channel"
|
||||
|
||||
def download(url):
|
||||
@@ -57,7 +68,6 @@ def get_msi_cabs(msi):
|
||||
|
||||
def first(items, cond):
|
||||
return next(item for item in items if cond(item))
|
||||
|
||||
|
||||
### parse command-line arguments
|
||||
|
||||
@@ -135,9 +145,7 @@ if not args.accept_license:
|
||||
if not accept or accept[0].lower() != "y":
|
||||
exit(0)
|
||||
|
||||
shutil.rmtree(OUTPUT, ignore_errors = True)
|
||||
shutil.rmtree(SDK_OUTPUT, ignore_errors = True)
|
||||
OUTPUT.mkdir()
|
||||
total_download = 0
|
||||
|
||||
### download Windows SDK
|
||||
@@ -157,7 +165,7 @@ for arch in archs:
|
||||
msvc_packages.append(f"microsoft.vc.{msvc_ver}.crt.{arch}.desktop.base")
|
||||
msvc_packages.append(f"microsoft.vc.{msvc_ver}.crt.{arch}.store.base")
|
||||
msvc_packages.append(f"microsoft.vc.{msvc_ver}.asan.{arch}.base")
|
||||
|
||||
|
||||
for pkg in msvc_packages:
|
||||
p = first(packages[pkg], lambda p: p.get("language") in (None, "en-US"))
|
||||
for payload in p["payloads"]:
|
||||
@@ -226,15 +234,10 @@ lib = list((OUTPUT / "VC/Tools/MSVC/").glob("*/lib"))[0]
|
||||
|
||||
SDK_OUTPUT.mkdir(exist_ok=True)
|
||||
|
||||
|
||||
|
||||
for arch in archs:
|
||||
out_dir = SDK_OUTPUT / arch
|
||||
shutil.copytree(ucrt / arch, out_dir, dirs_exist_ok=True)
|
||||
shutil.copytree(um / arch, out_dir, dirs_exist_ok=True)
|
||||
shutil.copytree(um / arch, out_dir, dirs_exist_ok=True)
|
||||
shutil.copytree(lib / arch, out_dir, dirs_exist_ok=True)
|
||||
|
||||
### cleanup
|
||||
|
||||
shutil.rmtree(OUTPUT, ignore_errors=True)
|
||||
|
||||
print("Congratulations! The 'msvc_sdk' directory was successfully generated.")
|
||||
|
||||
176
releasenotes.md
176
releasenotes.md
@@ -1,5 +1,175 @@
|
||||
# C3C Release Notes
|
||||
|
||||
## 0.6.1 Change list
|
||||
|
||||
### Changes / improvements
|
||||
- Addition of $append and $concat functions.
|
||||
- Added $$str_hash, $$str_upper, $$str_lower, $$str_find builtins.
|
||||
- Improved error notes when call expressions have errors.
|
||||
- Trailing body arguments may now be `&ref`, `#hash`, `$const` and `$Type` arguments.
|
||||
- "panic-msg" setting to suppress panic message output.
|
||||
- Require `@export` functions to have `@export` types.
|
||||
- Disallow leading/trailing/duplicate '_' in module names.
|
||||
- Updated mangling.
|
||||
- Added `$$unaligned_load` and `$$unaligned_store`.
|
||||
- `--no-headers` option to suppress creating headers when generating a library.
|
||||
- Support c-file compilation in libraries.
|
||||
- Allow using $defined(&a[1]) to check if the operation is supported.
|
||||
- Max number of members in a struct is limited to 65535.
|
||||
- The maximum number of parameters in a call is now 255, up from 127.
|
||||
- Array comparison now uses built-in memcmp on LLVM to enable optimizations.
|
||||
- Prevent implicit array casts to pointers with higher alignment #1237.
|
||||
- Macro `$case` statements now pick the first match and does not evaluate the rest.
|
||||
- `manifest.json` is now checked for incorrect keys.
|
||||
- Added `--list-manifest-properties` to list the available properties in `manifest.json`.
|
||||
- Indexing into a constant array / struct now works at compile time.
|
||||
- Improved error message when trying user foreach with an untyped list.
|
||||
|
||||
### Fixes
|
||||
- Error with unsigned compare in `@ensure` when early returning 0 #1207.
|
||||
- Prevent Mach-O from removing `@init` and `@dynamic` in a more reliable way #1200.
|
||||
- Fix of missing copy of parameterized custom attributes.
|
||||
- Fixed crash on certain recursive function definitions #1209.
|
||||
- Return the typekind "FUNC" for a function pointer.
|
||||
- No longer possible to dereference a function pointer.
|
||||
- Fix bug with @jump miscompile.
|
||||
- Bit negate does implicit integer promotion.
|
||||
- Bitstructs, unions and flexible arrays now correctly emitted in headers.
|
||||
- Fix distinct inline conversions.
|
||||
- Bit negating const zero flags would give an incorrect result.
|
||||
- Fix to scalar -> vector conversions.
|
||||
- Bug fix for rethrow + defer catch.
|
||||
- Wrong size for structs containing overaligned structs #1219
|
||||
- $typeof(*x) should be valid when x is an `[out]` parameter #1226
|
||||
- Fix ABI lowering for 128 bit vectors on Linux.
|
||||
- Bad error message when using a generic method without generic parameters #1228
|
||||
- Private function called from nested macro not visible to linker #1232
|
||||
- Bitstructs in structs would not be correctly be handled in some cases.
|
||||
- Fix problem where a $$FUNC would return "<GLOBAL>" when evaluated for a static in a function #1236.
|
||||
- `ordinal` is no longer a valid associated value name for enums.
|
||||
- Constants defined by indexing into another constant could fail codegen.
|
||||
- Stdlib nolibc code bugs fixed.
|
||||
- Regression: duplicate symbols with static variable declared in macro #1248.
|
||||
- Unsplat with named parameters was accidentally disallowed.
|
||||
- Reference parameter doesn't work with vector subscript #1250.
|
||||
- The msvc_sdk script failed to work properly on windows when run in folders with spaces.
|
||||
|
||||
### Stdlib changes
|
||||
- Added `remove_first_item` `remove_last_item` and `remove_item` as aliases for the `match` functions.
|
||||
- Added @str_hash, @str_upper, @str_lower, @str_find compile time macros.
|
||||
- Remove "panic" text from unreachable() when safe mode is turned off.
|
||||
- Added `@unaligned_store` and `@unaligned_load`.
|
||||
- Null ZString, DString or pointer prints "(null)" for printf.
|
||||
- Updated sorting API.
|
||||
- Insertion sort and counting sort added.
|
||||
- Added missing `mem` and `mem::allocator` functions for aligned allocations.
|
||||
- Added `new_init_with_array` and `temp_init_with_array` for List.
|
||||
- Fixed posix `NativeMutex.lock_timeout`.
|
||||
- Fixed `env::ARCH_32_BIT` and `env::ARCH_64_BIT`.
|
||||
- Added `time::us`.
|
||||
|
||||
## 0.6.0 Change list
|
||||
|
||||
### Changes / improvements
|
||||
- `@default` implementations for interfaces removed.
|
||||
- `any*` => `any`, same for interfaces.
|
||||
- Private / local globals now have `internal` visibility in LLVM.
|
||||
- Updated enum syntax.
|
||||
- 'rgba' also available for swizzling.
|
||||
- The name "subarray" has been replaced by the more well known name "slice' across the codebase.
|
||||
- Improved alignment handling.
|
||||
- Add `--output-dir` to command line. #1155
|
||||
- Allow making distinct types out of "void", "typeid", "anyfault" and faults.
|
||||
- Removed `--system-linker` setting.
|
||||
- "Try" expressions may not be any binary or unary expressions. So for example `try foo() + 1` is disallowed.
|
||||
- Added `$$REGISTER_SIZE` for int register size.
|
||||
- `assert(false)` only allowed in unused branches or in tests. Compile time failed asserts is a compile time error.
|
||||
- Require expression blocks returning values to have the value used.
|
||||
- Detect "unsigned >= 0" as errors.
|
||||
- Improve callstack debug information #1184.
|
||||
- Request jump table using @jump for switches.
|
||||
- Improved inline debug information.
|
||||
- Improved error messages on inlined macros.
|
||||
- Introduce MSVC compatible SIMD ABI.
|
||||
- `$foreach` doesn't create an implicit syntactic scope.
|
||||
- Error of `@if` depends on `@if`
|
||||
- Support `defer (catch err)`
|
||||
- Added `print-input` command argument to print all files used for compilation
|
||||
- Allow recursive function definitions as long as they are pointers. #1182
|
||||
- Default CPU to native if less than AVX, otherwise use AVX.
|
||||
- Bounds checking on length for `foo[1:2]` slicing #1191.
|
||||
- Foreach uses non-wrapping add/dec.
|
||||
|
||||
### Fixes
|
||||
- Fixed issue in safe mode when converting enums.
|
||||
- Better checking of operator methods.
|
||||
- Bug when assigning an optional from an optional.
|
||||
- Lambdas were not type checked thoroughly #1185.
|
||||
- Fix problems using reflection on interface types #1203.
|
||||
- `@param` with unnamed macro varargs could crash the compiler.
|
||||
- Compiler crash using enum nameof from different module #1205.
|
||||
- Incorrect length passed to scratch buffer printf.
|
||||
- Casting to a bitstruct would be allowed even if the type was the wrong size.
|
||||
- Generic modules parameterized with constants would sometimes get the wrong parameterized module name causing conversion errors #1192.
|
||||
- Duplicate emit of expressions on negation would incorrectly compile negated macros.
|
||||
- Casting a slice address to its pointer type should not compile #1193.
|
||||
- Union is not properly zero-initialized with designated initializer #1194.
|
||||
- Compile time fmod evaluates to 0 #1195.
|
||||
- Assertion failed when casting (unsigned) argument to enum #1196
|
||||
- Correct debug info on parameters without storage.
|
||||
- Fix location on foreach debug output.
|
||||
- Compiler crash on designated initializer for structs with bitstruct.
|
||||
|
||||
### Stdlib changes
|
||||
- "init_new/init_temp" removed.
|
||||
- LinkedList API rewritten.
|
||||
- List "pop" and "remove" function now return Optionals.
|
||||
- RingBuffer API rewritten. Allocator interface changed.
|
||||
- Deprecated Allocator, DString and mem functions removed.
|
||||
- "identity" functions are now constants for Matrix and Complex numbers.
|
||||
- Removed 'append' from Object and List, replaced by 'push'.
|
||||
- `GenericList` renamed `AnyList`.
|
||||
- Proper handling of '.' and Win32 '//server' paths.
|
||||
- Path normalization - fix possible null terminator out of bounds.
|
||||
- Add 'zstr' variants for `string::new_format` / `string::tformat`.
|
||||
- Fix mutex and wait signatures for Win32.
|
||||
|
||||
## 0.5.5 Change list
|
||||
|
||||
### Changes / improvements
|
||||
- Disallow multiple `_` in a row in digits, e.g. `1__000`.
|
||||
- Added `@link` attribute.
|
||||
- New 'linker' build option.
|
||||
- "linker" project setting updated, "system-linker" removed.
|
||||
|
||||
### Fixes
|
||||
- Struct/union members now correctly rejects members without storage size #1147.
|
||||
- `math::pow` will now correctly promote integer arguments.
|
||||
- Pointer difference would fail where alignment != size (structs etc) #1150
|
||||
- Fixed array calculation for npot2 vectors.
|
||||
- $$memcpy_inline and $$memset_inline fixed.
|
||||
- `.$Type = ...` and `.$foo = ...` now works #1156.
|
||||
- `int.min` incorrect behaviour #1154.
|
||||
- Bitstruct cast to other bitstruct by way of underlying type would fail #1159.
|
||||
- Bug in `time.add_seconds` #1162.
|
||||
- Remove initial './' in Win32 and convert '/' to '\' for paths when running a binary.
|
||||
- 'output' directory for projects was incorrect in templates.
|
||||
- Regression: no stacktrace.
|
||||
- For MacOS, running with higher optimization would crash as initializers were removed.
|
||||
- `compile-run` and `run` now returns the proper return code.
|
||||
- Allow String constants -> ichar*, and allow integer pointers to explicitly convert between unsigned signed.
|
||||
- Bug in unaligned return value lowering for Aarch64.
|
||||
|
||||
### Stdlib changes
|
||||
- Added `new_aligned` and `alloc_aligned` functions to prevent accidental under-alignment when allocating simd.
|
||||
- Fixes to realloc of aligned allocations
|
||||
- Use native Windows calls on aligned allocations on Windows.
|
||||
- mem::copy_inline, mem::clear_inline and mem::set_inline added.
|
||||
- mem::copy / clear / set no longer has an `$inline` attribute.
|
||||
- Native aligned libc malloc on Windows & POSIX.
|
||||
- Simplification of the allocator interface.
|
||||
- CoreFoundation only linked on MacOS when used.
|
||||
|
||||
## 0.5.4 Change list
|
||||
|
||||
### Changes / improvements
|
||||
@@ -325,13 +495,13 @@
|
||||
- Fixed errors on flexible array slices.
|
||||
- Fix of `readdir` issues on macOS.
|
||||
- Fix to slice assignment of distinct types.
|
||||
- Fix of issue casting subarrays to distinct types.
|
||||
- Fix of issue casting slices to distinct types.
|
||||
- Fixes to `split`, `rindex_of`.
|
||||
- List no longer uses the temp allocator by default.
|
||||
- Remove test global when not in test mode.
|
||||
- Fix sum/product on floats.
|
||||
- Fix error on void! return of macros.
|
||||
- Removed too permissive casts on subarrays.
|
||||
- Removed too permissive casts on slices.
|
||||
- Using C files correctly places objects in the build folder.
|
||||
- Fix of overaligned deref.
|
||||
- Fix negating a float vector.
|
||||
@@ -434,7 +604,7 @@
|
||||
- Added type.inner and type.len reflection.
|
||||
- Support float mod operations.
|
||||
- Add float.max/min.
|
||||
- Allow [in] contract to be used on subarray types.
|
||||
- Allow [in] contract to be used on slices.
|
||||
- Add linker and linked dir arguments to build files.
|
||||
- Auto-import std::core.
|
||||
- LLVM 15 support.
|
||||
|
||||
@@ -18,7 +18,7 @@ Some short names:
|
||||
| int | cond | sw/sn | always | explptr | no | expand | explbase | edist | no | no | no | no | no | edist | no |
|
||||
| float | cond | expl | sw/sn | no | no | expand | no | edist | no | no | no | no | no | no | no |
|
||||
| pointer | cond | explptr | no | ptrconv | arve | expand | no | edist | no | no | no | yes | expl | no | expl |
|
||||
| subarray | cond | no | no | no | saconv | no | no | edist | no? | no | no | no | no | no | no |
|
||||
| slice | cond | no | no | no | saconv | no | no | edist | no? | no | no | no | no | no | no |
|
||||
| vec | cond | no | no | no | no | as base | no | edist | expl | no | no | no | no | no | no |
|
||||
| bits | no | explbase | no | no | no | no | no? | edist | explbase | no | no | no | no | no | no |
|
||||
| distc | edist | edist | edist | edist | edist | edist | edist | edist | edist | edist | edist | edist | edist | edist | edist |
|
||||
|
||||
@@ -9,7 +9,7 @@ fault InterpretError
|
||||
INTEPRET_FAILED
|
||||
}
|
||||
|
||||
fn void! print_error(usz pos, String err)
|
||||
fn void! print_error_type_at(usz pos, String err)
|
||||
{
|
||||
io::printfn("Error at %s: %s", pos, err);
|
||||
return InterpretError.INTEPRET_FAILED?;
|
||||
@@ -25,10 +25,10 @@ fn void! brainf(String program)
|
||||
switch (c)
|
||||
{
|
||||
case '<':
|
||||
if (!mem) return print_error(sp, "Memory underflow");
|
||||
if (!mem) return print_error_type_at(sp, "Memory underflow");
|
||||
mem--;
|
||||
case '>':
|
||||
if (mem == BF_MEM - 1) return print_error(sp, "Memory overflow");
|
||||
if (mem == BF_MEM - 1) return print_error_type_at(sp, "Memory overflow");
|
||||
mem++;
|
||||
case '+':
|
||||
memory[mem]++;
|
||||
@@ -45,7 +45,7 @@ fn void! brainf(String program)
|
||||
usz start = sp - 1;
|
||||
while (indent)
|
||||
{
|
||||
if (sp == program.len) return print_error(start, "No matching ']'");
|
||||
if (sp == program.len) return print_error_type_at(start, "No matching ']'");
|
||||
switch (program[sp++])
|
||||
{
|
||||
case ']': indent--;
|
||||
@@ -59,7 +59,7 @@ fn void! brainf(String program)
|
||||
usz indent = 1;
|
||||
while (indent)
|
||||
{
|
||||
if (!sp) return print_error(start, "No matching '['");
|
||||
if (!sp) return print_error_type_at(start, "No matching '['");
|
||||
switch (program[--sp])
|
||||
{
|
||||
case ']': indent++;
|
||||
|
||||
@@ -27,7 +27,7 @@ struct Summary
|
||||
bool ok;
|
||||
}
|
||||
|
||||
fn void! Summary.print(Summary *s, OutStream* out)
|
||||
fn void! Summary.print(Summary *s, OutStream out)
|
||||
{
|
||||
io::fprintf(out, "Summary({ .title = %s, .ok = %s})", s.title.get() ?? "missing", s.ok)!;
|
||||
}
|
||||
@@ -76,7 +76,7 @@ fn void main()
|
||||
const String[] URLS = { "good", "title-empty", "title-missing", "head-missing", "fail" };
|
||||
DynamicArenaAllocator dynamic_arena;
|
||||
dynamic_arena.init(1024, allocator::heap());
|
||||
OutStream* out = io::stdout();
|
||||
OutStream out = io::stdout();
|
||||
foreach (String url : URLS)
|
||||
{
|
||||
mem::@scoped(&dynamic_arena)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
module levenshtein;
|
||||
import std::math;
|
||||
|
||||
// This levenshtein exercises C3 subarrays.
|
||||
// This levenshtein exercises C3 slices.
|
||||
fn int levenshtein(String s, String t)
|
||||
{
|
||||
// if either string is empty, difference is inserting all chars
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user