@series-inc/rundot-syncplay 6.0.0-rc.2 → 6.0.0-rc.21
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/README.md +133 -0
- package/core/deterministic-math.d.ts +75 -1
- package/core/native/instance_ledger.hpp +1095 -0
- package/core/native/legacy_checkpoint_ids.hpp +107 -0
- package/core/native/physics/abi.cpp +39 -211
- package/core/native/physics/abi.hpp +2 -6
- package/core/native/physics/articulation.cpp +1434 -0
- package/core/native/physics/articulation.hpp +279 -0
- package/core/native/physics/buoyancy.cpp +1588 -0
- package/core/native/physics/buoyancy.hpp +254 -0
- package/core/native/physics/capsule_triangle.cpp +168 -0
- package/core/native/physics/capsule_triangle.hpp +40 -0
- package/core/native/physics/capsule_triangle_native.inc +291 -0
- package/core/native/physics/certified_sum.hpp +218 -0
- package/core/native/physics/character.cpp +638 -0
- package/core/native/physics/character.hpp +169 -0
- package/core/native/physics/checkpoint_schema.hpp +379 -0
- package/core/native/physics/cloth.cpp +1401 -0
- package/core/native/physics/cloth.hpp +357 -0
- package/core/native/physics/constraint_rows.cpp +363 -2
- package/core/native/physics/constraint_rows.hpp +72 -1
- package/core/native/physics/deformable_query.cpp +1630 -0
- package/core/native/physics/deformable_query.hpp +578 -0
- package/core/native/physics/distance.cpp +1166 -0
- package/core/native/physics/distance.hpp +151 -0
- package/core/native/physics/exact/exact-applied.cpp +101 -0
- package/core/native/physics/exact/exact-applied.hpp +112 -0
- package/core/native/physics/exact/exact-dyadic.cpp +691 -0
- package/core/native/physics/exact/exact-dyadic.hpp +58 -0
- package/core/native/physics/exact/exact-operation.hpp +51 -0
- package/core/native/physics/exact/word-core.hpp +262 -0
- package/core/native/physics/extended_abi.cpp +1242 -0
- package/core/native/physics/extended_abi.hpp +355 -0
- package/core/native/physics/extended_abi_characters.cpp +261 -0
- package/core/native/physics/extended_abi_characters.hpp +434 -0
- package/core/native/physics/extended_abi_core_features.cpp +713 -0
- package/core/native/physics/extended_abi_core_features.hpp +226 -0
- package/core/native/physics/extended_abi_deformables.cpp +448 -0
- package/core/native/physics/extended_abi_deformables.hpp +867 -0
- package/core/native/physics/extended_abi_vehicles.cpp +411 -0
- package/core/native/physics/extended_abi_vehicles.hpp +222 -0
- package/core/native/physics/fracture.cpp +1981 -0
- package/core/native/physics/fracture.hpp +460 -0
- package/core/native/physics/gear.cpp +563 -0
- package/core/native/physics/gear.hpp +158 -0
- package/core/native/physics/geometry_memory.hpp +476 -0
- package/core/native/physics/geometry_store.cpp +547 -0
- package/core/native/physics/geometry_store.hpp +134 -0
- package/core/native/physics/lag_compensation.cpp +1626 -0
- package/core/native/physics/lag_compensation.hpp +405 -0
- package/core/native/physics/mass_properties.cpp +449 -0
- package/core/native/physics/mass_properties.hpp +23 -0
- package/core/native/physics/material_pose.hpp +988 -0
- package/core/native/physics/particles.cpp +1065 -0
- package/core/native/physics/particles.hpp +261 -0
- package/core/native/physics/path_joint.cpp +781 -0
- package/core/native/physics/path_joint.hpp +154 -0
- package/core/native/physics/physics.cpp +13043 -1065
- package/core/native/physics/physics.hpp +184 -8
- package/core/native/physics/planar_deformable.cpp +2433 -0
- package/core/native/physics/planar_deformable.hpp +889 -0
- package/core/native/physics/platformer_2d.cpp +1353 -0
- package/core/native/physics/platformer_2d.hpp +340 -0
- package/core/native/physics/powered_ragdoll.cpp +1100 -0
- package/core/native/physics/powered_ragdoll.hpp +279 -0
- package/core/native/physics/powertrain.cpp +586 -0
- package/core/native/physics/powertrain.hpp +180 -0
- package/core/native/physics/pulley.cpp +311 -0
- package/core/native/physics/pulley.hpp +121 -0
- package/core/native/physics/ragdoll.cpp +586 -0
- package/core/native/physics/ragdoll.hpp +221 -0
- package/core/native/physics/rope.cpp +692 -0
- package/core/native/physics/rope.hpp +215 -0
- package/core/native/physics/rounded_convex.cpp +1229 -0
- package/core/native/physics/rounded_convex.hpp +287 -0
- package/core/native/physics/sdf_geometry.cpp +1821 -0
- package/core/native/physics/sdf_geometry.hpp +474 -0
- package/core/native/physics/soft_body.cpp +1449 -0
- package/core/native/physics/soft_body.hpp +328 -0
- package/core/native/physics/soft_collision.cpp +1862 -0
- package/core/native/physics/soft_collision.hpp +561 -0
- package/core/native/physics/surface_velocity.cpp +366 -0
- package/core/native/physics/surface_velocity.hpp +243 -0
- package/core/native/physics/topology.cpp +357 -39
- package/core/native/physics/topology.hpp +97 -6
- package/core/native/physics/tracks.cpp +1179 -0
- package/core/native/physics/tracks.hpp +404 -0
- package/core/native/physics/vehicle.cpp +629 -0
- package/core/native/physics/vehicle.hpp +191 -0
- package/core/native/physics/voxel_cooking.cpp +1929 -0
- package/core/native/physics/voxel_cooking.hpp +395 -0
- package/core/native/physics/world.cpp +5290 -854
- package/core/native/physics/world.hpp +796 -24
- package/core/native/physics/world_abi.cpp +3840 -306
- package/core/native/physics/world_abi.hpp +530 -10
- package/core/native/physics/world_digest_visitor.hpp +277 -0
- package/core/native/physics/world_strong_digest.hpp +557 -0
- package/core/native/physics2d/ccd.cpp +1302 -0
- package/core/native/physics2d/ccd.hpp +141 -0
- package/core/native/physics2d/checkpoint_memory.hpp +349 -0
- package/core/native/physics2d/clock_primitives.hpp +138 -0
- package/core/native/physics2d/commands.cpp +174 -28
- package/core/native/physics2d/commands.hpp +3 -1
- package/core/native/physics2d/compound.cpp +581 -0
- package/core/native/physics2d/compound.hpp +248 -0
- package/core/native/physics2d/contour.cpp +325 -0
- package/core/native/physics2d/contour.hpp +143 -0
- package/core/native/physics2d/frame_scope.hpp +124 -0
- package/core/native/physics2d/joint_phases.hpp +93 -0
- package/core/native/physics2d/joints.cpp +587 -150
- package/core/native/physics2d/joints.hpp +5 -0
- package/core/native/physics2d/memory_private.hpp +125 -0
- package/core/native/physics2d/owned_abi.cpp +1354 -879
- package/core/native/physics2d/owned_abi.hpp +190 -52
- package/core/native/physics2d/phase_test_hooks.hpp +17 -0
- package/core/native/physics2d/physics2d.cpp +2073 -120
- package/core/native/physics2d/physics2d.hpp +154 -2
- package/core/native/physics2d/queries.cpp +1055 -79
- package/core/native/physics2d/queries.hpp +71 -5
- package/core/native/physics2d/world.cpp +1313 -353
- package/core/native/physics2d/world.hpp +124 -13
- package/core/native/sha256/sha256_compress_unsigned.hpp +68 -0
- package/core/native/sha256/sha256_fips_constants.hpp +24 -0
- package/core/native/sha256/sha256_length.hpp +56 -0
- package/core/native/sha256/sha256_memo.hpp +48 -0
- package/core/native/sha256/sha256_sink.hpp +234 -0
- package/core/physics2d-owned.d.ts +466 -126
- package/core/runtime.d.ts +192 -19
- package/core/scripts/build-native.mjs +34 -0
- package/core/scripts/build-wasm-physics.mjs +272 -23
- package/core/scripts/extended-physics-profile.mjs +634 -0
- package/core/scripts/generate-physics2d-vectors.mjs +59 -0
- package/core/src/kinetix-deterministic-math.d.ts +2 -0
- package/core/src/kinetix-deterministic-math.mjs +513 -27
- package/core/src/kinetix-owned-runtime-adapter.mjs +788 -0
- package/core/src/kinetix-runtime-adapter-utils.mjs +54 -6
- package/core/src/kinetix-runtime-contract.mjs +55 -9
- package/core/src/kinetix-runtime-identity.mjs +12 -1
- package/core/src/kinetix-session-config.mjs +7 -2
- package/core/src/kinetix-state-report.mjs +36 -0
- package/core/src/physics/abi-constants.mjs +5 -4
- package/core/src/physics/articulation.d.mts +194 -0
- package/core/src/physics/articulation.mjs +1141 -0
- package/core/src/physics/buoyancy.d.mts +143 -0
- package/core/src/physics/buoyancy.mjs +268 -0
- package/core/src/physics/cloth.d.mts +314 -0
- package/core/src/physics/cloth.mjs +1156 -0
- package/core/src/physics/deformable-query.d.mts +217 -0
- package/core/src/physics/deformable-query.mjs +1412 -0
- package/core/src/physics/distance.d.mts +160 -0
- package/core/src/physics/distance.mjs +175 -0
- package/core/src/physics/extended-wasm-instance.d.mts +5 -0
- package/core/src/physics/extended-wasm-instance.mjs +48 -0
- package/core/src/physics/feature-api.d.mts +74 -0
- package/core/src/physics/feature-api.mjs +98 -0
- package/core/src/physics/fracture.d.mts +133 -0
- package/core/src/physics/fracture.mjs +1324 -0
- package/core/src/physics/gear.d.mts +585 -0
- package/core/src/physics/gear.mjs +955 -0
- package/core/src/physics/lag-compensation.d.mts +101 -0
- package/core/src/physics/lag-compensation.mjs +1100 -0
- package/core/src/physics/mass-properties.d.mts +114 -0
- package/core/src/physics/mass-properties.mjs +137 -468
- package/core/src/physics/particles.d.mts +177 -0
- package/core/src/physics/particles.mjs +758 -0
- package/core/src/physics/path-joint.d.mts +171 -0
- package/core/src/physics/path-joint.mjs +1231 -0
- package/core/src/physics/physics-extended-wasm-module.generated.mjs +13 -0
- package/core/src/physics/physics-profile-wasm-module.generated.mjs +48 -0
- package/core/src/physics/physics-wasm-module.generated.mjs +34 -5
- package/core/src/physics/powered-ragdoll.d.mts +188 -0
- package/core/src/physics/powered-ragdoll.mjs +1043 -0
- package/core/src/physics/powertrain.d.mts +260 -0
- package/core/src/physics/powertrain.mjs +621 -0
- package/core/src/physics/pulley.d.mts +98 -0
- package/core/src/physics/pulley.mjs +686 -0
- package/core/src/physics/ragdoll.d.mts +289 -0
- package/core/src/physics/ragdoll.mjs +1323 -0
- package/core/src/physics/rope.d.mts +241 -0
- package/core/src/physics/rope.mjs +319 -0
- package/core/src/physics/rounded-convex.d.mts +328 -0
- package/core/src/physics/rounded-convex.mjs +1404 -0
- package/core/src/physics/sdf-geometry.d.mts +117 -0
- package/core/src/physics/sdf-geometry.mjs +1204 -0
- package/core/src/physics/soft-body.d.mts +144 -0
- package/core/src/physics/soft-body.mjs +1285 -0
- package/core/src/physics/soft-collision.d.mts +651 -0
- package/core/src/physics/soft-collision.mjs +1883 -0
- package/core/src/physics/surface-velocity.d.mts +156 -0
- package/core/src/physics/surface-velocity.mjs +722 -0
- package/core/src/physics/tracks.d.mts +411 -0
- package/core/src/physics/tracks.mjs +1074 -0
- package/core/src/physics/vehicle.d.mts +3 -0
- package/core/src/physics/vehicle.mjs +172 -0
- package/core/src/physics/voxel-cooking.d.mts +219 -0
- package/core/src/physics/voxel-cooking.mjs +1499 -0
- package/core/src/physics/voxel-swept-quad.mjs +110 -0
- package/core/src/physics/wasm-instance.mjs +160 -23
- package/core/src/physics/world.d.mts +62 -0
- package/core/src/physics/world.mjs +1947 -152
- package/core/src/physics2d/body-state.mjs +218 -15
- package/core/src/physics2d/collision.d.mts +52 -0
- package/core/src/physics2d/collision.mjs +234 -0
- package/core/src/physics2d/compound.mjs +454 -0
- package/core/src/physics2d/contour-state.mjs +771 -0
- package/core/src/physics2d/feature-api.d.mts +211 -0
- package/core/src/physics2d/feature-api.mjs +3 -0
- package/core/src/physics2d/fluid-state.mjs +145 -0
- package/core/src/physics2d/joint-state.mjs +1 -1
- package/core/src/physics2d/owned-queries.mjs +3 -1
- package/core/src/physics2d/owned-transport.mjs +268 -122
- package/core/src/physics2d/owned-world.mjs +353 -230
- package/core/src/physics2d/particle-fluid.d.mts +152 -0
- package/core/src/physics2d/particle-fluid.mjs +233 -0
- package/core/src/physics2d/physics2d-wasm-module.generated.mjs +27 -10
- package/core/src/physics2d/query-codec.mjs +61 -13
- package/core/src/physics2d/submerged-geometry.d.mts +51 -0
- package/core/src/physics2d/submerged-geometry.mjs +186 -0
- package/core/src/runtime.mjs +23 -0
- package/core/src/shared/bytes.d.mts +2 -0
- package/core/src/shared/bytes.mjs +33 -0
- package/core/src/shared/sha256.d.mts +3 -0
- package/core/src/shared/sha256.mjs +72 -22
- package/core/units.d.ts +50 -0
- package/core/world.d.ts +253 -5
- package/dist/authority-room.d.ts +9 -0
- package/dist/authority-room.js +45 -16
- package/dist/browser.d.ts +6 -5
- package/dist/browser.js +4 -3
- package/dist/cadence.d.ts +7 -0
- package/dist/cadence.js +19 -0
- package/dist/canonical.d.ts +1 -0
- package/dist/canonical.js +3 -0
- package/dist/checksum-policy.d.ts +10 -0
- package/dist/checksum-policy.js +14 -0
- package/dist/collider-cooking-hull.d.ts +18 -0
- package/dist/collider-cooking-hull.js +248 -0
- package/dist/collider-cooking.d.ts +140 -0
- package/dist/collider-cooking.js +794 -0
- package/dist/compat/v5.d.ts +121 -0
- package/dist/compat/v5.js +479 -0
- package/dist/creator.d.ts +7 -6
- package/dist/creator.js +4 -3
- package/dist/game-byte-section.d.ts +36 -0
- package/dist/game-byte-section.js +350 -0
- package/dist/index.d.ts +13 -6
- package/dist/index.js +9 -4
- package/dist/instant-replay.d.ts +27 -2
- package/dist/instant-replay.js +91 -16
- package/dist/lag-compensation3d.js +11 -10
- package/dist/match-log.d.ts +21 -4
- package/dist/match-log.js +92 -9
- package/dist/math.d.ts +2 -0
- package/dist/math.js +1 -0
- package/dist/modules/character-3d.d.ts +16 -1
- package/dist/modules/character-3d.js +42 -5
- package/dist/modules/combat.js +6 -5
- package/dist/movement3d-revision1.d.ts +2 -0
- package/dist/movement3d-revision1.js +973 -0
- package/dist/movement3d.d.ts +53 -3
- package/dist/movement3d.js +917 -247
- package/dist/networked-client.d.ts +15 -0
- package/dist/networked-client.js +378 -105
- package/dist/node.d.ts +3 -2
- package/dist/node.js +2 -1
- package/dist/physics/2d.d.ts +38 -4
- package/dist/physics/2d.js +57 -36
- package/dist/physics/3d.d.ts +755 -111
- package/dist/physics/3d.js +2355 -158
- package/dist/physics/edit-result-graph.d.ts +36 -0
- package/dist/physics/edit-result-graph.js +141 -0
- package/dist/physics/game-codec3d.d.ts +41 -0
- package/dist/physics/game-codec3d.js +544 -0
- package/dist/physics/installed-checkpoint3d.d.ts +13 -0
- package/dist/physics/installed-checkpoint3d.js +120 -0
- package/dist/physics/installed-runtime3d.d.ts +51 -0
- package/dist/physics/installed-runtime3d.js +372 -0
- package/dist/physics/installed-state-commitment3d.d.ts +2 -0
- package/dist/physics/installed-state-commitment3d.js +24 -0
- package/dist/physics/units.d.ts +38 -0
- package/dist/physics/units.js +56 -0
- package/dist/physics/vehicle-state3d.d.ts +93 -0
- package/dist/physics/vehicle-state3d.js +1191 -0
- package/dist/physics/voxel-body-state.d.ts +61 -0
- package/dist/physics/voxel-body-state.js +410 -0
- package/dist/r3f-render-adapter.d.ts +59 -1
- package/dist/r3f-render-adapter.js +137 -2
- package/dist/replay.d.ts +13 -0
- package/dist/replay.js +95 -70
- package/dist/run-room-transport.js +5 -1
- package/dist/runner.d.ts +17 -2
- package/dist/runner.js +472 -84
- package/dist/runtime-cleanup.d.ts +54 -0
- package/dist/runtime-cleanup.js +266 -0
- package/dist/runtime-lease.d.ts +11 -1
- package/dist/runtime-lease.js +23 -10
- package/dist/runtime-room-metadata.d.ts +2 -8
- package/dist/runtime-room-metadata.js +11 -14
- package/dist/runtime-session.d.ts +8 -0
- package/dist/runtime-session.js +222 -58
- package/dist/schema-codegen.js +3 -0
- package/dist/schema-dsl-binary.d.ts +10 -4
- package/dist/schema-dsl-binary.js +375 -76
- package/dist/scratch-pool.d.ts +32 -0
- package/dist/scratch-pool.js +60 -0
- package/dist/sdk-session.d.ts +1 -0
- package/dist/sdk-session.js +4 -0
- package/dist/secret-config.js +2 -60
- package/dist/secret-crypto.js +70 -36
- package/dist/session-snapshot.d.ts +15 -4
- package/dist/session-snapshot.js +35 -33
- package/dist/session-wire.js +14 -22
- package/dist/sparse-set.d.ts +1 -0
- package/dist/sparse-set.js +22 -0
- package/dist/spatial/spatial-grid-3d.d.ts +41 -0
- package/dist/spatial/spatial-grid-3d.js +115 -0
- package/dist/synctest.d.ts +9 -0
- package/dist/synctest.js +106 -17
- package/dist/testing-network.d.ts +2 -0
- package/dist/testing-network.js +122 -18
- package/dist/testing.d.ts +27 -6
- package/dist/testing.js +297 -56
- package/dist/tools/index.d.ts +3 -2
- package/dist/tools/index.js +3 -2
- package/dist/tools/module-certification.d.ts +6 -0
- package/dist/tools/module-certification.js +26 -2
- package/package.json +18 -5
- package/core/scripts/run-box3d-benchmark.mjs +0 -350
- package/core/src/physics/index.mjs +0 -3169
|
@@ -0,0 +1,1095 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <algorithm>
|
|
4
|
+
#include <array>
|
|
5
|
+
#include <atomic>
|
|
6
|
+
#include <cstddef>
|
|
7
|
+
#include <cstdint>
|
|
8
|
+
#include <cstdlib>
|
|
9
|
+
#include <cstring>
|
|
10
|
+
#include <limits>
|
|
11
|
+
#include <new>
|
|
12
|
+
#include <span>
|
|
13
|
+
#include <type_traits>
|
|
14
|
+
|
|
15
|
+
#if defined(__EMSCRIPTEN__)
|
|
16
|
+
#include <emscripten/heap.h>
|
|
17
|
+
#endif
|
|
18
|
+
|
|
19
|
+
namespace rundot::kinetix::memory {
|
|
20
|
+
|
|
21
|
+
enum class LedgerClass : std::uint8_t {
|
|
22
|
+
Registry,
|
|
23
|
+
World,
|
|
24
|
+
Storage,
|
|
25
|
+
Geometry,
|
|
26
|
+
Material,
|
|
27
|
+
Checkpoint,
|
|
28
|
+
Control,
|
|
29
|
+
Metadata,
|
|
30
|
+
Scratch,
|
|
31
|
+
Count,
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
inline constexpr std::size_t kLedgerClassCount = static_cast<std::size_t>(LedgerClass::Count);
|
|
35
|
+
// Deformable and fracture snapshot restore each have fourteen caller ranges.
|
|
36
|
+
inline constexpr std::size_t kMaxCallerRanges = 14;
|
|
37
|
+
|
|
38
|
+
struct Physics3DModule {
|
|
39
|
+
static constexpr std::uint64_t kRetainedCeilingBytes = 160ULL << 20;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
struct Physics2DModule {
|
|
43
|
+
static constexpr std::uint64_t kRetainedCeilingBytes = 160ULL << 20;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
struct LedgerSnapshot {
|
|
47
|
+
std::array<std::uint64_t, kLedgerClassCount> bytes{};
|
|
48
|
+
std::array<std::uint64_t, kLedgerClassCount> highWaterBytes{};
|
|
49
|
+
std::uint64_t retainedBytes = 0;
|
|
50
|
+
std::uint64_t retainedHighWater = 0;
|
|
51
|
+
std::uint64_t scratchBytes = 0;
|
|
52
|
+
std::uint64_t scratchHighWater = 0;
|
|
53
|
+
std::uint64_t liveWorlds = 0;
|
|
54
|
+
std::uint64_t liveSlots = 0;
|
|
55
|
+
std::uint64_t highWaterSlots = 0;
|
|
56
|
+
std::uint64_t wasmMemoryBytes = 0;
|
|
57
|
+
bool imbalanced = false;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
class SpinMutex {
|
|
61
|
+
public:
|
|
62
|
+
void lock() noexcept {
|
|
63
|
+
while (flag_.test_and_set(std::memory_order_acquire)) {
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
void unlock() noexcept { flag_.clear(std::memory_order_release); }
|
|
68
|
+
|
|
69
|
+
private:
|
|
70
|
+
std::atomic_flag flag_ = ATOMIC_FLAG_INIT;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
class SpinLockGuard {
|
|
74
|
+
public:
|
|
75
|
+
explicit SpinLockGuard(SpinMutex& mutex) noexcept : mutex_(mutex) { mutex_.lock(); }
|
|
76
|
+
~SpinLockGuard() noexcept { mutex_.unlock(); }
|
|
77
|
+
SpinLockGuard(const SpinLockGuard&) = delete;
|
|
78
|
+
SpinLockGuard& operator=(const SpinLockGuard&) = delete;
|
|
79
|
+
|
|
80
|
+
private:
|
|
81
|
+
SpinMutex& mutex_;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
enum class RangeRole : std::uint8_t { Input, Output };
|
|
85
|
+
|
|
86
|
+
struct CallerRange {
|
|
87
|
+
const void* data = nullptr;
|
|
88
|
+
std::size_t bytes = 0;
|
|
89
|
+
std::size_t alignment = 1;
|
|
90
|
+
RangeRole role = RangeRole::Input;
|
|
91
|
+
// elementBytes == 0 keeps a legacy range. A nonzero value must match bytes.
|
|
92
|
+
std::uint64_t count = 0;
|
|
93
|
+
std::uint64_t elementBytes = 0;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
enum class LeaseStatus : std::uint8_t { Ok, UnknownLease, Busy };
|
|
97
|
+
enum class AdmitStatus : std::uint8_t {
|
|
98
|
+
Ok,
|
|
99
|
+
TooManyRanges,
|
|
100
|
+
InvalidRange,
|
|
101
|
+
UnknownLease,
|
|
102
|
+
Overlap,
|
|
103
|
+
Busy,
|
|
104
|
+
PinOverflow,
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
struct LeaseHeader;
|
|
108
|
+
|
|
109
|
+
namespace detail {
|
|
110
|
+
|
|
111
|
+
struct RangeNode {
|
|
112
|
+
std::uintptr_t begin = 0;
|
|
113
|
+
std::uintptr_t end = 0;
|
|
114
|
+
RangeNode* left = nullptr;
|
|
115
|
+
RangeNode* right = nullptr;
|
|
116
|
+
LeaseHeader* owner = nullptr;
|
|
117
|
+
std::int32_t height = 1;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
inline bool isPowerOfTwo(std::size_t value) noexcept {
|
|
121
|
+
return value != 0 && (value & (value - 1)) == 0;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
inline bool addOverflow(std::size_t left, std::size_t right, std::size_t& result) noexcept {
|
|
125
|
+
if (right > std::numeric_limits<std::size_t>::max() - left) return true;
|
|
126
|
+
result = left + right;
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
inline bool validRole(RangeRole role) noexcept {
|
|
131
|
+
return role == RangeRole::Input || role == RangeRole::Output;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Reject a count whose product does not fit in size_t. elementBytes == 0 is legacy.
|
|
135
|
+
inline bool countedProduct(std::uint64_t count, std::uint64_t elementBytes, std::size_t& bytes) noexcept {
|
|
136
|
+
if (elementBytes == 0) {
|
|
137
|
+
bytes = 0;
|
|
138
|
+
return count == 0;
|
|
139
|
+
}
|
|
140
|
+
if (elementBytes > static_cast<std::uint64_t>(std::numeric_limits<std::size_t>::max())) return false;
|
|
141
|
+
const auto element = static_cast<std::size_t>(elementBytes);
|
|
142
|
+
if (count > static_cast<std::uint64_t>(std::numeric_limits<std::size_t>::max() / element)) return false;
|
|
143
|
+
bytes = static_cast<std::size_t>(count) * element;
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
inline bool makeRange(const CallerRange& range, std::uintptr_t& begin, std::uintptr_t& end) noexcept {
|
|
148
|
+
if (!validRole(range.role)) return false;
|
|
149
|
+
if (range.elementBytes != 0) {
|
|
150
|
+
std::size_t product = 0;
|
|
151
|
+
if (!countedProduct(range.count, range.elementBytes, product)) return false;
|
|
152
|
+
if (product != range.bytes) return false;
|
|
153
|
+
}
|
|
154
|
+
if (range.bytes == 0) {
|
|
155
|
+
begin = reinterpret_cast<std::uintptr_t>(range.data);
|
|
156
|
+
end = begin;
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
if (range.data == nullptr || !isPowerOfTwo(range.alignment)) return false;
|
|
160
|
+
begin = reinterpret_cast<std::uintptr_t>(range.data);
|
|
161
|
+
if ((begin & static_cast<std::uintptr_t>(range.alignment - 1)) != 0) return false;
|
|
162
|
+
const auto maxAddress = std::numeric_limits<std::uintptr_t>::max();
|
|
163
|
+
if (range.bytes > static_cast<std::size_t>(maxAddress - begin)) return false;
|
|
164
|
+
end = begin + static_cast<std::uintptr_t>(range.bytes);
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
inline bool overlaps(std::uintptr_t leftBegin, std::uintptr_t leftEnd,
|
|
169
|
+
std::uintptr_t rightBegin, std::uintptr_t rightEnd) noexcept {
|
|
170
|
+
return leftBegin < rightEnd && rightBegin < leftEnd;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
class RangeIndex {
|
|
174
|
+
public:
|
|
175
|
+
bool insert(RangeNode& node) noexcept {
|
|
176
|
+
if (size_ == std::numeric_limits<std::size_t>::max()) return false;
|
|
177
|
+
if (findByBegin(node.begin) != nullptr) return false;
|
|
178
|
+
if (node.begin < node.end) {
|
|
179
|
+
if (findContaining(node.begin) != nullptr) return false;
|
|
180
|
+
const auto* next = lowerBound(node.begin);
|
|
181
|
+
if (next != nullptr && next->begin < node.end) return false;
|
|
182
|
+
}
|
|
183
|
+
node.left = nullptr;
|
|
184
|
+
node.right = nullptr;
|
|
185
|
+
node.height = 1;
|
|
186
|
+
root_ = insertNode(root_, &node);
|
|
187
|
+
++size_;
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
bool erase(RangeNode& node) noexcept {
|
|
192
|
+
if (findByBegin(node.begin) != &node) return false;
|
|
193
|
+
root_ = eraseNode(root_, node.begin);
|
|
194
|
+
--size_;
|
|
195
|
+
node.left = nullptr;
|
|
196
|
+
node.right = nullptr;
|
|
197
|
+
node.height = 1;
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
RangeNode* findByBegin(std::uintptr_t begin, std::size_t* visits = nullptr) const noexcept {
|
|
202
|
+
std::size_t count = 0;
|
|
203
|
+
auto* current = root_;
|
|
204
|
+
while (current != nullptr) {
|
|
205
|
+
++count;
|
|
206
|
+
if (begin < current->begin) current = current->left;
|
|
207
|
+
else if (begin > current->begin) current = current->right;
|
|
208
|
+
else {
|
|
209
|
+
if (visits != nullptr) *visits = count;
|
|
210
|
+
return current;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (visits != nullptr) *visits = count;
|
|
214
|
+
return nullptr;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
RangeNode* findContaining(std::uintptr_t address, std::size_t* visits = nullptr) const noexcept {
|
|
218
|
+
std::size_t count = 0;
|
|
219
|
+
auto* current = root_;
|
|
220
|
+
while (current != nullptr) {
|
|
221
|
+
++count;
|
|
222
|
+
if (address < current->begin) current = current->left;
|
|
223
|
+
else if (address >= current->end) current = current->right;
|
|
224
|
+
else {
|
|
225
|
+
if (visits != nullptr) *visits = count;
|
|
226
|
+
return current;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
if (visits != nullptr) *visits = count;
|
|
230
|
+
return nullptr;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
bool overlaps(std::uintptr_t begin, std::uintptr_t end) const noexcept {
|
|
234
|
+
if (begin >= end) return false;
|
|
235
|
+
if (findContaining(begin) != nullptr) return true;
|
|
236
|
+
const auto* next = lowerBound(begin);
|
|
237
|
+
return next != nullptr && next->begin < end;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
std::size_t size() const noexcept { return size_; }
|
|
241
|
+
std::int32_t depth() const noexcept { return nodeHeight(root_); }
|
|
242
|
+
|
|
243
|
+
private:
|
|
244
|
+
static std::int32_t nodeHeight(const RangeNode* node) noexcept {
|
|
245
|
+
return node == nullptr ? 0 : node->height;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
static void update(RangeNode* node) noexcept {
|
|
249
|
+
node->height = 1 + std::max(nodeHeight(node->left), nodeHeight(node->right));
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
static RangeNode* rotateRight(RangeNode* root) noexcept {
|
|
253
|
+
auto* pivot = root->left;
|
|
254
|
+
root->left = pivot->right;
|
|
255
|
+
pivot->right = root;
|
|
256
|
+
update(root);
|
|
257
|
+
update(pivot);
|
|
258
|
+
return pivot;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
static RangeNode* rotateLeft(RangeNode* root) noexcept {
|
|
262
|
+
auto* pivot = root->right;
|
|
263
|
+
root->right = pivot->left;
|
|
264
|
+
pivot->left = root;
|
|
265
|
+
update(root);
|
|
266
|
+
update(pivot);
|
|
267
|
+
return pivot;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
static RangeNode* balance(RangeNode* node) noexcept {
|
|
271
|
+
update(node);
|
|
272
|
+
const auto delta = nodeHeight(node->left) - nodeHeight(node->right);
|
|
273
|
+
if (delta > 1) {
|
|
274
|
+
if (nodeHeight(node->left->left) < nodeHeight(node->left->right)) {
|
|
275
|
+
node->left = rotateLeft(node->left);
|
|
276
|
+
}
|
|
277
|
+
return rotateRight(node);
|
|
278
|
+
}
|
|
279
|
+
if (delta < -1) {
|
|
280
|
+
if (nodeHeight(node->right->right) < nodeHeight(node->right->left)) {
|
|
281
|
+
node->right = rotateRight(node->right);
|
|
282
|
+
}
|
|
283
|
+
return rotateLeft(node);
|
|
284
|
+
}
|
|
285
|
+
return node;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
static RangeNode* insertNode(RangeNode* root, RangeNode* node) noexcept {
|
|
289
|
+
if (root == nullptr) return node;
|
|
290
|
+
if (node->begin < root->begin) root->left = insertNode(root->left, node);
|
|
291
|
+
else root->right = insertNode(root->right, node);
|
|
292
|
+
return balance(root);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
static RangeNode* takeMinimum(RangeNode* root, RangeNode*& minimum) noexcept {
|
|
296
|
+
if (root->left == nullptr) {
|
|
297
|
+
minimum = root;
|
|
298
|
+
return root->right;
|
|
299
|
+
}
|
|
300
|
+
root->left = takeMinimum(root->left, minimum);
|
|
301
|
+
return balance(root);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
static RangeNode* eraseNode(RangeNode* root, std::uintptr_t begin) noexcept {
|
|
305
|
+
if (begin < root->begin) root->left = eraseNode(root->left, begin);
|
|
306
|
+
else if (begin > root->begin) root->right = eraseNode(root->right, begin);
|
|
307
|
+
else {
|
|
308
|
+
if (root->left == nullptr) return root->right;
|
|
309
|
+
if (root->right == nullptr) return root->left;
|
|
310
|
+
RangeNode* successor = nullptr;
|
|
311
|
+
auto* rightRemainder = takeMinimum(root->right, successor);
|
|
312
|
+
successor->left = root->left;
|
|
313
|
+
successor->right = rightRemainder;
|
|
314
|
+
return balance(successor);
|
|
315
|
+
}
|
|
316
|
+
return balance(root);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
const RangeNode* lowerBound(std::uintptr_t begin) const noexcept {
|
|
320
|
+
auto* current = root_;
|
|
321
|
+
const RangeNode* found = nullptr;
|
|
322
|
+
while (current != nullptr) {
|
|
323
|
+
if (current->begin >= begin) {
|
|
324
|
+
found = current;
|
|
325
|
+
current = current->left;
|
|
326
|
+
} else current = current->right;
|
|
327
|
+
}
|
|
328
|
+
return found;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
RangeNode* root_ = nullptr;
|
|
332
|
+
std::size_t size_ = 0;
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
} // namespace detail
|
|
336
|
+
|
|
337
|
+
struct alignas(16) LeaseHeader {
|
|
338
|
+
detail::RangeNode node{};
|
|
339
|
+
std::uint64_t chargedBytes = 0;
|
|
340
|
+
std::uint32_t pins = 0;
|
|
341
|
+
bool live = false;
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
static_assert(alignof(LeaseHeader) >= 16);
|
|
345
|
+
|
|
346
|
+
#ifdef RUNDOT_PHYSICS_PHASE_TEST_HOOKS
|
|
347
|
+
struct LedgerTestHooks {
|
|
348
|
+
void (*allocationAttempt)(std::size_t bytes) noexcept = nullptr;
|
|
349
|
+
bool (*failAllocation)(std::size_t bytes) noexcept = nullptr;
|
|
350
|
+
void (*beforePhysicalFree)(void* pointer) noexcept = nullptr;
|
|
351
|
+
};
|
|
352
|
+
#endif
|
|
353
|
+
|
|
354
|
+
template<class Module>
|
|
355
|
+
class ScratchLeases;
|
|
356
|
+
|
|
357
|
+
template<class Module>
|
|
358
|
+
class Pins;
|
|
359
|
+
|
|
360
|
+
template<class Module>
|
|
361
|
+
class AllocationLedger;
|
|
362
|
+
|
|
363
|
+
template<class T, class Module>
|
|
364
|
+
class Admitted {
|
|
365
|
+
friend class Pins<Module>;
|
|
366
|
+
|
|
367
|
+
public:
|
|
368
|
+
using element_type = T;
|
|
369
|
+
|
|
370
|
+
~Admitted() noexcept;
|
|
371
|
+
Admitted(const Admitted&) = delete;
|
|
372
|
+
Admitted& operator=(const Admitted&) = delete;
|
|
373
|
+
Admitted(Admitted&& other) noexcept
|
|
374
|
+
: pointer_(other.pointer_), count_(other.count_), authorized_(other.authorized_), lease_(other.lease_) {
|
|
375
|
+
other.pointer_ = nullptr;
|
|
376
|
+
other.count_ = 0;
|
|
377
|
+
other.authorized_ = false;
|
|
378
|
+
other.lease_ = nullptr;
|
|
379
|
+
}
|
|
380
|
+
Admitted& operator=(Admitted&& other) noexcept {
|
|
381
|
+
if (this == &other) return *this;
|
|
382
|
+
releasePin();
|
|
383
|
+
pointer_ = other.pointer_;
|
|
384
|
+
count_ = other.count_;
|
|
385
|
+
authorized_ = other.authorized_;
|
|
386
|
+
lease_ = other.lease_;
|
|
387
|
+
other.pointer_ = nullptr;
|
|
388
|
+
other.count_ = 0;
|
|
389
|
+
other.authorized_ = false;
|
|
390
|
+
other.lease_ = nullptr;
|
|
391
|
+
return *this;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
T* data() const noexcept { return pointer_; }
|
|
395
|
+
std::size_t size() const noexcept { return count_; }
|
|
396
|
+
std::size_t size_bytes() const noexcept { return count_ * sizeof(T); }
|
|
397
|
+
explicit operator bool() const noexcept { return authorized_; }
|
|
398
|
+
std::span<T> span() const noexcept { return std::span<T>{pointer_, count_}; }
|
|
399
|
+
|
|
400
|
+
private:
|
|
401
|
+
Admitted(T* pointer, std::size_t count, bool authorized, LeaseHeader* lease = nullptr) noexcept
|
|
402
|
+
: pointer_(pointer), count_(count), authorized_(authorized), lease_(lease) {}
|
|
403
|
+
void releasePin() noexcept;
|
|
404
|
+
|
|
405
|
+
T* pointer_ = nullptr;
|
|
406
|
+
std::size_t count_ = 0;
|
|
407
|
+
bool authorized_ = false;
|
|
408
|
+
LeaseHeader* lease_ = nullptr;
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
template<class Module>
|
|
412
|
+
class InstanceLedger {
|
|
413
|
+
template<class>
|
|
414
|
+
friend class ScratchLeases;
|
|
415
|
+
template<class>
|
|
416
|
+
friend class AllocationLedger;
|
|
417
|
+
|
|
418
|
+
struct alignas(std::max_align_t) AllocationHeader {
|
|
419
|
+
std::uint64_t magic = 0;
|
|
420
|
+
void* raw = nullptr;
|
|
421
|
+
AllocationLedger<Module>* child = nullptr;
|
|
422
|
+
std::uint64_t chargedBytes = 0;
|
|
423
|
+
std::size_t requestedBytes = 0;
|
|
424
|
+
std::size_t requestedAlignment = 0;
|
|
425
|
+
LedgerClass category = LedgerClass::Count;
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
static constexpr std::uint64_t kAllocationMagic = 0x4b494e455449584dULL;
|
|
429
|
+
|
|
430
|
+
public:
|
|
431
|
+
static constexpr std::uint64_t kRetainedCeilingBytes = Module::kRetainedCeilingBytes;
|
|
432
|
+
|
|
433
|
+
static InstanceLedger& instance() noexcept {
|
|
434
|
+
static InstanceLedger ledger;
|
|
435
|
+
return ledger;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
[[nodiscard]] bool tryClaim(LedgerClass category, std::uint64_t bytes) noexcept {
|
|
439
|
+
SpinLockGuard lock(mutex_);
|
|
440
|
+
if (imbalanced_) return false;
|
|
441
|
+
const auto index = classIndex(category);
|
|
442
|
+
if (index >= kLedgerClassCount) {
|
|
443
|
+
imbalanced_ = true;
|
|
444
|
+
return false;
|
|
445
|
+
}
|
|
446
|
+
if (bytes > std::numeric_limits<std::uint64_t>::max() - bytes_[index]) {
|
|
447
|
+
imbalanced_ = true;
|
|
448
|
+
return false;
|
|
449
|
+
}
|
|
450
|
+
const auto nextClassBytes = bytes_[index] + bytes;
|
|
451
|
+
if (category == LedgerClass::Scratch) {
|
|
452
|
+
bytes_[index] = nextClassBytes;
|
|
453
|
+
raiseHighWaterLocked(index);
|
|
454
|
+
scratchHighWater_ = std::max(scratchHighWater_, nextClassBytes);
|
|
455
|
+
return true;
|
|
456
|
+
}
|
|
457
|
+
const auto ceiling = retainedCeilingLocked();
|
|
458
|
+
if (retainedBytes_ > ceiling || bytes > ceiling - retainedBytes_) return false;
|
|
459
|
+
bytes_[index] = nextClassBytes;
|
|
460
|
+
retainedBytes_ += bytes;
|
|
461
|
+
raiseHighWaterLocked(index);
|
|
462
|
+
retainedHighWater_ = std::max(retainedHighWater_, retainedBytes_);
|
|
463
|
+
return true;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
void release(LedgerClass category, std::uint64_t bytes) noexcept {
|
|
467
|
+
SpinLockGuard lock(mutex_);
|
|
468
|
+
const auto index = classIndex(category);
|
|
469
|
+
if (index >= kLedgerClassCount || bytes > bytes_[index]) {
|
|
470
|
+
imbalanced_ = true;
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
bytes_[index] -= bytes;
|
|
474
|
+
if (category != LedgerClass::Scratch) retainedBytes_ -= bytes;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
[[nodiscard]] void* tryAllocate(LedgerClass category, std::size_t bytes,
|
|
478
|
+
std::size_t alignment, bool zero = false) noexcept {
|
|
479
|
+
return tryAllocateInternal(category, bytes, alignment, zero, nullptr);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
void deallocate(LedgerClass expectedCategory, void* pointer, std::size_t expectedBytes,
|
|
483
|
+
std::size_t expectedAlignment) noexcept {
|
|
484
|
+
deallocateInternal(expectedCategory, pointer, expectedBytes, expectedAlignment, nullptr, false);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
[[nodiscard]] bool tryRegisterWorld() noexcept {
|
|
488
|
+
SpinLockGuard lock(mutex_);
|
|
489
|
+
if (imbalanced_) return false;
|
|
490
|
+
if (liveWorlds_ == std::numeric_limits<std::uint64_t>::max()) {
|
|
491
|
+
imbalanced_ = true;
|
|
492
|
+
return false;
|
|
493
|
+
}
|
|
494
|
+
++liveWorlds_;
|
|
495
|
+
return true;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
void unregisterWorld() noexcept {
|
|
499
|
+
SpinLockGuard lock(mutex_);
|
|
500
|
+
if (liveWorlds_ == 0) {
|
|
501
|
+
imbalanced_ = true;
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
--liveWorlds_;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
[[nodiscard]] bool tryClaimSlot() noexcept {
|
|
508
|
+
SpinLockGuard lock(mutex_);
|
|
509
|
+
if (imbalanced_) return false;
|
|
510
|
+
if (liveSlots_ == std::numeric_limits<std::uint64_t>::max()) {
|
|
511
|
+
imbalanced_ = true;
|
|
512
|
+
return false;
|
|
513
|
+
}
|
|
514
|
+
++liveSlots_;
|
|
515
|
+
highWaterSlots_ = std::max(highWaterSlots_, liveSlots_);
|
|
516
|
+
return true;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
void releaseSlot() noexcept {
|
|
520
|
+
SpinLockGuard lock(mutex_);
|
|
521
|
+
if (liveSlots_ == 0) {
|
|
522
|
+
imbalanced_ = true;
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
--liveSlots_;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
[[nodiscard]] bool resetHighWater() noexcept {
|
|
529
|
+
SpinLockGuard lock(mutex_);
|
|
530
|
+
if (imbalanced_ || liveWorlds_ != 0 || liveSlots_ != 0 || retainedBytes_ != 0) return false;
|
|
531
|
+
for (const auto bytes : bytes_) {
|
|
532
|
+
if (bytes != 0) return false;
|
|
533
|
+
}
|
|
534
|
+
highWaterBytes_.fill(0);
|
|
535
|
+
retainedHighWater_ = 0;
|
|
536
|
+
scratchHighWater_ = 0;
|
|
537
|
+
highWaterSlots_ = 0;
|
|
538
|
+
return true;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
[[nodiscard]] LedgerSnapshot read() const noexcept {
|
|
542
|
+
SpinLockGuard lock(mutex_);
|
|
543
|
+
LedgerSnapshot snapshot{};
|
|
544
|
+
snapshot.bytes = bytes_;
|
|
545
|
+
snapshot.highWaterBytes = highWaterBytes_;
|
|
546
|
+
snapshot.retainedBytes = retainedBytes_;
|
|
547
|
+
snapshot.retainedHighWater = retainedHighWater_;
|
|
548
|
+
snapshot.scratchBytes = bytes_[classIndex(LedgerClass::Scratch)];
|
|
549
|
+
snapshot.scratchHighWater = scratchHighWater_;
|
|
550
|
+
snapshot.liveWorlds = liveWorlds_;
|
|
551
|
+
snapshot.liveSlots = liveSlots_;
|
|
552
|
+
snapshot.highWaterSlots = highWaterSlots_;
|
|
553
|
+
snapshot.imbalanced = imbalanced_;
|
|
554
|
+
#if defined(__EMSCRIPTEN__)
|
|
555
|
+
snapshot.wasmMemoryBytes = static_cast<std::uint64_t>(emscripten_get_heap_size());
|
|
556
|
+
#endif
|
|
557
|
+
return snapshot;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
void noteImbalance() noexcept {
|
|
561
|
+
SpinLockGuard lock(mutex_);
|
|
562
|
+
imbalanced_ = true;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
#if defined(RUNDOT_PHYSICS_PHASE_TEST_HOOKS) || defined(RUNDOT_PHYSICS2D_PHASE_TEST_HOOKS)
|
|
566
|
+
void setTestCeilingBytes(std::uint64_t bytes) noexcept {
|
|
567
|
+
SpinLockGuard lock(mutex_);
|
|
568
|
+
effectiveCeilingBytes_ = std::min(bytes, kRetainedCeilingBytes);
|
|
569
|
+
}
|
|
570
|
+
void resetTestCeilingBytes() noexcept { setTestCeilingBytes(kRetainedCeilingBytes); }
|
|
571
|
+
#endif
|
|
572
|
+
|
|
573
|
+
#ifdef RUNDOT_PHYSICS_PHASE_TEST_HOOKS
|
|
574
|
+
static void setTestHooks(const LedgerTestHooks& hooks) noexcept { testHooks_ = hooks; }
|
|
575
|
+
static void clearTestHooks() noexcept { testHooks_ = LedgerTestHooks{}; }
|
|
576
|
+
#endif
|
|
577
|
+
|
|
578
|
+
private:
|
|
579
|
+
std::uint64_t retainedCeilingLocked() const noexcept {
|
|
580
|
+
#if defined(RUNDOT_PHYSICS_PHASE_TEST_HOOKS) || defined(RUNDOT_PHYSICS2D_PHASE_TEST_HOOKS)
|
|
581
|
+
return effectiveCeilingBytes_;
|
|
582
|
+
#else
|
|
583
|
+
return kRetainedCeilingBytes;
|
|
584
|
+
#endif
|
|
585
|
+
}
|
|
586
|
+
InstanceLedger() noexcept = default;
|
|
587
|
+
InstanceLedger(const InstanceLedger&) = delete;
|
|
588
|
+
InstanceLedger& operator=(const InstanceLedger&) = delete;
|
|
589
|
+
|
|
590
|
+
[[nodiscard]] void* tryAllocateInternal(LedgerClass category, std::size_t bytes,
|
|
591
|
+
std::size_t alignment, bool zero,
|
|
592
|
+
AllocationLedger<Module>* child) noexcept {
|
|
593
|
+
if (classIndex(category) >= kLedgerClassCount || !detail::isPowerOfTwo(alignment)) return nullptr;
|
|
594
|
+
const auto payloadBytes = bytes == 0 ? std::size_t{1} : bytes;
|
|
595
|
+
const auto effectiveAlignment = std::max(alignment, alignof(AllocationHeader));
|
|
596
|
+
std::size_t totalBytes = sizeof(AllocationHeader);
|
|
597
|
+
if (detail::addOverflow(totalBytes, effectiveAlignment - 1, totalBytes)
|
|
598
|
+
|| detail::addOverflow(totalBytes, payloadBytes, totalBytes)) return nullptr;
|
|
599
|
+
if (totalBytes > std::numeric_limits<std::uint64_t>::max()) return nullptr;
|
|
600
|
+
const auto charge = static_cast<std::uint64_t>(totalBytes);
|
|
601
|
+
if (!tryClaim(category, charge)) return nullptr;
|
|
602
|
+
if (child != nullptr && !child->claimLocal(charge)) {
|
|
603
|
+
noteImbalance();
|
|
604
|
+
release(category, charge);
|
|
605
|
+
return nullptr;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
void* raw = allocateRaw(totalBytes);
|
|
609
|
+
if (raw == nullptr) {
|
|
610
|
+
if (child != nullptr) child->releaseLocal(charge);
|
|
611
|
+
release(category, charge);
|
|
612
|
+
return nullptr;
|
|
613
|
+
}
|
|
614
|
+
std::uintptr_t payloadAddress = 0;
|
|
615
|
+
if (!alignedPayloadAddress(raw, sizeof(AllocationHeader), effectiveAlignment, payloadAddress)) {
|
|
616
|
+
freeRaw(raw);
|
|
617
|
+
if (child != nullptr) child->releaseLocal(charge);
|
|
618
|
+
release(category, charge);
|
|
619
|
+
return nullptr;
|
|
620
|
+
}
|
|
621
|
+
auto* header = reinterpret_cast<AllocationHeader*>(payloadAddress - sizeof(AllocationHeader));
|
|
622
|
+
::new (static_cast<void*>(header)) AllocationHeader{
|
|
623
|
+
kAllocationMagic, raw, child, charge, bytes, alignment, category,
|
|
624
|
+
};
|
|
625
|
+
auto* payload = reinterpret_cast<void*>(payloadAddress);
|
|
626
|
+
if (zero) std::memset(payload, 0, payloadBytes);
|
|
627
|
+
return payload;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
void deallocateInternal(LedgerClass expectedCategory, void* pointer, std::size_t expectedBytes,
|
|
631
|
+
std::size_t expectedAlignment, AllocationLedger<Module>* expectedChild,
|
|
632
|
+
bool checkChild) noexcept {
|
|
633
|
+
if (pointer == nullptr) return;
|
|
634
|
+
auto* header = reinterpret_cast<AllocationHeader*>(
|
|
635
|
+
static_cast<std::uintptr_t>(reinterpret_cast<std::uintptr_t>(pointer) - sizeof(AllocationHeader)));
|
|
636
|
+
if (header->magic != kAllocationMagic || header->raw == nullptr) {
|
|
637
|
+
noteImbalance();
|
|
638
|
+
return;
|
|
639
|
+
}
|
|
640
|
+
const auto raw = header->raw;
|
|
641
|
+
const auto chargedBytes = header->chargedBytes;
|
|
642
|
+
const auto actualBytes = header->requestedBytes;
|
|
643
|
+
const auto actualAlignment = header->requestedAlignment;
|
|
644
|
+
const auto actualCategory = header->category;
|
|
645
|
+
auto* actualChild = header->child;
|
|
646
|
+
header->magic = 0;
|
|
647
|
+
if (expectedCategory != actualCategory || expectedBytes != actualBytes
|
|
648
|
+
|| expectedAlignment != actualAlignment || (checkChild && expectedChild != actualChild)) noteImbalance();
|
|
649
|
+
freeRaw(raw);
|
|
650
|
+
release(actualCategory, chargedBytes);
|
|
651
|
+
if (actualChild != nullptr) actualChild->releaseLocal(chargedBytes);
|
|
652
|
+
}
|
|
653
|
+
static constexpr std::size_t classIndex(LedgerClass category) noexcept {
|
|
654
|
+
return static_cast<std::size_t>(category);
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
static bool alignedPayloadAddress(void* raw, std::size_t prefix, std::size_t alignment,
|
|
658
|
+
std::uintptr_t& payloadAddress) noexcept {
|
|
659
|
+
const auto base = reinterpret_cast<std::uintptr_t>(raw);
|
|
660
|
+
const auto maxAddress = std::numeric_limits<std::uintptr_t>::max();
|
|
661
|
+
if (base > maxAddress - prefix || base + prefix > maxAddress - (alignment - 1)) return false;
|
|
662
|
+
const auto first = base + prefix;
|
|
663
|
+
payloadAddress = (first + (alignment - 1)) & ~static_cast<std::uintptr_t>(alignment - 1);
|
|
664
|
+
return payloadAddress >= first;
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
static void* allocateRaw(std::size_t bytes) noexcept {
|
|
668
|
+
#ifdef RUNDOT_PHYSICS_PHASE_TEST_HOOKS
|
|
669
|
+
if (testHooks_.allocationAttempt != nullptr) testHooks_.allocationAttempt(bytes);
|
|
670
|
+
if (testHooks_.failAllocation != nullptr && testHooks_.failAllocation(bytes)) return nullptr;
|
|
671
|
+
#endif
|
|
672
|
+
return std::malloc(bytes);
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
static void freeRaw(void* pointer) noexcept {
|
|
676
|
+
#ifdef RUNDOT_PHYSICS_PHASE_TEST_HOOKS
|
|
677
|
+
if (testHooks_.beforePhysicalFree != nullptr) testHooks_.beforePhysicalFree(pointer);
|
|
678
|
+
#endif
|
|
679
|
+
std::free(pointer);
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
void raiseHighWaterLocked(std::size_t index) noexcept {
|
|
683
|
+
highWaterBytes_[index] = std::max(highWaterBytes_[index], bytes_[index]);
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
void* allocateRawForLease(std::size_t bytes) noexcept { return allocateRaw(bytes); }
|
|
687
|
+
void freeRawForLease(void* pointer) noexcept { freeRaw(pointer); }
|
|
688
|
+
|
|
689
|
+
mutable SpinMutex mutex_{};
|
|
690
|
+
std::array<std::uint64_t, kLedgerClassCount> bytes_{};
|
|
691
|
+
std::array<std::uint64_t, kLedgerClassCount> highWaterBytes_{};
|
|
692
|
+
std::uint64_t retainedBytes_ = 0;
|
|
693
|
+
std::uint64_t retainedHighWater_ = 0;
|
|
694
|
+
std::uint64_t scratchHighWater_ = 0;
|
|
695
|
+
std::uint64_t liveWorlds_ = 0;
|
|
696
|
+
std::uint64_t liveSlots_ = 0;
|
|
697
|
+
std::uint64_t highWaterSlots_ = 0;
|
|
698
|
+
bool imbalanced_ = false;
|
|
699
|
+
#if defined(RUNDOT_PHYSICS_PHASE_TEST_HOOKS) || defined(RUNDOT_PHYSICS2D_PHASE_TEST_HOOKS)
|
|
700
|
+
std::uint64_t effectiveCeilingBytes_ = kRetainedCeilingBytes;
|
|
701
|
+
#endif
|
|
702
|
+
#ifdef RUNDOT_PHYSICS_PHASE_TEST_HOOKS
|
|
703
|
+
static inline LedgerTestHooks testHooks_{};
|
|
704
|
+
#endif
|
|
705
|
+
};
|
|
706
|
+
|
|
707
|
+
template<class Module>
|
|
708
|
+
class AllocationLedger {
|
|
709
|
+
friend class InstanceLedger<Module>;
|
|
710
|
+
|
|
711
|
+
public:
|
|
712
|
+
explicit AllocationLedger(LedgerClass category,
|
|
713
|
+
InstanceLedger<Module>& parent = InstanceLedger<Module>::instance()) noexcept
|
|
714
|
+
: parent_(&parent), category_(category) {}
|
|
715
|
+
AllocationLedger(const AllocationLedger&) = delete;
|
|
716
|
+
AllocationLedger& operator=(const AllocationLedger&) = delete;
|
|
717
|
+
|
|
718
|
+
[[nodiscard]] void* tryAllocate(std::size_t bytes, std::size_t alignment) noexcept {
|
|
719
|
+
return parent_->tryAllocateInternal(category_, bytes, alignment, false, this);
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
void deallocate(void* pointer, std::size_t bytes, std::size_t alignment) noexcept {
|
|
723
|
+
parent_->deallocateInternal(category_, pointer, bytes, alignment, this, true);
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
[[nodiscard]] std::uint64_t liveBytes() const noexcept {
|
|
727
|
+
SpinLockGuard lock(mutex_);
|
|
728
|
+
return liveBytes_;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
[[nodiscard]] std::uint64_t highWaterBytes() const noexcept {
|
|
732
|
+
SpinLockGuard lock(mutex_);
|
|
733
|
+
return highWaterBytes_;
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
private:
|
|
737
|
+
[[nodiscard]] bool claimLocal(std::uint64_t bytes) noexcept {
|
|
738
|
+
SpinLockGuard lock(mutex_);
|
|
739
|
+
if (imbalanced_ || bytes > std::numeric_limits<std::uint64_t>::max() - liveBytes_) {
|
|
740
|
+
imbalanced_ = true;
|
|
741
|
+
return false;
|
|
742
|
+
}
|
|
743
|
+
liveBytes_ += bytes;
|
|
744
|
+
highWaterBytes_ = std::max(highWaterBytes_, liveBytes_);
|
|
745
|
+
return true;
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
void releaseLocal(std::uint64_t bytes) noexcept {
|
|
749
|
+
bool underflow = false;
|
|
750
|
+
{
|
|
751
|
+
SpinLockGuard lock(mutex_);
|
|
752
|
+
if (bytes > liveBytes_) {
|
|
753
|
+
imbalanced_ = true;
|
|
754
|
+
underflow = true;
|
|
755
|
+
} else liveBytes_ -= bytes;
|
|
756
|
+
}
|
|
757
|
+
if (underflow) parent_->noteImbalance();
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
InstanceLedger<Module>* parent_ = nullptr;
|
|
761
|
+
LedgerClass category_ = LedgerClass::Count;
|
|
762
|
+
mutable SpinMutex mutex_{};
|
|
763
|
+
std::uint64_t liveBytes_ = 0;
|
|
764
|
+
std::uint64_t highWaterBytes_ = 0;
|
|
765
|
+
bool imbalanced_ = false;
|
|
766
|
+
};
|
|
767
|
+
|
|
768
|
+
template<class T, LedgerClass Category, class Module>
|
|
769
|
+
class LedgerAllocator {
|
|
770
|
+
template<class, LedgerClass, class>
|
|
771
|
+
friend class LedgerAllocator;
|
|
772
|
+
|
|
773
|
+
static_assert(Category != LedgerClass::Scratch);
|
|
774
|
+
|
|
775
|
+
public:
|
|
776
|
+
using value_type = T;
|
|
777
|
+
using size_type = std::size_t;
|
|
778
|
+
using difference_type = std::ptrdiff_t;
|
|
779
|
+
|
|
780
|
+
LedgerAllocator() noexcept = default;
|
|
781
|
+
template<class U>
|
|
782
|
+
LedgerAllocator(const LedgerAllocator<U, Category, Module>&) noexcept {}
|
|
783
|
+
|
|
784
|
+
[[nodiscard]] T* allocate(std::size_t count) {
|
|
785
|
+
if (count > std::numeric_limits<std::size_t>::max() / sizeof(T)) allocationFailure();
|
|
786
|
+
const auto bytes = count * sizeof(T);
|
|
787
|
+
auto* result = static_cast<T*>(InstanceLedger<Module>::instance().tryAllocate(
|
|
788
|
+
Category, bytes, alignof(T)));
|
|
789
|
+
if (result == nullptr) allocationFailure();
|
|
790
|
+
return result;
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
void deallocate(T* pointer, std::size_t count) noexcept {
|
|
794
|
+
if (pointer == nullptr) return;
|
|
795
|
+
if (count > std::numeric_limits<std::size_t>::max() / sizeof(T)) {
|
|
796
|
+
InstanceLedger<Module>::instance().deallocate(Category, pointer, 0, alignof(T));
|
|
797
|
+
return;
|
|
798
|
+
}
|
|
799
|
+
InstanceLedger<Module>::instance().deallocate(Category, pointer, count * sizeof(T), alignof(T));
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
template<class U>
|
|
803
|
+
struct rebind { using other = LedgerAllocator<U, Category, Module>; };
|
|
804
|
+
|
|
805
|
+
template<class U>
|
|
806
|
+
bool operator==(const LedgerAllocator<U, Category, Module>&) const noexcept { return true; }
|
|
807
|
+
|
|
808
|
+
template<class U>
|
|
809
|
+
bool operator!=(const LedgerAllocator<U, Category, Module>&) const noexcept { return false; }
|
|
810
|
+
|
|
811
|
+
private:
|
|
812
|
+
[[noreturn]] static void allocationFailure() {
|
|
813
|
+
#if defined(__cpp_exceptions)
|
|
814
|
+
throw std::bad_alloc{};
|
|
815
|
+
#else
|
|
816
|
+
std::abort();
|
|
817
|
+
#endif
|
|
818
|
+
}
|
|
819
|
+
};
|
|
820
|
+
|
|
821
|
+
template<class Module>
|
|
822
|
+
class Pins {
|
|
823
|
+
friend class ScratchLeases<Module>;
|
|
824
|
+
|
|
825
|
+
struct Record {
|
|
826
|
+
LeaseHeader* lease = nullptr;
|
|
827
|
+
const void* data = nullptr;
|
|
828
|
+
std::size_t bytes = 0;
|
|
829
|
+
RangeRole role = RangeRole::Input;
|
|
830
|
+
};
|
|
831
|
+
|
|
832
|
+
public:
|
|
833
|
+
Pins() noexcept = default;
|
|
834
|
+
~Pins() noexcept;
|
|
835
|
+
Pins(const Pins&) = delete;
|
|
836
|
+
Pins& operator=(const Pins&) = delete;
|
|
837
|
+
Pins(Pins&&) = delete;
|
|
838
|
+
Pins& operator=(Pins&&) = delete;
|
|
839
|
+
|
|
840
|
+
[[nodiscard]] bool active() const noexcept { return active_; }
|
|
841
|
+
[[nodiscard]] std::size_t size() const noexcept { return count_; }
|
|
842
|
+
|
|
843
|
+
template<class T>
|
|
844
|
+
[[nodiscard]] Admitted<T, Module> view(std::size_t index) const noexcept {
|
|
845
|
+
static_assert(!std::is_void_v<T>);
|
|
846
|
+
if (!active_ || index >= count_) return Admitted<T, Module>{nullptr, 0, false};
|
|
847
|
+
const auto& record = records_[index];
|
|
848
|
+
if (record.role == RangeRole::Input && !std::is_const_v<T>) {
|
|
849
|
+
return Admitted<T, Module>{nullptr, 0, false};
|
|
850
|
+
}
|
|
851
|
+
if (record.role == RangeRole::Output && std::is_const_v<T>) {
|
|
852
|
+
return Admitted<T, Module>{nullptr, 0, false};
|
|
853
|
+
}
|
|
854
|
+
const auto address = reinterpret_cast<std::uintptr_t>(record.data);
|
|
855
|
+
if (record.bytes != 0 && address % alignof(T) != 0) {
|
|
856
|
+
return Admitted<T, Module>{nullptr, 0, false};
|
|
857
|
+
}
|
|
858
|
+
if (record.bytes % sizeof(T) != 0) {
|
|
859
|
+
return Admitted<T, Module>{nullptr, 0, false};
|
|
860
|
+
}
|
|
861
|
+
if (record.lease != nullptr && !ScratchLeases<Module>::instance().pinView(record.lease)) {
|
|
862
|
+
return Admitted<T, Module>{nullptr, 0, false};
|
|
863
|
+
}
|
|
864
|
+
return Admitted<T, Module>{static_cast<T*>(const_cast<void*>(record.data)),
|
|
865
|
+
record.bytes / sizeof(T), true, record.lease};
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
private:
|
|
869
|
+
void releasePins() noexcept;
|
|
870
|
+
|
|
871
|
+
std::array<Record, kMaxCallerRanges> records_{};
|
|
872
|
+
std::size_t count_ = 0;
|
|
873
|
+
bool active_ = false;
|
|
874
|
+
};
|
|
875
|
+
|
|
876
|
+
template<class Module>
|
|
877
|
+
class ScratchLeases {
|
|
878
|
+
friend class Pins<Module>;
|
|
879
|
+
template<class, class>
|
|
880
|
+
friend class Admitted;
|
|
881
|
+
|
|
882
|
+
public:
|
|
883
|
+
#ifdef RUNDOT_PHYSICS_PHASE_TEST_HOOKS
|
|
884
|
+
struct TestIndexStats {
|
|
885
|
+
std::size_t size = 0;
|
|
886
|
+
std::int32_t depth = 0;
|
|
887
|
+
};
|
|
888
|
+
#endif
|
|
889
|
+
|
|
890
|
+
static ScratchLeases& instance() noexcept {
|
|
891
|
+
static ScratchLeases leases;
|
|
892
|
+
return leases;
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
[[nodiscard]] void* acquire(std::uint32_t bytes) noexcept {
|
|
896
|
+
auto& ledger = InstanceLedger<Module>::instance();
|
|
897
|
+
constexpr auto alignment = std::max({std::size_t{16}, alignof(std::max_align_t), alignof(LeaseHeader)});
|
|
898
|
+
std::size_t totalBytes = sizeof(LeaseHeader);
|
|
899
|
+
if (detail::addOverflow(totalBytes, alignment - 1, totalBytes)
|
|
900
|
+
|| detail::addOverflow(totalBytes, static_cast<std::size_t>(bytes), totalBytes)
|
|
901
|
+
|| totalBytes > std::numeric_limits<std::uint64_t>::max()) return nullptr;
|
|
902
|
+
const auto chargedBytes = static_cast<std::uint64_t>(totalBytes);
|
|
903
|
+
if (!ledger.tryClaim(LedgerClass::Scratch, chargedBytes)) return nullptr;
|
|
904
|
+
|
|
905
|
+
void* raw = ledger.allocateRawForLease(totalBytes);
|
|
906
|
+
if (raw == nullptr) {
|
|
907
|
+
ledger.release(LedgerClass::Scratch, chargedBytes);
|
|
908
|
+
return nullptr;
|
|
909
|
+
}
|
|
910
|
+
std::uintptr_t payloadAddress = 0;
|
|
911
|
+
if (!InstanceLedger<Module>::alignedPayloadAddress(raw, sizeof(LeaseHeader), alignment, payloadAddress)
|
|
912
|
+
|| bytes > std::numeric_limits<std::uintptr_t>::max() - payloadAddress) {
|
|
913
|
+
ledger.freeRawForLease(raw);
|
|
914
|
+
ledger.release(LedgerClass::Scratch, chargedBytes);
|
|
915
|
+
return nullptr;
|
|
916
|
+
}
|
|
917
|
+
auto* header = ::new (raw) LeaseHeader{};
|
|
918
|
+
header->chargedBytes = chargedBytes;
|
|
919
|
+
header->live = true;
|
|
920
|
+
header->node.begin = payloadAddress;
|
|
921
|
+
header->node.end = payloadAddress + bytes;
|
|
922
|
+
header->node.owner = header;
|
|
923
|
+
|
|
924
|
+
bool inserted = false;
|
|
925
|
+
{
|
|
926
|
+
SpinLockGuard lock(mutex_);
|
|
927
|
+
inserted = index_.insert(header->node);
|
|
928
|
+
}
|
|
929
|
+
if (!inserted) {
|
|
930
|
+
header->live = false;
|
|
931
|
+
ledger.freeRawForLease(raw);
|
|
932
|
+
ledger.release(LedgerClass::Scratch, chargedBytes);
|
|
933
|
+
return nullptr;
|
|
934
|
+
}
|
|
935
|
+
return reinterpret_cast<void*>(payloadAddress);
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
[[nodiscard]] LeaseStatus release(void* payload) noexcept {
|
|
939
|
+
if (payload == nullptr) return LeaseStatus::UnknownLease;
|
|
940
|
+
const auto address = reinterpret_cast<std::uintptr_t>(payload);
|
|
941
|
+
LeaseHeader* header = nullptr;
|
|
942
|
+
{
|
|
943
|
+
SpinLockGuard lock(mutex_);
|
|
944
|
+
auto* node = index_.findByBegin(address);
|
|
945
|
+
if (node == nullptr || node->owner == nullptr || !node->owner->live) return LeaseStatus::UnknownLease;
|
|
946
|
+
header = node->owner;
|
|
947
|
+
if (header->pins != 0) return LeaseStatus::Busy;
|
|
948
|
+
if (!index_.erase(header->node)) {
|
|
949
|
+
header = nullptr;
|
|
950
|
+
} else header->live = false;
|
|
951
|
+
}
|
|
952
|
+
if (header == nullptr) {
|
|
953
|
+
InstanceLedger<Module>::instance().noteImbalance();
|
|
954
|
+
return LeaseStatus::UnknownLease;
|
|
955
|
+
}
|
|
956
|
+
const auto chargedBytes = header->chargedBytes;
|
|
957
|
+
auto& ledger = InstanceLedger<Module>::instance();
|
|
958
|
+
ledger.freeRawForLease(header);
|
|
959
|
+
ledger.release(LedgerClass::Scratch, chargedBytes);
|
|
960
|
+
return LeaseStatus::Ok;
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
[[nodiscard]] AdmitStatus admit(std::span<const CallerRange> ranges, Pins<Module>& pins) noexcept {
|
|
964
|
+
if (pins.active_) return AdmitStatus::Busy;
|
|
965
|
+
if (ranges.size() > kMaxCallerRanges) return AdmitStatus::TooManyRanges;
|
|
966
|
+
|
|
967
|
+
std::array<std::uintptr_t, kMaxCallerRanges> begins{};
|
|
968
|
+
std::array<std::uintptr_t, kMaxCallerRanges> ends{};
|
|
969
|
+
std::array<LeaseHeader*, kMaxCallerRanges> owners{};
|
|
970
|
+
for (std::size_t i = 0; i < ranges.size(); ++i) {
|
|
971
|
+
if (!detail::makeRange(ranges[i], begins[i], ends[i])) return AdmitStatus::InvalidRange;
|
|
972
|
+
if (ranges[i].bytes == 0) continue;
|
|
973
|
+
for (std::size_t j = 0; j < i; ++j) {
|
|
974
|
+
if (ranges[j].bytes == 0) continue;
|
|
975
|
+
const bool outputPair = ranges[i].role == RangeRole::Output
|
|
976
|
+
|| ranges[j].role == RangeRole::Output;
|
|
977
|
+
if (outputPair && detail::overlaps(begins[i], ends[i], begins[j], ends[j])) {
|
|
978
|
+
return AdmitStatus::Overlap;
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
{
|
|
984
|
+
SpinLockGuard lock(mutex_);
|
|
985
|
+
for (std::size_t i = 0; i < ranges.size(); ++i) {
|
|
986
|
+
if (ranges[i].bytes == 0) continue;
|
|
987
|
+
auto* node = index_.findContaining(begins[i]);
|
|
988
|
+
if (node == nullptr || ends[i] > node->end || node->owner == nullptr || !node->owner->live) {
|
|
989
|
+
return AdmitStatus::UnknownLease;
|
|
990
|
+
}
|
|
991
|
+
owners[i] = node->owner;
|
|
992
|
+
}
|
|
993
|
+
for (std::size_t i = 0; i < ranges.size(); ++i) {
|
|
994
|
+
if (owners[i] == nullptr) continue;
|
|
995
|
+
std::size_t additionalPins = 0;
|
|
996
|
+
for (std::size_t j = 0; j < ranges.size(); ++j) {
|
|
997
|
+
if (owners[j] == owners[i]) ++additionalPins;
|
|
998
|
+
}
|
|
999
|
+
if (additionalPins > std::numeric_limits<std::uint32_t>::max() - owners[i]->pins) {
|
|
1000
|
+
return AdmitStatus::PinOverflow;
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
for (std::size_t i = 0; i < ranges.size(); ++i) {
|
|
1004
|
+
if (owners[i] != nullptr) ++owners[i]->pins;
|
|
1005
|
+
}
|
|
1006
|
+
pins.count_ = ranges.size();
|
|
1007
|
+
for (std::size_t i = 0; i < ranges.size(); ++i) {
|
|
1008
|
+
pins.records_[i] = typename Pins<Module>::Record{
|
|
1009
|
+
owners[i], ranges[i].data, ranges[i].bytes, ranges[i].role,
|
|
1010
|
+
};
|
|
1011
|
+
}
|
|
1012
|
+
pins.active_ = true;
|
|
1013
|
+
}
|
|
1014
|
+
return AdmitStatus::Ok;
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
#ifdef RUNDOT_PHYSICS_PHASE_TEST_HOOKS
|
|
1018
|
+
[[nodiscard]] TestIndexStats testIndexStats() const noexcept {
|
|
1019
|
+
SpinLockGuard lock(mutex_);
|
|
1020
|
+
return TestIndexStats{index_.size(), index_.depth()};
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
[[nodiscard]] std::size_t testSearchVisits(const void* address) const noexcept {
|
|
1024
|
+
SpinLockGuard lock(mutex_);
|
|
1025
|
+
std::size_t visits = 0;
|
|
1026
|
+
(void)index_.findContaining(reinterpret_cast<std::uintptr_t>(address), &visits);
|
|
1027
|
+
return visits;
|
|
1028
|
+
}
|
|
1029
|
+
#endif
|
|
1030
|
+
|
|
1031
|
+
private:
|
|
1032
|
+
ScratchLeases() noexcept = default;
|
|
1033
|
+
ScratchLeases(const ScratchLeases&) = delete;
|
|
1034
|
+
ScratchLeases& operator=(const ScratchLeases&) = delete;
|
|
1035
|
+
|
|
1036
|
+
bool pinView(LeaseHeader* lease) noexcept {
|
|
1037
|
+
SpinLockGuard lock(mutex_);
|
|
1038
|
+
if (!lease->live || lease->pins == std::numeric_limits<std::uint32_t>::max()) return false;
|
|
1039
|
+
++lease->pins;
|
|
1040
|
+
return true;
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
void unpinView(LeaseHeader* lease) noexcept {
|
|
1044
|
+
bool inconsistent = false;
|
|
1045
|
+
{
|
|
1046
|
+
SpinLockGuard lock(mutex_);
|
|
1047
|
+
if (!lease->live || lease->pins == 0) inconsistent = true;
|
|
1048
|
+
else --lease->pins;
|
|
1049
|
+
}
|
|
1050
|
+
if (inconsistent) InstanceLedger<Module>::instance().noteImbalance();
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
void unpin(Pins<Module>& pins) noexcept {
|
|
1054
|
+
bool inconsistent = false;
|
|
1055
|
+
{
|
|
1056
|
+
SpinLockGuard lock(mutex_);
|
|
1057
|
+
for (std::size_t i = 0; i < pins.count_; ++i) {
|
|
1058
|
+
auto* lease = pins.records_[i].lease;
|
|
1059
|
+
if (lease == nullptr) continue;
|
|
1060
|
+
if (lease->pins == 0 || !lease->live) inconsistent = true;
|
|
1061
|
+
else --lease->pins;
|
|
1062
|
+
}
|
|
1063
|
+
pins.records_.fill(typename Pins<Module>::Record{});
|
|
1064
|
+
pins.count_ = 0;
|
|
1065
|
+
pins.active_ = false;
|
|
1066
|
+
}
|
|
1067
|
+
if (inconsistent) InstanceLedger<Module>::instance().noteImbalance();
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
mutable SpinMutex mutex_{};
|
|
1071
|
+
detail::RangeIndex index_{};
|
|
1072
|
+
};
|
|
1073
|
+
|
|
1074
|
+
template<class Module>
|
|
1075
|
+
Pins<Module>::~Pins() noexcept {
|
|
1076
|
+
releasePins();
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
template<class T, class Module>
|
|
1080
|
+
Admitted<T, Module>::~Admitted() noexcept {
|
|
1081
|
+
releasePin();
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
template<class T, class Module>
|
|
1085
|
+
void Admitted<T, Module>::releasePin() noexcept {
|
|
1086
|
+
if (lease_ != nullptr) ScratchLeases<Module>::instance().unpinView(lease_);
|
|
1087
|
+
lease_ = nullptr;
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
template<class Module>
|
|
1091
|
+
void Pins<Module>::releasePins() noexcept {
|
|
1092
|
+
if (active_) ScratchLeases<Module>::instance().unpin(*this);
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
} // namespace rundot::kinetix::memory
|