199 Commits

Author SHA1 Message Date
ziggoon
16875b3598 update std.heap.PageAllocator Windows implementation to remove race condition and utilize NtAllocateVirtualMemory / NtFreeVirtualMemory instead of VirtualAlloc and VirtualFree 2025-03-04 22:01:08 -06:00
Andrew Kelley
8683f25d24 std.heap.DebugAllocator: default wasm to 64K page size
including on freestanding
2025-02-22 21:06:00 -05:00
Alex Rønne Petersen
481b7bf3f0
std.Target: Remove functions that just wrap component functions.
Functions like isMinGW() and isGnuLibC() have a good reason to exist: They look
at multiple components of the target. But functions like isWasm(), isDarwin(),
isGnu(), etc only exist to save 4-8 characters. I don't think this is a good
enough reason to keep them, especially given that:

* It's not immediately obvious to a reader whether target.isDarwin() means the
  same thing as target.os.tag.isDarwin() precisely because isMinGW() and similar
  functions *do* look at multiple components.
* It's not clear where we would draw the line. The logical conclusion before
  this commit would be to also wrap Arch.isX86(), Os.Tag.isSolarish(),
  Abi.isOpenHarmony(), etc... this obviously quickly gets out of hand.
* It's nice to just have a single correct way of doing something.
2025-02-17 19:18:19 +01:00
schtvn
1b62469ec9
Fix build failure in sbrk allocator, caused by #20511 2025-02-17 15:37:19 +01:00
Meghan Denny
9142482372
std.ArrayList: popOrNull() -> pop() [v2] (#22720) 2025-02-10 04:21:31 +00:00
Andrew Kelley
42dbd35d3e std.heap.SmpAllocator: back to simple free
In practice this is fine because eventually alloc wins the race and
grabs that massive freelist.
2025-02-08 16:46:56 -08:00
Andrew Kelley
b09e3efad4 std.heap.SmpAllocator: alternate freelist accounting
Freelist length accounting in alloc had a negative impact, especially
with the integer type bumped up to u16, so I changed the system to be
based on counting slabs rather than total allocations.
2025-02-08 16:20:51 -08:00
Andrew Kelley
bb5a4036e8 std.heap.SmpAllocator; fix freelist accounting 2025-02-08 15:07:03 -08:00
Andrew Kelley
1754e014f5 std.heap.SmpAllocator: rotate on free sometimes
* slab length reduced to 64K
* track freelist length with u8s
* on free(), rotate if freelist length exceeds max_freelist_len

Prevents memory leakage in the scenario where one thread only allocates
and another thread only frees.
2025-02-07 14:41:49 -08:00
Andrew Kelley
a9d3005616 std.heap.SmpAllocator: fix detection of slab end 2025-02-07 14:41:49 -08:00
Andrew Kelley
3246150d45 std.heap.SmpAllocator: fix getCpuCount logic
it was always returning max_cpu_count
2025-02-07 14:41:49 -08:00
Andrew Kelley
88e2e60e88 std.heap.SmpAllocator: simplify by putting freelist node at start 2025-02-07 14:41:49 -08:00
Andrew Kelley
1ffae59fec std.heap.SmpAllocator: fix using wrong size class indices 2025-02-07 14:41:49 -08:00
Andrew Kelley
839c453d88 std.heap.SmpAllocator: eliminate the global mutex 2025-02-07 14:41:49 -08:00
Andrew Kelley
60765a9ee2 std.heap.SmpAllocator: implement searching on alloc
rotate a couple times before resorting to mapping more memory.
2025-02-07 14:41:49 -08:00
Andrew Kelley
84bf7a6701 std.heap.SmpAllocator: 256K slab_len
and no need for special handling of wasi and windows since we don't ask
for anything more than page-aligned.
2025-02-07 12:20:12 -08:00
Andrew Kelley
3d7c5cf64a std.heap: test smp_allocator 2025-02-07 12:20:12 -08:00
Andrew Kelley
51c4ffa410 add std.heap.SmpAllocator
An allocator intended to be used in -OReleaseFast mode when
multi-threading is enabled.
2025-02-07 12:20:12 -08:00
Andrew Kelley
b8f5cfed45 std.heap.SbrkAllocator: fix typo 2025-02-06 14:50:55 -08:00
Andrew Kelley
d0e1a6a23d std.heap.DebugAllocator: update unit tests for new impl
No longer need this windows-specific behavior.
2025-02-06 14:23:23 -08:00
Andrew Kelley
960190643a std.heap.DebugAllocator: make page size configurable 2025-02-06 14:23:23 -08:00
Andrew Kelley
cd99ab3229 std.heap: rename GeneralPurposeAllocator to DebugAllocator 2025-02-06 14:23:23 -08:00
Andrew Kelley
8282565ce5 std.heap.GeneralPurposeAllocator: fix UAF in resizeLarge
There was an ensureUnusedCapacity() call that invalidated a looked-up
hash table entry. Move it earlier.
2025-02-06 14:23:23 -08:00
Andrew Kelley
8626191549 std.heap.GeneralPurposeAllocator: fix slot_counts calculation
In larger small buckets, the comptime logic that computed slot count did
not verify that the number it produced was valid. Now it verifies it,
which made this bug into a compile error. Then I fixed the bug by
introducing a "minimum slots per bucket" declaration.
2025-02-06 14:23:23 -08:00
Andrew Kelley
d20d934a8a std: fix compilation under -lc 2025-02-06 14:23:23 -08:00
Andrew Kelley
def36f2e44 std.heap.GeneralPurposeAllocator: usize for used_bits
improves leak checking performance.
2025-02-06 14:23:23 -08:00
Andrew Kelley
c8e807c44e std.heap.GeneralPurposeAllocator: use for loops in leak check 2025-02-06 14:23:23 -08:00
Andrew Kelley
b14a350430 std.heap.GeneralPurposeAllocator: reduce page size to 512K
and fix compilation on 32-bit targets
2025-02-06 14:23:23 -08:00
Andrew Kelley
00b723dc9d std.heap.WasmAllocator: update to new Allocator API 2025-02-06 14:23:23 -08:00
Andrew Kelley
82b5a1d313 std.heap.GeneralPurposeAllocator: implement resize and remap 2025-02-06 14:23:23 -08:00
Andrew Kelley
0e0f0c9625 std.heap.GeneralPurposeAllocator: check canary in free 2025-02-06 14:23:23 -08:00
Andrew Kelley
8ff7481e82 std.heap.GeneralPurposeAllocator: inline small allocation metadata
Put the small allocation metadata directly into the (large) pages
allocated.
2025-02-06 14:23:23 -08:00
Andrew Kelley
1a6d87d699 std.heap.ThreadSafeAllocator: update to new Allocator API 2025-02-06 14:23:23 -08:00
Andrew Kelley
36e9b0f026 std.mem.Allocator: keep the undefined memset
Reversal on the decision: the Allocator interface is the correct place
for the memset to undefined because it allows Allocator implementations
to bypass the interface and use a backing allocator directly, skipping
the performance penalty of memsetting the entire allocation, which may
be very large, as well as having valuable zeroes on them.

closes #4298
2025-02-06 14:23:23 -08:00
Andrew Kelley
601f632c27 std.heap.GeneralPurposeAllocator: fix large alloc accounting
when mremap relocates an allocation
2025-02-06 14:23:23 -08:00
Andrew Kelley
f1717777a2 std.heap: delete LoggingAllocator and friends
I don't think these belong in std, at least not in their current form.

If someone wants to add these back I'd like to review the patch before
it lands.

Reverts 629e2e784495dd8ac91493fa7bb11e1772698e42
2025-02-06 14:23:23 -08:00
Andrew Kelley
0d8166be3f std: update to new Allocator API 2025-02-06 14:23:23 -08:00
Andrew Kelley
a4d4e086c5 introduce std.posix.mremap and use it
in std.heap.page_allocator
2025-02-06 14:23:23 -08:00
Andrew Kelley
7eeef5fb2b std.mem.Allocator: introduce remap function to the interface
This one changes the size of an allocation, allowing it to be relocated.
However, the implementation will still return `null` if it would be
equivalent to

new = alloc
memcpy(new, old)
free(old)

Mainly this prepares for taking advantage of `mremap` which I thought
would be a bigger deal but apparently is only available on Linux. Still,
we should use it on Linux.
2025-02-06 14:23:23 -08:00
Andrew Kelley
dd2fa4f75d std.heap.GeneralPurposeAllocator: runtime-known page size
no longer causes compilation failure.

This also addresses the problem of high map count causing OOM by
choosing a page size of 2MiB for most targets when the page_size_max is
smaller than this number.
2025-02-06 14:23:23 -08:00
Andrew Kelley
b23662feeb std.heap.WasmAllocator: use @splat syntax
preferred over array multiplication where possible.
2025-02-06 14:23:23 -08:00
Andrew Kelley
91f41bdc70 std.heap.PageAllocator: restore high alignment functionality
This allocator now supports alignments greater than page size, with the
same implementation as it used before.

This is a partial revert of ceb0a632cfd6a4eada6bd27bf6a3754e95dcac86.

It looks like VirtualAlloc2 has better solutions to this problem,
including features such as MEM_RESERVE_PLACEHOLDER and MEM_LARGE_PAGES.
This possibility can be investigated as a follow-up task.
2025-02-06 14:23:23 -08:00
Andrew Kelley
4913de3c88 GeneralPurposeAllocator: minimal fix
This keeps the implementation matching master branch, however,
introduces a compile error that applications can work around by
explicitly setting page_size_max and page_size_min to match their
computer's settings, in the case that those values are not already
equal.

I plan to rework this allocator in a follow-up enhancement with the goal
of reducing total active memory mappings.
2025-02-06 14:23:23 -08:00
Andrew Kelley
95a0474dc6 revert GPA to before this branch 2025-02-06 14:23:23 -08:00
Andrew Kelley
284de7d957 adjust runtime page size APIs
* fix merge conflicts
* rename the declarations
* reword documentation
* extract FixedBufferAllocator to separate file
* take advantage of locals
* remove the assertion about max alignment in Allocator API, leaving it
  Allocator implementation defined
* fix non-inline function call in start logic

The GeneralPurposeAllocator implementation is totally broken because it
uses global state but I didn't address that in this commit.
2025-02-06 14:23:23 -08:00
Archbirdplus
439667be04 runtime page size detection
heap.zig: define new default page sizes
heap.zig: add min/max_page_size and their options
lib/std/c: add miscellaneous declarations
heap.zig: add pageSize() and its options
switch to new page sizes, especially in GPA/stdlib
mem.zig: remove page_size
2025-02-06 14:23:23 -08:00
mlugg
dc5c827847
std.heap.GeneralPurposeAllocator: disable some tests on wasm32-wasi
The ZON PR (#20271) is causing these tests to inexplicably fail. It
doesn't seem like that PR is what's breaking GPA, so these tests are now
disabled. This is tracked by #22731.
2025-02-03 09:17:52 +00:00
Andrew Kelley
fecdc53a48 delete std.heap.WasmPageAllocator
This allocator has no purpose since it cannot truly fulfill the role of
page allocation, and std.heap.wasm_allocator is better both in terms of
performance and code size.

This commit redefines `std.heap.page_allocator` to be less strict:

"On operating systems that support memory mapping, this allocator makes
a syscall directly for every allocation and free. Otherwise, it falls
back to the preferred singleton for the target. Thread-safe."

This now matches how it was actually being implemented, and matches its
use sites - which are mainly as the backing allocator for
`std.heap.ArenaAllocator`.
2025-01-29 21:10:20 -08:00
John Benediktsson
884b1423a4 std.heap.memory_pool: make preheat() usable after init() 2025-01-28 00:06:54 +01:00
Krzysztof Wolicki
04c182274c Fix index calculation in WasmPageAllocator 2024-10-12 22:53:02 +02:00