@shd101wyy/yo 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +17 -0
- package/README.md +80 -0
- package/out/cjs/index.cjs +51 -0
- package/out/cjs/yo-cli.cjs +2158 -0
- package/out/esm/index.mjs +51 -0
- package/out/types/src/codegen/async/runtime.d.ts +2 -0
- package/out/types/src/codegen/async/state-code-gen.d.ts +10 -0
- package/out/types/src/codegen/async/state-machine.d.ts +13 -0
- package/out/types/src/codegen/c/collection.d.ts +3 -0
- package/out/types/src/codegen/codegen-c.d.ts +12 -0
- package/out/types/src/codegen/constants.d.ts +3 -0
- package/out/types/src/codegen/expressions/array.d.ts +4 -0
- package/out/types/src/codegen/expressions/generation.d.ts +11 -0
- package/out/types/src/codegen/expressions/index.d.ts +2 -0
- package/out/types/src/codegen/functions/collection.d.ts +5 -0
- package/out/types/src/codegen/functions/context.d.ts +57 -0
- package/out/types/src/codegen/functions/generation.d.ts +25 -0
- package/out/types/src/codegen/functions/index.d.ts +2 -0
- package/out/types/src/codegen/index.d.ts +20 -0
- package/out/types/src/codegen/parallelism/runtime.d.ts +2 -0
- package/out/types/src/codegen/types/collection.d.ts +8 -0
- package/out/types/src/codegen/types/generation.d.ts +13 -0
- package/out/types/src/codegen/types/index.d.ts +2 -0
- package/out/types/src/codegen/utils/fixup.d.ts +2 -0
- package/out/types/src/codegen/utils/index.d.ts +77 -0
- package/out/types/src/codegen/values/index.d.ts +1 -0
- package/out/types/src/emitter.d.ts +11 -0
- package/out/types/src/env.d.ts +85 -0
- package/out/types/src/error.d.ts +45 -0
- package/out/types/src/evaluator/async/await-analysis-types.d.ts +23 -0
- package/out/types/src/evaluator/async/await-analysis.d.ts +5 -0
- package/out/types/src/evaluator/builtins/alignof.d.ts +8 -0
- package/out/types/src/evaluator/builtins/and_or.d.ts +8 -0
- package/out/types/src/evaluator/builtins/arc_fns.d.ts +58 -0
- package/out/types/src/evaluator/builtins/array_fns.d.ts +0 -0
- package/out/types/src/evaluator/builtins/as.d.ts +8 -0
- package/out/types/src/evaluator/builtins/async_fns.d.ts +8 -0
- package/out/types/src/evaluator/builtins/compt_assert.d.ts +8 -0
- package/out/types/src/evaluator/builtins/compt_boolean_fns.d.ts +8 -0
- package/out/types/src/evaluator/builtins/compt_expect_error.d.ts +8 -0
- package/out/types/src/evaluator/builtins/compt_list_fns.d.ts +33 -0
- package/out/types/src/evaluator/builtins/compt_print.d.ts +8 -0
- package/out/types/src/evaluator/builtins/compt_string_fns.d.ts +8 -0
- package/out/types/src/evaluator/builtins/consume.d.ts +8 -0
- package/out/types/src/evaluator/builtins/drop.d.ts +8 -0
- package/out/types/src/evaluator/builtins/dup.d.ts +8 -0
- package/out/types/src/evaluator/builtins/expr_fns.d.ts +33 -0
- package/out/types/src/evaluator/builtins/future_fns.d.ts +8 -0
- package/out/types/src/evaluator/builtins/gc.d.ts +8 -0
- package/out/types/src/evaluator/builtins/gensym.d.ts +8 -0
- package/out/types/src/evaluator/builtins/impl_constraint.d.ts +8 -0
- package/out/types/src/evaluator/builtins/macro_expand.d.ts +8 -0
- package/out/types/src/evaluator/builtins/numeric_fns.d.ts +8 -0
- package/out/types/src/evaluator/builtins/panic.d.ts +8 -0
- package/out/types/src/evaluator/builtins/ptr_fns.d.ts +8 -0
- package/out/types/src/evaluator/builtins/quote.d.ts +13 -0
- package/out/types/src/evaluator/builtins/rc.d.ts +8 -0
- package/out/types/src/evaluator/builtins/sizeof.d.ts +8 -0
- package/out/types/src/evaluator/builtins/the.d.ts +8 -0
- package/out/types/src/evaluator/builtins/type_fns.d.ts +28 -0
- package/out/types/src/evaluator/builtins/va_start.d.ts +8 -0
- package/out/types/src/evaluator/builtins/var_fns.d.ts +18 -0
- package/out/types/src/evaluator/calls/array.d.ts +13 -0
- package/out/types/src/evaluator/calls/array_type.d.ts +11 -0
- package/out/types/src/evaluator/calls/closure_type.d.ts +11 -0
- package/out/types/src/evaluator/calls/compt_function.d.ts +19 -0
- package/out/types/src/evaluator/calls/compt_list_type.d.ts +11 -0
- package/out/types/src/evaluator/calls/function.d.ts +16 -0
- package/out/types/src/evaluator/calls/function_type.d.ts +15 -0
- package/out/types/src/evaluator/calls/helper.d.ts +42 -0
- package/out/types/src/evaluator/calls/iso.d.ts +15 -0
- package/out/types/src/evaluator/calls/module_type.d.ts +11 -0
- package/out/types/src/evaluator/calls/numeric_type.d.ts +15 -0
- package/out/types/src/evaluator/calls/pointer.d.ts +8 -0
- package/out/types/src/evaluator/calls/pointer_type.d.ts +14 -0
- package/out/types/src/evaluator/calls/type.d.ts +12 -0
- package/out/types/src/evaluator/context.d.ts +169 -0
- package/out/types/src/evaluator/exprs/_expr.d.ts +8 -0
- package/out/types/src/evaluator/exprs/assignment.d.ts +9 -0
- package/out/types/src/evaluator/exprs/begin.d.ts +10 -0
- package/out/types/src/evaluator/exprs/binding.d.ts +12 -0
- package/out/types/src/evaluator/exprs/c_include.d.ts +8 -0
- package/out/types/src/evaluator/exprs/cond.d.ts +8 -0
- package/out/types/src/evaluator/exprs/destructuring_assignment.d.ts +33 -0
- package/out/types/src/evaluator/exprs/exists.d.ts +0 -0
- package/out/types/src/evaluator/exprs/expr.d.ts +9 -0
- package/out/types/src/evaluator/exprs/extern.d.ts +8 -0
- package/out/types/src/evaluator/exprs/identifer_and_operator.d.ts +9 -0
- package/out/types/src/evaluator/exprs/import.d.ts +9 -0
- package/out/types/src/evaluator/exprs/initialization_assignment.d.ts +8 -0
- package/out/types/src/evaluator/exprs/match.d.ts +8 -0
- package/out/types/src/evaluator/exprs/open.d.ts +8 -0
- package/out/types/src/evaluator/exprs/property_access.d.ts +8 -0
- package/out/types/src/evaluator/exprs/recur.d.ts +8 -0
- package/out/types/src/evaluator/exprs/subtype_of.d.ts +21 -0
- package/out/types/src/evaluator/exprs/test.d.ts +8 -0
- package/out/types/src/evaluator/exprs/typeof.d.ts +8 -0
- package/out/types/src/evaluator/exprs/while.d.ts +8 -0
- package/out/types/src/evaluator/index.d.ts +26 -0
- package/out/types/src/evaluator/types/array.d.ts +8 -0
- package/out/types/src/evaluator/types/closure.d.ts +8 -0
- package/out/types/src/evaluator/types/compt_list.d.ts +8 -0
- package/out/types/src/evaluator/types/concrete_module.d.ts +8 -0
- package/out/types/src/evaluator/types/dyn.d.ts +8 -0
- package/out/types/src/evaluator/types/enum.d.ts +8 -0
- package/out/types/src/evaluator/types/expr_synthesizer.d.ts +14 -0
- package/out/types/src/evaluator/types/field.d.ts +14 -0
- package/out/types/src/evaluator/types/fn_module.d.ts +8 -0
- package/out/types/src/evaluator/types/function.d.ts +58 -0
- package/out/types/src/evaluator/types/future_module.d.ts +8 -0
- package/out/types/src/evaluator/types/module.d.ts +19 -0
- package/out/types/src/evaluator/types/newtype.d.ts +8 -0
- package/out/types/src/evaluator/types/object.d.ts +8 -0
- package/out/types/src/evaluator/types/proofs.d.ts +0 -0
- package/out/types/src/evaluator/types/slice.d.ts +8 -0
- package/out/types/src/evaluator/types/struct.d.ts +8 -0
- package/out/types/src/evaluator/types/synthesizer.d.ts +16 -0
- package/out/types/src/evaluator/types/tuple.d.ts +18 -0
- package/out/types/src/evaluator/types/union.d.ts +8 -0
- package/out/types/src/evaluator/types/utils.d.ts +71 -0
- package/out/types/src/evaluator/types/validation.d.ts +3 -0
- package/out/types/src/evaluator/utils/array-utils.d.ts +15 -0
- package/out/types/src/evaluator/utils/closure.d.ts +35 -0
- package/out/types/src/evaluator/utils.d.ts +4 -0
- package/out/types/src/evaluator/values/anonymous_function.d.ts +8 -0
- package/out/types/src/evaluator/values/anonymous_module.d.ts +17 -0
- package/out/types/src/evaluator/values/anonymous_struct.d.ts +8 -0
- package/out/types/src/evaluator/values/array.d.ts +8 -0
- package/out/types/src/evaluator/values/boolean.d.ts +3 -0
- package/out/types/src/evaluator/values/char.d.ts +3 -0
- package/out/types/src/evaluator/values/compt_list.d.ts +8 -0
- package/out/types/src/evaluator/values/dyn.d.ts +8 -0
- package/out/types/src/evaluator/values/float.d.ts +4 -0
- package/out/types/src/evaluator/values/integer.d.ts +4 -0
- package/out/types/src/evaluator/values/module.d.ts +58 -0
- package/out/types/src/evaluator/values/string.d.ts +3 -0
- package/out/types/src/evaluator/values/tuple.d.ts +32 -0
- package/out/types/src/expr.d.ts +456 -0
- package/out/types/src/function-value.d.ts +42 -0
- package/out/types/src/index.d.ts +4 -0
- package/out/types/src/lexer.d.ts +2 -0
- package/out/types/src/logger.d.ts +1 -0
- package/out/types/src/module-manager.d.ts +30 -0
- package/out/types/src/naming-checker.d.ts +4 -0
- package/out/types/src/parser.d.ts +33 -0
- package/out/types/src/test-runner.d.ts +30 -0
- package/out/types/src/tests/codegen.test.d.ts +1 -0
- package/out/types/src/tests/fixme.test.d.ts +1 -0
- package/out/types/src/tests/module-manager.test.d.ts +1 -0
- package/out/types/src/tests/parser.test.d.ts +1 -0
- package/out/types/src/tests/sample.test.d.ts +0 -0
- package/out/types/src/tests/std.test.d.ts +1 -0
- package/out/types/src/token.d.ts +40 -0
- package/out/types/src/type-value.d.ts +7 -0
- package/out/types/src/types/compatibility.d.ts +16 -0
- package/out/types/src/types/creators.d.ts +73 -0
- package/out/types/src/types/definitions.d.ts +218 -0
- package/out/types/src/types/guards.d.ts +70 -0
- package/out/types/src/types/hierarchy.d.ts +4 -0
- package/out/types/src/types/index.d.ts +7 -0
- package/out/types/src/types/module_field.d.ts +2 -0
- package/out/types/src/types/tags.d.ts +45 -0
- package/out/types/src/types/utils.d.ts +50 -0
- package/out/types/src/unit-value.d.ts +7 -0
- package/out/types/src/utils.d.ts +6 -0
- package/out/types/src/value-tag.d.ts +29 -0
- package/out/types/src/value.d.ts +110 -0
- package/out/types/src/yo-cli.d.ts +1 -0
- package/out/types/tsconfig.tsbuildinfo +1 -0
- package/package.json +57 -0
- package/scripts/check-liburing.js +76 -0
- package/std/alg/hash.yo +50 -0
- package/std/allocator.yo +113 -0
- package/std/allocators/c_allocator.yo +118 -0
- package/std/async.yo +13 -0
- package/std/collections/array_list.yo +415 -0
- package/std/collections/hash_map.yo +482 -0
- package/std/collections/hash_set.yo +706 -0
- package/std/collections/index.yo +11 -0
- package/std/collections/linked_list.yo +439 -0
- package/std/error.yo +0 -0
- package/std/gc.yo +10 -0
- package/std/index.yo +12 -0
- package/std/io/file.yo +191 -0
- package/std/io/index.yo +5 -0
- package/std/libc/assert.yo +39 -0
- package/std/libc/ctype.yo +57 -0
- package/std/libc/errno.yo +182 -0
- package/std/libc/float.yo +87 -0
- package/std/libc/index.yo +29 -0
- package/std/libc/limits.yo +65 -0
- package/std/libc/math.yo +679 -0
- package/std/libc/signal.yo +101 -0
- package/std/libc/stdatomic.yo +213 -0
- package/std/libc/stdint.yo +214 -0
- package/std/libc/stdio.yo +225 -0
- package/std/libc/stdlib.yo +204 -0
- package/std/libc/string.yo +151 -0
- package/std/libc/time.yo +92 -0
- package/std/libc/unistd.yo +130 -0
- package/std/monad.yo +152 -0
- package/std/prelude.yo +3094 -0
- package/std/string/index.yo +8 -0
- package/std/string/rune.yo +82 -0
- package/std/string/string.yo +288 -0
- package/std/sync.yo +95 -0
- package/std/thread.yo +36 -0
- package/std/time.yo +13 -0
- package/std/worker.yo +36 -0
- package/vendor/mimalloc/.gitattributes +12 -0
- package/vendor/mimalloc/CMakeLists.txt +763 -0
- package/vendor/mimalloc/LICENSE +21 -0
- package/vendor/mimalloc/SECURITY.md +41 -0
- package/vendor/mimalloc/azure-pipelines.yml +251 -0
- package/vendor/mimalloc/bin/mimalloc-redirect-arm64.dll +0 -0
- package/vendor/mimalloc/bin/mimalloc-redirect-arm64.lib +0 -0
- package/vendor/mimalloc/bin/mimalloc-redirect-arm64ec.dll +0 -0
- package/vendor/mimalloc/bin/mimalloc-redirect-arm64ec.lib +0 -0
- package/vendor/mimalloc/bin/mimalloc-redirect.dll +0 -0
- package/vendor/mimalloc/bin/mimalloc-redirect.lib +0 -0
- package/vendor/mimalloc/bin/mimalloc-redirect32.dll +0 -0
- package/vendor/mimalloc/bin/mimalloc-redirect32.lib +0 -0
- package/vendor/mimalloc/bin/minject-arm64.exe +0 -0
- package/vendor/mimalloc/bin/minject.exe +0 -0
- package/vendor/mimalloc/bin/minject32.exe +0 -0
- package/vendor/mimalloc/bin/readme.md +118 -0
- package/vendor/mimalloc/cmake/JoinPaths.cmake +23 -0
- package/vendor/mimalloc/cmake/mimalloc-config-version.cmake +19 -0
- package/vendor/mimalloc/cmake/mimalloc-config.cmake +14 -0
- package/vendor/mimalloc/contrib/docker/alpine/Dockerfile +23 -0
- package/vendor/mimalloc/contrib/docker/alpine-arm32v7/Dockerfile +28 -0
- package/vendor/mimalloc/contrib/docker/alpine-x86/Dockerfile +28 -0
- package/vendor/mimalloc/contrib/docker/manylinux-x64/Dockerfile +23 -0
- package/vendor/mimalloc/contrib/docker/readme.md +10 -0
- package/vendor/mimalloc/contrib/vcpkg/portfile.cmake +64 -0
- package/vendor/mimalloc/contrib/vcpkg/readme.md +40 -0
- package/vendor/mimalloc/contrib/vcpkg/usage +20 -0
- package/vendor/mimalloc/contrib/vcpkg/vcpkg-cmake-wrapper.cmake +20 -0
- package/vendor/mimalloc/contrib/vcpkg/vcpkg.json +48 -0
- package/vendor/mimalloc/doc/bench-2020/bench-c5-18xlarge-2020-01-20-a.svg +887 -0
- package/vendor/mimalloc/doc/bench-2020/bench-c5-18xlarge-2020-01-20-b.svg +1185 -0
- package/vendor/mimalloc/doc/bench-2020/bench-c5-18xlarge-2020-01-20-rss-a.svg +757 -0
- package/vendor/mimalloc/doc/bench-2020/bench-c5-18xlarge-2020-01-20-rss-b.svg +1028 -0
- package/vendor/mimalloc/doc/bench-2020/bench-r5a-1.svg +769 -0
- package/vendor/mimalloc/doc/bench-2020/bench-r5a-12xlarge-2020-01-16-a.svg +868 -0
- package/vendor/mimalloc/doc/bench-2020/bench-r5a-12xlarge-2020-01-16-b.svg +1157 -0
- package/vendor/mimalloc/doc/bench-2020/bench-r5a-2.svg +983 -0
- package/vendor/mimalloc/doc/bench-2020/bench-r5a-rss-1.svg +683 -0
- package/vendor/mimalloc/doc/bench-2020/bench-r5a-rss-2.svg +854 -0
- package/vendor/mimalloc/doc/bench-2020/bench-spec-rss.svg +713 -0
- package/vendor/mimalloc/doc/bench-2020/bench-spec.svg +713 -0
- package/vendor/mimalloc/doc/bench-2020/bench-z4-1.svg +890 -0
- package/vendor/mimalloc/doc/bench-2020/bench-z4-2.svg +1146 -0
- package/vendor/mimalloc/doc/bench-2020/bench-z4-rss-1.svg +796 -0
- package/vendor/mimalloc/doc/bench-2020/bench-z4-rss-2.svg +974 -0
- package/vendor/mimalloc/doc/bench-2021/bench-amd5950x-2021-01-30-a.svg +952 -0
- package/vendor/mimalloc/doc/bench-2021/bench-amd5950x-2021-01-30-b.svg +1255 -0
- package/vendor/mimalloc/doc/bench-2021/bench-c5-18xlarge-2021-01-30-a.svg +955 -0
- package/vendor/mimalloc/doc/bench-2021/bench-c5-18xlarge-2021-01-30-b.svg +1269 -0
- package/vendor/mimalloc/doc/bench-2021/bench-c5-18xlarge-2021-01-30-rss-a.svg +836 -0
- package/vendor/mimalloc/doc/bench-2021/bench-c5-18xlarge-2021-01-30-rss-b.svg +1131 -0
- package/vendor/mimalloc/doc/bench-2021/bench-macmini-2021-01-30.svg +766 -0
- package/vendor/mimalloc/doc/doxyfile +2895 -0
- package/vendor/mimalloc/doc/ds-logo.jpg +0 -0
- package/vendor/mimalloc/doc/ds-logo.png +0 -0
- package/vendor/mimalloc/doc/mimalloc-doc.h +1452 -0
- package/vendor/mimalloc/doc/mimalloc-doxygen.css +60 -0
- package/vendor/mimalloc/doc/mimalloc-logo-100.png +0 -0
- package/vendor/mimalloc/doc/mimalloc-logo.png +0 -0
- package/vendor/mimalloc/doc/mimalloc-logo.svg +161 -0
- package/vendor/mimalloc/doc/spades-logo.png +0 -0
- package/vendor/mimalloc/doc/unreal-logo.svg +43 -0
- package/vendor/mimalloc/ide/vs2022/mimalloc-lib.vcxproj +500 -0
- package/vendor/mimalloc/ide/vs2022/mimalloc-lib.vcxproj.filters +108 -0
- package/vendor/mimalloc/ide/vs2022/mimalloc-override-dll.vcxproj +508 -0
- package/vendor/mimalloc/ide/vs2022/mimalloc-override-dll.vcxproj.filters +111 -0
- package/vendor/mimalloc/ide/vs2022/mimalloc-override-test-dep.vcxproj +355 -0
- package/vendor/mimalloc/ide/vs2022/mimalloc-override-test.vcxproj +360 -0
- package/vendor/mimalloc/ide/vs2022/mimalloc-test-api.vcxproj +295 -0
- package/vendor/mimalloc/ide/vs2022/mimalloc-test-stress.vcxproj +292 -0
- package/vendor/mimalloc/ide/vs2022/mimalloc-test.vcxproj +289 -0
- package/vendor/mimalloc/ide/vs2022/mimalloc.sln +151 -0
- package/vendor/mimalloc/include/mimalloc/atomic.h +557 -0
- package/vendor/mimalloc/include/mimalloc/internal.h +1153 -0
- package/vendor/mimalloc/include/mimalloc/prim.h +421 -0
- package/vendor/mimalloc/include/mimalloc/track.h +145 -0
- package/vendor/mimalloc/include/mimalloc/types.h +685 -0
- package/vendor/mimalloc/include/mimalloc-new-delete.h +66 -0
- package/vendor/mimalloc/include/mimalloc-override.h +68 -0
- package/vendor/mimalloc/include/mimalloc-stats.h +103 -0
- package/vendor/mimalloc/include/mimalloc.h +612 -0
- package/vendor/mimalloc/mimalloc.pc.in +11 -0
- package/vendor/mimalloc/readme.md +946 -0
- package/vendor/mimalloc/src/alloc-aligned.c +360 -0
- package/vendor/mimalloc/src/alloc-override.c +316 -0
- package/vendor/mimalloc/src/alloc-posix.c +185 -0
- package/vendor/mimalloc/src/alloc.c +692 -0
- package/vendor/mimalloc/src/arena-abandon.c +346 -0
- package/vendor/mimalloc/src/arena.c +1043 -0
- package/vendor/mimalloc/src/bitmap.c +441 -0
- package/vendor/mimalloc/src/bitmap.h +119 -0
- package/vendor/mimalloc/src/free.c +572 -0
- package/vendor/mimalloc/src/heap.c +733 -0
- package/vendor/mimalloc/src/init.c +714 -0
- package/vendor/mimalloc/src/libc.c +334 -0
- package/vendor/mimalloc/src/options.c +663 -0
- package/vendor/mimalloc/src/os.c +770 -0
- package/vendor/mimalloc/src/page-queue.c +390 -0
- package/vendor/mimalloc/src/page.c +1049 -0
- package/vendor/mimalloc/src/prim/emscripten/prim.c +249 -0
- package/vendor/mimalloc/src/prim/osx/alloc-override-zone.c +461 -0
- package/vendor/mimalloc/src/prim/osx/prim.c +9 -0
- package/vendor/mimalloc/src/prim/prim.c +76 -0
- package/vendor/mimalloc/src/prim/readme.md +9 -0
- package/vendor/mimalloc/src/prim/unix/prim.c +934 -0
- package/vendor/mimalloc/src/prim/wasi/prim.c +284 -0
- package/vendor/mimalloc/src/prim/windows/etw-mimalloc.wprp +61 -0
- package/vendor/mimalloc/src/prim/windows/etw.h +905 -0
- package/vendor/mimalloc/src/prim/windows/etw.man +0 -0
- package/vendor/mimalloc/src/prim/windows/prim.c +878 -0
- package/vendor/mimalloc/src/prim/windows/readme.md +17 -0
- package/vendor/mimalloc/src/random.c +258 -0
- package/vendor/mimalloc/src/segment-map.c +142 -0
- package/vendor/mimalloc/src/segment.c +1702 -0
- package/vendor/mimalloc/src/static.c +41 -0
- package/vendor/mimalloc/src/stats.c +635 -0
- package/vendor/mimalloc/test/CMakeLists.txt +56 -0
- package/vendor/mimalloc/test/main-override-dep.cpp +51 -0
- package/vendor/mimalloc/test/main-override-dep.h +11 -0
- package/vendor/mimalloc/test/main-override-static.c +539 -0
- package/vendor/mimalloc/test/main-override.c +36 -0
- package/vendor/mimalloc/test/main-override.cpp +497 -0
- package/vendor/mimalloc/test/main.c +46 -0
- package/vendor/mimalloc/test/readme.md +16 -0
- package/vendor/mimalloc/test/test-api-fill.c +343 -0
- package/vendor/mimalloc/test/test-api.c +466 -0
- package/vendor/mimalloc/test/test-stress.c +428 -0
- package/vendor/mimalloc/test/test-wrong.c +92 -0
- package/vendor/mimalloc/test/testhelper.h +49 -0
|
@@ -0,0 +1,1153 @@
|
|
|
1
|
+
/* ----------------------------------------------------------------------------
|
|
2
|
+
Copyright (c) 2018-2023, Microsoft Research, Daan Leijen
|
|
3
|
+
This is free software; you can redistribute it and/or modify it under the
|
|
4
|
+
terms of the MIT license. A copy of the license can be found in the file
|
|
5
|
+
"LICENSE" at the root of this distribution.
|
|
6
|
+
-----------------------------------------------------------------------------*/
|
|
7
|
+
#pragma once
|
|
8
|
+
#ifndef MIMALLOC_INTERNAL_H
|
|
9
|
+
#define MIMALLOC_INTERNAL_H
|
|
10
|
+
|
|
11
|
+
// --------------------------------------------------------------------------
|
|
12
|
+
// This file contains the internal API's of mimalloc and various utility
|
|
13
|
+
// functions and macros.
|
|
14
|
+
// --------------------------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
#include "types.h"
|
|
17
|
+
#include "track.h"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
// --------------------------------------------------------------------------
|
|
21
|
+
// Compiler defines
|
|
22
|
+
// --------------------------------------------------------------------------
|
|
23
|
+
|
|
24
|
+
#if (MI_DEBUG>0)
|
|
25
|
+
#define mi_trace_message(...) _mi_trace_message(__VA_ARGS__)
|
|
26
|
+
#else
|
|
27
|
+
#define mi_trace_message(...)
|
|
28
|
+
#endif
|
|
29
|
+
|
|
30
|
+
#define mi_decl_cache_align mi_decl_align(64)
|
|
31
|
+
|
|
32
|
+
#if defined(_MSC_VER)
|
|
33
|
+
#pragma warning(disable:4127) // suppress constant conditional warning (due to MI_SECURE paths)
|
|
34
|
+
#pragma warning(disable:26812) // unscoped enum warning
|
|
35
|
+
#define mi_decl_noinline __declspec(noinline)
|
|
36
|
+
#define mi_decl_thread __declspec(thread)
|
|
37
|
+
#define mi_decl_align(a) __declspec(align(a))
|
|
38
|
+
#define mi_decl_noreturn __declspec(noreturn)
|
|
39
|
+
#define mi_decl_weak
|
|
40
|
+
#define mi_decl_hidden
|
|
41
|
+
#define mi_decl_cold
|
|
42
|
+
#elif (defined(__GNUC__) && (__GNUC__ >= 3)) || defined(__clang__) // includes clang and icc
|
|
43
|
+
#define mi_decl_noinline __attribute__((noinline))
|
|
44
|
+
#define mi_decl_thread __thread
|
|
45
|
+
#define mi_decl_align(a) __attribute__((aligned(a)))
|
|
46
|
+
#define mi_decl_noreturn __attribute__((noreturn))
|
|
47
|
+
#define mi_decl_weak __attribute__((weak))
|
|
48
|
+
#define mi_decl_hidden __attribute__((visibility("hidden")))
|
|
49
|
+
#if (__GNUC__ >= 4) || defined(__clang__)
|
|
50
|
+
#define mi_decl_cold __attribute__((cold))
|
|
51
|
+
#else
|
|
52
|
+
#define mi_decl_cold
|
|
53
|
+
#endif
|
|
54
|
+
#elif __cplusplus >= 201103L // c++11
|
|
55
|
+
#define mi_decl_noinline
|
|
56
|
+
#define mi_decl_thread thread_local
|
|
57
|
+
#define mi_decl_align(a) alignas(a)
|
|
58
|
+
#define mi_decl_noreturn [[noreturn]]
|
|
59
|
+
#define mi_decl_weak
|
|
60
|
+
#define mi_decl_hidden
|
|
61
|
+
#define mi_decl_cold
|
|
62
|
+
#else
|
|
63
|
+
#define mi_decl_noinline
|
|
64
|
+
#define mi_decl_thread __thread // hope for the best :-)
|
|
65
|
+
#define mi_decl_align(a)
|
|
66
|
+
#define mi_decl_noreturn
|
|
67
|
+
#define mi_decl_weak
|
|
68
|
+
#define mi_decl_hidden
|
|
69
|
+
#define mi_decl_cold
|
|
70
|
+
#endif
|
|
71
|
+
|
|
72
|
+
#if defined(__GNUC__) || defined(__clang__)
|
|
73
|
+
#define mi_unlikely(x) (__builtin_expect(!!(x),false))
|
|
74
|
+
#define mi_likely(x) (__builtin_expect(!!(x),true))
|
|
75
|
+
#elif (defined(__cplusplus) && (__cplusplus >= 202002L)) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
|
|
76
|
+
#define mi_unlikely(x) (x) [[unlikely]]
|
|
77
|
+
#define mi_likely(x) (x) [[likely]]
|
|
78
|
+
#else
|
|
79
|
+
#define mi_unlikely(x) (x)
|
|
80
|
+
#define mi_likely(x) (x)
|
|
81
|
+
#endif
|
|
82
|
+
|
|
83
|
+
#ifndef __has_builtin
|
|
84
|
+
#define __has_builtin(x) 0
|
|
85
|
+
#endif
|
|
86
|
+
|
|
87
|
+
#if defined(__cplusplus)
|
|
88
|
+
#define mi_decl_externc extern "C"
|
|
89
|
+
#else
|
|
90
|
+
#define mi_decl_externc
|
|
91
|
+
#endif
|
|
92
|
+
|
|
93
|
+
#if defined(__EMSCRIPTEN__) && !defined(__wasi__)
|
|
94
|
+
#define __wasi__
|
|
95
|
+
#endif
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
// --------------------------------------------------------------------------
|
|
99
|
+
// Internal functions
|
|
100
|
+
// --------------------------------------------------------------------------
|
|
101
|
+
|
|
102
|
+
// "libc.c"
|
|
103
|
+
#include <stdarg.h>
|
|
104
|
+
int _mi_vsnprintf(char* buf, size_t bufsize, const char* fmt, va_list args);
|
|
105
|
+
int _mi_snprintf(char* buf, size_t buflen, const char* fmt, ...);
|
|
106
|
+
char _mi_toupper(char c);
|
|
107
|
+
int _mi_strnicmp(const char* s, const char* t, size_t n);
|
|
108
|
+
void _mi_strlcpy(char* dest, const char* src, size_t dest_size);
|
|
109
|
+
void _mi_strlcat(char* dest, const char* src, size_t dest_size);
|
|
110
|
+
size_t _mi_strlen(const char* s);
|
|
111
|
+
size_t _mi_strnlen(const char* s, size_t max_len);
|
|
112
|
+
bool _mi_getenv(const char* name, char* result, size_t result_size);
|
|
113
|
+
|
|
114
|
+
// "options.c"
|
|
115
|
+
void _mi_fputs(mi_output_fun* out, void* arg, const char* prefix, const char* message);
|
|
116
|
+
void _mi_fprintf(mi_output_fun* out, void* arg, const char* fmt, ...);
|
|
117
|
+
void _mi_message(const char* fmt, ...);
|
|
118
|
+
void _mi_warning_message(const char* fmt, ...);
|
|
119
|
+
void _mi_verbose_message(const char* fmt, ...);
|
|
120
|
+
void _mi_trace_message(const char* fmt, ...);
|
|
121
|
+
void _mi_options_init(void);
|
|
122
|
+
long _mi_option_get_fast(mi_option_t option);
|
|
123
|
+
void _mi_error_message(int err, const char* fmt, ...);
|
|
124
|
+
|
|
125
|
+
// random.c
|
|
126
|
+
void _mi_random_init(mi_random_ctx_t* ctx);
|
|
127
|
+
void _mi_random_init_weak(mi_random_ctx_t* ctx);
|
|
128
|
+
void _mi_random_reinit_if_weak(mi_random_ctx_t * ctx);
|
|
129
|
+
void _mi_random_split(mi_random_ctx_t* ctx, mi_random_ctx_t* new_ctx);
|
|
130
|
+
uintptr_t _mi_random_next(mi_random_ctx_t* ctx);
|
|
131
|
+
uintptr_t _mi_heap_random_next(mi_heap_t* heap);
|
|
132
|
+
uintptr_t _mi_os_random_weak(uintptr_t extra_seed);
|
|
133
|
+
static inline uintptr_t _mi_random_shuffle(uintptr_t x);
|
|
134
|
+
|
|
135
|
+
// init.c
|
|
136
|
+
extern mi_decl_hidden mi_decl_cache_align mi_stats_t _mi_stats_main;
|
|
137
|
+
extern mi_decl_hidden mi_decl_cache_align const mi_page_t _mi_page_empty;
|
|
138
|
+
void _mi_auto_process_init(void);
|
|
139
|
+
void mi_cdecl _mi_auto_process_done(void) mi_attr_noexcept;
|
|
140
|
+
bool _mi_is_redirected(void);
|
|
141
|
+
bool _mi_allocator_init(const char** message);
|
|
142
|
+
void _mi_allocator_done(void);
|
|
143
|
+
bool _mi_is_main_thread(void);
|
|
144
|
+
size_t _mi_current_thread_count(void);
|
|
145
|
+
bool _mi_preloading(void); // true while the C runtime is not initialized yet
|
|
146
|
+
void _mi_thread_done(mi_heap_t* heap);
|
|
147
|
+
void _mi_thread_data_collect(void);
|
|
148
|
+
void _mi_tld_init(mi_tld_t* tld, mi_heap_t* bheap);
|
|
149
|
+
mi_threadid_t _mi_thread_id(void) mi_attr_noexcept;
|
|
150
|
+
mi_heap_t* _mi_heap_main_get(void); // statically allocated main backing heap
|
|
151
|
+
mi_subproc_t* _mi_subproc_from_id(mi_subproc_id_t subproc_id);
|
|
152
|
+
void _mi_heap_guarded_init(mi_heap_t* heap);
|
|
153
|
+
|
|
154
|
+
// os.c
|
|
155
|
+
void _mi_os_init(void); // called from process init
|
|
156
|
+
void* _mi_os_alloc(size_t size, mi_memid_t* memid);
|
|
157
|
+
void* _mi_os_zalloc(size_t size, mi_memid_t* memid);
|
|
158
|
+
void _mi_os_free(void* p, size_t size, mi_memid_t memid);
|
|
159
|
+
void _mi_os_free_ex(void* p, size_t size, bool still_committed, mi_memid_t memid);
|
|
160
|
+
|
|
161
|
+
size_t _mi_os_page_size(void);
|
|
162
|
+
size_t _mi_os_good_alloc_size(size_t size);
|
|
163
|
+
bool _mi_os_has_overcommit(void);
|
|
164
|
+
bool _mi_os_has_virtual_reserve(void);
|
|
165
|
+
|
|
166
|
+
bool _mi_os_reset(void* addr, size_t size);
|
|
167
|
+
bool _mi_os_decommit(void* addr, size_t size);
|
|
168
|
+
bool _mi_os_unprotect(void* addr, size_t size);
|
|
169
|
+
bool _mi_os_purge(void* p, size_t size);
|
|
170
|
+
bool _mi_os_purge_ex(void* p, size_t size, bool allow_reset, size_t stat_size);
|
|
171
|
+
void _mi_os_reuse(void* p, size_t size);
|
|
172
|
+
mi_decl_nodiscard bool _mi_os_commit(void* p, size_t size, bool* is_zero);
|
|
173
|
+
mi_decl_nodiscard bool _mi_os_commit_ex(void* addr, size_t size, bool* is_zero, size_t stat_size);
|
|
174
|
+
bool _mi_os_protect(void* addr, size_t size);
|
|
175
|
+
|
|
176
|
+
void* _mi_os_alloc_aligned(size_t size, size_t alignment, bool commit, bool allow_large, mi_memid_t* memid);
|
|
177
|
+
void* _mi_os_alloc_aligned_at_offset(size_t size, size_t alignment, size_t align_offset, bool commit, bool allow_large, mi_memid_t* memid);
|
|
178
|
+
|
|
179
|
+
void* _mi_os_get_aligned_hint(size_t try_alignment, size_t size);
|
|
180
|
+
bool _mi_os_use_large_page(size_t size, size_t alignment);
|
|
181
|
+
size_t _mi_os_large_page_size(void);
|
|
182
|
+
void* _mi_os_alloc_huge_os_pages(size_t pages, int numa_node, mi_msecs_t max_secs, size_t* pages_reserved, size_t* psize, mi_memid_t* memid);
|
|
183
|
+
|
|
184
|
+
int _mi_os_numa_node_count(void);
|
|
185
|
+
int _mi_os_numa_node(void);
|
|
186
|
+
|
|
187
|
+
// arena.c
|
|
188
|
+
mi_arena_id_t _mi_arena_id_none(void);
|
|
189
|
+
void _mi_arena_free(void* p, size_t size, size_t still_committed_size, mi_memid_t memid);
|
|
190
|
+
void* _mi_arena_alloc(size_t size, bool commit, bool allow_large, mi_arena_id_t req_arena_id, mi_memid_t* memid);
|
|
191
|
+
void* _mi_arena_alloc_aligned(size_t size, size_t alignment, size_t align_offset, bool commit, bool allow_large, mi_arena_id_t req_arena_id, mi_memid_t* memid);
|
|
192
|
+
bool _mi_arena_memid_is_suitable(mi_memid_t memid, mi_arena_id_t request_arena_id);
|
|
193
|
+
bool _mi_arena_contains(const void* p);
|
|
194
|
+
void _mi_arenas_collect(bool force_purge);
|
|
195
|
+
void _mi_arena_unsafe_destroy_all(void);
|
|
196
|
+
|
|
197
|
+
bool _mi_arena_segment_clear_abandoned(mi_segment_t* segment);
|
|
198
|
+
void _mi_arena_segment_mark_abandoned(mi_segment_t* segment);
|
|
199
|
+
|
|
200
|
+
void* _mi_arena_meta_zalloc(size_t size, mi_memid_t* memid);
|
|
201
|
+
void _mi_arena_meta_free(void* p, mi_memid_t memid, size_t size);
|
|
202
|
+
|
|
203
|
+
typedef struct mi_arena_field_cursor_s { // abstract struct
|
|
204
|
+
size_t os_list_count; // max entries to visit in the OS abandoned list
|
|
205
|
+
size_t start; // start arena idx (may need to be wrapped)
|
|
206
|
+
size_t end; // end arena idx (exclusive, may need to be wrapped)
|
|
207
|
+
size_t bitmap_idx; // current bit idx for an arena
|
|
208
|
+
mi_subproc_t* subproc; // only visit blocks in this sub-process
|
|
209
|
+
bool visit_all; // ensure all abandoned blocks are seen (blocking)
|
|
210
|
+
bool hold_visit_lock; // if the subproc->abandoned_os_visit_lock is held
|
|
211
|
+
} mi_arena_field_cursor_t;
|
|
212
|
+
void _mi_arena_field_cursor_init(mi_heap_t* heap, mi_subproc_t* subproc, bool visit_all, mi_arena_field_cursor_t* current);
|
|
213
|
+
mi_segment_t* _mi_arena_segment_clear_abandoned_next(mi_arena_field_cursor_t* previous);
|
|
214
|
+
void _mi_arena_field_cursor_done(mi_arena_field_cursor_t* current);
|
|
215
|
+
|
|
216
|
+
// "segment-map.c"
|
|
217
|
+
void _mi_segment_map_allocated_at(const mi_segment_t* segment);
|
|
218
|
+
void _mi_segment_map_freed_at(const mi_segment_t* segment);
|
|
219
|
+
void _mi_segment_map_unsafe_destroy(void);
|
|
220
|
+
|
|
221
|
+
// "segment.c"
|
|
222
|
+
mi_page_t* _mi_segment_page_alloc(mi_heap_t* heap, size_t block_size, size_t page_alignment, mi_segments_tld_t* tld);
|
|
223
|
+
void _mi_segment_page_free(mi_page_t* page, bool force, mi_segments_tld_t* tld);
|
|
224
|
+
void _mi_segment_page_abandon(mi_page_t* page, mi_segments_tld_t* tld);
|
|
225
|
+
bool _mi_segment_try_reclaim_abandoned( mi_heap_t* heap, bool try_all, mi_segments_tld_t* tld);
|
|
226
|
+
void _mi_segment_collect(mi_segment_t* segment, bool force);
|
|
227
|
+
|
|
228
|
+
#if MI_HUGE_PAGE_ABANDON
|
|
229
|
+
void _mi_segment_huge_page_free(mi_segment_t* segment, mi_page_t* page, mi_block_t* block);
|
|
230
|
+
#else
|
|
231
|
+
void _mi_segment_huge_page_reset(mi_segment_t* segment, mi_page_t* page, mi_block_t* block);
|
|
232
|
+
#endif
|
|
233
|
+
|
|
234
|
+
uint8_t* _mi_segment_page_start(const mi_segment_t* segment, const mi_page_t* page, size_t* page_size); // page start for any page
|
|
235
|
+
void _mi_abandoned_reclaim_all(mi_heap_t* heap, mi_segments_tld_t* tld);
|
|
236
|
+
void _mi_abandoned_collect(mi_heap_t* heap, bool force, mi_segments_tld_t* tld);
|
|
237
|
+
bool _mi_segment_attempt_reclaim(mi_heap_t* heap, mi_segment_t* segment);
|
|
238
|
+
bool _mi_segment_visit_blocks(mi_segment_t* segment, int heap_tag, bool visit_blocks, mi_block_visit_fun* visitor, void* arg);
|
|
239
|
+
|
|
240
|
+
// "page.c"
|
|
241
|
+
void* _mi_malloc_generic(mi_heap_t* heap, size_t size, bool zero, size_t huge_alignment) mi_attr_noexcept mi_attr_malloc;
|
|
242
|
+
|
|
243
|
+
void _mi_page_retire(mi_page_t* page) mi_attr_noexcept; // free the page if there are no other pages with many free blocks
|
|
244
|
+
void _mi_page_unfull(mi_page_t* page);
|
|
245
|
+
void _mi_page_free(mi_page_t* page, mi_page_queue_t* pq, bool force); // free the page
|
|
246
|
+
void _mi_page_abandon(mi_page_t* page, mi_page_queue_t* pq); // abandon the page, to be picked up by another thread...
|
|
247
|
+
void _mi_page_force_abandon(mi_page_t* page);
|
|
248
|
+
|
|
249
|
+
void _mi_heap_delayed_free_all(mi_heap_t* heap);
|
|
250
|
+
bool _mi_heap_delayed_free_partial(mi_heap_t* heap);
|
|
251
|
+
void _mi_heap_collect_retired(mi_heap_t* heap, bool force);
|
|
252
|
+
|
|
253
|
+
void _mi_page_use_delayed_free(mi_page_t* page, mi_delayed_t delay, bool override_never);
|
|
254
|
+
bool _mi_page_try_use_delayed_free(mi_page_t* page, mi_delayed_t delay, bool override_never);
|
|
255
|
+
size_t _mi_page_queue_append(mi_heap_t* heap, mi_page_queue_t* pq, mi_page_queue_t* append);
|
|
256
|
+
void _mi_deferred_free(mi_heap_t* heap, bool force);
|
|
257
|
+
|
|
258
|
+
void _mi_page_free_collect(mi_page_t* page,bool force);
|
|
259
|
+
void _mi_page_reclaim(mi_heap_t* heap, mi_page_t* page); // callback from segments
|
|
260
|
+
|
|
261
|
+
size_t _mi_page_bin(const mi_page_t* page); // for stats
|
|
262
|
+
size_t _mi_bin_size(size_t bin); // for stats
|
|
263
|
+
size_t _mi_bin(size_t size); // for stats
|
|
264
|
+
|
|
265
|
+
// "heap.c"
|
|
266
|
+
void _mi_heap_init(mi_heap_t* heap, mi_tld_t* tld, mi_arena_id_t arena_id, bool noreclaim, uint8_t tag);
|
|
267
|
+
void _mi_heap_destroy_pages(mi_heap_t* heap);
|
|
268
|
+
void _mi_heap_collect_abandon(mi_heap_t* heap);
|
|
269
|
+
void _mi_heap_set_default_direct(mi_heap_t* heap);
|
|
270
|
+
bool _mi_heap_memid_is_suitable(mi_heap_t* heap, mi_memid_t memid);
|
|
271
|
+
void _mi_heap_unsafe_destroy_all(mi_heap_t* heap);
|
|
272
|
+
mi_heap_t* _mi_heap_by_tag(mi_heap_t* heap, uint8_t tag);
|
|
273
|
+
void _mi_heap_area_init(mi_heap_area_t* area, mi_page_t* page);
|
|
274
|
+
bool _mi_heap_area_visit_blocks(const mi_heap_area_t* area, mi_page_t* page, mi_block_visit_fun* visitor, void* arg);
|
|
275
|
+
|
|
276
|
+
// "stats.c"
|
|
277
|
+
void _mi_stats_done(mi_stats_t* stats);
|
|
278
|
+
void _mi_stats_merge_thread(mi_tld_t* tld);
|
|
279
|
+
mi_msecs_t _mi_clock_now(void);
|
|
280
|
+
mi_msecs_t _mi_clock_end(mi_msecs_t start);
|
|
281
|
+
mi_msecs_t _mi_clock_start(void);
|
|
282
|
+
|
|
283
|
+
// "alloc.c"
|
|
284
|
+
void* _mi_page_malloc_zero(mi_heap_t* heap, mi_page_t* page, size_t size, bool zero) mi_attr_noexcept; // called from `_mi_malloc_generic`
|
|
285
|
+
void* _mi_page_malloc(mi_heap_t* heap, mi_page_t* page, size_t size) mi_attr_noexcept; // called from `_mi_heap_malloc_aligned`
|
|
286
|
+
void* _mi_page_malloc_zeroed(mi_heap_t* heap, mi_page_t* page, size_t size) mi_attr_noexcept; // called from `_mi_heap_malloc_aligned`
|
|
287
|
+
void* _mi_heap_malloc_zero(mi_heap_t* heap, size_t size, bool zero) mi_attr_noexcept;
|
|
288
|
+
void* _mi_heap_malloc_zero_ex(mi_heap_t* heap, size_t size, bool zero, size_t huge_alignment) mi_attr_noexcept; // called from `_mi_heap_malloc_aligned`
|
|
289
|
+
void* _mi_heap_realloc_zero(mi_heap_t* heap, void* p, size_t newsize, bool zero) mi_attr_noexcept;
|
|
290
|
+
mi_block_t* _mi_page_ptr_unalign(const mi_page_t* page, const void* p);
|
|
291
|
+
bool _mi_free_delayed_block(mi_block_t* block);
|
|
292
|
+
void _mi_free_generic(mi_segment_t* segment, mi_page_t* page, bool is_local, void* p) mi_attr_noexcept; // for runtime integration
|
|
293
|
+
void _mi_padding_shrink(const mi_page_t* page, const mi_block_t* block, const size_t min_size);
|
|
294
|
+
|
|
295
|
+
#if MI_DEBUG>1
|
|
296
|
+
bool _mi_page_is_valid(mi_page_t* page);
|
|
297
|
+
#endif
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
/* -----------------------------------------------------------
|
|
301
|
+
Error codes passed to `_mi_fatal_error`
|
|
302
|
+
All are recoverable but EFAULT is a serious error and aborts by default in secure mode.
|
|
303
|
+
For portability define undefined error codes using common Unix codes:
|
|
304
|
+
<https://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Errors/unix_system_errors.html>
|
|
305
|
+
----------------------------------------------------------- */
|
|
306
|
+
#include <errno.h>
|
|
307
|
+
#ifndef EAGAIN // double free
|
|
308
|
+
#define EAGAIN (11)
|
|
309
|
+
#endif
|
|
310
|
+
#ifndef ENOMEM // out of memory
|
|
311
|
+
#define ENOMEM (12)
|
|
312
|
+
#endif
|
|
313
|
+
#ifndef EFAULT // corrupted free-list or meta-data
|
|
314
|
+
#define EFAULT (14)
|
|
315
|
+
#endif
|
|
316
|
+
#ifndef EINVAL // trying to free an invalid pointer
|
|
317
|
+
#define EINVAL (22)
|
|
318
|
+
#endif
|
|
319
|
+
#ifndef EOVERFLOW // count*size overflow
|
|
320
|
+
#define EOVERFLOW (75)
|
|
321
|
+
#endif
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
// ------------------------------------------------------
|
|
325
|
+
// Assertions
|
|
326
|
+
// ------------------------------------------------------
|
|
327
|
+
|
|
328
|
+
#if (MI_DEBUG)
|
|
329
|
+
// use our own assertion to print without memory allocation
|
|
330
|
+
mi_decl_noreturn mi_decl_cold void _mi_assert_fail(const char* assertion, const char* fname, unsigned int line, const char* func) mi_attr_noexcept;
|
|
331
|
+
#define mi_assert(expr) ((expr) ? (void)0 : _mi_assert_fail(#expr,__FILE__,__LINE__,__func__))
|
|
332
|
+
#else
|
|
333
|
+
#define mi_assert(x)
|
|
334
|
+
#endif
|
|
335
|
+
|
|
336
|
+
#if (MI_DEBUG>1)
|
|
337
|
+
#define mi_assert_internal mi_assert
|
|
338
|
+
#else
|
|
339
|
+
#define mi_assert_internal(x)
|
|
340
|
+
#endif
|
|
341
|
+
|
|
342
|
+
#if (MI_DEBUG>2)
|
|
343
|
+
#define mi_assert_expensive mi_assert
|
|
344
|
+
#else
|
|
345
|
+
#define mi_assert_expensive(x)
|
|
346
|
+
#endif
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
/* -----------------------------------------------------------
|
|
351
|
+
Inlined definitions
|
|
352
|
+
----------------------------------------------------------- */
|
|
353
|
+
#define MI_UNUSED(x) (void)(x)
|
|
354
|
+
#if (MI_DEBUG>0)
|
|
355
|
+
#define MI_UNUSED_RELEASE(x)
|
|
356
|
+
#else
|
|
357
|
+
#define MI_UNUSED_RELEASE(x) MI_UNUSED(x)
|
|
358
|
+
#endif
|
|
359
|
+
|
|
360
|
+
#define MI_INIT4(x) x(),x(),x(),x()
|
|
361
|
+
#define MI_INIT8(x) MI_INIT4(x),MI_INIT4(x)
|
|
362
|
+
#define MI_INIT16(x) MI_INIT8(x),MI_INIT8(x)
|
|
363
|
+
#define MI_INIT32(x) MI_INIT16(x),MI_INIT16(x)
|
|
364
|
+
#define MI_INIT64(x) MI_INIT32(x),MI_INIT32(x)
|
|
365
|
+
#define MI_INIT128(x) MI_INIT64(x),MI_INIT64(x)
|
|
366
|
+
#define MI_INIT256(x) MI_INIT128(x),MI_INIT128(x)
|
|
367
|
+
#define MI_INIT74(x) MI_INIT64(x),MI_INIT8(x),x(),x()
|
|
368
|
+
|
|
369
|
+
#include <string.h>
|
|
370
|
+
// initialize a local variable to zero; use memset as compilers optimize constant sized memset's
|
|
371
|
+
#define _mi_memzero_var(x) memset(&x,0,sizeof(x))
|
|
372
|
+
|
|
373
|
+
// Is `x` a power of two? (0 is considered a power of two)
|
|
374
|
+
static inline bool _mi_is_power_of_two(uintptr_t x) {
|
|
375
|
+
return ((x & (x - 1)) == 0);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// Is a pointer aligned?
|
|
379
|
+
static inline bool _mi_is_aligned(void* p, size_t alignment) {
|
|
380
|
+
mi_assert_internal(alignment != 0);
|
|
381
|
+
return (((uintptr_t)p % alignment) == 0);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// Align upwards
|
|
385
|
+
static inline uintptr_t _mi_align_up(uintptr_t sz, size_t alignment) {
|
|
386
|
+
mi_assert_internal(alignment != 0);
|
|
387
|
+
uintptr_t mask = alignment - 1;
|
|
388
|
+
if ((alignment & mask) == 0) { // power of two?
|
|
389
|
+
return ((sz + mask) & ~mask);
|
|
390
|
+
}
|
|
391
|
+
else {
|
|
392
|
+
return (((sz + mask)/alignment)*alignment);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// Align downwards
|
|
397
|
+
static inline uintptr_t _mi_align_down(uintptr_t sz, size_t alignment) {
|
|
398
|
+
mi_assert_internal(alignment != 0);
|
|
399
|
+
uintptr_t mask = alignment - 1;
|
|
400
|
+
if ((alignment & mask) == 0) { // power of two?
|
|
401
|
+
return (sz & ~mask);
|
|
402
|
+
}
|
|
403
|
+
else {
|
|
404
|
+
return ((sz / alignment) * alignment);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// Align a pointer upwards
|
|
409
|
+
static inline void* mi_align_up_ptr(void* p, size_t alignment) {
|
|
410
|
+
return (void*)_mi_align_up((uintptr_t)p, alignment);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// Align a pointer downwards
|
|
414
|
+
static inline void* mi_align_down_ptr(void* p, size_t alignment) {
|
|
415
|
+
return (void*)_mi_align_down((uintptr_t)p, alignment);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
// Divide upwards: `s <= _mi_divide_up(s,d)*d < s+d`.
|
|
420
|
+
static inline uintptr_t _mi_divide_up(uintptr_t size, size_t divider) {
|
|
421
|
+
mi_assert_internal(divider != 0);
|
|
422
|
+
return (divider == 0 ? size : ((size + divider - 1) / divider));
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+
// clamp an integer
|
|
427
|
+
static inline size_t _mi_clamp(size_t sz, size_t min, size_t max) {
|
|
428
|
+
if (sz < min) return min;
|
|
429
|
+
else if (sz > max) return max;
|
|
430
|
+
else return sz;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// Is memory zero initialized?
|
|
434
|
+
static inline bool mi_mem_is_zero(const void* p, size_t size) {
|
|
435
|
+
for (size_t i = 0; i < size; i++) {
|
|
436
|
+
if (((uint8_t*)p)[i] != 0) return false;
|
|
437
|
+
}
|
|
438
|
+
return true;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
// Align a byte size to a size in _machine words_,
|
|
443
|
+
// i.e. byte size == `wsize*sizeof(void*)`.
|
|
444
|
+
static inline size_t _mi_wsize_from_size(size_t size) {
|
|
445
|
+
mi_assert_internal(size <= SIZE_MAX - sizeof(uintptr_t));
|
|
446
|
+
return (size + sizeof(uintptr_t) - 1) / sizeof(uintptr_t);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// Overflow detecting multiply
|
|
450
|
+
#if __has_builtin(__builtin_umul_overflow) || (defined(__GNUC__) && (__GNUC__ >= 5))
|
|
451
|
+
#include <limits.h> // UINT_MAX, ULONG_MAX
|
|
452
|
+
#if defined(_CLOCK_T) // for Illumos
|
|
453
|
+
#undef _CLOCK_T
|
|
454
|
+
#endif
|
|
455
|
+
static inline bool mi_mul_overflow(size_t count, size_t size, size_t* total) {
|
|
456
|
+
#if (SIZE_MAX == ULONG_MAX)
|
|
457
|
+
return __builtin_umull_overflow(count, size, (unsigned long *)total);
|
|
458
|
+
#elif (SIZE_MAX == UINT_MAX)
|
|
459
|
+
return __builtin_umul_overflow(count, size, (unsigned int *)total);
|
|
460
|
+
#else
|
|
461
|
+
return __builtin_umulll_overflow(count, size, (unsigned long long *)total);
|
|
462
|
+
#endif
|
|
463
|
+
}
|
|
464
|
+
#else /* __builtin_umul_overflow is unavailable */
|
|
465
|
+
static inline bool mi_mul_overflow(size_t count, size_t size, size_t* total) {
|
|
466
|
+
#define MI_MUL_COULD_OVERFLOW ((size_t)1 << (4*sizeof(size_t))) // sqrt(SIZE_MAX)
|
|
467
|
+
*total = count * size;
|
|
468
|
+
// note: gcc/clang optimize this to directly check the overflow flag
|
|
469
|
+
return ((size >= MI_MUL_COULD_OVERFLOW || count >= MI_MUL_COULD_OVERFLOW) && size > 0 && (SIZE_MAX / size) < count);
|
|
470
|
+
}
|
|
471
|
+
#endif
|
|
472
|
+
|
|
473
|
+
// Safe multiply `count*size` into `total`; return `true` on overflow.
|
|
474
|
+
static inline bool mi_count_size_overflow(size_t count, size_t size, size_t* total) {
|
|
475
|
+
if (count==1) { // quick check for the case where count is one (common for C++ allocators)
|
|
476
|
+
*total = size;
|
|
477
|
+
return false;
|
|
478
|
+
}
|
|
479
|
+
else if mi_unlikely(mi_mul_overflow(count, size, total)) {
|
|
480
|
+
#if MI_DEBUG > 0
|
|
481
|
+
_mi_error_message(EOVERFLOW, "allocation request is too large (%zu * %zu bytes)\n", count, size);
|
|
482
|
+
#endif
|
|
483
|
+
*total = SIZE_MAX;
|
|
484
|
+
return true;
|
|
485
|
+
}
|
|
486
|
+
else return false;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
/*----------------------------------------------------------------------------------------
|
|
491
|
+
Heap functions
|
|
492
|
+
------------------------------------------------------------------------------------------- */
|
|
493
|
+
|
|
494
|
+
extern mi_decl_hidden const mi_heap_t _mi_heap_empty; // read-only empty heap, initial value of the thread local default heap
|
|
495
|
+
|
|
496
|
+
static inline bool mi_heap_is_backing(const mi_heap_t* heap) {
|
|
497
|
+
return (heap->tld->heap_backing == heap);
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
static inline bool mi_heap_is_initialized(mi_heap_t* heap) {
|
|
501
|
+
mi_assert_internal(heap != NULL);
|
|
502
|
+
return (heap != NULL && heap != &_mi_heap_empty);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
static inline uintptr_t _mi_ptr_cookie(const void* p) {
|
|
506
|
+
extern mi_decl_hidden mi_heap_t _mi_heap_main;
|
|
507
|
+
mi_assert_internal(_mi_heap_main.cookie != 0);
|
|
508
|
+
return ((uintptr_t)p ^ _mi_heap_main.cookie);
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/* -----------------------------------------------------------
|
|
512
|
+
Pages
|
|
513
|
+
----------------------------------------------------------- */
|
|
514
|
+
|
|
515
|
+
static inline mi_page_t* _mi_heap_get_free_small_page(mi_heap_t* heap, size_t size) {
|
|
516
|
+
mi_assert_internal(size <= (MI_SMALL_SIZE_MAX + MI_PADDING_SIZE));
|
|
517
|
+
const size_t idx = _mi_wsize_from_size(size);
|
|
518
|
+
mi_assert_internal(idx < MI_PAGES_DIRECT);
|
|
519
|
+
return heap->pages_free_direct[idx];
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
// Segment that contains the pointer
|
|
523
|
+
// Large aligned blocks may be aligned at N*MI_SEGMENT_SIZE (inside a huge segment > MI_SEGMENT_SIZE),
|
|
524
|
+
// and we need align "down" to the segment info which is `MI_SEGMENT_SIZE` bytes before it;
|
|
525
|
+
// therefore we align one byte before `p`.
|
|
526
|
+
// We check for NULL afterwards on 64-bit systems to improve codegen for `mi_free`.
|
|
527
|
+
static inline mi_segment_t* _mi_ptr_segment(const void* p) {
|
|
528
|
+
mi_segment_t* const segment = (mi_segment_t*)(((uintptr_t)p - 1) & ~MI_SEGMENT_MASK);
|
|
529
|
+
#if MI_INTPTR_SIZE <= 4
|
|
530
|
+
return (p==NULL ? NULL : segment);
|
|
531
|
+
#else
|
|
532
|
+
return ((intptr_t)segment <= 0 ? NULL : segment);
|
|
533
|
+
#endif
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
static inline mi_page_t* mi_slice_to_page(mi_slice_t* s) {
|
|
537
|
+
mi_assert_internal(s->slice_offset== 0 && s->slice_count > 0);
|
|
538
|
+
return (mi_page_t*)(s);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
static inline mi_slice_t* mi_page_to_slice(mi_page_t* p) {
|
|
542
|
+
mi_assert_internal(p->slice_offset== 0 && p->slice_count > 0);
|
|
543
|
+
return (mi_slice_t*)(p);
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
// Segment belonging to a page
|
|
547
|
+
static inline mi_segment_t* _mi_page_segment(const mi_page_t* page) {
|
|
548
|
+
mi_assert_internal(page!=NULL);
|
|
549
|
+
mi_segment_t* segment = _mi_ptr_segment(page);
|
|
550
|
+
mi_assert_internal(segment == NULL || ((mi_slice_t*)page >= segment->slices && (mi_slice_t*)page < segment->slices + segment->slice_entries));
|
|
551
|
+
return segment;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
static inline mi_slice_t* mi_slice_first(const mi_slice_t* slice) {
|
|
555
|
+
mi_slice_t* start = (mi_slice_t*)((uint8_t*)slice - slice->slice_offset);
|
|
556
|
+
mi_assert_internal(start >= _mi_ptr_segment(slice)->slices);
|
|
557
|
+
mi_assert_internal(start->slice_offset == 0);
|
|
558
|
+
mi_assert_internal(start + start->slice_count > slice);
|
|
559
|
+
return start;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
// Get the page containing the pointer (performance critical as it is called in mi_free)
|
|
563
|
+
static inline mi_page_t* _mi_segment_page_of(const mi_segment_t* segment, const void* p) {
|
|
564
|
+
mi_assert_internal(p > (void*)segment);
|
|
565
|
+
ptrdiff_t diff = (uint8_t*)p - (uint8_t*)segment;
|
|
566
|
+
mi_assert_internal(diff > 0 && diff <= (ptrdiff_t)MI_SEGMENT_SIZE);
|
|
567
|
+
size_t idx = (size_t)diff >> MI_SEGMENT_SLICE_SHIFT;
|
|
568
|
+
mi_assert_internal(idx <= segment->slice_entries);
|
|
569
|
+
mi_slice_t* slice0 = (mi_slice_t*)&segment->slices[idx];
|
|
570
|
+
mi_slice_t* slice = mi_slice_first(slice0); // adjust to the block that holds the page data
|
|
571
|
+
mi_assert_internal(slice->slice_offset == 0);
|
|
572
|
+
mi_assert_internal(slice >= segment->slices && slice < segment->slices + segment->slice_entries);
|
|
573
|
+
return mi_slice_to_page(slice);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
// Quick page start for initialized pages
|
|
577
|
+
static inline uint8_t* mi_page_start(const mi_page_t* page) {
|
|
578
|
+
mi_assert_internal(page->page_start != NULL);
|
|
579
|
+
mi_assert_expensive(_mi_segment_page_start(_mi_page_segment(page),page,NULL) == page->page_start);
|
|
580
|
+
return page->page_start;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
// Get the page containing the pointer
|
|
584
|
+
static inline mi_page_t* _mi_ptr_page(void* p) {
|
|
585
|
+
mi_assert_internal(p!=NULL);
|
|
586
|
+
return _mi_segment_page_of(_mi_ptr_segment(p), p);
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
// Get the block size of a page (special case for huge objects)
|
|
590
|
+
static inline size_t mi_page_block_size(const mi_page_t* page) {
|
|
591
|
+
mi_assert_internal(page->block_size > 0);
|
|
592
|
+
return page->block_size;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
static inline bool mi_page_is_huge(const mi_page_t* page) {
|
|
596
|
+
mi_assert_internal((page->is_huge && _mi_page_segment(page)->kind == MI_SEGMENT_HUGE) ||
|
|
597
|
+
(!page->is_huge && _mi_page_segment(page)->kind != MI_SEGMENT_HUGE));
|
|
598
|
+
return page->is_huge;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
// Get the usable block size of a page without fixed padding.
|
|
602
|
+
// This may still include internal padding due to alignment and rounding up size classes.
|
|
603
|
+
static inline size_t mi_page_usable_block_size(const mi_page_t* page) {
|
|
604
|
+
return mi_page_block_size(page) - MI_PADDING_SIZE;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
// size of a segment
|
|
608
|
+
static inline size_t mi_segment_size(mi_segment_t* segment) {
|
|
609
|
+
return segment->segment_slices * MI_SEGMENT_SLICE_SIZE;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
static inline uint8_t* mi_segment_end(mi_segment_t* segment) {
|
|
613
|
+
return (uint8_t*)segment + mi_segment_size(segment);
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
// Thread free access
|
|
617
|
+
static inline mi_block_t* mi_page_thread_free(const mi_page_t* page) {
|
|
618
|
+
return (mi_block_t*)(mi_atomic_load_relaxed(&((mi_page_t*)page)->xthread_free) & ~3);
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
static inline mi_delayed_t mi_page_thread_free_flag(const mi_page_t* page) {
|
|
622
|
+
return (mi_delayed_t)(mi_atomic_load_relaxed(&((mi_page_t*)page)->xthread_free) & 3);
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
// Heap access
|
|
626
|
+
static inline mi_heap_t* mi_page_heap(const mi_page_t* page) {
|
|
627
|
+
return (mi_heap_t*)(mi_atomic_load_relaxed(&((mi_page_t*)page)->xheap));
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
static inline void mi_page_set_heap(mi_page_t* page, mi_heap_t* heap) {
|
|
631
|
+
mi_assert_internal(mi_page_thread_free_flag(page) != MI_DELAYED_FREEING);
|
|
632
|
+
mi_atomic_store_release(&page->xheap,(uintptr_t)heap);
|
|
633
|
+
if (heap != NULL) { page->heap_tag = heap->tag; }
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
// Thread free flag helpers
|
|
637
|
+
static inline mi_block_t* mi_tf_block(mi_thread_free_t tf) {
|
|
638
|
+
return (mi_block_t*)(tf & ~0x03);
|
|
639
|
+
}
|
|
640
|
+
static inline mi_delayed_t mi_tf_delayed(mi_thread_free_t tf) {
|
|
641
|
+
return (mi_delayed_t)(tf & 0x03);
|
|
642
|
+
}
|
|
643
|
+
static inline mi_thread_free_t mi_tf_make(mi_block_t* block, mi_delayed_t delayed) {
|
|
644
|
+
return (mi_thread_free_t)((uintptr_t)block | (uintptr_t)delayed);
|
|
645
|
+
}
|
|
646
|
+
static inline mi_thread_free_t mi_tf_set_delayed(mi_thread_free_t tf, mi_delayed_t delayed) {
|
|
647
|
+
return mi_tf_make(mi_tf_block(tf),delayed);
|
|
648
|
+
}
|
|
649
|
+
static inline mi_thread_free_t mi_tf_set_block(mi_thread_free_t tf, mi_block_t* block) {
|
|
650
|
+
return mi_tf_make(block, mi_tf_delayed(tf));
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
// are all blocks in a page freed?
|
|
654
|
+
// note: needs up-to-date used count, (as the `xthread_free` list may not be empty). see `_mi_page_collect_free`.
|
|
655
|
+
static inline bool mi_page_all_free(const mi_page_t* page) {
|
|
656
|
+
mi_assert_internal(page != NULL);
|
|
657
|
+
return (page->used == 0);
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
// are there any available blocks?
|
|
661
|
+
static inline bool mi_page_has_any_available(const mi_page_t* page) {
|
|
662
|
+
mi_assert_internal(page != NULL && page->reserved > 0);
|
|
663
|
+
return (page->used < page->reserved || (mi_page_thread_free(page) != NULL));
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
// are there immediately available blocks, i.e. blocks available on the free list.
|
|
667
|
+
static inline bool mi_page_immediate_available(const mi_page_t* page) {
|
|
668
|
+
mi_assert_internal(page != NULL);
|
|
669
|
+
return (page->free != NULL);
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
// is more than 7/8th of a page in use?
|
|
673
|
+
static inline bool mi_page_is_mostly_used(const mi_page_t* page) {
|
|
674
|
+
if (page==NULL) return true;
|
|
675
|
+
uint16_t frac = page->reserved / 8U;
|
|
676
|
+
return (page->reserved - page->used <= frac);
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
static inline mi_page_queue_t* mi_page_queue(const mi_heap_t* heap, size_t size) {
|
|
680
|
+
return &((mi_heap_t*)heap)->pages[_mi_bin(size)];
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
//-----------------------------------------------------------
|
|
686
|
+
// Page flags
|
|
687
|
+
//-----------------------------------------------------------
|
|
688
|
+
static inline bool mi_page_is_in_full(const mi_page_t* page) {
|
|
689
|
+
return page->flags.x.in_full;
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
static inline void mi_page_set_in_full(mi_page_t* page, bool in_full) {
|
|
693
|
+
page->flags.x.in_full = in_full;
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
static inline bool mi_page_has_aligned(const mi_page_t* page) {
|
|
697
|
+
return page->flags.x.has_aligned;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
static inline void mi_page_set_has_aligned(mi_page_t* page, bool has_aligned) {
|
|
701
|
+
page->flags.x.has_aligned = has_aligned;
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
/* -------------------------------------------------------------------
|
|
705
|
+
Guarded objects
|
|
706
|
+
------------------------------------------------------------------- */
|
|
707
|
+
#if MI_GUARDED
|
|
708
|
+
static inline bool mi_block_ptr_is_guarded(const mi_block_t* block, const void* p) {
|
|
709
|
+
const ptrdiff_t offset = (uint8_t*)p - (uint8_t*)block;
|
|
710
|
+
return (offset >= (ptrdiff_t)(sizeof(mi_block_t)) && block->next == MI_BLOCK_TAG_GUARDED);
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
static inline bool mi_heap_malloc_use_guarded(mi_heap_t* heap, size_t size) {
|
|
714
|
+
// this code is written to result in fast assembly as it is on the hot path for allocation
|
|
715
|
+
const size_t count = heap->guarded_sample_count - 1; // if the rate was 0, this will underflow and count for a long time..
|
|
716
|
+
if mi_likely(count != 0) {
|
|
717
|
+
// no sample
|
|
718
|
+
heap->guarded_sample_count = count;
|
|
719
|
+
return false;
|
|
720
|
+
}
|
|
721
|
+
else if (size >= heap->guarded_size_min && size <= heap->guarded_size_max) {
|
|
722
|
+
// use guarded allocation
|
|
723
|
+
heap->guarded_sample_count = heap->guarded_sample_rate; // reset
|
|
724
|
+
return (heap->guarded_sample_rate != 0);
|
|
725
|
+
}
|
|
726
|
+
else {
|
|
727
|
+
// failed size criteria, rewind count (but don't write to an empty heap)
|
|
728
|
+
if (heap->guarded_sample_rate != 0) { heap->guarded_sample_count = 1; }
|
|
729
|
+
return false;
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
mi_decl_restrict void* _mi_heap_malloc_guarded(mi_heap_t* heap, size_t size, bool zero) mi_attr_noexcept;
|
|
734
|
+
|
|
735
|
+
#endif
|
|
736
|
+
|
|
737
|
+
|
|
738
|
+
/* -------------------------------------------------------------------
|
|
739
|
+
Encoding/Decoding the free list next pointers
|
|
740
|
+
|
|
741
|
+
This is to protect against buffer overflow exploits where the
|
|
742
|
+
free list is mutated. Many hardened allocators xor the next pointer `p`
|
|
743
|
+
with a secret key `k1`, as `p^k1`. This prevents overwriting with known
|
|
744
|
+
values but might be still too weak: if the attacker can guess
|
|
745
|
+
the pointer `p` this can reveal `k1` (since `p^k1^p == k1`).
|
|
746
|
+
Moreover, if multiple blocks can be read as well, the attacker can
|
|
747
|
+
xor both as `(p1^k1) ^ (p2^k1) == p1^p2` which may reveal a lot
|
|
748
|
+
about the pointers (and subsequently `k1`).
|
|
749
|
+
|
|
750
|
+
Instead mimalloc uses an extra key `k2` and encodes as `((p^k2)<<<k1)+k1`.
|
|
751
|
+
Since these operations are not associative, the above approaches do not
|
|
752
|
+
work so well any more even if the `p` can be guesstimated. For example,
|
|
753
|
+
for the read case we can subtract two entries to discard the `+k1` term,
|
|
754
|
+
but that leads to `((p1^k2)<<<k1) - ((p2^k2)<<<k1)` at best.
|
|
755
|
+
We include the left-rotation since xor and addition are otherwise linear
|
|
756
|
+
in the lowest bit. Finally, both keys are unique per page which reduces
|
|
757
|
+
the re-use of keys by a large factor.
|
|
758
|
+
|
|
759
|
+
We also pass a separate `null` value to be used as `NULL` or otherwise
|
|
760
|
+
`(k2<<<k1)+k1` would appear (too) often as a sentinel value.
|
|
761
|
+
------------------------------------------------------------------- */
|
|
762
|
+
|
|
763
|
+
static inline bool mi_is_in_same_segment(const void* p, const void* q) {
|
|
764
|
+
return (_mi_ptr_segment(p) == _mi_ptr_segment(q));
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
static inline bool mi_is_in_same_page(const void* p, const void* q) {
|
|
768
|
+
mi_segment_t* segment = _mi_ptr_segment(p);
|
|
769
|
+
if (_mi_ptr_segment(q) != segment) return false;
|
|
770
|
+
// assume q may be invalid // return (_mi_segment_page_of(segment, p) == _mi_segment_page_of(segment, q));
|
|
771
|
+
mi_page_t* page = _mi_segment_page_of(segment, p);
|
|
772
|
+
size_t psize;
|
|
773
|
+
uint8_t* start = _mi_segment_page_start(segment, page, &psize);
|
|
774
|
+
return (start <= (uint8_t*)q && (uint8_t*)q < start + psize);
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
static inline uintptr_t mi_rotl(uintptr_t x, uintptr_t shift) {
|
|
778
|
+
shift %= MI_INTPTR_BITS;
|
|
779
|
+
return (shift==0 ? x : ((x << shift) | (x >> (MI_INTPTR_BITS - shift))));
|
|
780
|
+
}
|
|
781
|
+
static inline uintptr_t mi_rotr(uintptr_t x, uintptr_t shift) {
|
|
782
|
+
shift %= MI_INTPTR_BITS;
|
|
783
|
+
return (shift==0 ? x : ((x >> shift) | (x << (MI_INTPTR_BITS - shift))));
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
static inline void* mi_ptr_decode(const void* null, const mi_encoded_t x, const uintptr_t* keys) {
|
|
787
|
+
void* p = (void*)(mi_rotr(x - keys[0], keys[0]) ^ keys[1]);
|
|
788
|
+
return (p==null ? NULL : p);
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
static inline mi_encoded_t mi_ptr_encode(const void* null, const void* p, const uintptr_t* keys) {
|
|
792
|
+
uintptr_t x = (uintptr_t)(p==NULL ? null : p);
|
|
793
|
+
return mi_rotl(x ^ keys[1], keys[0]) + keys[0];
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
static inline uint32_t mi_ptr_encode_canary(const void* null, const void* p, const uintptr_t* keys) {
|
|
797
|
+
const uint32_t x = (uint32_t)(mi_ptr_encode(null,p,keys));
|
|
798
|
+
// make the lowest byte 0 to prevent spurious read overflows which could be a security issue (issue #951)
|
|
799
|
+
#ifdef MI_BIG_ENDIAN
|
|
800
|
+
return (x & 0x00FFFFFF);
|
|
801
|
+
#else
|
|
802
|
+
return (x & 0xFFFFFF00);
|
|
803
|
+
#endif
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
static inline mi_block_t* mi_block_nextx( const void* null, const mi_block_t* block, const uintptr_t* keys ) {
|
|
807
|
+
mi_track_mem_defined(block,sizeof(mi_block_t));
|
|
808
|
+
mi_block_t* next;
|
|
809
|
+
#ifdef MI_ENCODE_FREELIST
|
|
810
|
+
next = (mi_block_t*)mi_ptr_decode(null, block->next, keys);
|
|
811
|
+
#else
|
|
812
|
+
MI_UNUSED(keys); MI_UNUSED(null);
|
|
813
|
+
next = (mi_block_t*)block->next;
|
|
814
|
+
#endif
|
|
815
|
+
mi_track_mem_noaccess(block,sizeof(mi_block_t));
|
|
816
|
+
return next;
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
static inline void mi_block_set_nextx(const void* null, mi_block_t* block, const mi_block_t* next, const uintptr_t* keys) {
|
|
820
|
+
mi_track_mem_undefined(block,sizeof(mi_block_t));
|
|
821
|
+
#ifdef MI_ENCODE_FREELIST
|
|
822
|
+
block->next = mi_ptr_encode(null, next, keys);
|
|
823
|
+
#else
|
|
824
|
+
MI_UNUSED(keys); MI_UNUSED(null);
|
|
825
|
+
block->next = (mi_encoded_t)next;
|
|
826
|
+
#endif
|
|
827
|
+
mi_track_mem_noaccess(block,sizeof(mi_block_t));
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
static inline mi_block_t* mi_block_next(const mi_page_t* page, const mi_block_t* block) {
|
|
831
|
+
#ifdef MI_ENCODE_FREELIST
|
|
832
|
+
mi_block_t* next = mi_block_nextx(page,block,page->keys);
|
|
833
|
+
// check for free list corruption: is `next` at least in the same page?
|
|
834
|
+
// TODO: check if `next` is `page->block_size` aligned?
|
|
835
|
+
if mi_unlikely(next!=NULL && !mi_is_in_same_page(block, next)) {
|
|
836
|
+
_mi_error_message(EFAULT, "corrupted free list entry of size %zub at %p: value 0x%zx\n", mi_page_block_size(page), block, (uintptr_t)next);
|
|
837
|
+
next = NULL;
|
|
838
|
+
}
|
|
839
|
+
return next;
|
|
840
|
+
#else
|
|
841
|
+
MI_UNUSED(page);
|
|
842
|
+
return mi_block_nextx(page,block,NULL);
|
|
843
|
+
#endif
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
static inline void mi_block_set_next(const mi_page_t* page, mi_block_t* block, const mi_block_t* next) {
|
|
847
|
+
#ifdef MI_ENCODE_FREELIST
|
|
848
|
+
mi_block_set_nextx(page,block,next, page->keys);
|
|
849
|
+
#else
|
|
850
|
+
MI_UNUSED(page);
|
|
851
|
+
mi_block_set_nextx(page,block,next,NULL);
|
|
852
|
+
#endif
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
|
|
856
|
+
// -------------------------------------------------------------------
|
|
857
|
+
// commit mask
|
|
858
|
+
// -------------------------------------------------------------------
|
|
859
|
+
|
|
860
|
+
static inline void mi_commit_mask_create_empty(mi_commit_mask_t* cm) {
|
|
861
|
+
for (size_t i = 0; i < MI_COMMIT_MASK_FIELD_COUNT; i++) {
|
|
862
|
+
cm->mask[i] = 0;
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
static inline void mi_commit_mask_create_full(mi_commit_mask_t* cm) {
|
|
867
|
+
for (size_t i = 0; i < MI_COMMIT_MASK_FIELD_COUNT; i++) {
|
|
868
|
+
cm->mask[i] = ~((size_t)0);
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
static inline bool mi_commit_mask_is_empty(const mi_commit_mask_t* cm) {
|
|
873
|
+
for (size_t i = 0; i < MI_COMMIT_MASK_FIELD_COUNT; i++) {
|
|
874
|
+
if (cm->mask[i] != 0) return false;
|
|
875
|
+
}
|
|
876
|
+
return true;
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
static inline bool mi_commit_mask_is_full(const mi_commit_mask_t* cm) {
|
|
880
|
+
for (size_t i = 0; i < MI_COMMIT_MASK_FIELD_COUNT; i++) {
|
|
881
|
+
if (cm->mask[i] != ~((size_t)0)) return false;
|
|
882
|
+
}
|
|
883
|
+
return true;
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
// defined in `segment.c`:
|
|
887
|
+
size_t _mi_commit_mask_committed_size(const mi_commit_mask_t* cm, size_t total);
|
|
888
|
+
size_t _mi_commit_mask_next_run(const mi_commit_mask_t* cm, size_t* idx);
|
|
889
|
+
|
|
890
|
+
#define mi_commit_mask_foreach(cm,idx,count) \
|
|
891
|
+
idx = 0; \
|
|
892
|
+
while ((count = _mi_commit_mask_next_run(cm,&idx)) > 0) {
|
|
893
|
+
|
|
894
|
+
#define mi_commit_mask_foreach_end() \
|
|
895
|
+
idx += count; \
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
|
|
899
|
+
|
|
900
|
+
/* -----------------------------------------------------------
|
|
901
|
+
memory id's
|
|
902
|
+
----------------------------------------------------------- */
|
|
903
|
+
|
|
904
|
+
static inline mi_memid_t _mi_memid_create(mi_memkind_t memkind) {
|
|
905
|
+
mi_memid_t memid;
|
|
906
|
+
_mi_memzero_var(memid);
|
|
907
|
+
memid.memkind = memkind;
|
|
908
|
+
return memid;
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
static inline mi_memid_t _mi_memid_none(void) {
|
|
912
|
+
return _mi_memid_create(MI_MEM_NONE);
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
static inline mi_memid_t _mi_memid_create_os(void* base, size_t size, bool committed, bool is_zero, bool is_large) {
|
|
916
|
+
mi_memid_t memid = _mi_memid_create(MI_MEM_OS);
|
|
917
|
+
memid.mem.os.base = base;
|
|
918
|
+
memid.mem.os.size = size;
|
|
919
|
+
memid.initially_committed = committed;
|
|
920
|
+
memid.initially_zero = is_zero;
|
|
921
|
+
memid.is_pinned = is_large;
|
|
922
|
+
return memid;
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
|
|
926
|
+
// -------------------------------------------------------------------
|
|
927
|
+
// Fast "random" shuffle
|
|
928
|
+
// -------------------------------------------------------------------
|
|
929
|
+
|
|
930
|
+
static inline uintptr_t _mi_random_shuffle(uintptr_t x) {
|
|
931
|
+
if (x==0) { x = 17; } // ensure we don't get stuck in generating zeros
|
|
932
|
+
#if (MI_INTPTR_SIZE>=8)
|
|
933
|
+
// by Sebastiano Vigna, see: <http://xoshiro.di.unimi.it/splitmix64.c>
|
|
934
|
+
x ^= x >> 30;
|
|
935
|
+
x *= 0xbf58476d1ce4e5b9UL;
|
|
936
|
+
x ^= x >> 27;
|
|
937
|
+
x *= 0x94d049bb133111ebUL;
|
|
938
|
+
x ^= x >> 31;
|
|
939
|
+
#elif (MI_INTPTR_SIZE==4)
|
|
940
|
+
// by Chris Wellons, see: <https://nullprogram.com/blog/2018/07/31/>
|
|
941
|
+
x ^= x >> 16;
|
|
942
|
+
x *= 0x7feb352dUL;
|
|
943
|
+
x ^= x >> 15;
|
|
944
|
+
x *= 0x846ca68bUL;
|
|
945
|
+
x ^= x >> 16;
|
|
946
|
+
#endif
|
|
947
|
+
return x;
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
|
|
951
|
+
|
|
952
|
+
// -----------------------------------------------------------------------
|
|
953
|
+
// Count bits: trailing or leading zeros (with MI_INTPTR_BITS on all zero)
|
|
954
|
+
// -----------------------------------------------------------------------
|
|
955
|
+
|
|
956
|
+
#if defined(__GNUC__)
|
|
957
|
+
|
|
958
|
+
#include <limits.h> // LONG_MAX
|
|
959
|
+
#define MI_HAVE_FAST_BITSCAN
|
|
960
|
+
static inline size_t mi_clz(size_t x) {
|
|
961
|
+
if (x==0) return MI_SIZE_BITS;
|
|
962
|
+
#if (SIZE_MAX == ULONG_MAX)
|
|
963
|
+
return __builtin_clzl(x);
|
|
964
|
+
#else
|
|
965
|
+
return __builtin_clzll(x);
|
|
966
|
+
#endif
|
|
967
|
+
}
|
|
968
|
+
static inline size_t mi_ctz(size_t x) {
|
|
969
|
+
if (x==0) return MI_SIZE_BITS;
|
|
970
|
+
#if (SIZE_MAX == ULONG_MAX)
|
|
971
|
+
return __builtin_ctzl(x);
|
|
972
|
+
#else
|
|
973
|
+
return __builtin_ctzll(x);
|
|
974
|
+
#endif
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
#elif defined(_MSC_VER)
|
|
978
|
+
|
|
979
|
+
#include <limits.h> // LONG_MAX
|
|
980
|
+
#include <intrin.h> // BitScanReverse64
|
|
981
|
+
#define MI_HAVE_FAST_BITSCAN
|
|
982
|
+
static inline size_t mi_clz(size_t x) {
|
|
983
|
+
if (x==0) return MI_SIZE_BITS;
|
|
984
|
+
unsigned long idx;
|
|
985
|
+
#if (SIZE_MAX == ULONG_MAX)
|
|
986
|
+
_BitScanReverse(&idx, x);
|
|
987
|
+
#else
|
|
988
|
+
_BitScanReverse64(&idx, x);
|
|
989
|
+
#endif
|
|
990
|
+
return ((MI_SIZE_BITS - 1) - (size_t)idx);
|
|
991
|
+
}
|
|
992
|
+
static inline size_t mi_ctz(size_t x) {
|
|
993
|
+
if (x==0) return MI_SIZE_BITS;
|
|
994
|
+
unsigned long idx;
|
|
995
|
+
#if (SIZE_MAX == ULONG_MAX)
|
|
996
|
+
_BitScanForward(&idx, x);
|
|
997
|
+
#else
|
|
998
|
+
_BitScanForward64(&idx, x);
|
|
999
|
+
#endif
|
|
1000
|
+
return (size_t)idx;
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
#else
|
|
1004
|
+
|
|
1005
|
+
static inline size_t mi_ctz_generic32(uint32_t x) {
|
|
1006
|
+
// de Bruijn multiplication, see <http://supertech.csail.mit.edu/papers/debruijn.pdf>
|
|
1007
|
+
static const uint8_t debruijn[32] = {
|
|
1008
|
+
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
|
|
1009
|
+
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
|
|
1010
|
+
};
|
|
1011
|
+
if (x==0) return 32;
|
|
1012
|
+
return debruijn[(uint32_t)((x & -(int32_t)x) * (uint32_t)(0x077CB531U)) >> 27];
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
static inline size_t mi_clz_generic32(uint32_t x) {
|
|
1016
|
+
// de Bruijn multiplication, see <http://supertech.csail.mit.edu/papers/debruijn.pdf>
|
|
1017
|
+
static const uint8_t debruijn[32] = {
|
|
1018
|
+
31, 22, 30, 21, 18, 10, 29, 2, 20, 17, 15, 13, 9, 6, 28, 1,
|
|
1019
|
+
23, 19, 11, 3, 16, 14, 7, 24, 12, 4, 8, 25, 5, 26, 27, 0
|
|
1020
|
+
};
|
|
1021
|
+
if (x==0) return 32;
|
|
1022
|
+
x |= x >> 1;
|
|
1023
|
+
x |= x >> 2;
|
|
1024
|
+
x |= x >> 4;
|
|
1025
|
+
x |= x >> 8;
|
|
1026
|
+
x |= x >> 16;
|
|
1027
|
+
return debruijn[(uint32_t)(x * (uint32_t)(0x07C4ACDDU)) >> 27];
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
static inline size_t mi_ctz(size_t x) {
|
|
1031
|
+
if (x==0) return MI_SIZE_BITS;
|
|
1032
|
+
#if (MI_SIZE_BITS <= 32)
|
|
1033
|
+
return mi_ctz_generic32((uint32_t)x);
|
|
1034
|
+
#else
|
|
1035
|
+
const uint32_t lo = (uint32_t)x;
|
|
1036
|
+
if (lo != 0) {
|
|
1037
|
+
return mi_ctz_generic32(lo);
|
|
1038
|
+
}
|
|
1039
|
+
else {
|
|
1040
|
+
return (32 + mi_ctz_generic32((uint32_t)(x>>32)));
|
|
1041
|
+
}
|
|
1042
|
+
#endif
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
static inline size_t mi_clz(size_t x) {
|
|
1046
|
+
if (x==0) return MI_SIZE_BITS;
|
|
1047
|
+
#if (MI_SIZE_BITS <= 32)
|
|
1048
|
+
return mi_clz_generic32((uint32_t)x);
|
|
1049
|
+
#else
|
|
1050
|
+
const uint32_t hi = (uint32_t)(x>>32);
|
|
1051
|
+
if (hi != 0) {
|
|
1052
|
+
return mi_clz_generic32(hi);
|
|
1053
|
+
}
|
|
1054
|
+
else {
|
|
1055
|
+
return 32 + mi_clz_generic32((uint32_t)x);
|
|
1056
|
+
}
|
|
1057
|
+
#endif
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
#endif
|
|
1061
|
+
|
|
1062
|
+
// "bit scan reverse": Return index of the highest bit (or MI_SIZE_BITS if `x` is zero)
|
|
1063
|
+
static inline size_t mi_bsr(size_t x) {
|
|
1064
|
+
return (x==0 ? MI_SIZE_BITS : MI_SIZE_BITS - 1 - mi_clz(x));
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
size_t _mi_popcount_generic(size_t x);
|
|
1068
|
+
|
|
1069
|
+
static inline size_t mi_popcount(size_t x) {
|
|
1070
|
+
if (x<=1) return x;
|
|
1071
|
+
if (x==SIZE_MAX) return MI_SIZE_BITS;
|
|
1072
|
+
#if defined(__GNUC__)
|
|
1073
|
+
#if (SIZE_MAX == ULONG_MAX)
|
|
1074
|
+
return __builtin_popcountl(x);
|
|
1075
|
+
#else
|
|
1076
|
+
return __builtin_popcountll(x);
|
|
1077
|
+
#endif
|
|
1078
|
+
#else
|
|
1079
|
+
return _mi_popcount_generic(x);
|
|
1080
|
+
#endif
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
// ---------------------------------------------------------------------------------
|
|
1084
|
+
// Provide our own `_mi_memcpy` for potential performance optimizations.
|
|
1085
|
+
//
|
|
1086
|
+
// For now, only on Windows with msvc/clang-cl we optimize to `rep movsb` if
|
|
1087
|
+
// we happen to run on x86/x64 cpu's that have "fast short rep movsb" (FSRM) support
|
|
1088
|
+
// (AMD Zen3+ (~2020) or Intel Ice Lake+ (~2017). See also issue #201 and pr #253.
|
|
1089
|
+
// ---------------------------------------------------------------------------------
|
|
1090
|
+
|
|
1091
|
+
#if !MI_TRACK_ENABLED && defined(_WIN32) && (defined(_M_IX86) || defined(_M_X64))
|
|
1092
|
+
#include <intrin.h>
|
|
1093
|
+
extern mi_decl_hidden bool _mi_cpu_has_fsrm;
|
|
1094
|
+
extern mi_decl_hidden bool _mi_cpu_has_erms;
|
|
1095
|
+
static inline void _mi_memcpy(void* dst, const void* src, size_t n) {
|
|
1096
|
+
if ((_mi_cpu_has_fsrm && n <= 128) || (_mi_cpu_has_erms && n > 128)) {
|
|
1097
|
+
__movsb((unsigned char*)dst, (const unsigned char*)src, n);
|
|
1098
|
+
}
|
|
1099
|
+
else {
|
|
1100
|
+
memcpy(dst, src, n);
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
static inline void _mi_memzero(void* dst, size_t n) {
|
|
1104
|
+
if ((_mi_cpu_has_fsrm && n <= 128) || (_mi_cpu_has_erms && n > 128)) {
|
|
1105
|
+
__stosb((unsigned char*)dst, 0, n);
|
|
1106
|
+
}
|
|
1107
|
+
else {
|
|
1108
|
+
memset(dst, 0, n);
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
#else
|
|
1112
|
+
static inline void _mi_memcpy(void* dst, const void* src, size_t n) {
|
|
1113
|
+
memcpy(dst, src, n);
|
|
1114
|
+
}
|
|
1115
|
+
static inline void _mi_memzero(void* dst, size_t n) {
|
|
1116
|
+
memset(dst, 0, n);
|
|
1117
|
+
}
|
|
1118
|
+
#endif
|
|
1119
|
+
|
|
1120
|
+
// -------------------------------------------------------------------------------
|
|
1121
|
+
// The `_mi_memcpy_aligned` can be used if the pointers are machine-word aligned
|
|
1122
|
+
// This is used for example in `mi_realloc`.
|
|
1123
|
+
// -------------------------------------------------------------------------------
|
|
1124
|
+
|
|
1125
|
+
#if (defined(__GNUC__) && (__GNUC__ >= 4)) || defined(__clang__)
|
|
1126
|
+
// On GCC/CLang we provide a hint that the pointers are word aligned.
|
|
1127
|
+
static inline void _mi_memcpy_aligned(void* dst, const void* src, size_t n) {
|
|
1128
|
+
mi_assert_internal(((uintptr_t)dst % MI_INTPTR_SIZE == 0) && ((uintptr_t)src % MI_INTPTR_SIZE == 0));
|
|
1129
|
+
void* adst = __builtin_assume_aligned(dst, MI_INTPTR_SIZE);
|
|
1130
|
+
const void* asrc = __builtin_assume_aligned(src, MI_INTPTR_SIZE);
|
|
1131
|
+
_mi_memcpy(adst, asrc, n);
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
static inline void _mi_memzero_aligned(void* dst, size_t n) {
|
|
1135
|
+
mi_assert_internal((uintptr_t)dst % MI_INTPTR_SIZE == 0);
|
|
1136
|
+
void* adst = __builtin_assume_aligned(dst, MI_INTPTR_SIZE);
|
|
1137
|
+
_mi_memzero(adst, n);
|
|
1138
|
+
}
|
|
1139
|
+
#else
|
|
1140
|
+
// Default fallback on `_mi_memcpy`
|
|
1141
|
+
static inline void _mi_memcpy_aligned(void* dst, const void* src, size_t n) {
|
|
1142
|
+
mi_assert_internal(((uintptr_t)dst % MI_INTPTR_SIZE == 0) && ((uintptr_t)src % MI_INTPTR_SIZE == 0));
|
|
1143
|
+
_mi_memcpy(dst, src, n);
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
static inline void _mi_memzero_aligned(void* dst, size_t n) {
|
|
1147
|
+
mi_assert_internal((uintptr_t)dst % MI_INTPTR_SIZE == 0);
|
|
1148
|
+
_mi_memzero(dst, n);
|
|
1149
|
+
}
|
|
1150
|
+
#endif
|
|
1151
|
+
|
|
1152
|
+
|
|
1153
|
+
#endif
|