Files
bun.sh/src/pool.zig
Jarred Sumner e75c711c68 Upgrade to latest Zig, remove dependency on patched version of Zig (#96)
* 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
2021-12-30 21:12:32 -08:00

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;
}
};
}