@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.
Files changed (339) hide show
  1. package/LICENSE.md +17 -0
  2. package/README.md +80 -0
  3. package/out/cjs/index.cjs +51 -0
  4. package/out/cjs/yo-cli.cjs +2158 -0
  5. package/out/esm/index.mjs +51 -0
  6. package/out/types/src/codegen/async/runtime.d.ts +2 -0
  7. package/out/types/src/codegen/async/state-code-gen.d.ts +10 -0
  8. package/out/types/src/codegen/async/state-machine.d.ts +13 -0
  9. package/out/types/src/codegen/c/collection.d.ts +3 -0
  10. package/out/types/src/codegen/codegen-c.d.ts +12 -0
  11. package/out/types/src/codegen/constants.d.ts +3 -0
  12. package/out/types/src/codegen/expressions/array.d.ts +4 -0
  13. package/out/types/src/codegen/expressions/generation.d.ts +11 -0
  14. package/out/types/src/codegen/expressions/index.d.ts +2 -0
  15. package/out/types/src/codegen/functions/collection.d.ts +5 -0
  16. package/out/types/src/codegen/functions/context.d.ts +57 -0
  17. package/out/types/src/codegen/functions/generation.d.ts +25 -0
  18. package/out/types/src/codegen/functions/index.d.ts +2 -0
  19. package/out/types/src/codegen/index.d.ts +20 -0
  20. package/out/types/src/codegen/parallelism/runtime.d.ts +2 -0
  21. package/out/types/src/codegen/types/collection.d.ts +8 -0
  22. package/out/types/src/codegen/types/generation.d.ts +13 -0
  23. package/out/types/src/codegen/types/index.d.ts +2 -0
  24. package/out/types/src/codegen/utils/fixup.d.ts +2 -0
  25. package/out/types/src/codegen/utils/index.d.ts +77 -0
  26. package/out/types/src/codegen/values/index.d.ts +1 -0
  27. package/out/types/src/emitter.d.ts +11 -0
  28. package/out/types/src/env.d.ts +85 -0
  29. package/out/types/src/error.d.ts +45 -0
  30. package/out/types/src/evaluator/async/await-analysis-types.d.ts +23 -0
  31. package/out/types/src/evaluator/async/await-analysis.d.ts +5 -0
  32. package/out/types/src/evaluator/builtins/alignof.d.ts +8 -0
  33. package/out/types/src/evaluator/builtins/and_or.d.ts +8 -0
  34. package/out/types/src/evaluator/builtins/arc_fns.d.ts +58 -0
  35. package/out/types/src/evaluator/builtins/array_fns.d.ts +0 -0
  36. package/out/types/src/evaluator/builtins/as.d.ts +8 -0
  37. package/out/types/src/evaluator/builtins/async_fns.d.ts +8 -0
  38. package/out/types/src/evaluator/builtins/compt_assert.d.ts +8 -0
  39. package/out/types/src/evaluator/builtins/compt_boolean_fns.d.ts +8 -0
  40. package/out/types/src/evaluator/builtins/compt_expect_error.d.ts +8 -0
  41. package/out/types/src/evaluator/builtins/compt_list_fns.d.ts +33 -0
  42. package/out/types/src/evaluator/builtins/compt_print.d.ts +8 -0
  43. package/out/types/src/evaluator/builtins/compt_string_fns.d.ts +8 -0
  44. package/out/types/src/evaluator/builtins/consume.d.ts +8 -0
  45. package/out/types/src/evaluator/builtins/drop.d.ts +8 -0
  46. package/out/types/src/evaluator/builtins/dup.d.ts +8 -0
  47. package/out/types/src/evaluator/builtins/expr_fns.d.ts +33 -0
  48. package/out/types/src/evaluator/builtins/future_fns.d.ts +8 -0
  49. package/out/types/src/evaluator/builtins/gc.d.ts +8 -0
  50. package/out/types/src/evaluator/builtins/gensym.d.ts +8 -0
  51. package/out/types/src/evaluator/builtins/impl_constraint.d.ts +8 -0
  52. package/out/types/src/evaluator/builtins/macro_expand.d.ts +8 -0
  53. package/out/types/src/evaluator/builtins/numeric_fns.d.ts +8 -0
  54. package/out/types/src/evaluator/builtins/panic.d.ts +8 -0
  55. package/out/types/src/evaluator/builtins/ptr_fns.d.ts +8 -0
  56. package/out/types/src/evaluator/builtins/quote.d.ts +13 -0
  57. package/out/types/src/evaluator/builtins/rc.d.ts +8 -0
  58. package/out/types/src/evaluator/builtins/sizeof.d.ts +8 -0
  59. package/out/types/src/evaluator/builtins/the.d.ts +8 -0
  60. package/out/types/src/evaluator/builtins/type_fns.d.ts +28 -0
  61. package/out/types/src/evaluator/builtins/va_start.d.ts +8 -0
  62. package/out/types/src/evaluator/builtins/var_fns.d.ts +18 -0
  63. package/out/types/src/evaluator/calls/array.d.ts +13 -0
  64. package/out/types/src/evaluator/calls/array_type.d.ts +11 -0
  65. package/out/types/src/evaluator/calls/closure_type.d.ts +11 -0
  66. package/out/types/src/evaluator/calls/compt_function.d.ts +19 -0
  67. package/out/types/src/evaluator/calls/compt_list_type.d.ts +11 -0
  68. package/out/types/src/evaluator/calls/function.d.ts +16 -0
  69. package/out/types/src/evaluator/calls/function_type.d.ts +15 -0
  70. package/out/types/src/evaluator/calls/helper.d.ts +42 -0
  71. package/out/types/src/evaluator/calls/iso.d.ts +15 -0
  72. package/out/types/src/evaluator/calls/module_type.d.ts +11 -0
  73. package/out/types/src/evaluator/calls/numeric_type.d.ts +15 -0
  74. package/out/types/src/evaluator/calls/pointer.d.ts +8 -0
  75. package/out/types/src/evaluator/calls/pointer_type.d.ts +14 -0
  76. package/out/types/src/evaluator/calls/type.d.ts +12 -0
  77. package/out/types/src/evaluator/context.d.ts +169 -0
  78. package/out/types/src/evaluator/exprs/_expr.d.ts +8 -0
  79. package/out/types/src/evaluator/exprs/assignment.d.ts +9 -0
  80. package/out/types/src/evaluator/exprs/begin.d.ts +10 -0
  81. package/out/types/src/evaluator/exprs/binding.d.ts +12 -0
  82. package/out/types/src/evaluator/exprs/c_include.d.ts +8 -0
  83. package/out/types/src/evaluator/exprs/cond.d.ts +8 -0
  84. package/out/types/src/evaluator/exprs/destructuring_assignment.d.ts +33 -0
  85. package/out/types/src/evaluator/exprs/exists.d.ts +0 -0
  86. package/out/types/src/evaluator/exprs/expr.d.ts +9 -0
  87. package/out/types/src/evaluator/exprs/extern.d.ts +8 -0
  88. package/out/types/src/evaluator/exprs/identifer_and_operator.d.ts +9 -0
  89. package/out/types/src/evaluator/exprs/import.d.ts +9 -0
  90. package/out/types/src/evaluator/exprs/initialization_assignment.d.ts +8 -0
  91. package/out/types/src/evaluator/exprs/match.d.ts +8 -0
  92. package/out/types/src/evaluator/exprs/open.d.ts +8 -0
  93. package/out/types/src/evaluator/exprs/property_access.d.ts +8 -0
  94. package/out/types/src/evaluator/exprs/recur.d.ts +8 -0
  95. package/out/types/src/evaluator/exprs/subtype_of.d.ts +21 -0
  96. package/out/types/src/evaluator/exprs/test.d.ts +8 -0
  97. package/out/types/src/evaluator/exprs/typeof.d.ts +8 -0
  98. package/out/types/src/evaluator/exprs/while.d.ts +8 -0
  99. package/out/types/src/evaluator/index.d.ts +26 -0
  100. package/out/types/src/evaluator/types/array.d.ts +8 -0
  101. package/out/types/src/evaluator/types/closure.d.ts +8 -0
  102. package/out/types/src/evaluator/types/compt_list.d.ts +8 -0
  103. package/out/types/src/evaluator/types/concrete_module.d.ts +8 -0
  104. package/out/types/src/evaluator/types/dyn.d.ts +8 -0
  105. package/out/types/src/evaluator/types/enum.d.ts +8 -0
  106. package/out/types/src/evaluator/types/expr_synthesizer.d.ts +14 -0
  107. package/out/types/src/evaluator/types/field.d.ts +14 -0
  108. package/out/types/src/evaluator/types/fn_module.d.ts +8 -0
  109. package/out/types/src/evaluator/types/function.d.ts +58 -0
  110. package/out/types/src/evaluator/types/future_module.d.ts +8 -0
  111. package/out/types/src/evaluator/types/module.d.ts +19 -0
  112. package/out/types/src/evaluator/types/newtype.d.ts +8 -0
  113. package/out/types/src/evaluator/types/object.d.ts +8 -0
  114. package/out/types/src/evaluator/types/proofs.d.ts +0 -0
  115. package/out/types/src/evaluator/types/slice.d.ts +8 -0
  116. package/out/types/src/evaluator/types/struct.d.ts +8 -0
  117. package/out/types/src/evaluator/types/synthesizer.d.ts +16 -0
  118. package/out/types/src/evaluator/types/tuple.d.ts +18 -0
  119. package/out/types/src/evaluator/types/union.d.ts +8 -0
  120. package/out/types/src/evaluator/types/utils.d.ts +71 -0
  121. package/out/types/src/evaluator/types/validation.d.ts +3 -0
  122. package/out/types/src/evaluator/utils/array-utils.d.ts +15 -0
  123. package/out/types/src/evaluator/utils/closure.d.ts +35 -0
  124. package/out/types/src/evaluator/utils.d.ts +4 -0
  125. package/out/types/src/evaluator/values/anonymous_function.d.ts +8 -0
  126. package/out/types/src/evaluator/values/anonymous_module.d.ts +17 -0
  127. package/out/types/src/evaluator/values/anonymous_struct.d.ts +8 -0
  128. package/out/types/src/evaluator/values/array.d.ts +8 -0
  129. package/out/types/src/evaluator/values/boolean.d.ts +3 -0
  130. package/out/types/src/evaluator/values/char.d.ts +3 -0
  131. package/out/types/src/evaluator/values/compt_list.d.ts +8 -0
  132. package/out/types/src/evaluator/values/dyn.d.ts +8 -0
  133. package/out/types/src/evaluator/values/float.d.ts +4 -0
  134. package/out/types/src/evaluator/values/integer.d.ts +4 -0
  135. package/out/types/src/evaluator/values/module.d.ts +58 -0
  136. package/out/types/src/evaluator/values/string.d.ts +3 -0
  137. package/out/types/src/evaluator/values/tuple.d.ts +32 -0
  138. package/out/types/src/expr.d.ts +456 -0
  139. package/out/types/src/function-value.d.ts +42 -0
  140. package/out/types/src/index.d.ts +4 -0
  141. package/out/types/src/lexer.d.ts +2 -0
  142. package/out/types/src/logger.d.ts +1 -0
  143. package/out/types/src/module-manager.d.ts +30 -0
  144. package/out/types/src/naming-checker.d.ts +4 -0
  145. package/out/types/src/parser.d.ts +33 -0
  146. package/out/types/src/test-runner.d.ts +30 -0
  147. package/out/types/src/tests/codegen.test.d.ts +1 -0
  148. package/out/types/src/tests/fixme.test.d.ts +1 -0
  149. package/out/types/src/tests/module-manager.test.d.ts +1 -0
  150. package/out/types/src/tests/parser.test.d.ts +1 -0
  151. package/out/types/src/tests/sample.test.d.ts +0 -0
  152. package/out/types/src/tests/std.test.d.ts +1 -0
  153. package/out/types/src/token.d.ts +40 -0
  154. package/out/types/src/type-value.d.ts +7 -0
  155. package/out/types/src/types/compatibility.d.ts +16 -0
  156. package/out/types/src/types/creators.d.ts +73 -0
  157. package/out/types/src/types/definitions.d.ts +218 -0
  158. package/out/types/src/types/guards.d.ts +70 -0
  159. package/out/types/src/types/hierarchy.d.ts +4 -0
  160. package/out/types/src/types/index.d.ts +7 -0
  161. package/out/types/src/types/module_field.d.ts +2 -0
  162. package/out/types/src/types/tags.d.ts +45 -0
  163. package/out/types/src/types/utils.d.ts +50 -0
  164. package/out/types/src/unit-value.d.ts +7 -0
  165. package/out/types/src/utils.d.ts +6 -0
  166. package/out/types/src/value-tag.d.ts +29 -0
  167. package/out/types/src/value.d.ts +110 -0
  168. package/out/types/src/yo-cli.d.ts +1 -0
  169. package/out/types/tsconfig.tsbuildinfo +1 -0
  170. package/package.json +57 -0
  171. package/scripts/check-liburing.js +76 -0
  172. package/std/alg/hash.yo +50 -0
  173. package/std/allocator.yo +113 -0
  174. package/std/allocators/c_allocator.yo +118 -0
  175. package/std/async.yo +13 -0
  176. package/std/collections/array_list.yo +415 -0
  177. package/std/collections/hash_map.yo +482 -0
  178. package/std/collections/hash_set.yo +706 -0
  179. package/std/collections/index.yo +11 -0
  180. package/std/collections/linked_list.yo +439 -0
  181. package/std/error.yo +0 -0
  182. package/std/gc.yo +10 -0
  183. package/std/index.yo +12 -0
  184. package/std/io/file.yo +191 -0
  185. package/std/io/index.yo +5 -0
  186. package/std/libc/assert.yo +39 -0
  187. package/std/libc/ctype.yo +57 -0
  188. package/std/libc/errno.yo +182 -0
  189. package/std/libc/float.yo +87 -0
  190. package/std/libc/index.yo +29 -0
  191. package/std/libc/limits.yo +65 -0
  192. package/std/libc/math.yo +679 -0
  193. package/std/libc/signal.yo +101 -0
  194. package/std/libc/stdatomic.yo +213 -0
  195. package/std/libc/stdint.yo +214 -0
  196. package/std/libc/stdio.yo +225 -0
  197. package/std/libc/stdlib.yo +204 -0
  198. package/std/libc/string.yo +151 -0
  199. package/std/libc/time.yo +92 -0
  200. package/std/libc/unistd.yo +130 -0
  201. package/std/monad.yo +152 -0
  202. package/std/prelude.yo +3094 -0
  203. package/std/string/index.yo +8 -0
  204. package/std/string/rune.yo +82 -0
  205. package/std/string/string.yo +288 -0
  206. package/std/sync.yo +95 -0
  207. package/std/thread.yo +36 -0
  208. package/std/time.yo +13 -0
  209. package/std/worker.yo +36 -0
  210. package/vendor/mimalloc/.gitattributes +12 -0
  211. package/vendor/mimalloc/CMakeLists.txt +763 -0
  212. package/vendor/mimalloc/LICENSE +21 -0
  213. package/vendor/mimalloc/SECURITY.md +41 -0
  214. package/vendor/mimalloc/azure-pipelines.yml +251 -0
  215. package/vendor/mimalloc/bin/mimalloc-redirect-arm64.dll +0 -0
  216. package/vendor/mimalloc/bin/mimalloc-redirect-arm64.lib +0 -0
  217. package/vendor/mimalloc/bin/mimalloc-redirect-arm64ec.dll +0 -0
  218. package/vendor/mimalloc/bin/mimalloc-redirect-arm64ec.lib +0 -0
  219. package/vendor/mimalloc/bin/mimalloc-redirect.dll +0 -0
  220. package/vendor/mimalloc/bin/mimalloc-redirect.lib +0 -0
  221. package/vendor/mimalloc/bin/mimalloc-redirect32.dll +0 -0
  222. package/vendor/mimalloc/bin/mimalloc-redirect32.lib +0 -0
  223. package/vendor/mimalloc/bin/minject-arm64.exe +0 -0
  224. package/vendor/mimalloc/bin/minject.exe +0 -0
  225. package/vendor/mimalloc/bin/minject32.exe +0 -0
  226. package/vendor/mimalloc/bin/readme.md +118 -0
  227. package/vendor/mimalloc/cmake/JoinPaths.cmake +23 -0
  228. package/vendor/mimalloc/cmake/mimalloc-config-version.cmake +19 -0
  229. package/vendor/mimalloc/cmake/mimalloc-config.cmake +14 -0
  230. package/vendor/mimalloc/contrib/docker/alpine/Dockerfile +23 -0
  231. package/vendor/mimalloc/contrib/docker/alpine-arm32v7/Dockerfile +28 -0
  232. package/vendor/mimalloc/contrib/docker/alpine-x86/Dockerfile +28 -0
  233. package/vendor/mimalloc/contrib/docker/manylinux-x64/Dockerfile +23 -0
  234. package/vendor/mimalloc/contrib/docker/readme.md +10 -0
  235. package/vendor/mimalloc/contrib/vcpkg/portfile.cmake +64 -0
  236. package/vendor/mimalloc/contrib/vcpkg/readme.md +40 -0
  237. package/vendor/mimalloc/contrib/vcpkg/usage +20 -0
  238. package/vendor/mimalloc/contrib/vcpkg/vcpkg-cmake-wrapper.cmake +20 -0
  239. package/vendor/mimalloc/contrib/vcpkg/vcpkg.json +48 -0
  240. package/vendor/mimalloc/doc/bench-2020/bench-c5-18xlarge-2020-01-20-a.svg +887 -0
  241. package/vendor/mimalloc/doc/bench-2020/bench-c5-18xlarge-2020-01-20-b.svg +1185 -0
  242. package/vendor/mimalloc/doc/bench-2020/bench-c5-18xlarge-2020-01-20-rss-a.svg +757 -0
  243. package/vendor/mimalloc/doc/bench-2020/bench-c5-18xlarge-2020-01-20-rss-b.svg +1028 -0
  244. package/vendor/mimalloc/doc/bench-2020/bench-r5a-1.svg +769 -0
  245. package/vendor/mimalloc/doc/bench-2020/bench-r5a-12xlarge-2020-01-16-a.svg +868 -0
  246. package/vendor/mimalloc/doc/bench-2020/bench-r5a-12xlarge-2020-01-16-b.svg +1157 -0
  247. package/vendor/mimalloc/doc/bench-2020/bench-r5a-2.svg +983 -0
  248. package/vendor/mimalloc/doc/bench-2020/bench-r5a-rss-1.svg +683 -0
  249. package/vendor/mimalloc/doc/bench-2020/bench-r5a-rss-2.svg +854 -0
  250. package/vendor/mimalloc/doc/bench-2020/bench-spec-rss.svg +713 -0
  251. package/vendor/mimalloc/doc/bench-2020/bench-spec.svg +713 -0
  252. package/vendor/mimalloc/doc/bench-2020/bench-z4-1.svg +890 -0
  253. package/vendor/mimalloc/doc/bench-2020/bench-z4-2.svg +1146 -0
  254. package/vendor/mimalloc/doc/bench-2020/bench-z4-rss-1.svg +796 -0
  255. package/vendor/mimalloc/doc/bench-2020/bench-z4-rss-2.svg +974 -0
  256. package/vendor/mimalloc/doc/bench-2021/bench-amd5950x-2021-01-30-a.svg +952 -0
  257. package/vendor/mimalloc/doc/bench-2021/bench-amd5950x-2021-01-30-b.svg +1255 -0
  258. package/vendor/mimalloc/doc/bench-2021/bench-c5-18xlarge-2021-01-30-a.svg +955 -0
  259. package/vendor/mimalloc/doc/bench-2021/bench-c5-18xlarge-2021-01-30-b.svg +1269 -0
  260. package/vendor/mimalloc/doc/bench-2021/bench-c5-18xlarge-2021-01-30-rss-a.svg +836 -0
  261. package/vendor/mimalloc/doc/bench-2021/bench-c5-18xlarge-2021-01-30-rss-b.svg +1131 -0
  262. package/vendor/mimalloc/doc/bench-2021/bench-macmini-2021-01-30.svg +766 -0
  263. package/vendor/mimalloc/doc/doxyfile +2895 -0
  264. package/vendor/mimalloc/doc/ds-logo.jpg +0 -0
  265. package/vendor/mimalloc/doc/ds-logo.png +0 -0
  266. package/vendor/mimalloc/doc/mimalloc-doc.h +1452 -0
  267. package/vendor/mimalloc/doc/mimalloc-doxygen.css +60 -0
  268. package/vendor/mimalloc/doc/mimalloc-logo-100.png +0 -0
  269. package/vendor/mimalloc/doc/mimalloc-logo.png +0 -0
  270. package/vendor/mimalloc/doc/mimalloc-logo.svg +161 -0
  271. package/vendor/mimalloc/doc/spades-logo.png +0 -0
  272. package/vendor/mimalloc/doc/unreal-logo.svg +43 -0
  273. package/vendor/mimalloc/ide/vs2022/mimalloc-lib.vcxproj +500 -0
  274. package/vendor/mimalloc/ide/vs2022/mimalloc-lib.vcxproj.filters +108 -0
  275. package/vendor/mimalloc/ide/vs2022/mimalloc-override-dll.vcxproj +508 -0
  276. package/vendor/mimalloc/ide/vs2022/mimalloc-override-dll.vcxproj.filters +111 -0
  277. package/vendor/mimalloc/ide/vs2022/mimalloc-override-test-dep.vcxproj +355 -0
  278. package/vendor/mimalloc/ide/vs2022/mimalloc-override-test.vcxproj +360 -0
  279. package/vendor/mimalloc/ide/vs2022/mimalloc-test-api.vcxproj +295 -0
  280. package/vendor/mimalloc/ide/vs2022/mimalloc-test-stress.vcxproj +292 -0
  281. package/vendor/mimalloc/ide/vs2022/mimalloc-test.vcxproj +289 -0
  282. package/vendor/mimalloc/ide/vs2022/mimalloc.sln +151 -0
  283. package/vendor/mimalloc/include/mimalloc/atomic.h +557 -0
  284. package/vendor/mimalloc/include/mimalloc/internal.h +1153 -0
  285. package/vendor/mimalloc/include/mimalloc/prim.h +421 -0
  286. package/vendor/mimalloc/include/mimalloc/track.h +145 -0
  287. package/vendor/mimalloc/include/mimalloc/types.h +685 -0
  288. package/vendor/mimalloc/include/mimalloc-new-delete.h +66 -0
  289. package/vendor/mimalloc/include/mimalloc-override.h +68 -0
  290. package/vendor/mimalloc/include/mimalloc-stats.h +103 -0
  291. package/vendor/mimalloc/include/mimalloc.h +612 -0
  292. package/vendor/mimalloc/mimalloc.pc.in +11 -0
  293. package/vendor/mimalloc/readme.md +946 -0
  294. package/vendor/mimalloc/src/alloc-aligned.c +360 -0
  295. package/vendor/mimalloc/src/alloc-override.c +316 -0
  296. package/vendor/mimalloc/src/alloc-posix.c +185 -0
  297. package/vendor/mimalloc/src/alloc.c +692 -0
  298. package/vendor/mimalloc/src/arena-abandon.c +346 -0
  299. package/vendor/mimalloc/src/arena.c +1043 -0
  300. package/vendor/mimalloc/src/bitmap.c +441 -0
  301. package/vendor/mimalloc/src/bitmap.h +119 -0
  302. package/vendor/mimalloc/src/free.c +572 -0
  303. package/vendor/mimalloc/src/heap.c +733 -0
  304. package/vendor/mimalloc/src/init.c +714 -0
  305. package/vendor/mimalloc/src/libc.c +334 -0
  306. package/vendor/mimalloc/src/options.c +663 -0
  307. package/vendor/mimalloc/src/os.c +770 -0
  308. package/vendor/mimalloc/src/page-queue.c +390 -0
  309. package/vendor/mimalloc/src/page.c +1049 -0
  310. package/vendor/mimalloc/src/prim/emscripten/prim.c +249 -0
  311. package/vendor/mimalloc/src/prim/osx/alloc-override-zone.c +461 -0
  312. package/vendor/mimalloc/src/prim/osx/prim.c +9 -0
  313. package/vendor/mimalloc/src/prim/prim.c +76 -0
  314. package/vendor/mimalloc/src/prim/readme.md +9 -0
  315. package/vendor/mimalloc/src/prim/unix/prim.c +934 -0
  316. package/vendor/mimalloc/src/prim/wasi/prim.c +284 -0
  317. package/vendor/mimalloc/src/prim/windows/etw-mimalloc.wprp +61 -0
  318. package/vendor/mimalloc/src/prim/windows/etw.h +905 -0
  319. package/vendor/mimalloc/src/prim/windows/etw.man +0 -0
  320. package/vendor/mimalloc/src/prim/windows/prim.c +878 -0
  321. package/vendor/mimalloc/src/prim/windows/readme.md +17 -0
  322. package/vendor/mimalloc/src/random.c +258 -0
  323. package/vendor/mimalloc/src/segment-map.c +142 -0
  324. package/vendor/mimalloc/src/segment.c +1702 -0
  325. package/vendor/mimalloc/src/static.c +41 -0
  326. package/vendor/mimalloc/src/stats.c +635 -0
  327. package/vendor/mimalloc/test/CMakeLists.txt +56 -0
  328. package/vendor/mimalloc/test/main-override-dep.cpp +51 -0
  329. package/vendor/mimalloc/test/main-override-dep.h +11 -0
  330. package/vendor/mimalloc/test/main-override-static.c +539 -0
  331. package/vendor/mimalloc/test/main-override.c +36 -0
  332. package/vendor/mimalloc/test/main-override.cpp +497 -0
  333. package/vendor/mimalloc/test/main.c +46 -0
  334. package/vendor/mimalloc/test/readme.md +16 -0
  335. package/vendor/mimalloc/test/test-api-fill.c +343 -0
  336. package/vendor/mimalloc/test/test-api.c +466 -0
  337. package/vendor/mimalloc/test/test-stress.c +428 -0
  338. package/vendor/mimalloc/test/test-wrong.c +92 -0
  339. package/vendor/mimalloc/test/testhelper.h +49 -0
@@ -0,0 +1,557 @@
1
+ /* ----------------------------------------------------------------------------
2
+ Copyright (c) 2018-2024 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_ATOMIC_H
9
+ #define MIMALLOC_ATOMIC_H
10
+
11
+ // include windows.h or pthreads.h
12
+ #if defined(_WIN32)
13
+ #ifndef WIN32_LEAN_AND_MEAN
14
+ #define WIN32_LEAN_AND_MEAN
15
+ #endif
16
+ #include <windows.h>
17
+ #elif !defined(__wasi__) && (!defined(__EMSCRIPTEN__) || defined(__EMSCRIPTEN_PTHREADS__))
18
+ #define MI_USE_PTHREADS
19
+ #include <pthread.h>
20
+ #endif
21
+
22
+ // --------------------------------------------------------------------------------------------
23
+ // Atomics
24
+ // We need to be portable between C, C++, and MSVC.
25
+ // We base the primitives on the C/C++ atomics and create a minimal wrapper for MSVC in C compilation mode.
26
+ // This is why we try to use only `uintptr_t` and `<type>*` as atomic types.
27
+ // To gain better insight in the range of used atomics, we use explicitly named memory order operations
28
+ // instead of passing the memory order as a parameter.
29
+ // -----------------------------------------------------------------------------------------------
30
+
31
+ #if defined(__cplusplus)
32
+ // Use C++ atomics
33
+ #include <atomic>
34
+ #define _Atomic(tp) std::atomic<tp>
35
+ #define mi_atomic(name) std::atomic_##name
36
+ #define mi_memory_order(name) std::memory_order_##name
37
+ #if (__cplusplus >= 202002L) // c++20, see issue #571
38
+ #define MI_ATOMIC_VAR_INIT(x) x
39
+ #elif !defined(ATOMIC_VAR_INIT)
40
+ #define MI_ATOMIC_VAR_INIT(x) x
41
+ #else
42
+ #define MI_ATOMIC_VAR_INIT(x) ATOMIC_VAR_INIT(x)
43
+ #endif
44
+ #elif defined(_MSC_VER)
45
+ // Use MSVC C wrapper for C11 atomics
46
+ #define _Atomic(tp) tp
47
+ #define MI_ATOMIC_VAR_INIT(x) x
48
+ #define mi_atomic(name) mi_atomic_##name
49
+ #define mi_memory_order(name) mi_memory_order_##name
50
+ #else
51
+ // Use C11 atomics
52
+ #include <stdatomic.h>
53
+ #define mi_atomic(name) atomic_##name
54
+ #define mi_memory_order(name) memory_order_##name
55
+ #if (__STDC_VERSION__ >= 201710L) // c17, see issue #735
56
+ #define MI_ATOMIC_VAR_INIT(x) x
57
+ #elif !defined(ATOMIC_VAR_INIT)
58
+ #define MI_ATOMIC_VAR_INIT(x) x
59
+ #else
60
+ #define MI_ATOMIC_VAR_INIT(x) ATOMIC_VAR_INIT(x)
61
+ #endif
62
+ #endif
63
+
64
+ // Various defines for all used memory orders in mimalloc
65
+ #define mi_atomic_cas_weak(p,expected,desired,mem_success,mem_fail) \
66
+ mi_atomic(compare_exchange_weak_explicit)(p,expected,desired,mem_success,mem_fail)
67
+
68
+ #define mi_atomic_cas_strong(p,expected,desired,mem_success,mem_fail) \
69
+ mi_atomic(compare_exchange_strong_explicit)(p,expected,desired,mem_success,mem_fail)
70
+
71
+ #define mi_atomic_load_acquire(p) mi_atomic(load_explicit)(p,mi_memory_order(acquire))
72
+ #define mi_atomic_load_relaxed(p) mi_atomic(load_explicit)(p,mi_memory_order(relaxed))
73
+ #define mi_atomic_store_release(p,x) mi_atomic(store_explicit)(p,x,mi_memory_order(release))
74
+ #define mi_atomic_store_relaxed(p,x) mi_atomic(store_explicit)(p,x,mi_memory_order(relaxed))
75
+ #define mi_atomic_exchange_relaxed(p,x) mi_atomic(exchange_explicit)(p,x,mi_memory_order(relaxed))
76
+ #define mi_atomic_exchange_release(p,x) mi_atomic(exchange_explicit)(p,x,mi_memory_order(release))
77
+ #define mi_atomic_exchange_acq_rel(p,x) mi_atomic(exchange_explicit)(p,x,mi_memory_order(acq_rel))
78
+ #define mi_atomic_cas_weak_release(p,exp,des) mi_atomic_cas_weak(p,exp,des,mi_memory_order(release),mi_memory_order(relaxed))
79
+ #define mi_atomic_cas_weak_acq_rel(p,exp,des) mi_atomic_cas_weak(p,exp,des,mi_memory_order(acq_rel),mi_memory_order(acquire))
80
+ #define mi_atomic_cas_strong_release(p,exp,des) mi_atomic_cas_strong(p,exp,des,mi_memory_order(release),mi_memory_order(relaxed))
81
+ #define mi_atomic_cas_strong_acq_rel(p,exp,des) mi_atomic_cas_strong(p,exp,des,mi_memory_order(acq_rel),mi_memory_order(acquire))
82
+
83
+ #define mi_atomic_add_relaxed(p,x) mi_atomic(fetch_add_explicit)(p,x,mi_memory_order(relaxed))
84
+ #define mi_atomic_sub_relaxed(p,x) mi_atomic(fetch_sub_explicit)(p,x,mi_memory_order(relaxed))
85
+ #define mi_atomic_add_acq_rel(p,x) mi_atomic(fetch_add_explicit)(p,x,mi_memory_order(acq_rel))
86
+ #define mi_atomic_sub_acq_rel(p,x) mi_atomic(fetch_sub_explicit)(p,x,mi_memory_order(acq_rel))
87
+ #define mi_atomic_and_acq_rel(p,x) mi_atomic(fetch_and_explicit)(p,x,mi_memory_order(acq_rel))
88
+ #define mi_atomic_or_acq_rel(p,x) mi_atomic(fetch_or_explicit)(p,x,mi_memory_order(acq_rel))
89
+
90
+ #define mi_atomic_increment_relaxed(p) mi_atomic_add_relaxed(p,(uintptr_t)1)
91
+ #define mi_atomic_decrement_relaxed(p) mi_atomic_sub_relaxed(p,(uintptr_t)1)
92
+ #define mi_atomic_increment_acq_rel(p) mi_atomic_add_acq_rel(p,(uintptr_t)1)
93
+ #define mi_atomic_decrement_acq_rel(p) mi_atomic_sub_acq_rel(p,(uintptr_t)1)
94
+
95
+ static inline void mi_atomic_yield(void);
96
+ static inline intptr_t mi_atomic_addi(_Atomic(intptr_t)*p, intptr_t add);
97
+ static inline intptr_t mi_atomic_subi(_Atomic(intptr_t)*p, intptr_t sub);
98
+
99
+
100
+ #if defined(__cplusplus) || !defined(_MSC_VER)
101
+
102
+ // In C++/C11 atomics we have polymorphic atomics so can use the typed `ptr` variants (where `tp` is the type of atomic value)
103
+ // We use these macros so we can provide a typed wrapper in MSVC in C compilation mode as well
104
+ #define mi_atomic_load_ptr_acquire(tp,p) mi_atomic_load_acquire(p)
105
+ #define mi_atomic_load_ptr_relaxed(tp,p) mi_atomic_load_relaxed(p)
106
+
107
+ // In C++ we need to add casts to help resolve templates if NULL is passed
108
+ #if defined(__cplusplus)
109
+ #define mi_atomic_store_ptr_release(tp,p,x) mi_atomic_store_release(p,(tp*)x)
110
+ #define mi_atomic_store_ptr_relaxed(tp,p,x) mi_atomic_store_relaxed(p,(tp*)x)
111
+ #define mi_atomic_cas_ptr_weak_release(tp,p,exp,des) mi_atomic_cas_weak_release(p,exp,(tp*)des)
112
+ #define mi_atomic_cas_ptr_weak_acq_rel(tp,p,exp,des) mi_atomic_cas_weak_acq_rel(p,exp,(tp*)des)
113
+ #define mi_atomic_cas_ptr_strong_release(tp,p,exp,des) mi_atomic_cas_strong_release(p,exp,(tp*)des)
114
+ #define mi_atomic_cas_ptr_strong_acq_rel(tp,p,exp,des) mi_atomic_cas_strong_acq_rel(p,exp,(tp*)des)
115
+ #define mi_atomic_exchange_ptr_relaxed(tp,p,x) mi_atomic_exchange_relaxed(p,(tp*)x)
116
+ #define mi_atomic_exchange_ptr_release(tp,p,x) mi_atomic_exchange_release(p,(tp*)x)
117
+ #define mi_atomic_exchange_ptr_acq_rel(tp,p,x) mi_atomic_exchange_acq_rel(p,(tp*)x)
118
+ #else
119
+ #define mi_atomic_store_ptr_release(tp,p,x) mi_atomic_store_release(p,x)
120
+ #define mi_atomic_store_ptr_relaxed(tp,p,x) mi_atomic_store_relaxed(p,x)
121
+ #define mi_atomic_cas_ptr_weak_release(tp,p,exp,des) mi_atomic_cas_weak_release(p,exp,des)
122
+ #define mi_atomic_cas_ptr_weak_acq_rel(tp,p,exp,des) mi_atomic_cas_weak_acq_rel(p,exp,des)
123
+ #define mi_atomic_cas_ptr_strong_release(tp,p,exp,des) mi_atomic_cas_strong_release(p,exp,des)
124
+ #define mi_atomic_cas_ptr_strong_acq_rel(tp,p,exp,des) mi_atomic_cas_strong_acq_rel(p,exp,des)
125
+ #define mi_atomic_exchange_ptr_relaxed(tp,p,x) mi_atomic_exchange_relaxed(p,x)
126
+ #define mi_atomic_exchange_ptr_release(tp,p,x) mi_atomic_exchange_release(p,x)
127
+ #define mi_atomic_exchange_ptr_acq_rel(tp,p,x) mi_atomic_exchange_acq_rel(p,x)
128
+ #endif
129
+
130
+ // These are used by the statistics
131
+ static inline int64_t mi_atomic_addi64_relaxed(volatile int64_t* p, int64_t add) {
132
+ return mi_atomic(fetch_add_explicit)((_Atomic(int64_t)*)p, add, mi_memory_order(relaxed));
133
+ }
134
+ static inline void mi_atomic_void_addi64_relaxed(volatile int64_t* p, const volatile int64_t* padd) {
135
+ const int64_t add = mi_atomic_load_relaxed((_Atomic(int64_t)*)padd);
136
+ if (add != 0) {
137
+ mi_atomic(fetch_add_explicit)((_Atomic(int64_t)*)p, add, mi_memory_order(relaxed));
138
+ }
139
+ }
140
+ static inline void mi_atomic_maxi64_relaxed(volatile int64_t* p, int64_t x) {
141
+ int64_t current = mi_atomic_load_relaxed((_Atomic(int64_t)*)p);
142
+ while (current < x && !mi_atomic_cas_weak_release((_Atomic(int64_t)*)p, &current, x)) { /* nothing */ };
143
+ }
144
+
145
+ // Used by timers
146
+ #define mi_atomic_loadi64_acquire(p) mi_atomic(load_explicit)(p,mi_memory_order(acquire))
147
+ #define mi_atomic_loadi64_relaxed(p) mi_atomic(load_explicit)(p,mi_memory_order(relaxed))
148
+ #define mi_atomic_storei64_release(p,x) mi_atomic(store_explicit)(p,x,mi_memory_order(release))
149
+ #define mi_atomic_storei64_relaxed(p,x) mi_atomic(store_explicit)(p,x,mi_memory_order(relaxed))
150
+
151
+ #define mi_atomic_casi64_strong_acq_rel(p,e,d) mi_atomic_cas_strong_acq_rel(p,e,d)
152
+ #define mi_atomic_addi64_acq_rel(p,i) mi_atomic_add_acq_rel(p,i)
153
+
154
+
155
+ #elif defined(_MSC_VER)
156
+
157
+ // Legacy MSVC plain C compilation wrapper that uses Interlocked operations to model C11 atomics.
158
+ #include <intrin.h>
159
+ #ifdef _WIN64
160
+ typedef LONG64 msc_intptr_t;
161
+ #define MI_64(f) f##64
162
+ #else
163
+ typedef LONG msc_intptr_t;
164
+ #define MI_64(f) f
165
+ #endif
166
+
167
+ typedef enum mi_memory_order_e {
168
+ mi_memory_order_relaxed,
169
+ mi_memory_order_consume,
170
+ mi_memory_order_acquire,
171
+ mi_memory_order_release,
172
+ mi_memory_order_acq_rel,
173
+ mi_memory_order_seq_cst
174
+ } mi_memory_order;
175
+
176
+ static inline uintptr_t mi_atomic_fetch_add_explicit(_Atomic(uintptr_t)*p, uintptr_t add, mi_memory_order mo) {
177
+ (void)(mo);
178
+ return (uintptr_t)MI_64(_InterlockedExchangeAdd)((volatile msc_intptr_t*)p, (msc_intptr_t)add);
179
+ }
180
+ static inline uintptr_t mi_atomic_fetch_sub_explicit(_Atomic(uintptr_t)*p, uintptr_t sub, mi_memory_order mo) {
181
+ (void)(mo);
182
+ return (uintptr_t)MI_64(_InterlockedExchangeAdd)((volatile msc_intptr_t*)p, -((msc_intptr_t)sub));
183
+ }
184
+ static inline uintptr_t mi_atomic_fetch_and_explicit(_Atomic(uintptr_t)*p, uintptr_t x, mi_memory_order mo) {
185
+ (void)(mo);
186
+ return (uintptr_t)MI_64(_InterlockedAnd)((volatile msc_intptr_t*)p, (msc_intptr_t)x);
187
+ }
188
+ static inline uintptr_t mi_atomic_fetch_or_explicit(_Atomic(uintptr_t)*p, uintptr_t x, mi_memory_order mo) {
189
+ (void)(mo);
190
+ return (uintptr_t)MI_64(_InterlockedOr)((volatile msc_intptr_t*)p, (msc_intptr_t)x);
191
+ }
192
+ static inline bool mi_atomic_compare_exchange_strong_explicit(_Atomic(uintptr_t)*p, uintptr_t* expected, uintptr_t desired, mi_memory_order mo1, mi_memory_order mo2) {
193
+ (void)(mo1); (void)(mo2);
194
+ uintptr_t read = (uintptr_t)MI_64(_InterlockedCompareExchange)((volatile msc_intptr_t*)p, (msc_intptr_t)desired, (msc_intptr_t)(*expected));
195
+ if (read == *expected) {
196
+ return true;
197
+ }
198
+ else {
199
+ *expected = read;
200
+ return false;
201
+ }
202
+ }
203
+ static inline bool mi_atomic_compare_exchange_weak_explicit(_Atomic(uintptr_t)*p, uintptr_t* expected, uintptr_t desired, mi_memory_order mo1, mi_memory_order mo2) {
204
+ return mi_atomic_compare_exchange_strong_explicit(p, expected, desired, mo1, mo2);
205
+ }
206
+ static inline uintptr_t mi_atomic_exchange_explicit(_Atomic(uintptr_t)*p, uintptr_t exchange, mi_memory_order mo) {
207
+ (void)(mo);
208
+ return (uintptr_t)MI_64(_InterlockedExchange)((volatile msc_intptr_t*)p, (msc_intptr_t)exchange);
209
+ }
210
+ static inline void mi_atomic_thread_fence(mi_memory_order mo) {
211
+ (void)(mo);
212
+ _Atomic(uintptr_t) x = 0;
213
+ mi_atomic_exchange_explicit(&x, 1, mo);
214
+ }
215
+ static inline uintptr_t mi_atomic_load_explicit(_Atomic(uintptr_t) const* p, mi_memory_order mo) {
216
+ (void)(mo);
217
+ #if defined(_M_IX86) || defined(_M_X64)
218
+ return *p;
219
+ #else
220
+ uintptr_t x = *p;
221
+ if (mo > mi_memory_order_relaxed) {
222
+ while (!mi_atomic_compare_exchange_weak_explicit((_Atomic(uintptr_t)*)p, &x, x, mo, mi_memory_order_relaxed)) { /* nothing */ };
223
+ }
224
+ return x;
225
+ #endif
226
+ }
227
+ static inline void mi_atomic_store_explicit(_Atomic(uintptr_t)*p, uintptr_t x, mi_memory_order mo) {
228
+ (void)(mo);
229
+ #if defined(_M_IX86) || defined(_M_X64)
230
+ *p = x;
231
+ #else
232
+ mi_atomic_exchange_explicit(p, x, mo);
233
+ #endif
234
+ }
235
+ static inline int64_t mi_atomic_loadi64_explicit(_Atomic(int64_t)*p, mi_memory_order mo) {
236
+ (void)(mo);
237
+ #if defined(_M_X64)
238
+ return *p;
239
+ #else
240
+ int64_t old = *p;
241
+ int64_t x = old;
242
+ while ((old = InterlockedCompareExchange64(p, x, old)) != x) {
243
+ x = old;
244
+ }
245
+ return x;
246
+ #endif
247
+ }
248
+ static inline void mi_atomic_storei64_explicit(_Atomic(int64_t)*p, int64_t x, mi_memory_order mo) {
249
+ (void)(mo);
250
+ #if defined(x_M_IX86) || defined(_M_X64)
251
+ *p = x;
252
+ #else
253
+ InterlockedExchange64(p, x);
254
+ #endif
255
+ }
256
+
257
+ // These are used by the statistics
258
+ static inline int64_t mi_atomic_addi64_relaxed(volatile _Atomic(int64_t)*p, int64_t add) {
259
+ #ifdef _WIN64
260
+ return (int64_t)mi_atomic_addi((int64_t*)p, add);
261
+ #else
262
+ int64_t current;
263
+ int64_t sum;
264
+ do {
265
+ current = *p;
266
+ sum = current + add;
267
+ } while (_InterlockedCompareExchange64(p, sum, current) != current);
268
+ return current;
269
+ #endif
270
+ }
271
+ static inline void mi_atomic_void_addi64_relaxed(volatile int64_t* p, const volatile int64_t* padd) {
272
+ const int64_t add = *padd;
273
+ if (add != 0) {
274
+ mi_atomic_addi64_relaxed((volatile _Atomic(int64_t)*)p, add);
275
+ }
276
+ }
277
+
278
+ static inline void mi_atomic_maxi64_relaxed(volatile _Atomic(int64_t)*p, int64_t x) {
279
+ int64_t current;
280
+ do {
281
+ current = *p;
282
+ } while (current < x && _InterlockedCompareExchange64(p, x, current) != current);
283
+ }
284
+
285
+ static inline void mi_atomic_addi64_acq_rel(volatile _Atomic(int64_t*)p, int64_t i) {
286
+ mi_atomic_addi64_relaxed(p, i);
287
+ }
288
+
289
+ static inline bool mi_atomic_casi64_strong_acq_rel(volatile _Atomic(int64_t*)p, int64_t* exp, int64_t des) {
290
+ int64_t read = _InterlockedCompareExchange64(p, des, *exp);
291
+ if (read == *exp) {
292
+ return true;
293
+ }
294
+ else {
295
+ *exp = read;
296
+ return false;
297
+ }
298
+ }
299
+
300
+ // The pointer macros cast to `uintptr_t`.
301
+ #define mi_atomic_load_ptr_acquire(tp,p) (tp*)mi_atomic_load_acquire((_Atomic(uintptr_t)*)(p))
302
+ #define mi_atomic_load_ptr_relaxed(tp,p) (tp*)mi_atomic_load_relaxed((_Atomic(uintptr_t)*)(p))
303
+ #define mi_atomic_store_ptr_release(tp,p,x) mi_atomic_store_release((_Atomic(uintptr_t)*)(p),(uintptr_t)(x))
304
+ #define mi_atomic_store_ptr_relaxed(tp,p,x) mi_atomic_store_relaxed((_Atomic(uintptr_t)*)(p),(uintptr_t)(x))
305
+ #define mi_atomic_cas_ptr_weak_release(tp,p,exp,des) mi_atomic_cas_weak_release((_Atomic(uintptr_t)*)(p),(uintptr_t*)exp,(uintptr_t)des)
306
+ #define mi_atomic_cas_ptr_weak_acq_rel(tp,p,exp,des) mi_atomic_cas_weak_acq_rel((_Atomic(uintptr_t)*)(p),(uintptr_t*)exp,(uintptr_t)des)
307
+ #define mi_atomic_cas_ptr_strong_release(tp,p,exp,des) mi_atomic_cas_strong_release((_Atomic(uintptr_t)*)(p),(uintptr_t*)exp,(uintptr_t)des)
308
+ #define mi_atomic_cas_ptr_strong_acq_rel(tp,p,exp,des) mi_atomic_cas_strong_acq_rel((_Atomic(uintptr_t)*)(p),(uintptr_t*)exp,(uintptr_t)des)
309
+ #define mi_atomic_exchange_ptr_relaxed(tp,p,x) (tp*)mi_atomic_exchange_relaxed((_Atomic(uintptr_t)*)(p),(uintptr_t)x)
310
+ #define mi_atomic_exchange_ptr_release(tp,p,x) (tp*)mi_atomic_exchange_release((_Atomic(uintptr_t)*)(p),(uintptr_t)x)
311
+ #define mi_atomic_exchange_ptr_acq_rel(tp,p,x) (tp*)mi_atomic_exchange_acq_rel((_Atomic(uintptr_t)*)(p),(uintptr_t)x)
312
+
313
+ #define mi_atomic_loadi64_acquire(p) mi_atomic(loadi64_explicit)(p,mi_memory_order(acquire))
314
+ #define mi_atomic_loadi64_relaxed(p) mi_atomic(loadi64_explicit)(p,mi_memory_order(relaxed))
315
+ #define mi_atomic_storei64_release(p,x) mi_atomic(storei64_explicit)(p,x,mi_memory_order(release))
316
+ #define mi_atomic_storei64_relaxed(p,x) mi_atomic(storei64_explicit)(p,x,mi_memory_order(relaxed))
317
+
318
+
319
+ #endif
320
+
321
+
322
+ // Atomically add a signed value; returns the previous value.
323
+ static inline intptr_t mi_atomic_addi(_Atomic(intptr_t)*p, intptr_t add) {
324
+ return (intptr_t)mi_atomic_add_acq_rel((_Atomic(uintptr_t)*)p, (uintptr_t)add);
325
+ }
326
+
327
+ // Atomically subtract a signed value; returns the previous value.
328
+ static inline intptr_t mi_atomic_subi(_Atomic(intptr_t)*p, intptr_t sub) {
329
+ return (intptr_t)mi_atomic_addi(p, -sub);
330
+ }
331
+
332
+
333
+ // ----------------------------------------------------------------------
334
+ // Once and Guard
335
+ // ----------------------------------------------------------------------
336
+
337
+ typedef _Atomic(uintptr_t) mi_atomic_once_t;
338
+
339
+ // Returns true only on the first invocation
340
+ static inline bool mi_atomic_once( mi_atomic_once_t* once ) {
341
+ if (mi_atomic_load_relaxed(once) != 0) return false; // quick test
342
+ uintptr_t expected = 0;
343
+ return mi_atomic_cas_strong_acq_rel(once, &expected, (uintptr_t)1); // try to set to 1
344
+ }
345
+
346
+ typedef _Atomic(uintptr_t) mi_atomic_guard_t;
347
+
348
+ // Allows only one thread to execute at a time
349
+ #define mi_atomic_guard(guard) \
350
+ uintptr_t _mi_guard_expected = 0; \
351
+ for(bool _mi_guard_once = true; \
352
+ _mi_guard_once && mi_atomic_cas_strong_acq_rel(guard,&_mi_guard_expected,(uintptr_t)1); \
353
+ (mi_atomic_store_release(guard,(uintptr_t)0), _mi_guard_once = false) )
354
+
355
+
356
+
357
+ // ----------------------------------------------------------------------
358
+ // Yield
359
+ // ----------------------------------------------------------------------
360
+
361
+ #if defined(__cplusplus)
362
+ #include <thread>
363
+ static inline void mi_atomic_yield(void) {
364
+ std::this_thread::yield();
365
+ }
366
+ #elif defined(_WIN32)
367
+ static inline void mi_atomic_yield(void) {
368
+ YieldProcessor();
369
+ }
370
+ #elif defined(__SSE2__)
371
+ #include <emmintrin.h>
372
+ static inline void mi_atomic_yield(void) {
373
+ _mm_pause();
374
+ }
375
+ #elif (defined(__GNUC__) || defined(__clang__)) && \
376
+ (defined(__x86_64__) || defined(__i386__) || \
377
+ defined(__aarch64__) || defined(__arm__) || \
378
+ defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) || defined(__POWERPC__))
379
+ #if defined(__x86_64__) || defined(__i386__)
380
+ static inline void mi_atomic_yield(void) {
381
+ __asm__ volatile ("pause" ::: "memory");
382
+ }
383
+ #elif defined(__aarch64__)
384
+ static inline void mi_atomic_yield(void) {
385
+ __asm__ volatile("wfe");
386
+ }
387
+ #elif defined(__arm__)
388
+ #if __ARM_ARCH >= 7
389
+ static inline void mi_atomic_yield(void) {
390
+ __asm__ volatile("yield" ::: "memory");
391
+ }
392
+ #else
393
+ static inline void mi_atomic_yield(void) {
394
+ __asm__ volatile ("nop" ::: "memory");
395
+ }
396
+ #endif
397
+ #elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) || defined(__POWERPC__)
398
+ #ifdef __APPLE__
399
+ static inline void mi_atomic_yield(void) {
400
+ __asm__ volatile ("or r27,r27,r27" ::: "memory");
401
+ }
402
+ #else
403
+ static inline void mi_atomic_yield(void) {
404
+ __asm__ __volatile__ ("or 27,27,27" ::: "memory");
405
+ }
406
+ #endif
407
+ #endif
408
+ #elif defined(__sun)
409
+ // Fallback for other archs
410
+ #include <synch.h>
411
+ static inline void mi_atomic_yield(void) {
412
+ smt_pause();
413
+ }
414
+ #elif defined(__wasi__)
415
+ #include <sched.h>
416
+ static inline void mi_atomic_yield(void) {
417
+ sched_yield();
418
+ }
419
+ #else
420
+ #include <unistd.h>
421
+ static inline void mi_atomic_yield(void) {
422
+ sleep(0);
423
+ }
424
+ #endif
425
+
426
+
427
+ // ----------------------------------------------------------------------
428
+ // Locks
429
+ // These do not have to be recursive and should be light-weight
430
+ // in-process only locks. Only used for reserving arena's and to
431
+ // maintain the abandoned list.
432
+ // ----------------------------------------------------------------------
433
+ #if _MSC_VER
434
+ #pragma warning(disable:26110) // unlock with holding lock
435
+ #endif
436
+
437
+ #define mi_lock(lock) for(bool _go = (mi_lock_acquire(lock),true); _go; (mi_lock_release(lock), _go=false) )
438
+
439
+ #if defined(_WIN32)
440
+
441
+ #if 1
442
+ #define mi_lock_t SRWLOCK // slim reader-writer lock
443
+
444
+ static inline bool mi_lock_try_acquire(mi_lock_t* lock) {
445
+ return TryAcquireSRWLockExclusive(lock);
446
+ }
447
+ static inline void mi_lock_acquire(mi_lock_t* lock) {
448
+ AcquireSRWLockExclusive(lock);
449
+ }
450
+ static inline void mi_lock_release(mi_lock_t* lock) {
451
+ ReleaseSRWLockExclusive(lock);
452
+ }
453
+ static inline void mi_lock_init(mi_lock_t* lock) {
454
+ InitializeSRWLock(lock);
455
+ }
456
+ static inline void mi_lock_done(mi_lock_t* lock) {
457
+ (void)(lock);
458
+ }
459
+
460
+ #else
461
+ #define mi_lock_t CRITICAL_SECTION
462
+
463
+ static inline bool mi_lock_try_acquire(mi_lock_t* lock) {
464
+ return TryEnterCriticalSection(lock);
465
+ }
466
+ static inline void mi_lock_acquire(mi_lock_t* lock) {
467
+ EnterCriticalSection(lock);
468
+ }
469
+ static inline void mi_lock_release(mi_lock_t* lock) {
470
+ LeaveCriticalSection(lock);
471
+ }
472
+ static inline void mi_lock_init(mi_lock_t* lock) {
473
+ InitializeCriticalSection(lock);
474
+ }
475
+ static inline void mi_lock_done(mi_lock_t* lock) {
476
+ DeleteCriticalSection(lock);
477
+ }
478
+
479
+ #endif
480
+
481
+ #elif defined(MI_USE_PTHREADS)
482
+
483
+ void _mi_error_message(int err, const char* fmt, ...);
484
+
485
+ #define mi_lock_t pthread_mutex_t
486
+
487
+ static inline bool mi_lock_try_acquire(mi_lock_t* lock) {
488
+ return (pthread_mutex_trylock(lock) == 0);
489
+ }
490
+ static inline void mi_lock_acquire(mi_lock_t* lock) {
491
+ const int err = pthread_mutex_lock(lock);
492
+ if (err != 0) {
493
+ _mi_error_message(err, "internal error: lock cannot be acquired\n");
494
+ }
495
+ }
496
+ static inline void mi_lock_release(mi_lock_t* lock) {
497
+ pthread_mutex_unlock(lock);
498
+ }
499
+ static inline void mi_lock_init(mi_lock_t* lock) {
500
+ pthread_mutex_init(lock, NULL);
501
+ }
502
+ static inline void mi_lock_done(mi_lock_t* lock) {
503
+ pthread_mutex_destroy(lock);
504
+ }
505
+
506
+ #elif defined(__cplusplus)
507
+
508
+ #include <mutex>
509
+ #define mi_lock_t std::mutex
510
+
511
+ static inline bool mi_lock_try_acquire(mi_lock_t* lock) {
512
+ return lock->try_lock();
513
+ }
514
+ static inline void mi_lock_acquire(mi_lock_t* lock) {
515
+ lock->lock();
516
+ }
517
+ static inline void mi_lock_release(mi_lock_t* lock) {
518
+ lock->unlock();
519
+ }
520
+ static inline void mi_lock_init(mi_lock_t* lock) {
521
+ (void)(lock);
522
+ }
523
+ static inline void mi_lock_done(mi_lock_t* lock) {
524
+ (void)(lock);
525
+ }
526
+
527
+ #else
528
+
529
+ // fall back to poor man's locks.
530
+ // this should only be the case in a single-threaded environment (like __wasi__)
531
+
532
+ #define mi_lock_t _Atomic(uintptr_t)
533
+
534
+ static inline bool mi_lock_try_acquire(mi_lock_t* lock) {
535
+ uintptr_t expected = 0;
536
+ return mi_atomic_cas_strong_acq_rel(lock, &expected, (uintptr_t)1);
537
+ }
538
+ static inline void mi_lock_acquire(mi_lock_t* lock) {
539
+ for (int i = 0; i < 1000; i++) { // for at most 1000 tries?
540
+ if (mi_lock_try_acquire(lock)) return;
541
+ mi_atomic_yield();
542
+ }
543
+ }
544
+ static inline void mi_lock_release(mi_lock_t* lock) {
545
+ mi_atomic_store_release(lock, (uintptr_t)0);
546
+ }
547
+ static inline void mi_lock_init(mi_lock_t* lock) {
548
+ mi_lock_release(lock);
549
+ }
550
+ static inline void mi_lock_done(mi_lock_t* lock) {
551
+ (void)(lock);
552
+ }
553
+
554
+ #endif
555
+
556
+
557
+ #endif // __MIMALLOC_ATOMIC_H