aboutsummaryrefslogtreecommitdiff
path: root/src/allocators.zig
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-06-06 18:34:01 -0700
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-06-06 18:34:01 -0700
commitd49e0a5fa129152c27b70a57d1cc7a2af770577c (patch)
tree1ac581bda71fec5dfd09a6ab508a1adfca80b358 /src/allocators.zig
parente66466cc1a453db1370a199a32729441747761bb (diff)
downloadbun-d49e0a5fa129152c27b70a57d1cc7a2af770577c.tar.gz
bun-d49e0a5fa129152c27b70a57d1cc7a2af770577c.tar.zst
bun-d49e0a5fa129152c27b70a57d1cc7a2af770577c.zip
WIP node module bundles
Former-commit-id: 797b2ff557542e9d318c953b840b102695711888
Diffstat (limited to 'src/allocators.zig')
-rw-r--r--src/allocators.zig125
1 files changed, 4 insertions, 121 deletions
diff --git a/src/allocators.zig b/src/allocators.zig
index 8ebde9150..1bee408a0 100644
--- a/src/allocators.zig
+++ b/src/allocators.zig
@@ -113,7 +113,8 @@ pub const ItemStatus = enum(u3) {
const hasDeinit = std.meta.trait.hasFn("deinit")(ValueType);
-pub fn BSSList(comptime ValueType: type, comptime count: anytype) type {
+pub fn BSSList(comptime ValueType: type, comptime _count: anytype) type {
+ const count = _count * 2;
const max_index = count - 1;
var list_type: type = undefined;
var list_count = count;
@@ -233,7 +234,8 @@ pub fn BSSList(comptime ValueType: type, comptime count: anytype) type {
}
};
}
-pub fn BSSStringList(comptime count: usize, comptime item_length: usize) type {
+pub fn BSSStringList(comptime _count: usize, comptime item_length: usize) type {
+ const count = _count * 2;
const max_index = count - 1;
const ValueType = []const u8;
@@ -352,125 +354,6 @@ pub fn BSSStringList(comptime count: usize, comptime item_length: usize) type {
};
}
-pub fn TBSSStringList(comptime count: usize, comptime item_length: usize) type {
- const max_index = count - 1;
- const ValueType = []const u8;
-
- return struct {
- const Allocator = std.mem.Allocator;
- const Self = @This();
-
- pub threadlocal var slice_buf: [count][]const u8 = undefined;
- pub threadlocal var slice_buf_used: u16 = 0;
- pub threadlocal var backing_buf: [count * item_length]u8 = undefined;
- pub threadlocal var backing_buf_used: u64 = undefined;
- pub threadlocal var instance: Self = undefined;
- pub const ListIndex = packed struct {
- index: u31,
- is_overflowing: bool = false,
- };
- overflow_list: std.ArrayListUnmanaged(ValueType),
- allocator: *Allocator,
-
- pub fn init(allocator: *std.mem.Allocator) *Self {
- instance = Self{
- .allocator = allocator,
- .overflow_list = std.ArrayListUnmanaged(ValueType){},
- };
-
- return &instance;
- }
-
- pub fn isOverflowing() bool {
- return slice_buf_used >= @as(u16, count);
- }
-
- pub fn at(self: *const Self, index: IndexType) ?ValueType {
- if (index.index == NotFound.index or index.index == Unassigned.index) return null;
-
- if (index.is_overflowing) {
- return &self.overflow_list.items[index.index];
- } else {
- return &slice_buf[index.index];
- }
- }
-
- pub fn exists(self: *Self, value: ValueType) bool {
- return isSliceInBuffer(value, slice_buf);
- }
-
- pub fn editableSlice(slice: []const u8) []u8 {
- return constStrToU8(slice);
- }
-
- pub fn append(self: *Self, _value: anytype) ![]const u8 {
- var value = _value;
- if (value.len + backing_buf_used < backing_buf.len - 1) {
- const start = backing_buf_used;
- backing_buf_used += value.len;
- std.mem.copy(u8, backing_buf[start..backing_buf_used], _value);
- value = backing_buf[start..backing_buf_used];
- } else {
- value = try self.allocator.dupe(u8, _value);
- }
-
- var result = ListIndex{ .index = std.math.maxInt(u31), .is_overflowing = slice_buf_used > max_index };
-
- if (result.is_overflowing) {
- result.index = @intCast(u31, self.overflow_list.items.len);
- } else {
- result.index = slice_buf_used;
- slice_buf_used += 1;
- if (slice_buf_used >= max_index) {
- self.overflow_list = try @TypeOf(self.overflow_list).initCapacity(self.allocator, count);
- }
- }
-
- if (result.is_overflowing) {
- if (self.overflow_list.items.len == result.index) {
- const real_index = self.overflow_list.items.len;
- try self.overflow_list.append(self.allocator, value);
- } else {
- self.overflow_list.items[result.index] = value;
- }
-
- return self.overflow_list.items[result.index];
- } else {
- slice_buf[result.index] = value;
-
- return slice_buf[result.index];
- }
- }
-
- pub fn remove(self: *Self, index: ListIndex) void {
- @compileError("Not implemented yet.");
- // switch (index) {
- // Unassigned.index => {
- // self.index.remove(_key);
- // },
- // NotFound.index => {
- // self.index.remove(_key);
- // },
- // 0...max_index => {
- // if (hasDeinit(ValueType)) {
- // slice_buf[index].deinit();
- // }
- // slice_buf[index] = undefined;
- // },
- // else => {
- // const i = index - count;
- // if (hasDeinit(ValueType)) {
- // self.overflow_list.items[i].deinit();
- // }
- // self.overflow_list.items[index - count] = undefined;
- // },
- // }
-
- // return index;
- }
- };
-}
-
pub fn BSSMap(comptime ValueType: type, comptime count: anytype, store_keys: bool, estimated_key_length: usize) type {
const max_index = count - 1;
const BSSMapType = struct {