Skip to content

Commit

Permalink
Merge pull request #18067 from ziglang/use-BoundedArray-less
Browse files Browse the repository at this point in the history
std: use BoundedArray less
  • Loading branch information
andrewrk authored Nov 22, 2023
2 parents d5e21a4 + a34a51e commit e4977f3
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 163 deletions.
35 changes: 18 additions & 17 deletions deps/aro/aro/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -582,10 +582,9 @@ fn generateFloatMacros(w: anytype, prefix: []const u8, semantics: target_util.FP
},
);

var defPrefix = std.BoundedArray(u8, 32).init(0) catch unreachable;
defPrefix.writer().print("__{s}_", .{prefix}) catch return error.OutOfMemory;

const prefix_slice = defPrefix.constSlice();
var def_prefix_buf: [32]u8 = undefined;
const prefix_slice = std.fmt.bufPrint(&def_prefix_buf, "__{s}_", .{prefix}) catch
return error.OutOfMemory;

try w.print("#define {s}DENORM_MIN__ {s}{s}\n", .{ prefix_slice, denormMin, ext });
try w.print("#define {s}HAS_DENORM__\n", .{prefix_slice});
Expand Down Expand Up @@ -770,18 +769,18 @@ fn generateExactWidthType(comp: *const Compilation, w: anytype, mapper: StrInt.T
ty = if (unsigned) comp.types.int64.makeIntegerUnsigned() else comp.types.int64;
}

var prefix = std.BoundedArray(u8, 16).init(0) catch unreachable;
prefix.writer().print("{s}{d}", .{ if (unsigned) "__UINT" else "__INT", width }) catch return error.OutOfMemory;
var buffer: [16]u8 = undefined;
const suffix = "_TYPE__";
const full = std.fmt.bufPrint(&buffer, "{s}{d}{s}", .{
if (unsigned) "__UINT" else "__INT", width, suffix,
}) catch return error.OutOfMemory;

{
const len = prefix.len;
defer prefix.resize(len) catch unreachable; // restoring previous size
prefix.appendSliceAssumeCapacity("_TYPE__");
try generateTypeMacro(w, mapper, prefix.constSlice(), ty, comp.langopts);
}
try generateTypeMacro(w, mapper, full, ty, comp.langopts);

const prefix = full[0 .. full.len - suffix.len]; // remove "_TYPE__"

try comp.generateFmt(prefix.constSlice(), w, ty);
try comp.generateSuffixMacro(prefix.constSlice(), w, ty);
try comp.generateFmt(prefix, w, ty);
try comp.generateSuffixMacro(prefix, w, ty);
}

pub fn hasFloat128(comp: *const Compilation) bool {
Expand Down Expand Up @@ -908,10 +907,12 @@ fn generateExactWidthIntMax(comp: *const Compilation, w: anytype, specifier: Typ
ty = if (unsigned) comp.types.int64.makeIntegerUnsigned() else comp.types.int64;
}

var name = std.BoundedArray(u8, 6).init(0) catch unreachable;
name.writer().print("{s}{d}", .{ if (unsigned) "UINT" else "INT", bit_count }) catch return error.OutOfMemory;
var name_buffer: [6]u8 = undefined;
const name = std.fmt.bufPrint(&name_buffer, "{s}{d}", .{
if (unsigned) "UINT" else "INT", bit_count,
}) catch return error.OutOfMemory;

return comp.generateIntMax(w, name.constSlice(), ty);
return comp.generateIntMax(w, name, ty);
}

fn generateIntWidth(comp: *Compilation, w: anytype, name: []const u8, ty: Type) !void {
Expand Down
41 changes: 24 additions & 17 deletions deps/aro/aro/Driver/GCCDetector.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn appendToolPath(self: *const GCCDetector, tc: *Toolchain) !void {
}, .program);
}

fn addDefaultGCCPrefixes(prefixes: *PathPrefixes, tc: *const Toolchain) !void {
fn addDefaultGCCPrefixes(prefixes: *std.ArrayListUnmanaged([]const u8), tc: *const Toolchain) !void {
const sysroot = tc.getSysroot();
const target = tc.getTarget();
if (sysroot.len == 0 and target.os.tag == .linux and tc.filesystem.exists("/opt/rh")) {
Expand Down Expand Up @@ -57,14 +57,12 @@ fn addDefaultGCCPrefixes(prefixes: *PathPrefixes, tc: *const Toolchain) !void {
}
}

const PathPrefixes = std.BoundedArray([]const u8, 16);

fn collectLibDirsAndTriples(
tc: *Toolchain,
lib_dirs: *PathPrefixes,
triple_aliases: *PathPrefixes,
biarch_libdirs: *PathPrefixes,
biarch_triple_aliases: *PathPrefixes,
lib_dirs: *std.ArrayListUnmanaged([]const u8),
triple_aliases: *std.ArrayListUnmanaged([]const u8),
biarch_libdirs: *std.ArrayListUnmanaged([]const u8),
biarch_triple_aliases: *std.ArrayListUnmanaged([]const u8),
) !void {
const AArch64LibDirs: [2][]const u8 = .{ "/lib64", "/lib" };
const AArch64Triples: [4][]const u8 = .{ "aarch64-none-linux-gnu", "aarch64-linux-gnu", "aarch64-redhat-linux", "aarch64-suse-linux" };
Expand Down Expand Up @@ -408,10 +406,18 @@ pub fn discover(self: *GCCDetector, tc: *Toolchain) !void {
else
target_util.get32BitArchVariant(target);

var candidate_lib_dirs: PathPrefixes = .{};
var candidate_triple_aliases: PathPrefixes = .{};
var candidate_biarch_lib_dirs: PathPrefixes = .{};
var candidate_biarch_triple_aliases: PathPrefixes = .{};
var candidate_lib_dirs_buffer: [16][]const u8 = undefined;
var candidate_lib_dirs = std.ArrayListUnmanaged([]const u8).initBuffer(&candidate_lib_dirs_buffer);

var candidate_triple_aliases_buffer: [16][]const u8 = undefined;
var candidate_triple_aliases = std.ArrayListUnmanaged([]const u8).initBuffer(&candidate_triple_aliases_buffer);

var candidate_biarch_lib_dirs_buffer: [16][]const u8 = undefined;
var candidate_biarch_lib_dirs = std.ArrayListUnmanaged([]const u8).initBuffer(&candidate_biarch_lib_dirs_buffer);

var candidate_biarch_triple_aliases_buffer: [16][]const u8 = undefined;
var candidate_biarch_triple_aliases = std.ArrayListUnmanaged([]const u8).initBuffer(&candidate_biarch_triple_aliases_buffer);

try collectLibDirsAndTriples(
tc,
&candidate_lib_dirs,
Expand All @@ -433,7 +439,8 @@ pub fn discover(self: *GCCDetector, tc: *Toolchain) !void {
}
}

var prefixes: PathPrefixes = .{};
var prefixes_buf: [16][]const u8 = undefined;
var prefixes = std.ArrayListUnmanaged([]const u8).initBuffer(&prefixes_buf);
const gcc_toolchain_dir = gccToolchainDir(tc);
if (gcc_toolchain_dir.len != 0) {
const adjusted = if (gcc_toolchain_dir[gcc_toolchain_dir.len - 1] == '/')
Expand All @@ -455,10 +462,10 @@ pub fn discover(self: *GCCDetector, tc: *Toolchain) !void {
}

const v0 = GCCVersion.parse("0.0.0");
for (prefixes.constSlice()) |prefix| {
for (prefixes.items) |prefix| {
if (!tc.filesystem.exists(prefix)) continue;

for (candidate_lib_dirs.constSlice()) |suffix| {
for (candidate_lib_dirs.items) |suffix| {
defer fib.reset();
const lib_dir = std.fs.path.join(fib.allocator(), &.{ prefix, suffix }) catch continue;
if (!tc.filesystem.exists(lib_dir)) continue;
Expand All @@ -467,17 +474,17 @@ pub fn discover(self: *GCCDetector, tc: *Toolchain) !void {
const gcc_cross_dir_exists = tc.filesystem.joinedExists(&.{ lib_dir, "/gcc-cross" });

try self.scanLibDirForGCCTriple(tc, target, lib_dir, triple_str, false, gcc_dir_exists, gcc_cross_dir_exists);
for (candidate_triple_aliases.constSlice()) |candidate| {
for (candidate_triple_aliases.items) |candidate| {
try self.scanLibDirForGCCTriple(tc, target, lib_dir, candidate, false, gcc_dir_exists, gcc_cross_dir_exists);
}
}
for (candidate_biarch_lib_dirs.constSlice()) |suffix| {
for (candidate_biarch_lib_dirs.items) |suffix| {
const lib_dir = std.fs.path.join(fib.allocator(), &.{ prefix, suffix }) catch continue;
if (!tc.filesystem.exists(lib_dir)) continue;

const gcc_dir_exists = tc.filesystem.joinedExists(&.{ lib_dir, "/gcc" });
const gcc_cross_dir_exists = tc.filesystem.joinedExists(&.{ lib_dir, "/gcc-cross" });
for (candidate_biarch_triple_aliases.constSlice()) |candidate| {
for (candidate_biarch_triple_aliases.items) |candidate| {
try self.scanLibDirForGCCTriple(tc, target, lib_dir, candidate, true, gcc_dir_exists, gcc_cross_dir_exists);
}
}
Expand Down
4 changes: 2 additions & 2 deletions deps/aro/aro/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7796,7 +7796,7 @@ fn stringLiteral(p: *Parser) Error!Result {
}
},
};
for (char_literal_parser.errors.constSlice()) |item| {
for (char_literal_parser.errors()) |item| {
try p.errExtra(item.tag, p.tok_i, item.extra);
}
}
Expand Down Expand Up @@ -7911,7 +7911,7 @@ fn charLiteral(p: *Parser) Error!Result {
char_literal_parser.err(.char_lit_too_wide, .{ .none = {} });
}

for (char_literal_parser.errors.constSlice()) |item| {
for (char_literal_parser.errors()) |item| {
try p.errExtra(item.tag, p.tok_i, item.extra);
}
}
Expand Down
24 changes: 18 additions & 6 deletions deps/aro/aro/text_literal.zig
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ pub const Parser = struct {
max_codepoint: u21,
/// We only want to issue a max of 1 error per char literal
errored: bool = false,
errors: std.BoundedArray(CharDiagnostic, 4) = .{},
errors_buffer: [4]CharDiagnostic,
errors_len: usize,
comp: *const Compilation,

pub fn init(literal: []const u8, kind: Kind, max_codepoint: u21, comp: *const Compilation) Parser {
Expand All @@ -166,6 +167,8 @@ pub const Parser = struct {
.comp = comp,
.kind = kind,
.max_codepoint = max_codepoint,
.errors_buffer = undefined,
.errors_len = 0,
};
}

Expand All @@ -178,19 +181,28 @@ pub const Parser = struct {
};
}

pub fn errors(p: *Parser) []CharDiagnostic {
return p.errors_buffer[0..p.errors_len];
}

pub fn err(self: *Parser, tag: Diagnostics.Tag, extra: Diagnostics.Message.Extra) void {
if (self.errored) return;
self.errored = true;
const diagnostic = .{ .tag = tag, .extra = extra };
self.errors.append(diagnostic) catch {
_ = self.errors.pop();
self.errors.append(diagnostic) catch unreachable;
};
if (self.errors_len == self.errors_buffer.len) {
self.errors_buffer[self.errors_buffer.len - 1] = diagnostic;
} else {
self.errors_buffer[self.errors_len] = diagnostic;
self.errors_len += 1;
}
}

pub fn warn(self: *Parser, tag: Diagnostics.Tag, extra: Diagnostics.Message.Extra) void {
if (self.errored) return;
self.errors.append(.{ .tag = tag, .extra = extra }) catch {};
if (self.errors_len < self.errors_buffer.len) {
self.errors_buffer[self.errors_len] = .{ .tag = tag, .extra = extra };
self.errors_len += 1;
}
}

pub fn next(self: *Parser) ?Item {
Expand Down
11 changes: 11 additions & 0 deletions lib/std/array_list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,17 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
return self;
}

/// Initialize with externally-managed memory. The buffer determines the
/// capacity, and the length is set to zero.
/// When initialized this way, all methods that accept an Allocator
/// argument are illegal to call.
pub fn initBuffer(buffer: Slice) Self {
return .{
.items = buffer[0..0],
.capacity = buffer.len,
};
}

/// Release all allocated memory.
pub fn deinit(self: *Self, allocator: Allocator) void {
allocator.free(self.allocatedSlice());
Expand Down
Loading

0 comments on commit e4977f3

Please sign in to comment.