├── .gitignore ├── .vscode └── launch.json ├── CHANGELOG.md ├── Cargo.lock ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── examples └── graphics.rs └── src ├── graphics.rs └── lib.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "lldb", 9 | "request": "launch", 10 | "name": "Debug executable 'stupidalloc'", 11 | "cargo": { 12 | "args": [ 13 | "build", 14 | "--bin=stupidalloc", 15 | "--package=stupidalloc" 16 | ], 17 | "filter": { 18 | "name": "stupidalloc", 19 | "kind": "bin" 20 | } 21 | }, 22 | "args": [], 23 | "cwd": "${workspaceFolder}" 24 | }, 25 | { 26 | "type": "lldb", 27 | "request": "launch", 28 | "name": "Debug unit tests in executable 'stupidalloc'", 29 | "cargo": { 30 | "args": [ 31 | "test", 32 | "--no-run", 33 | "--bin=stupidalloc", 34 | "--package=stupidalloc" 35 | ], 36 | "filter": { 37 | "name": "stupidalloc", 38 | "kind": "bin" 39 | } 40 | }, 41 | "args": [], 42 | "cwd": "${workspaceFolder}" 43 | }, 44 | { 45 | "type": "lldb", 46 | "request": "launch", 47 | "name": "Debug example 'graphics'", 48 | "cargo": { 49 | "args": [ 50 | "build", 51 | "--example=graphics", 52 | "--package=stupidalloc", 53 | "--features=always-graphics" 54 | ], 55 | "filter": { 56 | "name": "graphics", 57 | "kind": "example" 58 | } 59 | }, 60 | "args": [], 61 | "cwd": "${workspaceFolder}", 62 | "env": { 63 | "RUST_BACKTRACE": "1" 64 | } 65 | } 66 | ] 67 | } -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 6 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 7 | 8 | ## [Unreleased] 9 | 10 | ## [0.2.2] - 2025-02-16 11 | 12 | ### Fixes 13 | - Fixed a new compiler error for pointer cast type coercion. 14 | 15 | ## [0.2.1] - 2023-12-29 16 | 17 | ### Added 18 | - `graphics` feature demo video 19 | 20 | ### Fixes 21 | - Fixed a bug where data allocated before main with an open window would make the program crash at the end because of normal process thread murder after main. 22 | - Fixed a typo in the changelog 23 | - Fixed a typo in the `graphics` example's error message when running without the appropriate feature. 24 | 25 | ## [0.2.0] - 2023-12-29 26 | 27 | ### Added 28 | 29 | - `logging` feature 30 | - Creates companion files with information about corresponding allocated memory 31 | - `graphics` feature 32 | - Allows user to create graphical windows to visually display and interact with allocated memory 33 | - `always-graphics` feature 34 | - Always create graphical window for every allocation 35 | - New function to disable stupid allocation in current thread 36 | - New example to showcase `graphics` feature 37 | 38 | ### Changed 39 | - Inner `HashMap` is now explicitely from [`hashbrown`](https://crates.io/crate/hashbrown) 40 | - Replaced inner map `Mutex` with `RwLock` 41 | - Behaviour of allocation fallback corrected for thread correctness 42 | 43 | ## [0.1.0] - 2023-07-08 44 | 45 | - `StupidAlloc` allocator 46 | - `interactive` feature, makes users confirm allocations and de-allocation using dialog boxes 47 | 48 | [Unreleased]: https://github.com/shadyfennec/stupidalloc/compare/v0.2.1..HEAD 49 | [0.2.1]: https://github.com/shadyfennec/stupidalloc/compare/v0.2.0..v0.2.1 50 | [0.2.0]: https://github.com/shadyfennec/stupidalloc/compare/v0.1.0..v0.2.0 51 | [0.1.0]: https://github.com/shadyfennec/stupidalloc/releases/tag/v0.1.0 -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 4 4 | 5 | [[package]] 6 | name = "ahash" 7 | version = "0.8.10" 8 | source = "git+https://github.com/tkaitchuck/aHash?tag=v0.8.11#7778357cf9a684b06aaada11788ac1cd796dc5b8" 9 | dependencies = [ 10 | "cfg-if", 11 | "once_cell", 12 | "version_check", 13 | "zerocopy", 14 | ] 15 | 16 | [[package]] 17 | name = "allocator-api2" 18 | version = "0.2.16" 19 | source = "registry+https://github.com/rust-lang/crates.io-index" 20 | checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5" 21 | 22 | [[package]] 23 | name = "ascii" 24 | version = "1.1.0" 25 | source = "registry+https://github.com/rust-lang/crates.io-index" 26 | checksum = "d92bec98840b8f03a5ff5413de5293bfcd8bf96467cf5452609f939ec6f5de16" 27 | 28 | [[package]] 29 | name = "autocfg" 30 | version = "1.1.0" 31 | source = "registry+https://github.com/rust-lang/crates.io-index" 32 | checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" 33 | 34 | [[package]] 35 | name = "bitflags" 36 | version = "1.3.2" 37 | source = "registry+https://github.com/rust-lang/crates.io-index" 38 | checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" 39 | 40 | [[package]] 41 | name = "bitflags" 42 | version = "2.5.0" 43 | source = "registry+https://github.com/rust-lang/crates.io-index" 44 | checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" 45 | 46 | [[package]] 47 | name = "block" 48 | version = "0.1.6" 49 | source = "registry+https://github.com/rust-lang/crates.io-index" 50 | checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a" 51 | 52 | [[package]] 53 | name = "bumpalo" 54 | version = "3.15.4" 55 | source = "registry+https://github.com/rust-lang/crates.io-index" 56 | checksum = "7ff69b9dd49fd426c69a0db9fc04dd934cdb6645ff000864d98f7e2af8830eaa" 57 | 58 | [[package]] 59 | name = "cc" 60 | version = "1.0.90" 61 | source = "registry+https://github.com/rust-lang/crates.io-index" 62 | checksum = "8cd6604a82acf3039f1144f54b8eb34e91ffba622051189e71b781822d5ee1f5" 63 | 64 | [[package]] 65 | name = "cfg-if" 66 | version = "1.0.0" 67 | source = "registry+https://github.com/rust-lang/crates.io-index" 68 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 69 | 70 | [[package]] 71 | name = "cocoa" 72 | version = "0.24.1" 73 | source = "registry+https://github.com/rust-lang/crates.io-index" 74 | checksum = "f425db7937052c684daec3bd6375c8abe2d146dca4b8b143d6db777c39138f3a" 75 | dependencies = [ 76 | "bitflags 1.3.2", 77 | "block", 78 | "cocoa-foundation", 79 | "core-foundation", 80 | "core-graphics", 81 | "foreign-types", 82 | "libc", 83 | "objc", 84 | ] 85 | 86 | [[package]] 87 | name = "cocoa-foundation" 88 | version = "0.1.2" 89 | source = "registry+https://github.com/rust-lang/crates.io-index" 90 | checksum = "8c6234cbb2e4c785b456c0644748b1ac416dd045799740356f8363dfe00c93f7" 91 | dependencies = [ 92 | "bitflags 1.3.2", 93 | "block", 94 | "core-foundation", 95 | "core-graphics-types", 96 | "libc", 97 | "objc", 98 | ] 99 | 100 | [[package]] 101 | name = "core-foundation" 102 | version = "0.9.4" 103 | source = "registry+https://github.com/rust-lang/crates.io-index" 104 | checksum = "91e195e091a93c46f7102ec7818a2aa394e1e1771c3ab4825963fa03e45afb8f" 105 | dependencies = [ 106 | "core-foundation-sys", 107 | "libc", 108 | ] 109 | 110 | [[package]] 111 | name = "core-foundation-sys" 112 | version = "0.8.6" 113 | source = "registry+https://github.com/rust-lang/crates.io-index" 114 | checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" 115 | 116 | [[package]] 117 | name = "core-graphics" 118 | version = "0.22.3" 119 | source = "registry+https://github.com/rust-lang/crates.io-index" 120 | checksum = "2581bbab3b8ffc6fcbd550bf46c355135d16e9ff2a6ea032ad6b9bf1d7efe4fb" 121 | dependencies = [ 122 | "bitflags 1.3.2", 123 | "core-foundation", 124 | "core-graphics-types", 125 | "foreign-types", 126 | "libc", 127 | ] 128 | 129 | [[package]] 130 | name = "core-graphics-types" 131 | version = "0.1.3" 132 | source = "registry+https://github.com/rust-lang/crates.io-index" 133 | checksum = "45390e6114f68f718cc7a830514a96f903cccd70d02a8f6d9f643ac4ba45afaf" 134 | dependencies = [ 135 | "bitflags 1.3.2", 136 | "core-foundation", 137 | "libc", 138 | ] 139 | 140 | [[package]] 141 | name = "cty" 142 | version = "0.2.2" 143 | source = "registry+https://github.com/rust-lang/crates.io-index" 144 | checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35" 145 | 146 | [[package]] 147 | name = "dirs-next" 148 | version = "2.0.0" 149 | source = "registry+https://github.com/rust-lang/crates.io-index" 150 | checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1" 151 | dependencies = [ 152 | "cfg-if", 153 | "dirs-sys-next", 154 | ] 155 | 156 | [[package]] 157 | name = "dirs-sys-next" 158 | version = "0.1.2" 159 | source = "registry+https://github.com/rust-lang/crates.io-index" 160 | checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d" 161 | dependencies = [ 162 | "libc", 163 | "redox_users", 164 | "winapi", 165 | ] 166 | 167 | [[package]] 168 | name = "dlib" 169 | version = "0.5.2" 170 | source = "registry+https://github.com/rust-lang/crates.io-index" 171 | checksum = "330c60081dcc4c72131f8eb70510f1ac07223e5d4163db481a04a0befcffa412" 172 | dependencies = [ 173 | "libloading", 174 | ] 175 | 176 | [[package]] 177 | name = "downcast-rs" 178 | version = "1.2.0" 179 | source = "registry+https://github.com/rust-lang/crates.io-index" 180 | checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" 181 | 182 | [[package]] 183 | name = "either" 184 | version = "1.10.0" 185 | source = "registry+https://github.com/rust-lang/crates.io-index" 186 | checksum = "11157ac094ffbdde99aa67b23417ebdd801842852b500e395a45a9c0aac03e4a" 187 | 188 | [[package]] 189 | name = "errno" 190 | version = "0.3.8" 191 | source = "registry+https://github.com/rust-lang/crates.io-index" 192 | checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" 193 | dependencies = [ 194 | "libc", 195 | "windows-sys", 196 | ] 197 | 198 | [[package]] 199 | name = "fastrand" 200 | version = "2.0.1" 201 | source = "registry+https://github.com/rust-lang/crates.io-index" 202 | checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" 203 | 204 | [[package]] 205 | name = "foreign-types" 206 | version = "0.3.2" 207 | source = "registry+https://github.com/rust-lang/crates.io-index" 208 | checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" 209 | dependencies = [ 210 | "foreign-types-shared", 211 | ] 212 | 213 | [[package]] 214 | name = "foreign-types-shared" 215 | version = "0.1.1" 216 | source = "registry+https://github.com/rust-lang/crates.io-index" 217 | checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" 218 | 219 | [[package]] 220 | name = "futures" 221 | version = "0.3.30" 222 | source = "registry+https://github.com/rust-lang/crates.io-index" 223 | checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0" 224 | dependencies = [ 225 | "futures-channel", 226 | "futures-core", 227 | "futures-executor", 228 | "futures-io", 229 | "futures-sink", 230 | "futures-task", 231 | "futures-util", 232 | ] 233 | 234 | [[package]] 235 | name = "futures-channel" 236 | version = "0.3.30" 237 | source = "registry+https://github.com/rust-lang/crates.io-index" 238 | checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" 239 | dependencies = [ 240 | "futures-core", 241 | "futures-sink", 242 | ] 243 | 244 | [[package]] 245 | name = "futures-core" 246 | version = "0.3.30" 247 | source = "registry+https://github.com/rust-lang/crates.io-index" 248 | checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" 249 | 250 | [[package]] 251 | name = "futures-executor" 252 | version = "0.3.30" 253 | source = "registry+https://github.com/rust-lang/crates.io-index" 254 | checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d" 255 | dependencies = [ 256 | "futures-core", 257 | "futures-task", 258 | "futures-util", 259 | ] 260 | 261 | [[package]] 262 | name = "futures-io" 263 | version = "0.3.30" 264 | source = "registry+https://github.com/rust-lang/crates.io-index" 265 | checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" 266 | 267 | [[package]] 268 | name = "futures-macro" 269 | version = "0.3.30" 270 | source = "registry+https://github.com/rust-lang/crates.io-index" 271 | checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" 272 | dependencies = [ 273 | "proc-macro2", 274 | "quote", 275 | "syn", 276 | ] 277 | 278 | [[package]] 279 | name = "futures-sink" 280 | version = "0.3.30" 281 | source = "registry+https://github.com/rust-lang/crates.io-index" 282 | checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5" 283 | 284 | [[package]] 285 | name = "futures-task" 286 | version = "0.3.30" 287 | source = "registry+https://github.com/rust-lang/crates.io-index" 288 | checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" 289 | 290 | [[package]] 291 | name = "futures-util" 292 | version = "0.3.30" 293 | source = "registry+https://github.com/rust-lang/crates.io-index" 294 | checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" 295 | dependencies = [ 296 | "futures-channel", 297 | "futures-core", 298 | "futures-io", 299 | "futures-macro", 300 | "futures-sink", 301 | "futures-task", 302 | "memchr", 303 | "pin-project-lite", 304 | "pin-utils", 305 | "slab", 306 | ] 307 | 308 | [[package]] 309 | name = "getrandom" 310 | version = "0.2.12" 311 | source = "registry+https://github.com/rust-lang/crates.io-index" 312 | checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" 313 | dependencies = [ 314 | "cfg-if", 315 | "libc", 316 | "wasi", 317 | ] 318 | 319 | [[package]] 320 | name = "hashbrown" 321 | version = "0.14.3" 322 | source = "registry+https://github.com/rust-lang/crates.io-index" 323 | checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" 324 | dependencies = [ 325 | "ahash", 326 | "allocator-api2", 327 | ] 328 | 329 | [[package]] 330 | name = "home" 331 | version = "0.5.9" 332 | source = "registry+https://github.com/rust-lang/crates.io-index" 333 | checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5" 334 | dependencies = [ 335 | "windows-sys", 336 | ] 337 | 338 | [[package]] 339 | name = "instant" 340 | version = "0.1.12" 341 | source = "registry+https://github.com/rust-lang/crates.io-index" 342 | checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" 343 | dependencies = [ 344 | "cfg-if", 345 | "js-sys", 346 | "wasm-bindgen", 347 | "web-sys", 348 | ] 349 | 350 | [[package]] 351 | name = "js-sys" 352 | version = "0.3.69" 353 | source = "registry+https://github.com/rust-lang/crates.io-index" 354 | checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d" 355 | dependencies = [ 356 | "wasm-bindgen", 357 | ] 358 | 359 | [[package]] 360 | name = "lazy_static" 361 | version = "1.4.0" 362 | source = "registry+https://github.com/rust-lang/crates.io-index" 363 | checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" 364 | 365 | [[package]] 366 | name = "libc" 367 | version = "0.2.153" 368 | source = "registry+https://github.com/rust-lang/crates.io-index" 369 | checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" 370 | 371 | [[package]] 372 | name = "libloading" 373 | version = "0.8.3" 374 | source = "registry+https://github.com/rust-lang/crates.io-index" 375 | checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19" 376 | dependencies = [ 377 | "cfg-if", 378 | "windows-targets", 379 | ] 380 | 381 | [[package]] 382 | name = "libredox" 383 | version = "0.0.1" 384 | source = "registry+https://github.com/rust-lang/crates.io-index" 385 | checksum = "85c833ca1e66078851dba29046874e38f08b2c883700aa29a03ddd3b23814ee8" 386 | dependencies = [ 387 | "bitflags 2.5.0", 388 | "libc", 389 | "redox_syscall", 390 | ] 391 | 392 | [[package]] 393 | name = "libredox" 394 | version = "0.0.2" 395 | source = "registry+https://github.com/rust-lang/crates.io-index" 396 | checksum = "3af92c55d7d839293953fcd0fda5ecfe93297cfde6ffbdec13b41d99c0ba6607" 397 | dependencies = [ 398 | "bitflags 2.5.0", 399 | "libc", 400 | "redox_syscall", 401 | ] 402 | 403 | [[package]] 404 | name = "linux-raw-sys" 405 | version = "0.4.13" 406 | source = "registry+https://github.com/rust-lang/crates.io-index" 407 | checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" 408 | 409 | [[package]] 410 | name = "log" 411 | version = "0.4.21" 412 | source = "registry+https://github.com/rust-lang/crates.io-index" 413 | checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" 414 | 415 | [[package]] 416 | name = "malloc_buf" 417 | version = "0.0.6" 418 | source = "registry+https://github.com/rust-lang/crates.io-index" 419 | checksum = "62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb" 420 | dependencies = [ 421 | "libc", 422 | ] 423 | 424 | [[package]] 425 | name = "memchr" 426 | version = "2.7.1" 427 | source = "registry+https://github.com/rust-lang/crates.io-index" 428 | checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" 429 | 430 | [[package]] 431 | name = "memmap2" 432 | version = "0.7.1" 433 | source = "registry+https://github.com/rust-lang/crates.io-index" 434 | checksum = "f49388d20533534cd19360ad3d6a7dadc885944aa802ba3995040c5ec11288c6" 435 | dependencies = [ 436 | "libc", 437 | ] 438 | 439 | [[package]] 440 | name = "memoffset" 441 | version = "0.6.5" 442 | source = "registry+https://github.com/rust-lang/crates.io-index" 443 | checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce" 444 | dependencies = [ 445 | "autocfg", 446 | ] 447 | 448 | [[package]] 449 | name = "minifb" 450 | version = "0.25.0" 451 | source = "registry+https://github.com/rust-lang/crates.io-index" 452 | checksum = "05eddefadb505d3dcb66a89fa77dd0936e72ec84e891cc8fc36e3c05bfe61103" 453 | dependencies = [ 454 | "cc", 455 | "dlib", 456 | "futures", 457 | "instant", 458 | "js-sys", 459 | "lazy_static", 460 | "libc", 461 | "orbclient", 462 | "raw-window-handle", 463 | "serde", 464 | "serde_derive", 465 | "tempfile", 466 | "wasm-bindgen-futures", 467 | "wayland-client", 468 | "wayland-cursor", 469 | "wayland-protocols", 470 | "winapi", 471 | "x11-dl", 472 | ] 473 | 474 | [[package]] 475 | name = "native-dialog" 476 | version = "0.6.4" 477 | source = "registry+https://github.com/rust-lang/crates.io-index" 478 | checksum = "1bbf55edb2747e4e4b3a9cd3989194b88aae32274b4422635dcf98aa6e84197b" 479 | dependencies = [ 480 | "ascii", 481 | "block", 482 | "cocoa", 483 | "dirs-next", 484 | "objc", 485 | "objc-foundation", 486 | "objc_id", 487 | "once_cell", 488 | "raw-window-handle", 489 | "thiserror", 490 | "wfd", 491 | "which", 492 | "winapi", 493 | ] 494 | 495 | [[package]] 496 | name = "nix" 497 | version = "0.24.3" 498 | source = "registry+https://github.com/rust-lang/crates.io-index" 499 | checksum = "fa52e972a9a719cecb6864fb88568781eb706bac2cd1d4f04a648542dbf78069" 500 | dependencies = [ 501 | "bitflags 1.3.2", 502 | "cfg-if", 503 | "libc", 504 | "memoffset", 505 | ] 506 | 507 | [[package]] 508 | name = "objc" 509 | version = "0.2.7" 510 | source = "registry+https://github.com/rust-lang/crates.io-index" 511 | checksum = "915b1b472bc21c53464d6c8461c9d3af805ba1ef837e1cac254428f4a77177b1" 512 | dependencies = [ 513 | "malloc_buf", 514 | ] 515 | 516 | [[package]] 517 | name = "objc-foundation" 518 | version = "0.1.1" 519 | source = "registry+https://github.com/rust-lang/crates.io-index" 520 | checksum = "1add1b659e36c9607c7aab864a76c7a4c2760cd0cd2e120f3fb8b952c7e22bf9" 521 | dependencies = [ 522 | "block", 523 | "objc", 524 | "objc_id", 525 | ] 526 | 527 | [[package]] 528 | name = "objc_id" 529 | version = "0.1.1" 530 | source = "registry+https://github.com/rust-lang/crates.io-index" 531 | checksum = "c92d4ddb4bd7b50d730c215ff871754d0da6b2178849f8a2a2ab69712d0c073b" 532 | dependencies = [ 533 | "objc", 534 | ] 535 | 536 | [[package]] 537 | name = "once_cell" 538 | version = "1.19.0" 539 | source = "registry+https://github.com/rust-lang/crates.io-index" 540 | checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" 541 | 542 | [[package]] 543 | name = "orbclient" 544 | version = "0.3.47" 545 | source = "registry+https://github.com/rust-lang/crates.io-index" 546 | checksum = "52f0d54bde9774d3a51dcf281a5def240c71996bc6ca05d2c847ec8b2b216166" 547 | dependencies = [ 548 | "libc", 549 | "libredox 0.0.2", 550 | "sdl2", 551 | "sdl2-sys", 552 | ] 553 | 554 | [[package]] 555 | name = "pin-project-lite" 556 | version = "0.2.13" 557 | source = "registry+https://github.com/rust-lang/crates.io-index" 558 | checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" 559 | 560 | [[package]] 561 | name = "pin-utils" 562 | version = "0.1.0" 563 | source = "registry+https://github.com/rust-lang/crates.io-index" 564 | checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" 565 | 566 | [[package]] 567 | name = "pkg-config" 568 | version = "0.3.30" 569 | source = "registry+https://github.com/rust-lang/crates.io-index" 570 | checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" 571 | 572 | [[package]] 573 | name = "proc-macro2" 574 | version = "1.0.79" 575 | source = "registry+https://github.com/rust-lang/crates.io-index" 576 | checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e" 577 | dependencies = [ 578 | "unicode-ident", 579 | ] 580 | 581 | [[package]] 582 | name = "quote" 583 | version = "1.0.35" 584 | source = "registry+https://github.com/rust-lang/crates.io-index" 585 | checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" 586 | dependencies = [ 587 | "proc-macro2", 588 | ] 589 | 590 | [[package]] 591 | name = "raw-window-handle" 592 | version = "0.4.3" 593 | source = "registry+https://github.com/rust-lang/crates.io-index" 594 | checksum = "b800beb9b6e7d2df1fe337c9e3d04e3af22a124460fb4c30fcc22c9117cefb41" 595 | dependencies = [ 596 | "cty", 597 | ] 598 | 599 | [[package]] 600 | name = "redox_syscall" 601 | version = "0.4.1" 602 | source = "registry+https://github.com/rust-lang/crates.io-index" 603 | checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" 604 | dependencies = [ 605 | "bitflags 1.3.2", 606 | ] 607 | 608 | [[package]] 609 | name = "redox_users" 610 | version = "0.4.4" 611 | source = "registry+https://github.com/rust-lang/crates.io-index" 612 | checksum = "a18479200779601e498ada4e8c1e1f50e3ee19deb0259c25825a98b5603b2cb4" 613 | dependencies = [ 614 | "getrandom", 615 | "libredox 0.0.1", 616 | "thiserror", 617 | ] 618 | 619 | [[package]] 620 | name = "rustix" 621 | version = "0.38.32" 622 | source = "registry+https://github.com/rust-lang/crates.io-index" 623 | checksum = "65e04861e65f21776e67888bfbea442b3642beaa0138fdb1dd7a84a52dffdb89" 624 | dependencies = [ 625 | "bitflags 2.5.0", 626 | "errno", 627 | "libc", 628 | "linux-raw-sys", 629 | "windows-sys", 630 | ] 631 | 632 | [[package]] 633 | name = "scoped-tls" 634 | version = "1.0.1" 635 | source = "registry+https://github.com/rust-lang/crates.io-index" 636 | checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" 637 | 638 | [[package]] 639 | name = "sdl2" 640 | version = "0.35.2" 641 | source = "registry+https://github.com/rust-lang/crates.io-index" 642 | checksum = "f7959277b623f1fb9e04aea73686c3ca52f01b2145f8ea16f4ff30d8b7623b1a" 643 | dependencies = [ 644 | "bitflags 1.3.2", 645 | "lazy_static", 646 | "libc", 647 | "sdl2-sys", 648 | ] 649 | 650 | [[package]] 651 | name = "sdl2-sys" 652 | version = "0.35.2" 653 | source = "registry+https://github.com/rust-lang/crates.io-index" 654 | checksum = "e3586be2cf6c0a8099a79a12b4084357aa9b3e0b0d7980e3b67aaf7a9d55f9f0" 655 | dependencies = [ 656 | "cfg-if", 657 | "libc", 658 | "version-compare", 659 | ] 660 | 661 | [[package]] 662 | name = "serde" 663 | version = "1.0.197" 664 | source = "registry+https://github.com/rust-lang/crates.io-index" 665 | checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" 666 | dependencies = [ 667 | "serde_derive", 668 | ] 669 | 670 | [[package]] 671 | name = "serde_derive" 672 | version = "1.0.197" 673 | source = "registry+https://github.com/rust-lang/crates.io-index" 674 | checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" 675 | dependencies = [ 676 | "proc-macro2", 677 | "quote", 678 | "syn", 679 | ] 680 | 681 | [[package]] 682 | name = "slab" 683 | version = "0.4.9" 684 | source = "registry+https://github.com/rust-lang/crates.io-index" 685 | checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" 686 | dependencies = [ 687 | "autocfg", 688 | ] 689 | 690 | [[package]] 691 | name = "smallvec" 692 | version = "1.13.1" 693 | source = "registry+https://github.com/rust-lang/crates.io-index" 694 | checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7" 695 | 696 | [[package]] 697 | name = "stupidalloc" 698 | version = "0.2.2" 699 | dependencies = [ 700 | "allocator-api2", 701 | "hashbrown", 702 | "lazy_static", 703 | "memmap2", 704 | "minifb", 705 | "native-dialog", 706 | ] 707 | 708 | [[package]] 709 | name = "syn" 710 | version = "2.0.53" 711 | source = "registry+https://github.com/rust-lang/crates.io-index" 712 | checksum = "7383cd0e49fff4b6b90ca5670bfd3e9d6a733b3f90c686605aa7eec8c4996032" 713 | dependencies = [ 714 | "proc-macro2", 715 | "quote", 716 | "unicode-ident", 717 | ] 718 | 719 | [[package]] 720 | name = "tempfile" 721 | version = "3.10.1" 722 | source = "registry+https://github.com/rust-lang/crates.io-index" 723 | checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1" 724 | dependencies = [ 725 | "cfg-if", 726 | "fastrand", 727 | "rustix", 728 | "windows-sys", 729 | ] 730 | 731 | [[package]] 732 | name = "thiserror" 733 | version = "1.0.58" 734 | source = "registry+https://github.com/rust-lang/crates.io-index" 735 | checksum = "03468839009160513471e86a034bb2c5c0e4baae3b43f79ffc55c4a5427b3297" 736 | dependencies = [ 737 | "thiserror-impl", 738 | ] 739 | 740 | [[package]] 741 | name = "thiserror-impl" 742 | version = "1.0.58" 743 | source = "registry+https://github.com/rust-lang/crates.io-index" 744 | checksum = "c61f3ba182994efc43764a46c018c347bc492c79f024e705f46567b418f6d4f7" 745 | dependencies = [ 746 | "proc-macro2", 747 | "quote", 748 | "syn", 749 | ] 750 | 751 | [[package]] 752 | name = "unicode-ident" 753 | version = "1.0.12" 754 | source = "registry+https://github.com/rust-lang/crates.io-index" 755 | checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" 756 | 757 | [[package]] 758 | name = "version-compare" 759 | version = "0.1.1" 760 | source = "registry+https://github.com/rust-lang/crates.io-index" 761 | checksum = "579a42fc0b8e0c63b76519a339be31bed574929511fa53c1a3acae26eb258f29" 762 | 763 | [[package]] 764 | name = "version_check" 765 | version = "0.9.4" 766 | source = "registry+https://github.com/rust-lang/crates.io-index" 767 | checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" 768 | 769 | [[package]] 770 | name = "wasi" 771 | version = "0.11.0+wasi-snapshot-preview1" 772 | source = "registry+https://github.com/rust-lang/crates.io-index" 773 | checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" 774 | 775 | [[package]] 776 | name = "wasm-bindgen" 777 | version = "0.2.92" 778 | source = "registry+https://github.com/rust-lang/crates.io-index" 779 | checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8" 780 | dependencies = [ 781 | "cfg-if", 782 | "wasm-bindgen-macro", 783 | ] 784 | 785 | [[package]] 786 | name = "wasm-bindgen-backend" 787 | version = "0.2.92" 788 | source = "registry+https://github.com/rust-lang/crates.io-index" 789 | checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da" 790 | dependencies = [ 791 | "bumpalo", 792 | "log", 793 | "once_cell", 794 | "proc-macro2", 795 | "quote", 796 | "syn", 797 | "wasm-bindgen-shared", 798 | ] 799 | 800 | [[package]] 801 | name = "wasm-bindgen-futures" 802 | version = "0.4.42" 803 | source = "registry+https://github.com/rust-lang/crates.io-index" 804 | checksum = "76bc14366121efc8dbb487ab05bcc9d346b3b5ec0eaa76e46594cabbe51762c0" 805 | dependencies = [ 806 | "cfg-if", 807 | "js-sys", 808 | "wasm-bindgen", 809 | "web-sys", 810 | ] 811 | 812 | [[package]] 813 | name = "wasm-bindgen-macro" 814 | version = "0.2.92" 815 | source = "registry+https://github.com/rust-lang/crates.io-index" 816 | checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726" 817 | dependencies = [ 818 | "quote", 819 | "wasm-bindgen-macro-support", 820 | ] 821 | 822 | [[package]] 823 | name = "wasm-bindgen-macro-support" 824 | version = "0.2.92" 825 | source = "registry+https://github.com/rust-lang/crates.io-index" 826 | checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" 827 | dependencies = [ 828 | "proc-macro2", 829 | "quote", 830 | "syn", 831 | "wasm-bindgen-backend", 832 | "wasm-bindgen-shared", 833 | ] 834 | 835 | [[package]] 836 | name = "wasm-bindgen-shared" 837 | version = "0.2.92" 838 | source = "registry+https://github.com/rust-lang/crates.io-index" 839 | checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" 840 | 841 | [[package]] 842 | name = "wayland-client" 843 | version = "0.29.5" 844 | source = "registry+https://github.com/rust-lang/crates.io-index" 845 | checksum = "3f3b068c05a039c9f755f881dc50f01732214f5685e379829759088967c46715" 846 | dependencies = [ 847 | "bitflags 1.3.2", 848 | "downcast-rs", 849 | "libc", 850 | "nix", 851 | "scoped-tls", 852 | "wayland-commons", 853 | "wayland-scanner", 854 | "wayland-sys", 855 | ] 856 | 857 | [[package]] 858 | name = "wayland-commons" 859 | version = "0.29.5" 860 | source = "registry+https://github.com/rust-lang/crates.io-index" 861 | checksum = "8691f134d584a33a6606d9d717b95c4fa20065605f798a3f350d78dced02a902" 862 | dependencies = [ 863 | "nix", 864 | "once_cell", 865 | "smallvec", 866 | "wayland-sys", 867 | ] 868 | 869 | [[package]] 870 | name = "wayland-cursor" 871 | version = "0.29.5" 872 | source = "registry+https://github.com/rust-lang/crates.io-index" 873 | checksum = "6865c6b66f13d6257bef1cd40cbfe8ef2f150fb8ebbdb1e8e873455931377661" 874 | dependencies = [ 875 | "nix", 876 | "wayland-client", 877 | "xcursor", 878 | ] 879 | 880 | [[package]] 881 | name = "wayland-protocols" 882 | version = "0.29.5" 883 | source = "registry+https://github.com/rust-lang/crates.io-index" 884 | checksum = "b950621f9354b322ee817a23474e479b34be96c2e909c14f7bc0100e9a970bc6" 885 | dependencies = [ 886 | "bitflags 1.3.2", 887 | "wayland-client", 888 | "wayland-commons", 889 | "wayland-scanner", 890 | ] 891 | 892 | [[package]] 893 | name = "wayland-scanner" 894 | version = "0.29.5" 895 | source = "registry+https://github.com/rust-lang/crates.io-index" 896 | checksum = "8f4303d8fa22ab852f789e75a967f0a2cdc430a607751c0499bada3e451cbd53" 897 | dependencies = [ 898 | "proc-macro2", 899 | "quote", 900 | "xml-rs", 901 | ] 902 | 903 | [[package]] 904 | name = "wayland-sys" 905 | version = "0.29.5" 906 | source = "registry+https://github.com/rust-lang/crates.io-index" 907 | checksum = "be12ce1a3c39ec7dba25594b97b42cb3195d54953ddb9d3d95a7c3902bc6e9d4" 908 | dependencies = [ 909 | "dlib", 910 | "lazy_static", 911 | "pkg-config", 912 | ] 913 | 914 | [[package]] 915 | name = "web-sys" 916 | version = "0.3.69" 917 | source = "registry+https://github.com/rust-lang/crates.io-index" 918 | checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef" 919 | dependencies = [ 920 | "js-sys", 921 | "wasm-bindgen", 922 | ] 923 | 924 | [[package]] 925 | name = "wfd" 926 | version = "0.1.7" 927 | source = "registry+https://github.com/rust-lang/crates.io-index" 928 | checksum = "e713040b67aae5bf1a0ae3e1ebba8cc29ab2b90da9aa1bff6e09031a8a41d7a8" 929 | dependencies = [ 930 | "libc", 931 | "winapi", 932 | ] 933 | 934 | [[package]] 935 | name = "which" 936 | version = "4.4.2" 937 | source = "registry+https://github.com/rust-lang/crates.io-index" 938 | checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7" 939 | dependencies = [ 940 | "either", 941 | "home", 942 | "once_cell", 943 | "rustix", 944 | ] 945 | 946 | [[package]] 947 | name = "winapi" 948 | version = "0.3.9" 949 | source = "registry+https://github.com/rust-lang/crates.io-index" 950 | checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" 951 | dependencies = [ 952 | "winapi-i686-pc-windows-gnu", 953 | "winapi-x86_64-pc-windows-gnu", 954 | ] 955 | 956 | [[package]] 957 | name = "winapi-i686-pc-windows-gnu" 958 | version = "0.4.0" 959 | source = "registry+https://github.com/rust-lang/crates.io-index" 960 | checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 961 | 962 | [[package]] 963 | name = "winapi-x86_64-pc-windows-gnu" 964 | version = "0.4.0" 965 | source = "registry+https://github.com/rust-lang/crates.io-index" 966 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 967 | 968 | [[package]] 969 | name = "windows-sys" 970 | version = "0.52.0" 971 | source = "registry+https://github.com/rust-lang/crates.io-index" 972 | checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" 973 | dependencies = [ 974 | "windows-targets", 975 | ] 976 | 977 | [[package]] 978 | name = "windows-targets" 979 | version = "0.52.4" 980 | source = "registry+https://github.com/rust-lang/crates.io-index" 981 | checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b" 982 | dependencies = [ 983 | "windows_aarch64_gnullvm", 984 | "windows_aarch64_msvc", 985 | "windows_i686_gnu", 986 | "windows_i686_msvc", 987 | "windows_x86_64_gnu", 988 | "windows_x86_64_gnullvm", 989 | "windows_x86_64_msvc", 990 | ] 991 | 992 | [[package]] 993 | name = "windows_aarch64_gnullvm" 994 | version = "0.52.4" 995 | source = "registry+https://github.com/rust-lang/crates.io-index" 996 | checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9" 997 | 998 | [[package]] 999 | name = "windows_aarch64_msvc" 1000 | version = "0.52.4" 1001 | source = "registry+https://github.com/rust-lang/crates.io-index" 1002 | checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675" 1003 | 1004 | [[package]] 1005 | name = "windows_i686_gnu" 1006 | version = "0.52.4" 1007 | source = "registry+https://github.com/rust-lang/crates.io-index" 1008 | checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3" 1009 | 1010 | [[package]] 1011 | name = "windows_i686_msvc" 1012 | version = "0.52.4" 1013 | source = "registry+https://github.com/rust-lang/crates.io-index" 1014 | checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02" 1015 | 1016 | [[package]] 1017 | name = "windows_x86_64_gnu" 1018 | version = "0.52.4" 1019 | source = "registry+https://github.com/rust-lang/crates.io-index" 1020 | checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03" 1021 | 1022 | [[package]] 1023 | name = "windows_x86_64_gnullvm" 1024 | version = "0.52.4" 1025 | source = "registry+https://github.com/rust-lang/crates.io-index" 1026 | checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177" 1027 | 1028 | [[package]] 1029 | name = "windows_x86_64_msvc" 1030 | version = "0.52.4" 1031 | source = "registry+https://github.com/rust-lang/crates.io-index" 1032 | checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8" 1033 | 1034 | [[package]] 1035 | name = "x11-dl" 1036 | version = "2.21.0" 1037 | source = "registry+https://github.com/rust-lang/crates.io-index" 1038 | checksum = "38735924fedd5314a6e548792904ed8c6de6636285cb9fec04d5b1db85c1516f" 1039 | dependencies = [ 1040 | "libc", 1041 | "once_cell", 1042 | "pkg-config", 1043 | ] 1044 | 1045 | [[package]] 1046 | name = "xcursor" 1047 | version = "0.3.5" 1048 | source = "registry+https://github.com/rust-lang/crates.io-index" 1049 | checksum = "6a0ccd7b4a5345edfcd0c3535718a4e9ff7798ffc536bb5b5a0e26ff84732911" 1050 | 1051 | [[package]] 1052 | name = "xml-rs" 1053 | version = "0.8.19" 1054 | source = "registry+https://github.com/rust-lang/crates.io-index" 1055 | checksum = "0fcb9cbac069e033553e8bb871be2fbdffcab578eb25bd0f7c508cedc6dcd75a" 1056 | 1057 | [[package]] 1058 | name = "zerocopy" 1059 | version = "0.7.32" 1060 | source = "registry+https://github.com/rust-lang/crates.io-index" 1061 | checksum = "74d4d3961e53fa4c9a25a8637fc2bfaf2595b3d3ae34875568a5cf64787716be" 1062 | dependencies = [ 1063 | "zerocopy-derive", 1064 | ] 1065 | 1066 | [[package]] 1067 | name = "zerocopy-derive" 1068 | version = "0.7.32" 1069 | source = "registry+https://github.com/rust-lang/crates.io-index" 1070 | checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6" 1071 | dependencies = [ 1072 | "proc-macro2", 1073 | "quote", 1074 | "syn", 1075 | ] 1076 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | authors = ["shadyfennec"] 3 | categories = ["memory-management"] 4 | description = "A stupid memory allocator that memory-maps allocations to files." 5 | edition = "2021" 6 | keywords = ["allocator", "memory"] 7 | license = "MIT OR Apache-2.0" 8 | name = "stupidalloc" 9 | readme = "README.md" 10 | repository = "https://github.com/shadyfennec/stupidalloc" 11 | version = "0.2.2" 12 | 13 | [package.metadata.docs.rs] 14 | all-features = true 15 | 16 | [features] 17 | always-graphics = ["graphics"] 18 | graphics = ["minifb"] 19 | interactive = ["native-dialog"] 20 | logging = [] 21 | 22 | [patch.crates-io] 23 | ahash = {git = "https://github.com/tkaitchuck/aHash", tag = "v0.8.11"} 24 | 25 | [dependencies] 26 | allocator-api2 = "0.2.16" 27 | hashbrown = {version = "0.14.3", features = ["ahash", "inline-more"]} 28 | lazy_static = "1.4.0" 29 | memmap2 = "0.7.1" 30 | minifb = {version = "0.25", optional = true} 31 | native-dialog = {version = "0.6.4", optional = true, features = ["windows_dpi_awareness", "windows_visual_styles"]} 32 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [2023] [shadyfennec] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2010 The Rust Project Developers 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Stupid alloc - what if memory allocation was annoying 2 | Mostly a weird exercise in how much you can make a memory allocator suck. 3 | 4 | This allocator will create and open files to use as the allocation's data, through a memory map. If you enable the `interactive` feature, it will even prompt you for a file name every time 5 | the program allocates something! Cool! 6 | 7 | 8 | ## How to use it 9 | don't 10 | 11 | ## No but really how does one use this 12 | Using `cargo add`: 13 | 14 | ```shell 15 | cargo add stupidalloc 16 | ``` 17 | 18 | Manually specifying the dependency in `Cargo.toml`: 19 | 20 | ```toml 21 | [dependencies] 22 | stupidalloc = { version = "0.2.1" } 23 | ``` 24 | 25 | ### The `interactive` feature 26 | The crate comes with a feature, `interactive`, that will open confirmation and file picker dialog windows instead of silently opening and allocating memory. Enable it at your own risk, 27 | as sometimes dialogs are unavailable. This crate uses [`native-dialog`](https://crates.io/crates/native-dialog) for this feature. 28 | 29 | ### Graphical interface 30 | The `graphics` feature creates graphical windows that display memory contents as black or white pixels, representing the bits of the allocations! Click on each pixel to either set the bit (left click) or clear the bit (right click). You can easily modify memory contents this way! 31 | 32 | Additionally, the `always-graphics` feature enables graphical windows for every single new allocation performed, and not just creation on-demand by the user. 33 | 34 | Graphical windows are created using the [`minifb`](https://crates.io/crates/minifb) crate. 35 | 36 | https://github.com/shadyfennec/stupidalloc/assets/68575248/b19790c7-bc9e-4a59-99c9-18d7e308739e 37 | 38 | ### Logging 39 | The `logging` crate creates companion logging files that record useful information about each allocation, using the familiar Markdown format. Useful for debugging! 40 | 41 | ## Using the allocator 42 | - You can use it as the global allocator of your program, but it may lead to wonkiness and weird stuff like prompting for allocations before `main()` is executed! 43 | 44 | ```rust 45 | use stupidalloc::StupidAlloc; 46 | 47 | #[global_allocator] 48 | static GLOBAL: StupidAlloc = StupidAlloc; 49 | 50 | fn main() { 51 | // ... 52 | } 53 | ``` 54 | 55 | - By using the [`allocator_api`](https://doc.rust-lang.org/beta/unstable-book/library-features/allocator-api.html) nightly feature, you can selectively 56 | allocate single objects with this allocator: 57 | 58 | ```rust 59 | // Requires nightly 60 | #![feature(allocator_api)] 61 | 62 | use stupidalloc::StupidAlloc; 63 | 64 | fn main() { 65 | let normal_box = Box::new(1); 66 | 67 | let stupid_box = Box::new_in(1, StupidAlloc); 68 | } 69 | ``` 70 | 71 | A cool usage is to stop the execution of your program (through your favourite `stdin` read) and then go look at the allocation files with a hex editor (might I recommend [Hexyl](https://github.com/sharkdp/hexyl)?) 72 | 73 | To help you with that, the allocator exposes a few helper functions: 74 | - `StupidAlloc.state()` returns a `HashMap` where the key is the address of the memory map (and so the address of the allocated object), and the value is a `PathBuf` to the associated file. 75 | - `StupidAlloc` implements `fmt::Display`, so running `println!("{StupidAlloc}")` will print a lovely summary of all the allocations currently being tracked. 76 | - `StupidAlloc.file_of(x)` will return the file associated to the linked object, if it exists. Obviously this only works with stuff allocated with the stupid allocator. An example of use: 77 | 78 | ```rust 79 | // Still requires nightly 80 | #![feature(allocator_api)] 81 | 82 | use stupidalloc::StupidAlloc; 83 | 84 | fn main() { 85 | let stupid_box = Box::new_in(1, StupidAlloc); 86 | 87 | // Since it's a Box, we need to pass &i32 to the function to get the 88 | // address of where the integer is. 89 | let file = StupidAlloc.file_of(&*stupid_box).unwrap(); 90 | 91 | // Go nuts with it! 92 | } 93 | ``` 94 | 95 | Another cool usage is to be able to see how stuff is laid out in memory, without 96 | having to use memory viewers or complicated GDB syntax! 97 | 98 | For example, ever wanted to see how a `Vec` is organised in memory? 99 | 100 | ```rust 101 | use stupidalloc::StupidAlloc; 102 | 103 | #[global_allocator] 104 | static GLOBAL: StupidAlloc = StupidAlloc; 105 | 106 | fn main() { 107 | let boxed_vec = Box::new(vec![1, 2, 3]); 108 | 109 | println!("{}", StupidAlloc.file_of(&*boxed_vec).unwrap().display()); 110 | 111 | // Somehow pause execution 112 | } 113 | ``` 114 | 115 | This program will print the path of the allocation file for the `Vec` struct 116 | (and not the allocation for the data of the `Vec`, because then we'd only see 117 | the numbers 1, 2, 3!). Open it in a hex viewer, and you can try and guess what 118 | each field is, and try to corroborate it with the [struct's definition](https://doc.rust-lang.org/stable/std/vec/struct.Vec.html). 119 | If your system allows you to (I know Windows can be a bit restrictive), try and 120 | modify the length and/or capacity fields and see what happens afterwards! 121 | 122 | ## Disclaimers 123 | - I do not claim that this library is perfect and free of any fault. Here there be typos and mistakes and examples that I didn't test and don't work. Send an issue if something's wrong! 124 | - If you don't have file picker / file dialog capabilities (minimal i3 installation, TTY-only, ...), `interactivity` won't work. 125 | - Similarly, if you don't have a graphical environment altogether, `graphics` and `always-graphics` won't work either. 126 | - I only tested this on Windows and Linux. If it doesn't work on MacOS or any other OS, sorry. If it doesn't work for you on Windows or Linux: weird! Hit me up. 127 | - If you mess with the memory files in any way you'll mess up with your program memory, but seeing as this is topologically the same as messing with `/proc/mem` I consider this a cool feature. 128 | - I'm probably going to work on this *a little bit more* to add some quality-of-life features, but that's it. It's a shitpost, not a serious library. 129 | 130 | ## (old) Demo 131 | https://github.com/shadyfennec/stupidalloc/assets/68575248/f2490dc1-8412-4450-9359-7387f79682ea 132 | -------------------------------------------------------------------------------- /examples/graphics.rs: -------------------------------------------------------------------------------- 1 | #![feature(allocator_api)] 2 | 3 | use stupidalloc::StupidAlloc; 4 | #[global_allocator] 5 | static GLOBAL: StupidAlloc = StupidAlloc; 6 | 7 | #[cfg(feature = "always-graphics")] 8 | fn main() { 9 | use std::io::Read; 10 | 11 | let mut string = String::with_capacity(16); 12 | 13 | println!("Type what you want below, it will be echoed back to you!"); 14 | 15 | std::io::stdin().lock().read_to_string(&mut string).unwrap(); 16 | 17 | println!("{string}"); 18 | } 19 | 20 | #[cfg(not(feature = "always-graphics"))] 21 | fn main() { 22 | eprintln!("This example is made to showcase the graphical display of stupidalloc. Running it without the `always-graphics` feature is useless."); 23 | } 24 | -------------------------------------------------------------------------------- /src/graphics.rs: -------------------------------------------------------------------------------- 1 | use std::{ 2 | alloc::System, 3 | fs::File, 4 | path::Path, 5 | sync::{ 6 | mpsc::{channel, Sender, TryRecvError}, 7 | Arc, 8 | }, 9 | thread::JoinHandle, 10 | time::Duration, 11 | }; 12 | 13 | use memmap2::{MmapMut, MmapOptions}; 14 | use minifb::{Scale, WindowOptions}; 15 | 16 | // iterator over bits of byte (LSB -> MSB) 17 | fn bits_as_pixels(byte: u8) -> impl Iterator { 18 | let byte = byte.reverse_bits(); 19 | 20 | (0..8).map(move |i| { 21 | // minifb pixel format is 0x00RRGGBB 22 | if byte >> i & 1 == 0 { 23 | //00RRGGBB 24 | 0x00000000 25 | } else { 26 | //00RRGGBB 27 | 0x00FFFFFF 28 | } 29 | }) 30 | } 31 | 32 | // code deduplication ugly function 33 | fn create_map_window_buffer( 34 | file: &File, 35 | name: &str, 36 | columns: usize, 37 | ) -> (MmapMut, minifb::Window, Vec) { 38 | let map = unsafe { MmapOptions::new().map_mut(file).unwrap() }; 39 | let mut window = minifb::Window::new( 40 | name, 41 | 8 * columns, 42 | map.len() / columns, 43 | WindowOptions { 44 | scale: Scale::X16, // so that bits aren't the size of a pixel of your screen 45 | ..Default::default() 46 | }, 47 | ) 48 | .unwrap(); 49 | window.limit_update_rate(Some(Duration::from_millis(16))); // 60 fps 😎 50 | 51 | let buffer = Vec::with_capacity_in(map.len() * 8, System); 52 | 53 | (map, window, buffer) 54 | } 55 | 56 | // messages sent by the allocator 57 | pub enum Message { 58 | // grow (or shrink or grow_zeroed actually) 59 | Grow, 60 | // dealloc 61 | Free, 62 | // new column size 63 | Resize { columns: usize }, 64 | } 65 | 66 | pub struct Window { 67 | // it's an option so that drop can join the thread by `take()`-ing it 68 | pub handle: Option>, 69 | pub tx: Sender, 70 | } 71 | 72 | impl Window { 73 | pub fn new(path: &Path, file: Arc, columns: usize) -> Self { 74 | let name = format!("Graphical view of memory @ {}", path.to_string_lossy()); 75 | 76 | let (tx, rx) = channel::(); 77 | 78 | let handle = std::thread::Builder::new() 79 | .name(name.clone()) 80 | .spawn(move || { 81 | let file = file; 82 | let mut columns = columns; 83 | 84 | let (mut map, mut window, mut buffer) = 85 | create_map_window_buffer(&file, &name, columns); 86 | 87 | loop { 88 | if !window.is_open() { 89 | break; 90 | } 91 | 92 | match rx.try_recv() { 93 | Err(TryRecvError::Empty) => {} 94 | Ok(Message::Free) | Err(TryRecvError::Disconnected) => { 95 | break; 96 | } 97 | Ok(Message::Grow) => { 98 | let (new_map, new_window, new_buffer) = 99 | create_map_window_buffer(&file, &name, columns); 100 | map = new_map; 101 | window = new_window; 102 | buffer = new_buffer; 103 | } 104 | Ok(Message::Resize { columns: c }) => { 105 | columns = c; 106 | let (new_map, new_window, new_buffer) = 107 | create_map_window_buffer(&file, &name, columns); 108 | map = new_map; 109 | window = new_window; 110 | buffer = new_buffer; 111 | } 112 | } 113 | 114 | // really proud of these two lines 115 | buffer.clear(); 116 | buffer.extend(map.iter().flat_map(|b| bits_as_pixels(*b))); 117 | 118 | window 119 | .update_with_buffer(&buffer, 8 * columns, map.len() / columns) 120 | .unwrap(); 121 | 122 | // i've been writing this feature for like 9 hours i'm too tired to try and de-duplicate this code 123 | // future me or anyone else you're welcome to but i'd rather go to bed than try and do that 124 | if window.get_mouse_down(minifb::MouseButton::Left) { 125 | // set bit 126 | if let Some((x, y)) = window.get_mouse_pos(minifb::MouseMode::Discard) { 127 | let x = x.floor() as usize; 128 | let y = y.floor() as usize; 129 | 130 | let bit = x % 8; 131 | let byte = (x / 8) + (y * columns); 132 | 133 | let mask = 1 << (7 - bit); 134 | 135 | map[byte] |= mask; 136 | } 137 | } else if window.get_mouse_down(minifb::MouseButton::Right) { 138 | // clear bit 139 | if let Some((x, y)) = window.get_mouse_pos(minifb::MouseMode::Discard) { 140 | let x = x.floor() as usize; 141 | let y = y.floor() as usize; 142 | 143 | let bit = x % 8; 144 | let byte = (x / 8) + (y * columns); 145 | 146 | let mask = 1 << (7 - bit); 147 | 148 | map[byte] &= !mask; 149 | } 150 | } 151 | } 152 | }) 153 | .unwrap(); 154 | 155 | Window { 156 | handle: Some(handle), 157 | tx, 158 | } 159 | } 160 | 161 | pub fn close(mut self) { 162 | if let Some(handle) = self.handle.take() { 163 | handle.join().unwrap(); 164 | } 165 | } 166 | 167 | pub fn is_finished(&self) -> bool { 168 | self.handle 169 | .as_ref() 170 | .map(|handle| handle.is_finished()) 171 | .unwrap_or(true) 172 | } 173 | } 174 | 175 | impl Drop for Window { 176 | fn drop(&mut self) { 177 | if let Some(handle) = self.handle.take() { 178 | handle.join().unwrap() 179 | } 180 | } 181 | } 182 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! A very stupid but maybe educational memory allocator. 2 | //! 3 | //! # Behaviour 4 | //! This [`Allocator`] will create, open and use a file for every single allocation 5 | //! performed through it. Obviously, doing this imples allocating stuff, 6 | //! which is kind of problematic. So, as a fallback, this allocator uses 7 | //! [`System`] when allocating during a memory allocation or de-allocation. 8 | //! 9 | //! # Usage example 10 | //! Use the allocator for a few items while keeping the global normal allocator 11 | //! 12 | //! ``` 13 | //! #![feature(allocator_api)] // You need this for the `new_in` functions. Requires nightly. 14 | //! use stupidalloc::StupidAlloc; 15 | //! 16 | //! let normal_box = Box::new(1u32); 17 | //! 18 | //! let stupid_box = Box::new_in(1u32, StupidAlloc); 19 | //! ``` 20 | //! 21 | //! Use the allocator as the global allocator. Warning: funky stuff may happen, 22 | //! such as allocations before main! 23 | //! ``` 24 | //! use stupidalloc::StupidAlloc; 25 | //! 26 | //! #[global_allocator] 27 | //! static GLOBAL: StupidAlloc = StupidAlloc; 28 | //! 29 | //! fn main() { 30 | //! // ... 31 | //! } 32 | //! ``` 33 | //! 34 | //! ## Interactivty 35 | //! By default, the allocator will silently and automatically allocate memory 36 | //! (as you would expect), by opening files in a temporary folder (as dictated 37 | //! by [`std::env::temp_dir()`]). A feature flag, `interactive`, will enable 38 | //! confirmation and file picking dialogs to pop up during allocations and 39 | //! de-allocations. More specifically: 40 | //! - On allocation, a confirmation message detailling the [`Layout`] needed for 41 | //! the allocation, followed by file picking dialog. If the first confirmation 42 | //! message is denied, or if no file is provided, the allocation fails. 43 | //! - On de-allocation, a confirmation message showing the address of the thing 44 | //! that was de-allocated shows up. It doesn't matter how it is handled, 45 | //! the de-allocation won't fail because of it. 46 | //! 47 | //! ## Graphics 48 | //! Enabling the `graphics` feature will allow you to create interactive graphical 49 | //! windows that will visually show the contents of the memory you allocate with 50 | //! this allocator. The data will be represented as rows of bytes, that are themselves 51 | //! represented as consecutive 8 bits. Graphically, each bit is shown as a black 52 | //! or white square, where black represents a `0`, and white represents a `1`. 53 | //! 54 | //! ### Modifying memory contents with the mouse 55 | //! Clicking with the left mouse button on a square will set the corresponding bit 56 | //! in memory, while using the right mouse button will clear it. You can thus 57 | //! directly modify memory using only your mouse, and directly see the results! 58 | //! 59 | //! ### Creation 60 | //! By default, allocating memory doesn't create a corresponding graphical window. 61 | //! To create a window, you can use `StupidAlloc::open_window_of()`, and you 62 | //! can adjust the number of bytes displayed on each row using 63 | //! `StupidAlloc::set_columns_of`: 64 | //! 65 | //! ```no_run 66 | //! #![feature(allocator_api)] // You need this for the `new_in` functions. Requires nightly. 67 | //! use stupidalloc::StupidAlloc; 68 | //! 69 | //! let stupid_box = Box::new_in(1u32, StupidAlloc); 70 | //! 71 | //! #[cfg(feature = "graphics")] 72 | //! { 73 | //! // Start with 8 columns. 74 | //! StupidAlloc.open_window_of(&*stupid_box, 8); 75 | //! 76 | //! // I changed my mind, I want 4 columns instead. 77 | //! StupidAlloc.set_columns_of(&*stupid_box, 4); 78 | //! } 79 | //! ``` 80 | //! 81 | //! If the `always-graphics` feature is enabled, then every allocation will be 82 | //! displayed automatically, without the need to call `open_window_of()`. 83 | //! 84 | //! ## Logging 85 | //! If the `logging` feature is enabled, each allocation will be accompanied by 86 | //! a companion log file, with the same path and name as the allocation file, but 87 | //! with a `.md` extension. Inside, information about the allocation will be 88 | //! written as the allocation is interacted with: 89 | //! - Metadata, such as corresponding allocation file, the [`Layout`], ... 90 | //! - Allocation and deallocation backtraces (requires the `RUST_BACKTRACE` 91 | //! environment variable to be set accordingly) 92 | //! - Every grow or shrink, with new [`Layout`] and corresponding backtrace 93 | //! 94 | //! Log files won't be deleted when the corresponding memory is freed, but they 95 | //! might get overwritten, either by you when using the `interactive` feature 96 | //! and specifying the same file name as a previous allocation's, or by 97 | //! subsequent executions of a program that uses this allocator. 98 | //! 99 | //! ## Multi-threading 100 | //! Internally, the allocator uses a [`RwLock`] when allocating and de-allocating. 101 | //! As such, using this in a multi-threaded context will yield even more awful 102 | //! performance. Performance is not the goal, but be warned nonetheless. 103 | 104 | #![feature(allocator_api)] 105 | #![feature(ptr_metadata)] 106 | #![feature(doc_cfg)] 107 | #![warn(missing_docs)] 108 | 109 | use core::fmt; 110 | use hashbrown::{hash_map::DefaultHashBuilder, HashMap}; 111 | use lazy_static::lazy_static; 112 | use memmap2::{MmapMut, MmapOptions}; 113 | use std::{ 114 | alloc::{AllocError, Allocator, GlobalAlloc, Layout, System}, 115 | fs::{File, OpenOptions}, 116 | path::PathBuf, 117 | ptr::NonNull, 118 | sync::{ 119 | atomic::{AtomicBool, AtomicUsize, Ordering}, 120 | Arc, Once, RwLock, 121 | }, 122 | }; 123 | 124 | #[cfg(feature = "interactive")] 125 | use native_dialog::{FileDialog, MessageDialog, MessageType}; 126 | 127 | #[cfg(feature = "logging")] 128 | use std::{backtrace::Backtrace, io::Write}; 129 | 130 | #[cfg(feature = "graphics")] 131 | mod graphics; 132 | 133 | // tuples are so 2016 let's use a struct instead 134 | struct AllocHandle { 135 | // memory map of the data 136 | map: MmapMut, 137 | // we use an arc so that we can share the handle with the graphical display 138 | // thread. 139 | file: Arc, 140 | // the path to the data-holding file. 141 | path: PathBuf, 142 | // the thread handle to the graphics thread, if enabled 143 | #[cfg(feature = "graphics")] 144 | window: Option, 145 | // the file handle of the logging file 146 | #[cfg(feature = "logging")] 147 | log_file: File, 148 | } 149 | 150 | lazy_static! { 151 | // use hashbrown map explicitly so that we can directly specify that it lives in 152 | // system allocator. 153 | static ref STUPID_MAP: RwLock> = 154 | RwLock::new(HashMap::new_in(allocator_api2::alloc::System)); 155 | } 156 | 157 | // these are thread_local because they must not interfere with other threads. 158 | thread_local! { 159 | // currently allocating? nonzero = yes. 160 | static ALLOCATING: AtomicUsize = AtomicUsize::new(0); 161 | // currently de-allocating? nonzero = yes. 162 | static DEALLOCATING: AtomicUsize = AtomicUsize::new(0); 163 | // thread-local inhibition boolean, true = use system. 164 | static LOCAL_SWITCH_OFF: AtomicBool = { 165 | // if init was completed, current thread is not main thread, disabling 166 | // by default. were it not for that, when using `always-graphics`, thread 167 | // internals would get allocated in recursion and that's the only viable 168 | // solution. 169 | if INIT_DETECTOR.is_completed() { 170 | AtomicBool::new(true) 171 | } else { 172 | // the init once was not called, so this is main thread (or more 173 | // generally the first thread that tries to use stupid alloc). allowing 174 | // stupid alloc by default. 175 | INIT_DETECTOR.call_once(|| {}); 176 | AtomicBool::new(false) 177 | } 178 | } 179 | } 180 | 181 | // if this Once is not initialized, this means we are between program entry point 182 | // and the first access to LOCAL_SWITCH_OFF (aka first stupid allocation). 183 | static INIT_DETECTOR: Once = Once::new(); 184 | 185 | // the number of byte columns used by default when opening a window for a new 186 | // allocation. default to 8 bytes (64 bits) per line. 187 | #[cfg(feature = "always-graphics")] 188 | static DEFAULT_GRAPHICS_COLUMNS: AtomicUsize = AtomicUsize::new(8); 189 | 190 | // returns true if we do allocate something. only does something with the 191 | // "interactive" feature enabled 192 | #[allow(unused_variables)] 193 | fn confirm_alloc(layout: Layout) -> bool { 194 | #[cfg(feature = "interactive")] 195 | { 196 | // show a lil' confirmation message before throwing you the 197 | // file chooser 198 | MessageDialog::new() 199 | .set_type(MessageType::Info) 200 | .set_title("Stupid allocation time!") 201 | .set_text(&format!( 202 | "Choose a file to allocate something for a layout of {layout:?}" 203 | )) 204 | .show_confirm() 205 | .unwrap() 206 | } 207 | 208 | #[cfg(not(feature = "interactive"))] 209 | { 210 | // if we're not interactive we don't ask the user if they want to 211 | // allocate stuff lol 212 | true 213 | } 214 | } 215 | 216 | // potentially returns a path to the file of the next allocation 217 | fn get_alloc_file_path() -> Option { 218 | #[cfg(feature = "interactive")] 219 | { 220 | // this is the file dialog thing 221 | FileDialog::new().show_save_single_file().unwrap() 222 | } 223 | #[cfg(not(feature = "interactive"))] 224 | { 225 | use std::sync::atomic::AtomicU64; 226 | 227 | // create a file with an increasing number for file name in the temp 228 | // folder. 229 | static ALLOC_FILE_COUNTER: AtomicU64 = AtomicU64::new(0); 230 | let path = std::env::temp_dir().join("stupidalloc"); // let's just say only one stupidalloc exists huh :) 231 | match std::fs::create_dir(&path) { 232 | Ok(_) => {} 233 | Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {} 234 | Err(e) => panic!("stupidalloc temp dir creation failed: {e}"), 235 | }; 236 | 237 | Some(path.join(format!( 238 | "alloc_{:010}.mem", 239 | ALLOC_FILE_COUNTER.fetch_add(1, Ordering::SeqCst) 240 | ))) 241 | } 242 | } 243 | 244 | /// The stupid allocator. 245 | /// 246 | /// See the [top-level documentation][crate] for more details. 247 | pub struct StupidAlloc; 248 | 249 | impl StupidAlloc { 250 | /// Enables or disables stupid allocation in the current thread, depending 251 | /// on the value passed as parameter. Passing `true` enables it, and `false` 252 | /// disables it. 253 | pub fn enable_in_thread(&self, value: bool) { 254 | LOCAL_SWITCH_OFF.with(|l| l.store(!value, Ordering::SeqCst)); 255 | } 256 | 257 | /// Return a [`HashMap`] where the key is an address of an allocation and 258 | /// the value is a [`PathBuf`]. 259 | pub fn state(&self) -> HashMap { 260 | STUPID_MAP 261 | .read() 262 | .unwrap() 263 | .iter() 264 | .map(|(&addr, handle)| (addr, handle.path.clone())) 265 | .collect() 266 | } 267 | 268 | /// Returns the [`PathBuf`] of the allocation of an element if it has been 269 | /// allocated with the stupid alloc. 270 | pub fn file_of(&self, value: &T) -> Option { 271 | STUPID_MAP 272 | .read() 273 | .unwrap() 274 | .iter() 275 | .find_map(|(&addr, handle)| { 276 | if (addr..addr + handle.map.len()) 277 | .contains(&(value as *const T as *const u8 as usize)) 278 | { 279 | Some(handle.path.clone()) 280 | } else { 281 | None 282 | } 283 | }) 284 | } 285 | 286 | /// Opens a graphical window displaying the memory contents of the data 287 | /// passed as a parameter, if it was allocated with stupid alloc. You must also 288 | /// specify the number of bytes displayed on each row using the `columns` 289 | /// parameter. 290 | #[cfg(feature = "graphics")] 291 | #[doc(cfg(feature = "graphics"))] 292 | pub fn open_window_of(&self, value: &T, columns: usize) { 293 | STUPID_MAP 294 | .write() 295 | .unwrap() 296 | .iter_mut() 297 | .for_each(|(&addr, handle)| { 298 | if (addr..addr + handle.map.len()) 299 | .contains(&(value as *const T as *const u8 as usize)) 300 | { 301 | if let Some(window) = handle.window.as_mut() { 302 | if window.is_finished() { 303 | *window = graphics::Window::new( 304 | &handle.path, 305 | Arc::clone(&handle.file), 306 | columns, 307 | ); 308 | } 309 | } else { 310 | handle.window = Some(graphics::Window::new( 311 | &handle.path, 312 | Arc::clone(&handle.file), 313 | columns, 314 | )); 315 | } 316 | } 317 | }) 318 | } 319 | 320 | /// If a graphical window is currently open for `value`, this sets its 321 | /// number of columns: the number of bytes (or groups of 8 bits) on each row. 322 | #[cfg(feature = "graphics")] 323 | #[doc(cfg(feature = "graphics"))] 324 | pub fn set_columns_of(&self, value: &T, columns: usize) { 325 | STUPID_MAP 326 | .write() 327 | .unwrap() 328 | .iter_mut() 329 | .for_each(|(&addr, handle)| { 330 | if (addr..addr + handle.map.len()) 331 | .contains(&(value as *const T as *const u8 as usize)) 332 | { 333 | if let Some(window) = handle.window.as_mut() { 334 | window 335 | .tx 336 | .send(graphics::Message::Resize { columns }) 337 | .unwrap(); 338 | } 339 | } 340 | }) 341 | } 342 | 343 | /// Closes any graphical window associated with `value`. 344 | #[cfg(feature = "graphics")] 345 | pub fn close_graphics_of(&self, value: &T) { 346 | STUPID_MAP 347 | .write() 348 | .unwrap() 349 | .iter_mut() 350 | .for_each(|(&addr, handle)| { 351 | if (addr..addr + handle.map.len()) 352 | .contains(&(value as *const T as *const u8 as usize)) 353 | { 354 | if let Some(window) = handle.window.take() { 355 | window.close() 356 | } 357 | } 358 | }) 359 | } 360 | 361 | // this function abstracts Allocator::allocate and Allocator::allocate_zeroed 362 | // since the only way to allocate memory with stupid alloc is to have the 363 | // contents zeroed already. in the spirit of not duplicating code, the 364 | // fallback (either System::allocate or System::allocate_zeroed) is passed 365 | // as a parameter. 366 | fn inner_allocate(&self, layout: Layout, fallback: F) -> Result, AllocError> 367 | where 368 | F: Fn(Layout) -> Result, AllocError>, 369 | { 370 | // we only allocate if 371 | // - we're allowed to 372 | // - we're not currently allocating with stupid alloc 373 | // - we're not currently de-allocating something from stupid alloc 374 | if LOCAL_SWITCH_OFF.with(|l| l.load(Ordering::SeqCst)) 375 | || DEALLOCATING.with(|d| d.load(Ordering::SeqCst)) != 0 376 | || ALLOCATING.with(|a| a.load(Ordering::SeqCst)) != 0 377 | { 378 | // THIS IS STUPIDALLOC BITCH!!! we clown in this muthafucka betta 379 | // take yo sensitive ass back to System 380 | fallback(layout) 381 | } else { 382 | // okay so first we tell the thread that we're allocating. 383 | // no recursive allocation allowed this bricked my PC twice already. 384 | ALLOCATING.with(|a| a.fetch_add(1, Ordering::SeqCst)); 385 | let result = { 386 | if confirm_alloc(layout) { 387 | let path = get_alloc_file_path(); 388 | 389 | if let Some(path) = path { 390 | let file = OpenOptions::new() 391 | .read(true) 392 | .write(true) 393 | .truncate(true) 394 | .create(true) 395 | .open(&path) 396 | .unwrap(); 397 | 398 | file.set_len(layout.size() as u64).unwrap(); 399 | let mut map = unsafe { MmapOptions::new().map_mut(&file).unwrap() }; 400 | 401 | let ptr = NonNull::from_raw_parts( 402 | NonNull::new(map.as_mut_ptr() as *mut ()).unwrap(), 403 | layout.size(), 404 | ); 405 | 406 | // do some logging if we're told to 407 | #[cfg(feature = "logging")] 408 | let log_file = { 409 | let mut log_path = path.clone(); 410 | log_path.set_extension("md"); 411 | 412 | let mut log_file = OpenOptions::new() 413 | .read(true) 414 | .write(true) 415 | .truncate(true) 416 | .create(true) 417 | .open(log_path) 418 | .unwrap(); 419 | 420 | writeln!( 421 | log_file, 422 | "# Metadata\n- Allocation path: {}\n- Layout: {layout:?}\n\n# Allocation\n```\n{}\n```\n\n# Events\n", 423 | path.to_string_lossy(), 424 | Backtrace::capture() 425 | ) 426 | .unwrap(); 427 | 428 | log_file 429 | }; 430 | 431 | // it's probably not necessary to specify System for 432 | // this arc, but better be safe. 433 | let file = Arc::new_in(file, System); 434 | 435 | // we have graphics? decide if we start with a window 436 | // for this alloc. 437 | #[cfg(feature = "graphics")] 438 | let window = { 439 | // the feature is enabled: go wild! 440 | #[cfg(feature = "always-graphics")] 441 | { 442 | Some(graphics::Window::new( 443 | &path, 444 | Arc::clone(&file), 445 | DEFAULT_GRAPHICS_COLUMNS.load(Ordering::SeqCst), 446 | )) 447 | } 448 | // or not: no 449 | #[cfg(not(feature = "always-graphics"))] 450 | { 451 | None 452 | } 453 | }; 454 | 455 | STUPID_MAP.write().unwrap().insert( 456 | ptr.as_ptr() as *mut u8 as usize, 457 | AllocHandle { 458 | file, 459 | map, 460 | path, 461 | #[cfg(feature = "graphics")] 462 | window, 463 | #[cfg(feature = "logging")] 464 | log_file, 465 | }, 466 | ); 467 | 468 | Ok(ptr) 469 | } else { 470 | Err(AllocError) 471 | } 472 | } else { 473 | Err(AllocError) 474 | } 475 | }; 476 | 477 | // okay finally tell the thread we finished this allocation. if it's 478 | // back to zero we can potentially stupid alloc again! 479 | ALLOCATING.with(|a| a.fetch_sub(1, Ordering::SeqCst)); 480 | 481 | result 482 | } 483 | } 484 | 485 | // like inner_allocate, this abstracts over grow, shrink and grow_zeroed, 486 | // since the implementation is the same for all of them, except which 487 | // function to use as a fallback. 488 | unsafe fn grow_or_shrink( 489 | &self, 490 | ptr: NonNull, 491 | old_layout: Layout, 492 | new_layout: Layout, 493 | fallback: F, 494 | ) -> Result, AllocError> 495 | where 496 | F: Fn(NonNull, Layout, Layout) -> Result, AllocError>, 497 | { 498 | let addr: usize = ptr.as_ptr() as usize; 499 | 500 | // same as allocate; if any of these is nonzero / true, we're guaranteed 501 | // the data was allocated by system. 502 | if LOCAL_SWITCH_OFF.with(|l| l.load(Ordering::SeqCst)) 503 | || DEALLOCATING.with(|d| d.load(Ordering::SeqCst)) != 0 504 | || ALLOCATING.with(|a| a.load(Ordering::SeqCst)) != 0 505 | { 506 | fallback(ptr, old_layout, new_layout) 507 | } else if STUPID_MAP.read().unwrap().contains_key(&addr) { 508 | let handle = STUPID_MAP.write().unwrap().remove(&addr).unwrap(); 509 | 510 | // grow or shrink, and growing zeroes stuff out. 511 | handle.file.set_len(new_layout.size() as u64).unwrap(); 512 | 513 | // new memory mapping to reflect new size. 514 | let mut map = unsafe { 515 | MmapOptions::new() 516 | .map_mut(&handle.file as &File /* thanks, memmap2 (sarcasm) */) 517 | .unwrap() 518 | }; 519 | 520 | // tell the window the size has changed 521 | #[cfg(feature = "graphics")] 522 | let window = { 523 | let mut window = handle.window; 524 | if let Some(window) = window.as_mut() { 525 | window.tx.send(graphics::Message::Grow).unwrap(); 526 | } 527 | window 528 | }; 529 | 530 | // log the event 531 | #[cfg(feature = "logging")] 532 | let log_file = { 533 | let mut log_file = handle.log_file; 534 | writeln!( 535 | log_file, 536 | "## Resize\nNew layout: {new_layout:?}\n```\n{}\n```\n", 537 | Backtrace::capture() 538 | ) 539 | .unwrap(); 540 | log_file 541 | }; 542 | 543 | let ptr = NonNull::from_raw_parts( 544 | NonNull::new(map.as_mut_ptr() as *mut ()).unwrap(), 545 | new_layout.size(), 546 | ); 547 | 548 | STUPID_MAP.write().unwrap().insert( 549 | ptr.as_ptr() as *mut u8 as usize, 550 | AllocHandle { 551 | file: handle.file, 552 | map, 553 | path: handle.path, 554 | #[cfg(feature = "graphics")] 555 | window, 556 | #[cfg(feature = "logging")] 557 | log_file, 558 | }, 559 | ); 560 | 561 | Ok(ptr) 562 | } else { 563 | // this really shouldn't happen i think. 564 | unreachable!( 565 | "invariants specify stupid alloc resize, but pointer not in stupid alloc registry" 566 | ) 567 | } 568 | } 569 | } 570 | 571 | impl fmt::Display for StupidAlloc { 572 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 573 | writeln!(f, "Stupid allocation state:")?; 574 | self.state().into_iter().try_for_each(|(addr, path)| { 575 | writeln!(f, "- 0x{addr:08x} @ {}", path.to_string_lossy()) 576 | })?; 577 | Ok(()) 578 | } 579 | } 580 | 581 | unsafe impl Allocator for StupidAlloc { 582 | fn allocate(&self, layout: Layout) -> Result, AllocError> { 583 | self.inner_allocate(layout, |layout| System.allocate(layout)) 584 | } 585 | 586 | unsafe fn grow( 587 | &self, 588 | ptr: NonNull, 589 | old_layout: Layout, 590 | new_layout: Layout, 591 | ) -> Result, AllocError> { 592 | debug_assert!( 593 | new_layout.size() >= old_layout.size(), 594 | "`new_layout.size()` must be greater than or equal to `old_layout.size()`" 595 | ); 596 | 597 | self.grow_or_shrink( 598 | ptr, 599 | old_layout, 600 | new_layout, 601 | |ptr, old_layout, new_layout| System.grow(ptr, old_layout, new_layout), 602 | ) 603 | } 604 | 605 | unsafe fn grow_zeroed( 606 | &self, 607 | ptr: NonNull, 608 | old_layout: Layout, 609 | new_layout: Layout, 610 | ) -> Result, AllocError> { 611 | debug_assert!( 612 | new_layout.size() >= old_layout.size(), 613 | "`new_layout.size()` must be greater than or equal to `old_layout.size()`" 614 | ); 615 | 616 | self.grow_or_shrink( 617 | ptr, 618 | old_layout, 619 | new_layout, 620 | |ptr, old_layout, new_layout| System.grow_zeroed(ptr, old_layout, new_layout), 621 | ) 622 | } 623 | 624 | unsafe fn shrink( 625 | &self, 626 | ptr: NonNull, 627 | old_layout: Layout, 628 | new_layout: Layout, 629 | ) -> Result, AllocError> { 630 | debug_assert!( 631 | new_layout.size() <= old_layout.size(), 632 | "`new_layout.size()` must be smaller than or equal to `old_layout.size()`" 633 | ); 634 | 635 | self.grow_or_shrink( 636 | ptr, 637 | old_layout, 638 | new_layout, 639 | |ptr, old_layout, new_layout| System.shrink(ptr, old_layout, new_layout), 640 | ) 641 | } 642 | 643 | fn allocate_zeroed(&self, layout: Layout) -> Result, AllocError> { 644 | self.inner_allocate(layout, |layout| System.allocate_zeroed(layout)) 645 | } 646 | 647 | unsafe fn deallocate(&self, ptr: NonNull, layout: Layout) { 648 | let addr: usize = ptr.as_ptr() as usize; 649 | 650 | // same as allocate, if any of these is nonzero / true, the data was 651 | // allocated by system. 652 | if LOCAL_SWITCH_OFF.with(|l| l.load(Ordering::SeqCst)) 653 | || DEALLOCATING.with(|d| d.load(Ordering::SeqCst)) != 0 654 | || ALLOCATING.with(|a| a.load(Ordering::SeqCst)) != 0 655 | { 656 | System.deallocate(ptr, layout); 657 | } else if STUPID_MAP.read().unwrap().contains_key(&addr) { 658 | // tell thread we're deallocating 659 | DEALLOCATING.with(|d| d.fetch_add(1, Ordering::SeqCst)); 660 | 661 | // remove handle from map 662 | let handle = STUPID_MAP.write().unwrap().remove(&addr).unwrap(); 663 | 664 | // log deallocation 665 | #[cfg(feature = "logging")] 666 | { 667 | let mut log_file = handle.log_file; 668 | writeln!( 669 | log_file, 670 | "# Deallocation\n```\n{}\n```", 671 | Backtrace::capture() 672 | ) 673 | .unwrap(); 674 | } 675 | 676 | // close graphical window 677 | #[cfg(feature = "graphics")] 678 | { 679 | // if there is a window, we need to destroy that first 680 | if let Some(window) = handle.window { 681 | window.tx.send(graphics::Message::Free).unwrap(); 682 | // originally i wanted to join the thread of the window 683 | // because that's what good people do, but since de-allocation 684 | // after main has ended means the threads were already killed, 685 | // we run into a weird issue where join panics because 686 | // its thread has been sweeped under itself and killed 687 | // without its consent. so for now, until i find a good way 688 | // of properly join a thread after main, let's just leave 689 | // them be. they're all going to terminate because of the 690 | // free message anyways. 691 | // 692 | // FIXME: find a way to join a thread even when it has been 693 | // killed by the end of process function. 694 | //window.close(); 695 | } 696 | } 697 | 698 | drop(handle.map); // the map needs to be dropped first 699 | drop(handle.file); // and then afterwards the file handle 700 | 701 | //std::thread::sleep(std::time::Duration::from_millis(1000)); 702 | 703 | // this needs to be done during a time where DEALLOCATING is true, 704 | // since it allocates and you'd end up in an infinite recursion. 705 | std::fs::remove_file(handle.path).unwrap(); 706 | 707 | // show a lil confirmation message box 708 | #[cfg(feature = "interactive")] 709 | let _ = MessageDialog::new() 710 | .set_type(MessageType::Info) 711 | .set_title("Stupid deallocation done!") 712 | .set_text(&format!( 713 | "Allocation of layout {layout:?} at address 0x{addr:08x} free'd!" 714 | )) 715 | .show_confirm() 716 | .unwrap(); 717 | 718 | // tell thread we're done deallocating 719 | DEALLOCATING.with(|a| a.fetch_sub(1, Ordering::SeqCst)); 720 | } else { 721 | unreachable!("invariants specify stupid alloc deallocation, but data not present in stupid alloc registry") 722 | } 723 | } 724 | } 725 | 726 | unsafe impl GlobalAlloc for StupidAlloc { 727 | unsafe fn alloc(&self, layout: Layout) -> *mut u8 { 728 | ::allocate(self, layout) 729 | .unwrap() 730 | .as_ptr() as _ 731 | } 732 | 733 | unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { 734 | ::deallocate(self, NonNull::new(ptr as _).unwrap(), layout) 735 | } 736 | 737 | unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { 738 | ::allocate_zeroed(self, layout) 739 | .unwrap() 740 | .as_ptr() as _ 741 | } 742 | } 743 | --------------------------------------------------------------------------------