@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,249 @@
|
|
|
1
|
+
/* ----------------------------------------------------------------------------
|
|
2
|
+
Copyright (c) 2018-2025, Microsoft Research, Daan Leijen, Alon Zakai
|
|
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
|
+
|
|
8
|
+
// This file is included in `src/prim/prim.c`
|
|
9
|
+
|
|
10
|
+
#include "mimalloc.h"
|
|
11
|
+
#include "mimalloc/internal.h"
|
|
12
|
+
#include "mimalloc/atomic.h"
|
|
13
|
+
#include "mimalloc/prim.h"
|
|
14
|
+
|
|
15
|
+
// Design
|
|
16
|
+
// ======
|
|
17
|
+
//
|
|
18
|
+
// mimalloc is built on top of emmalloc. emmalloc is a minimal allocator on top
|
|
19
|
+
// of sbrk. The reason for having three layers here is that we want mimalloc to
|
|
20
|
+
// be able to allocate and release system memory properly, the same way it would
|
|
21
|
+
// when using VirtualAlloc on Windows or mmap on POSIX, and sbrk is too limited.
|
|
22
|
+
// Specifically, sbrk can only go up and down, and not "skip" over regions, and
|
|
23
|
+
// so we end up either never freeing memory to the system, or we can get stuck
|
|
24
|
+
// with holes.
|
|
25
|
+
//
|
|
26
|
+
// Atm wasm generally does *not* free memory back the system: once grown, we do
|
|
27
|
+
// not shrink back down (https://github.com/WebAssembly/design/issues/1397).
|
|
28
|
+
// However, that is expected to improve
|
|
29
|
+
// (https://github.com/WebAssembly/memory-control/blob/main/proposals/memory-control/Overview.md)
|
|
30
|
+
// and so we do not want to bake those limitations in here.
|
|
31
|
+
//
|
|
32
|
+
// Even without that issue, we want our system allocator to handle holes, that
|
|
33
|
+
// is, it should merge freed regions and allow allocating new content there of
|
|
34
|
+
// the full size, etc., so that we do not waste space. That means that the
|
|
35
|
+
// system allocator really does need to handle the general problem of allocating
|
|
36
|
+
// and freeing variable-sized chunks of memory in a random order, like malloc/
|
|
37
|
+
// free do. And so it makes sense to layer mimalloc on top of such an
|
|
38
|
+
// implementation.
|
|
39
|
+
//
|
|
40
|
+
// emmalloc makes sense for the lower level because it is small and simple while
|
|
41
|
+
// still fully handling merging of holes etc. It is not the most efficient
|
|
42
|
+
// allocator, but our assumption is that mimalloc needs to be fast while the
|
|
43
|
+
// system allocator underneath it is called much less frequently.
|
|
44
|
+
//
|
|
45
|
+
|
|
46
|
+
//---------------------------------------------
|
|
47
|
+
// init
|
|
48
|
+
//---------------------------------------------
|
|
49
|
+
|
|
50
|
+
void _mi_prim_mem_init( mi_os_mem_config_t* config) {
|
|
51
|
+
config->page_size = 64*MI_KiB; // WebAssembly has a fixed page size: 64KiB
|
|
52
|
+
config->alloc_granularity = 16;
|
|
53
|
+
config->has_overcommit = false;
|
|
54
|
+
config->has_partial_free = false;
|
|
55
|
+
config->has_virtual_reserve = false;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
extern void emmalloc_free(void*);
|
|
59
|
+
|
|
60
|
+
int _mi_prim_free(void* addr, size_t size) {
|
|
61
|
+
if (size==0) return 0;
|
|
62
|
+
emmalloc_free(addr);
|
|
63
|
+
return 0;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
//---------------------------------------------
|
|
68
|
+
// Allocation
|
|
69
|
+
//---------------------------------------------
|
|
70
|
+
|
|
71
|
+
extern void* emmalloc_memalign(size_t alignment, size_t size);
|
|
72
|
+
|
|
73
|
+
// Note: the `try_alignment` is just a hint and the returned pointer is not guaranteed to be aligned.
|
|
74
|
+
int _mi_prim_alloc(void* hint_addr, size_t size, size_t try_alignment, bool commit, bool allow_large, bool* is_large, bool* is_zero, void** addr) {
|
|
75
|
+
MI_UNUSED(try_alignment); MI_UNUSED(allow_large); MI_UNUSED(commit); MI_UNUSED(hint_addr);
|
|
76
|
+
*is_large = false;
|
|
77
|
+
// TODO: Track the highest address ever seen; first uses of it are zeroes.
|
|
78
|
+
// That assumes no one else uses sbrk but us (they could go up,
|
|
79
|
+
// scribble, and then down), but we could assert on that perhaps.
|
|
80
|
+
*is_zero = false;
|
|
81
|
+
// emmalloc has a minimum alignment size.
|
|
82
|
+
#define MIN_EMMALLOC_ALIGN 8
|
|
83
|
+
if (try_alignment < MIN_EMMALLOC_ALIGN) {
|
|
84
|
+
try_alignment = MIN_EMMALLOC_ALIGN;
|
|
85
|
+
}
|
|
86
|
+
void* p = emmalloc_memalign(try_alignment, size);
|
|
87
|
+
*addr = p;
|
|
88
|
+
if (p == 0) {
|
|
89
|
+
return ENOMEM;
|
|
90
|
+
}
|
|
91
|
+
return 0;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
//---------------------------------------------
|
|
96
|
+
// Commit/Reset
|
|
97
|
+
//---------------------------------------------
|
|
98
|
+
|
|
99
|
+
int _mi_prim_commit(void* addr, size_t size, bool* is_zero) {
|
|
100
|
+
MI_UNUSED(addr); MI_UNUSED(size);
|
|
101
|
+
// See TODO above.
|
|
102
|
+
*is_zero = false;
|
|
103
|
+
return 0;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
int _mi_prim_decommit(void* addr, size_t size, bool* needs_recommit) {
|
|
107
|
+
MI_UNUSED(addr); MI_UNUSED(size);
|
|
108
|
+
*needs_recommit = false;
|
|
109
|
+
return 0;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
int _mi_prim_reset(void* addr, size_t size) {
|
|
113
|
+
MI_UNUSED(addr); MI_UNUSED(size);
|
|
114
|
+
return 0;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
int _mi_prim_reuse(void* addr, size_t size) {
|
|
118
|
+
MI_UNUSED(addr); MI_UNUSED(size);
|
|
119
|
+
return 0;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
int _mi_prim_protect(void* addr, size_t size, bool protect) {
|
|
123
|
+
MI_UNUSED(addr); MI_UNUSED(size); MI_UNUSED(protect);
|
|
124
|
+
return 0;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
//---------------------------------------------
|
|
129
|
+
// Huge pages and NUMA nodes
|
|
130
|
+
//---------------------------------------------
|
|
131
|
+
|
|
132
|
+
int _mi_prim_alloc_huge_os_pages(void* hint_addr, size_t size, int numa_node, bool* is_zero, void** addr) {
|
|
133
|
+
MI_UNUSED(hint_addr); MI_UNUSED(size); MI_UNUSED(numa_node);
|
|
134
|
+
*is_zero = true;
|
|
135
|
+
*addr = NULL;
|
|
136
|
+
return ENOSYS;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
size_t _mi_prim_numa_node(void) {
|
|
140
|
+
return 0;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
size_t _mi_prim_numa_node_count(void) {
|
|
144
|
+
return 1;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
//----------------------------------------------------------------
|
|
149
|
+
// Clock
|
|
150
|
+
//----------------------------------------------------------------
|
|
151
|
+
|
|
152
|
+
#include <emscripten/html5.h>
|
|
153
|
+
|
|
154
|
+
mi_msecs_t _mi_prim_clock_now(void) {
|
|
155
|
+
return emscripten_date_now();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
//----------------------------------------------------------------
|
|
160
|
+
// Process info
|
|
161
|
+
//----------------------------------------------------------------
|
|
162
|
+
|
|
163
|
+
void _mi_prim_process_info(mi_process_info_t* pinfo)
|
|
164
|
+
{
|
|
165
|
+
// use defaults
|
|
166
|
+
MI_UNUSED(pinfo);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
//----------------------------------------------------------------
|
|
171
|
+
// Output
|
|
172
|
+
//----------------------------------------------------------------
|
|
173
|
+
|
|
174
|
+
#include <emscripten/console.h>
|
|
175
|
+
|
|
176
|
+
void _mi_prim_out_stderr( const char* msg) {
|
|
177
|
+
emscripten_console_error(msg);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
//----------------------------------------------------------------
|
|
182
|
+
// Environment
|
|
183
|
+
//----------------------------------------------------------------
|
|
184
|
+
|
|
185
|
+
bool _mi_prim_getenv(const char* name, char* result, size_t result_size) {
|
|
186
|
+
// For code size reasons, do not support environ customization for now.
|
|
187
|
+
MI_UNUSED(name);
|
|
188
|
+
MI_UNUSED(result);
|
|
189
|
+
MI_UNUSED(result_size);
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
//----------------------------------------------------------------
|
|
195
|
+
// Random
|
|
196
|
+
//----------------------------------------------------------------
|
|
197
|
+
|
|
198
|
+
bool _mi_prim_random_buf(void* buf, size_t buf_len) {
|
|
199
|
+
int err = getentropy(buf, buf_len);
|
|
200
|
+
return !err;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
//----------------------------------------------------------------
|
|
205
|
+
// Thread init/done
|
|
206
|
+
//----------------------------------------------------------------
|
|
207
|
+
|
|
208
|
+
#if defined(MI_USE_PTHREADS)
|
|
209
|
+
|
|
210
|
+
// use pthread local storage keys to detect thread ending
|
|
211
|
+
// (and used with MI_TLS_PTHREADS for the default heap)
|
|
212
|
+
pthread_key_t _mi_heap_default_key = (pthread_key_t)(-1);
|
|
213
|
+
|
|
214
|
+
static void mi_pthread_done(void* value) {
|
|
215
|
+
if (value!=NULL) {
|
|
216
|
+
_mi_thread_done((mi_heap_t*)value);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
void _mi_prim_thread_init_auto_done(void) {
|
|
221
|
+
mi_assert_internal(_mi_heap_default_key == (pthread_key_t)(-1));
|
|
222
|
+
pthread_key_create(&_mi_heap_default_key, &mi_pthread_done);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
void _mi_prim_thread_done_auto_done(void) {
|
|
226
|
+
// nothing to do
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
void _mi_prim_thread_associate_default_heap(mi_heap_t* heap) {
|
|
230
|
+
if (_mi_heap_default_key != (pthread_key_t)(-1)) { // can happen during recursive invocation on freeBSD
|
|
231
|
+
pthread_setspecific(_mi_heap_default_key, heap);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
#else
|
|
236
|
+
|
|
237
|
+
void _mi_prim_thread_init_auto_done(void) {
|
|
238
|
+
// nothing
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
void _mi_prim_thread_done_auto_done(void) {
|
|
242
|
+
// nothing
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
void _mi_prim_thread_associate_default_heap(mi_heap_t* heap) {
|
|
246
|
+
MI_UNUSED(heap);
|
|
247
|
+
|
|
248
|
+
}
|
|
249
|
+
#endif
|
|
@@ -0,0 +1,461 @@
|
|
|
1
|
+
/* ----------------------------------------------------------------------------
|
|
2
|
+
Copyright (c) 2018-2022, 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
|
+
|
|
8
|
+
#include "mimalloc.h"
|
|
9
|
+
#include "mimalloc/internal.h"
|
|
10
|
+
|
|
11
|
+
#if defined(MI_MALLOC_OVERRIDE)
|
|
12
|
+
|
|
13
|
+
#if !defined(__APPLE__)
|
|
14
|
+
#error "this file should only be included on macOS"
|
|
15
|
+
#endif
|
|
16
|
+
|
|
17
|
+
/* ------------------------------------------------------
|
|
18
|
+
Override system malloc on macOS
|
|
19
|
+
This is done through the malloc zone interface.
|
|
20
|
+
It seems to be most robust in combination with interposing
|
|
21
|
+
though or otherwise we may get zone errors as there are could
|
|
22
|
+
be allocations done by the time we take over the
|
|
23
|
+
zone.
|
|
24
|
+
------------------------------------------------------ */
|
|
25
|
+
|
|
26
|
+
#include <AvailabilityMacros.h>
|
|
27
|
+
#include <malloc/malloc.h>
|
|
28
|
+
#include <string.h> // memset
|
|
29
|
+
#include <stdlib.h>
|
|
30
|
+
|
|
31
|
+
#ifdef __cplusplus
|
|
32
|
+
extern "C" {
|
|
33
|
+
#endif
|
|
34
|
+
|
|
35
|
+
#if defined(MAC_OS_X_VERSION_10_6) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
|
|
36
|
+
// only available from OSX 10.6
|
|
37
|
+
extern malloc_zone_t* malloc_default_purgeable_zone(void) __attribute__((weak_import));
|
|
38
|
+
#endif
|
|
39
|
+
|
|
40
|
+
/* ------------------------------------------------------
|
|
41
|
+
malloc zone members
|
|
42
|
+
------------------------------------------------------ */
|
|
43
|
+
|
|
44
|
+
static size_t zone_size(malloc_zone_t* zone, const void* p) {
|
|
45
|
+
MI_UNUSED(zone);
|
|
46
|
+
if (!mi_is_in_heap_region(p)){ return 0; } // not our pointer, bail out
|
|
47
|
+
return mi_usable_size(p);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
static void* zone_malloc(malloc_zone_t* zone, size_t size) {
|
|
51
|
+
MI_UNUSED(zone);
|
|
52
|
+
return mi_malloc(size);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
static void* zone_calloc(malloc_zone_t* zone, size_t count, size_t size) {
|
|
56
|
+
MI_UNUSED(zone);
|
|
57
|
+
return mi_calloc(count, size);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
static void* zone_valloc(malloc_zone_t* zone, size_t size) {
|
|
61
|
+
MI_UNUSED(zone);
|
|
62
|
+
return mi_malloc_aligned(size, _mi_os_page_size());
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
static void zone_free(malloc_zone_t* zone, void* p) {
|
|
66
|
+
MI_UNUSED(zone);
|
|
67
|
+
mi_cfree(p);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
static void* zone_realloc(malloc_zone_t* zone, void* p, size_t newsize) {
|
|
71
|
+
MI_UNUSED(zone);
|
|
72
|
+
return mi_realloc(p, newsize);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
static void* zone_memalign(malloc_zone_t* zone, size_t alignment, size_t size) {
|
|
76
|
+
MI_UNUSED(zone);
|
|
77
|
+
return mi_malloc_aligned(size,alignment);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
static void zone_destroy(malloc_zone_t* zone) {
|
|
81
|
+
MI_UNUSED(zone);
|
|
82
|
+
// todo: ignore for now?
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
static unsigned zone_batch_malloc(malloc_zone_t* zone, size_t size, void** ps, unsigned count) {
|
|
86
|
+
size_t i;
|
|
87
|
+
for (i = 0; i < count; i++) {
|
|
88
|
+
ps[i] = zone_malloc(zone, size);
|
|
89
|
+
if (ps[i] == NULL) break;
|
|
90
|
+
}
|
|
91
|
+
return i;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
static void zone_batch_free(malloc_zone_t* zone, void** ps, unsigned count) {
|
|
95
|
+
for(size_t i = 0; i < count; i++) {
|
|
96
|
+
zone_free(zone, ps[i]);
|
|
97
|
+
ps[i] = NULL;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
static size_t zone_pressure_relief(malloc_zone_t* zone, size_t size) {
|
|
102
|
+
MI_UNUSED(zone); MI_UNUSED(size);
|
|
103
|
+
mi_collect(false);
|
|
104
|
+
return 0;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
static void zone_free_definite_size(malloc_zone_t* zone, void* p, size_t size) {
|
|
108
|
+
MI_UNUSED(size);
|
|
109
|
+
zone_free(zone,p);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
static boolean_t zone_claimed_address(malloc_zone_t* zone, void* p) {
|
|
113
|
+
MI_UNUSED(zone);
|
|
114
|
+
return mi_is_in_heap_region(p);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
/* ------------------------------------------------------
|
|
119
|
+
Introspection members
|
|
120
|
+
------------------------------------------------------ */
|
|
121
|
+
|
|
122
|
+
static kern_return_t intro_enumerator(task_t task, void* p,
|
|
123
|
+
unsigned type_mask, vm_address_t zone_address,
|
|
124
|
+
memory_reader_t reader,
|
|
125
|
+
vm_range_recorder_t recorder)
|
|
126
|
+
{
|
|
127
|
+
// todo: enumerate all memory
|
|
128
|
+
MI_UNUSED(task); MI_UNUSED(p); MI_UNUSED(type_mask); MI_UNUSED(zone_address);
|
|
129
|
+
MI_UNUSED(reader); MI_UNUSED(recorder);
|
|
130
|
+
return KERN_SUCCESS;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
static size_t intro_good_size(malloc_zone_t* zone, size_t size) {
|
|
134
|
+
MI_UNUSED(zone);
|
|
135
|
+
return mi_good_size(size);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
static boolean_t intro_check(malloc_zone_t* zone) {
|
|
139
|
+
MI_UNUSED(zone);
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
static void intro_print(malloc_zone_t* zone, boolean_t verbose) {
|
|
144
|
+
MI_UNUSED(zone); MI_UNUSED(verbose);
|
|
145
|
+
mi_stats_print(NULL);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
static void intro_log(malloc_zone_t* zone, void* p) {
|
|
149
|
+
MI_UNUSED(zone); MI_UNUSED(p);
|
|
150
|
+
// todo?
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
static void intro_force_lock(malloc_zone_t* zone) {
|
|
154
|
+
MI_UNUSED(zone);
|
|
155
|
+
// todo?
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
static void intro_force_unlock(malloc_zone_t* zone) {
|
|
159
|
+
MI_UNUSED(zone);
|
|
160
|
+
// todo?
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
static void intro_statistics(malloc_zone_t* zone, malloc_statistics_t* stats) {
|
|
164
|
+
MI_UNUSED(zone);
|
|
165
|
+
// todo...
|
|
166
|
+
stats->blocks_in_use = 0;
|
|
167
|
+
stats->size_in_use = 0;
|
|
168
|
+
stats->max_size_in_use = 0;
|
|
169
|
+
stats->size_allocated = 0;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
static boolean_t intro_zone_locked(malloc_zone_t* zone) {
|
|
173
|
+
MI_UNUSED(zone);
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
/* ------------------------------------------------------
|
|
179
|
+
At process start, override the default allocator
|
|
180
|
+
------------------------------------------------------ */
|
|
181
|
+
|
|
182
|
+
#if defined(__GNUC__) && !defined(__clang__)
|
|
183
|
+
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
|
|
184
|
+
#endif
|
|
185
|
+
|
|
186
|
+
#if defined(__clang__)
|
|
187
|
+
#pragma clang diagnostic ignored "-Wc99-extensions"
|
|
188
|
+
#endif
|
|
189
|
+
|
|
190
|
+
static malloc_introspection_t mi_introspect = {
|
|
191
|
+
.enumerator = &intro_enumerator,
|
|
192
|
+
.good_size = &intro_good_size,
|
|
193
|
+
.check = &intro_check,
|
|
194
|
+
.print = &intro_print,
|
|
195
|
+
.log = &intro_log,
|
|
196
|
+
.force_lock = &intro_force_lock,
|
|
197
|
+
.force_unlock = &intro_force_unlock,
|
|
198
|
+
#if defined(MAC_OS_X_VERSION_10_6) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6) && !defined(__ppc__)
|
|
199
|
+
.statistics = &intro_statistics,
|
|
200
|
+
.zone_locked = &intro_zone_locked,
|
|
201
|
+
#endif
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
static malloc_zone_t mi_malloc_zone = {
|
|
205
|
+
// note: even with designators, the order is important for C++ compilation
|
|
206
|
+
//.reserved1 = NULL,
|
|
207
|
+
//.reserved2 = NULL,
|
|
208
|
+
.size = &zone_size,
|
|
209
|
+
.malloc = &zone_malloc,
|
|
210
|
+
.calloc = &zone_calloc,
|
|
211
|
+
.valloc = &zone_valloc,
|
|
212
|
+
.free = &zone_free,
|
|
213
|
+
.realloc = &zone_realloc,
|
|
214
|
+
.destroy = &zone_destroy,
|
|
215
|
+
.zone_name = "mimalloc",
|
|
216
|
+
.batch_malloc = &zone_batch_malloc,
|
|
217
|
+
.batch_free = &zone_batch_free,
|
|
218
|
+
.introspect = &mi_introspect,
|
|
219
|
+
#if defined(MAC_OS_X_VERSION_10_6) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6) && !defined(__ppc__)
|
|
220
|
+
#if defined(MAC_OS_X_VERSION_10_14) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_14)
|
|
221
|
+
.version = 10,
|
|
222
|
+
#else
|
|
223
|
+
.version = 9,
|
|
224
|
+
#endif
|
|
225
|
+
// switch to version 9+ on OSX 10.6 to support memalign.
|
|
226
|
+
.memalign = &zone_memalign,
|
|
227
|
+
.free_definite_size = &zone_free_definite_size,
|
|
228
|
+
#if defined(MAC_OS_X_VERSION_10_7) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7)
|
|
229
|
+
.pressure_relief = &zone_pressure_relief,
|
|
230
|
+
#endif
|
|
231
|
+
#if defined(MAC_OS_X_VERSION_10_14) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_14)
|
|
232
|
+
.claimed_address = &zone_claimed_address,
|
|
233
|
+
#endif
|
|
234
|
+
#else
|
|
235
|
+
.version = 4,
|
|
236
|
+
#endif
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
#ifdef __cplusplus
|
|
240
|
+
}
|
|
241
|
+
#endif
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
#if defined(MI_OSX_INTERPOSE) && defined(MI_SHARED_LIB_EXPORT)
|
|
245
|
+
|
|
246
|
+
// ------------------------------------------------------
|
|
247
|
+
// Override malloc_xxx and malloc_zone_xxx api's to use only
|
|
248
|
+
// our mimalloc zone. Since even the loader uses malloc
|
|
249
|
+
// on macOS, this ensures that all allocations go through
|
|
250
|
+
// mimalloc (as all calls are interposed).
|
|
251
|
+
// The main `malloc`, `free`, etc calls are interposed in `alloc-override.c`,
|
|
252
|
+
// Here, we also override macOS specific API's like
|
|
253
|
+
// `malloc_zone_calloc` etc. see <https://github.com/aosm/libmalloc/blob/master/man/malloc_zone_malloc.3>
|
|
254
|
+
// ------------------------------------------------------
|
|
255
|
+
|
|
256
|
+
static inline malloc_zone_t* mi_get_default_zone(void)
|
|
257
|
+
{
|
|
258
|
+
static bool init;
|
|
259
|
+
if mi_unlikely(!init) {
|
|
260
|
+
init = true;
|
|
261
|
+
malloc_zone_register(&mi_malloc_zone); // by calling register we avoid a zone error on free (see <http://eatmyrandom.blogspot.com/2010/03/mallocfree-interception-on-mac-os-x.html>)
|
|
262
|
+
}
|
|
263
|
+
return &mi_malloc_zone;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
mi_decl_externc int malloc_jumpstart(uintptr_t cookie);
|
|
267
|
+
mi_decl_externc void _malloc_fork_prepare(void);
|
|
268
|
+
mi_decl_externc void _malloc_fork_parent(void);
|
|
269
|
+
mi_decl_externc void _malloc_fork_child(void);
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
static malloc_zone_t* mi_malloc_create_zone(vm_size_t size, unsigned flags) {
|
|
273
|
+
MI_UNUSED(size); MI_UNUSED(flags);
|
|
274
|
+
return mi_get_default_zone();
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
static malloc_zone_t* mi_malloc_default_zone (void) {
|
|
278
|
+
return mi_get_default_zone();
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
static malloc_zone_t* mi_malloc_default_purgeable_zone(void) {
|
|
282
|
+
return mi_get_default_zone();
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
static void mi_malloc_destroy_zone(malloc_zone_t* zone) {
|
|
286
|
+
MI_UNUSED(zone);
|
|
287
|
+
// nothing.
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
static kern_return_t mi_malloc_get_all_zones (task_t task, memory_reader_t mr, vm_address_t** addresses, unsigned* count) {
|
|
291
|
+
MI_UNUSED(task); MI_UNUSED(mr);
|
|
292
|
+
if (addresses != NULL) *addresses = NULL;
|
|
293
|
+
if (count != NULL) *count = 0;
|
|
294
|
+
return KERN_SUCCESS;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
static const char* mi_malloc_get_zone_name(malloc_zone_t* zone) {
|
|
298
|
+
return (zone == NULL ? mi_malloc_zone.zone_name : zone->zone_name);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
static void mi_malloc_set_zone_name(malloc_zone_t* zone, const char* name) {
|
|
302
|
+
MI_UNUSED(zone); MI_UNUSED(name);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
static int mi_malloc_jumpstart(uintptr_t cookie) {
|
|
306
|
+
MI_UNUSED(cookie);
|
|
307
|
+
return 1; // or 0 for no error?
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
static void mi__malloc_fork_prepare(void) {
|
|
311
|
+
// nothing
|
|
312
|
+
}
|
|
313
|
+
static void mi__malloc_fork_parent(void) {
|
|
314
|
+
// nothing
|
|
315
|
+
}
|
|
316
|
+
static void mi__malloc_fork_child(void) {
|
|
317
|
+
// nothing
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
static void mi_malloc_printf(const char* fmt, ...) {
|
|
321
|
+
MI_UNUSED(fmt);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
static bool zone_check(malloc_zone_t* zone) {
|
|
325
|
+
MI_UNUSED(zone);
|
|
326
|
+
return true;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
static malloc_zone_t* zone_from_ptr(const void* p) {
|
|
330
|
+
MI_UNUSED(p);
|
|
331
|
+
return mi_get_default_zone();
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
static void zone_log(malloc_zone_t* zone, void* p) {
|
|
335
|
+
MI_UNUSED(zone); MI_UNUSED(p);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
static void zone_print(malloc_zone_t* zone, bool b) {
|
|
339
|
+
MI_UNUSED(zone); MI_UNUSED(b);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
static void zone_print_ptr_info(void* p) {
|
|
343
|
+
MI_UNUSED(p);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
static void zone_register(malloc_zone_t* zone) {
|
|
347
|
+
MI_UNUSED(zone);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
static void zone_unregister(malloc_zone_t* zone) {
|
|
351
|
+
MI_UNUSED(zone);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// use interposing so `DYLD_INSERT_LIBRARIES` works without `DYLD_FORCE_FLAT_NAMESPACE=1`
|
|
355
|
+
// See: <https://books.google.com/books?id=K8vUkpOXhN4C&pg=PA73>
|
|
356
|
+
struct mi_interpose_s {
|
|
357
|
+
const void* replacement;
|
|
358
|
+
const void* target;
|
|
359
|
+
};
|
|
360
|
+
#define MI_INTERPOSE_FUN(oldfun,newfun) { (const void*)&newfun, (const void*)&oldfun }
|
|
361
|
+
#define MI_INTERPOSE_MI(fun) MI_INTERPOSE_FUN(fun,mi_##fun)
|
|
362
|
+
#define MI_INTERPOSE_ZONE(fun) MI_INTERPOSE_FUN(malloc_##fun,fun)
|
|
363
|
+
__attribute__((used)) static const struct mi_interpose_s _mi_zone_interposes[] __attribute__((section("__DATA, __interpose"))) =
|
|
364
|
+
{
|
|
365
|
+
|
|
366
|
+
MI_INTERPOSE_MI(malloc_create_zone),
|
|
367
|
+
MI_INTERPOSE_MI(malloc_default_purgeable_zone),
|
|
368
|
+
MI_INTERPOSE_MI(malloc_default_zone),
|
|
369
|
+
MI_INTERPOSE_MI(malloc_destroy_zone),
|
|
370
|
+
MI_INTERPOSE_MI(malloc_get_all_zones),
|
|
371
|
+
MI_INTERPOSE_MI(malloc_get_zone_name),
|
|
372
|
+
MI_INTERPOSE_MI(malloc_jumpstart),
|
|
373
|
+
MI_INTERPOSE_MI(malloc_printf),
|
|
374
|
+
MI_INTERPOSE_MI(malloc_set_zone_name),
|
|
375
|
+
MI_INTERPOSE_MI(_malloc_fork_child),
|
|
376
|
+
MI_INTERPOSE_MI(_malloc_fork_parent),
|
|
377
|
+
MI_INTERPOSE_MI(_malloc_fork_prepare),
|
|
378
|
+
|
|
379
|
+
MI_INTERPOSE_ZONE(zone_batch_free),
|
|
380
|
+
MI_INTERPOSE_ZONE(zone_batch_malloc),
|
|
381
|
+
MI_INTERPOSE_ZONE(zone_calloc),
|
|
382
|
+
MI_INTERPOSE_ZONE(zone_check),
|
|
383
|
+
MI_INTERPOSE_ZONE(zone_free),
|
|
384
|
+
MI_INTERPOSE_ZONE(zone_from_ptr),
|
|
385
|
+
MI_INTERPOSE_ZONE(zone_log),
|
|
386
|
+
MI_INTERPOSE_ZONE(zone_malloc),
|
|
387
|
+
MI_INTERPOSE_ZONE(zone_memalign),
|
|
388
|
+
MI_INTERPOSE_ZONE(zone_print),
|
|
389
|
+
MI_INTERPOSE_ZONE(zone_print_ptr_info),
|
|
390
|
+
MI_INTERPOSE_ZONE(zone_realloc),
|
|
391
|
+
MI_INTERPOSE_ZONE(zone_register),
|
|
392
|
+
MI_INTERPOSE_ZONE(zone_unregister),
|
|
393
|
+
MI_INTERPOSE_ZONE(zone_valloc)
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
#else
|
|
398
|
+
|
|
399
|
+
// ------------------------------------------------------
|
|
400
|
+
// hook into the zone api's without interposing
|
|
401
|
+
// This is the official way of adding an allocator but
|
|
402
|
+
// it seems less robust than using interpose.
|
|
403
|
+
// ------------------------------------------------------
|
|
404
|
+
|
|
405
|
+
static inline malloc_zone_t* mi_get_default_zone(void)
|
|
406
|
+
{
|
|
407
|
+
// The first returned zone is the real default
|
|
408
|
+
malloc_zone_t** zones = NULL;
|
|
409
|
+
unsigned count = 0;
|
|
410
|
+
kern_return_t ret = malloc_get_all_zones(0, NULL, (vm_address_t**)&zones, &count);
|
|
411
|
+
if (ret == KERN_SUCCESS && count > 0) {
|
|
412
|
+
return zones[0];
|
|
413
|
+
}
|
|
414
|
+
else {
|
|
415
|
+
// fallback
|
|
416
|
+
return malloc_default_zone();
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
#if defined(__clang__)
|
|
421
|
+
__attribute__((constructor(101))) // highest priority
|
|
422
|
+
#else
|
|
423
|
+
__attribute__((constructor)) // priority level is not supported by gcc
|
|
424
|
+
#endif
|
|
425
|
+
__attribute__((used))
|
|
426
|
+
static void _mi_macos_override_malloc(void) {
|
|
427
|
+
malloc_zone_t* purgeable_zone = NULL;
|
|
428
|
+
|
|
429
|
+
#if defined(MAC_OS_X_VERSION_10_6) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
|
|
430
|
+
// force the purgeable zone to exist to avoid strange bugs
|
|
431
|
+
if (malloc_default_purgeable_zone) {
|
|
432
|
+
purgeable_zone = malloc_default_purgeable_zone();
|
|
433
|
+
}
|
|
434
|
+
#endif
|
|
435
|
+
|
|
436
|
+
// Register our zone.
|
|
437
|
+
// thomcc: I think this is still needed to put us in the zone list.
|
|
438
|
+
malloc_zone_register(&mi_malloc_zone);
|
|
439
|
+
// Unregister the default zone, this makes our zone the new default
|
|
440
|
+
// as that was the last registered.
|
|
441
|
+
malloc_zone_t *default_zone = mi_get_default_zone();
|
|
442
|
+
// thomcc: Unsure if the next test is *always* false or just false in the
|
|
443
|
+
// cases I've tried. I'm also unsure if the code inside is needed. at all
|
|
444
|
+
if (default_zone != &mi_malloc_zone) {
|
|
445
|
+
malloc_zone_unregister(default_zone);
|
|
446
|
+
|
|
447
|
+
// Reregister the default zone so free and realloc in that zone keep working.
|
|
448
|
+
malloc_zone_register(default_zone);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// Unregister, and re-register the purgeable_zone to avoid bugs if it occurs
|
|
452
|
+
// earlier than the default zone.
|
|
453
|
+
if (purgeable_zone != NULL) {
|
|
454
|
+
malloc_zone_unregister(purgeable_zone);
|
|
455
|
+
malloc_zone_register(purgeable_zone);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
}
|
|
459
|
+
#endif // MI_OSX_INTERPOSE
|
|
460
|
+
|
|
461
|
+
#endif // MI_MALLOC_OVERRIDE
|