Files
bun.sh/src/hive_array.zig
Ciro Spaciari 4515a6373e Improves Body.Value life cycle and Signal life cycle on server.zig (#2752)
* reestruct request body value and signal

* revert react-hello-world

* fix constructInto and test

* fmt

* fix body nullable

* Request can outlive RequestContext

* fmt

* BodyValue is now HiveRef

* hasDecl for Ref and HiveRef

* fix deinit call on Ref/HiveRef

* adds InlineBlob

* fix Bun.inspect when using InlineBlob

* revert InlineBlob

* Fix mimalloc-debug

* Add TODO note

* fix: make node:crypto Hash.copy work correctly (#2761)

This commit will also:
- add a CryptoHasher.copy function
- make CryptoHasher.digest reset the hasher so it can be reused

Resolves #2651

* 💅

* address unicode issue (#2763)

* Fix an oopsie

* Another oopsie

* use inline for

* Fixup

---------

Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>

* Request can outlive RequestContext

* fmt

* garantee to have the abort signnal attached to the server before abort the client on bun-server test

---------

Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Co-authored-by: Silver <14016168+silversquirl@users.noreply.github.com>
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
2023-04-28 13:40:44 -07:00

143 lines
4.2 KiB
Zig

const std = @import("std");
const assert = std.debug.assert;
const mem = std.mem;
const testing = std.testing;
/// An array that efficiently tracks which elements are in use.
/// The pointers are intended to be stable
/// Sorta related to https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p0447r15.html
pub fn HiveArray(comptime T: type, comptime capacity: u16) type {
return struct {
const Self = @This();
buffer: [capacity]T = undefined,
available: std.bit_set.IntegerBitSet(capacity) = std.bit_set.IntegerBitSet(capacity).initFull(),
pub const size = capacity;
pub fn init() Self {
return .{};
}
pub fn get(self: *Self) ?*T {
const index = self.available.findFirstSet() orelse return null;
self.available.unset(index);
return &self.buffer[index];
}
pub fn at(self: *Self, index: u16) *T {
assert(index < capacity);
return &self.buffer[index];
}
pub fn claim(self: *Self, index: u16) void {
assert(index < capacity);
assert(self.available.isSet(index));
self.available.unset(index);
}
pub fn indexOf(self: *const Self, value: *const T) ?u63 {
const start = &self.buffer;
const end = @ptrCast([*]const T, start) + capacity;
if (!(@ptrToInt(value) >= @ptrToInt(start) and @ptrToInt(value) < @ptrToInt(end)))
return null;
// aligned to the size of T
const index = (@ptrToInt(value) - @ptrToInt(start)) / @sizeOf(T);
assert(index < capacity);
assert(&self.buffer[index] == value);
return @truncate(u63, index);
}
pub fn in(self: *const Self, value: *const T) bool {
const start = &self.buffer;
const end = @ptrCast([*]const T, start) + capacity;
return (@ptrToInt(value) >= @ptrToInt(start) and @ptrToInt(value) < @ptrToInt(end));
}
pub fn put(self: *Self, value: *T) bool {
const index = self.indexOf(value) orelse return false;
assert(!self.available.isSet(index));
assert(&self.buffer[index] == value);
value.* = undefined;
self.available.set(index);
return true;
}
pub const Fallback = struct {
hive: HiveArray(T, capacity),
allocator: std.mem.Allocator,
pub const This = @This();
pub fn init(allocator: std.mem.Allocator) This {
return .{
.allocator = allocator,
.hive = HiveArray(T, capacity).init(),
};
}
pub fn get(self: *This) *T {
if (self.hive.get()) |value| {
return value;
}
return self.allocator.create(T) catch unreachable;
}
pub fn tryGet(self: *This) !*T {
if (self.hive.get()) |value| {
return value;
}
return try self.allocator.create(T);
}
pub fn put(self: *This, value: *T) void {
if (self.hive.put(value)) return;
self.allocator.destroy(value);
}
};
};
}
test "HiveArray" {
const size = 64;
// Choose an integer with a weird alignment
const Int = u127;
var a = HiveArray(Int, size).init();
{
var b = a.get().?;
try testing.expect(a.get().? != b);
try testing.expectEqual(a.indexOf(b), 0);
try testing.expect(a.put(b));
try testing.expect(a.get().? == b);
var c = a.get().?;
c.* = 123;
var d: Int = 12345;
try testing.expect(a.put(&d) == false);
try testing.expect(a.in(&d) == false);
}
a.available = @TypeOf(a.available).initFull();
{
var i: u63 = 0;
while (i < size) {
var b = a.get().?;
try testing.expectEqual(a.indexOf(b), i);
try testing.expect(a.put(b));
try testing.expect(a.get().? == b);
i = i + 1;
}
i = 0;
while (i < size) : (i += 1) {
try testing.expect(a.get() == null);
}
}
}