Files
bun.sh/src/paths/path_buffer_pool.zig
taylor.fish a57dee5721 Various safety improvements (safety.ThreadLock, stack traces, MimallocArena, RefCount, safety.alloc) (#21726)
* Move `DebugThreadLock` to `bun.safety`
* Enable in `ci_assert` builds, but store stack traces only in debug
builds
  * Reduce size of struct by making optional field non-optional
* Add `initLockedIfNonComptime` as a workaround for not being able to
call `initLocked` in comptime contexts
* Add `lockOrAssert` method to acquire the lock if unlocked, or else
assert that the current thread acquired the lock
* Add stack traces to `CriticalSection` and `AllocPtr` in debug builds
* Make `MimallocArena.init` infallible
* Make `MimallocArena.heap` non-nullable
* Rename `RefCount.active_counts` to `raw_count` and provide read-only
`get` method
* Add `bun.safety.alloc.assertEq` to assert that two allocators are
equal (avoiding comparison of undefined `ptr`s)

(For internal tracking: fixes STAB-917, STAB-918, STAB-962, STAB-963,
STAB-964, STAB-965)

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2025-08-11 13:40:07 -07:00

35 lines
1.3 KiB
Zig

// This pool exists because on Windows, each path buffer costs 64 KB.
// This makes the stack memory usage very unpredictable, which means we can't really know how much stack space we have left.
// This pool is a workaround to make the stack memory usage more predictable.
// We keep up to 4 path buffers alive per thread at a time.
fn PathBufferPoolT(comptime T: type) type {
return struct {
const Pool = ObjectPool(T, null, true, 4);
pub fn get() *T {
// use a thread-local allocator so mimalloc deletes it on thread deinit.
return &Pool.get(bun.threadLocalAllocator()).data;
}
pub fn put(buffer: *const T) void {
// there's no deinit function on T so @constCast is fine
var node: *Pool.Node = @alignCast(@fieldParentPtr("data", @constCast(buffer)));
node.release();
}
pub fn deleteAll() void {
Pool.deleteAll();
}
};
}
pub const path_buffer_pool = PathBufferPoolT(PathBuffer);
pub const w_path_buffer_pool = PathBufferPoolT(WPathBuffer);
pub const os_path_buffer_pool = if (Environment.isWindows) w_path_buffer_pool else path_buffer_pool;
const bun = @import("bun");
const Environment = bun.Environment;
const ObjectPool = bun.ObjectPool;
const PathBuffer = bun.PathBuffer;
const WPathBuffer = bun.WPathBuffer;