aboutsummaryrefslogtreecommitdiff
path: root/src/allocators.zig
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-12-01 04:49:57 -0800
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-12-16 19:18:51 -0800
commit118ed4d2abdc33afe86b8f7c371caed08bfe6c73 (patch)
tree2e3269c63305c55ea5ca3d61ac10b8d6101744fe /src/allocators.zig
parent9fcd2c53c874c27f143c16d48618b2c81be01d55 (diff)
downloadbun-118ed4d2abdc33afe86b8f7c371caed08bfe6c73.tar.gz
bun-118ed4d2abdc33afe86b8f7c371caed08bfe6c73.tar.zst
bun-118ed4d2abdc33afe86b8f7c371caed08bfe6c73.zip
[bun install] Generate a lockfile
Diffstat (limited to 'src/allocators.zig')
-rw-r--r--src/allocators.zig20
1 files changed, 19 insertions, 1 deletions
diff --git a/src/allocators.zig b/src/allocators.zig
index 8d1788f51..8be1227c1 100644
--- a/src/allocators.zig
+++ b/src/allocators.zig
@@ -247,6 +247,10 @@ pub fn BSSStringList(comptime _count: usize, comptime _item_length: usize) type
// only need the mutex on append
var mutex: Mutex = undefined;
+ const EmptyType = struct {
+ len: usize = 0,
+ };
+
pub fn init(allocator: *std.mem.Allocator) *Self {
if (!loaded) {
instance = Self{
@@ -277,6 +281,16 @@ pub fn BSSStringList(comptime _count: usize, comptime _item_length: usize) type
return constStrToU8(appended);
}
+ pub fn getMutable(self: *Self, len: usize) ![]u8 {
+ return try self.appendMutable(EmptyType, EmptyType{ .len = len });
+ }
+
+ pub fn print(self: *Self, comptime fmt: []const u8, args: anytype) ![]const u8 {
+ var buf = try self.appendMutable(EmptyType, EmptyType{ .len = std.fmt.count(fmt, args) + 1 });
+ buf[buf.len - 1] = 0;
+ return std.fmt.bufPrint(buf.ptr[0 .. buf.len - 1], fmt, args) catch unreachable;
+ }
+
pub fn append(self: *Self, comptime AppendType: type, _value: AppendType) ![]const u8 {
mutex.lock();
defer mutex.unlock();
@@ -307,7 +321,7 @@ pub fn BSSStringList(comptime _count: usize, comptime _item_length: usize) type
) ![]const u8 {
const value_len: usize = brk: {
switch (comptime AppendType) {
- []const u8, []u8, [:0]const u8, [:0]u8 => {
+ EmptyType, []const u8, []u8, [:0]const u8, [:0]u8 => {
break :brk _value.len;
},
else => {
@@ -327,6 +341,9 @@ pub fn BSSStringList(comptime _count: usize, comptime _item_length: usize) type
backing_buf_used += value_len;
switch (AppendType) {
+ EmptyType => {
+ backing_buf[backing_buf_used - 1] = 0;
+ },
[]const u8, []u8, [:0]const u8, [:0]u8 => {
std.mem.copy(u8, backing_buf[start .. backing_buf_used - 1], _value);
backing_buf[backing_buf_used - 1] = 0;
@@ -346,6 +363,7 @@ pub fn BSSStringList(comptime _count: usize, comptime _item_length: usize) type
var value_buf = try self.allocator.alloc(u8, value_len);
switch (comptime AppendType) {
+ EmptyType => {},
[]const u8, []u8, [:0]const u8, [:0]u8 => {
std.mem.copy(u8, value_buf, _value);
},