mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 02:48:50 +00:00
* Prepare to upgrade zig
* zig fmt
* AllocGate
* Update data_url.zig
* wip
* few files
* just headers now?
* I think everything works?
* Update mimalloc
* Update hash_map.zig
* Perf improvements to compensate for Allocgate
* Bump
* 📷
* Update bun.lockb
* Less branching
* [js parser] Slightly reduce memory usage
* Update js_parser.zig
* WIP remove unused
* [JS parser] WIP support for `with` keyword
* Remove more dead code
* Fix all the build errors!
* cleanup
* Move `network_thread` up
* Bump peechy
* Update README.md
49 lines
1.4 KiB
Zig
49 lines
1.4 KiB
Zig
const std = @import("std");
|
|
|
|
pub fn ObjectPool(comptime Type: type, comptime Init: (fn (allocator: std.mem.Allocator) anyerror!Type), comptime threadsafe: bool) type {
|
|
return struct {
|
|
const LinkedList = std.SinglyLinkedList(Type);
|
|
const Data = if (threadsafe)
|
|
struct {
|
|
pub threadlocal var list: LinkedList = undefined;
|
|
pub threadlocal var loaded: bool = false;
|
|
}
|
|
else
|
|
struct {
|
|
pub var list: LinkedList = undefined;
|
|
pub var loaded: bool = false;
|
|
};
|
|
|
|
const data = Data;
|
|
pub const Node = LinkedList.Node;
|
|
|
|
pub fn get(allocator: std.mem.Allocator) *LinkedList.Node {
|
|
if (data.loaded) {
|
|
if (data.list.popFirst()) |node| {
|
|
node.data.reset();
|
|
return node;
|
|
}
|
|
}
|
|
|
|
var new_node = allocator.create(LinkedList.Node) catch unreachable;
|
|
new_node.* = LinkedList.Node{
|
|
.data = Init(
|
|
allocator,
|
|
) catch unreachable,
|
|
};
|
|
|
|
return new_node;
|
|
}
|
|
|
|
pub fn release(node: *LinkedList.Node) void {
|
|
if (data.loaded) {
|
|
data.list.prepend(node);
|
|
return;
|
|
}
|
|
|
|
data.list = LinkedList{ .first = node };
|
|
data.loaded = true;
|
|
}
|
|
};
|
|
}
|