Skip to content

Commit f48ec4b

Browse files
committed
use long-lived arena for @cImport-generated Module
1 parent 1ad33f5 commit f48ec4b

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

src/Compilation.zig

+8-11
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ const resinator = @import("resinator.zig");
4141

4242
/// General-purpose allocator. Used for both temporary and long-term storage.
4343
gpa: Allocator,
44-
/// Arena-allocated memory used during initialization. Should be untouched until deinit.
45-
arena_state: std.heap.ArenaAllocator.State,
44+
/// Arena-allocated memory, mostly used during initialization. However, it can be used
45+
/// for other things requiring the same lifetime as the `Compilation`.
46+
arena: std.heap.ArenaAllocator,
4647
bin_file: *link.File,
4748
c_object_table: std.AutoArrayHashMapUnmanaged(*CObject, void) = .{},
4849
win32_resource_table: if (build_options.only_core_functionality) void else std.AutoArrayHashMapUnmanaged(*Win32Resource, void) =
@@ -124,7 +125,7 @@ cache_parent: *Cache,
124125
/// Path to own executable for invoking `zig clang`.
125126
self_exe_path: ?[]const u8,
126127
/// null means -fno-emit-bin.
127-
/// This is mutable memory allocated into the Compilation-lifetime arena (`arena_state`)
128+
/// This is mutable memory allocated into the Compilation-lifetime arena (`arena`)
128129
/// of exactly the correct size for "o/[digest]/[basename]".
129130
/// The basename is of the outputted binary file in case we don't know the directory yet.
130131
whole_bin_sub_path: ?[]u8,
@@ -1661,7 +1662,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
16611662
errdefer bin_file.destroy();
16621663
comp.* = .{
16631664
.gpa = gpa,
1664-
.arena_state = arena_allocator.state,
1665+
.arena = arena_allocator,
16651666
.zig_lib_directory = options.zig_lib_directory,
16661667
.local_cache_directory = options.local_cache_directory,
16671668
.global_cache_directory = options.global_cache_directory,
@@ -1979,7 +1980,8 @@ pub fn destroy(self: *Compilation) void {
19791980
if (self.owned_link_dir) |*dir| dir.close();
19801981

19811982
// This destroys `self`.
1982-
self.arena_state.promote(gpa).deinit();
1983+
var arena_instance = self.arena;
1984+
arena_instance.deinit();
19831985
}
19841986

19851987
pub fn clearMiscFailures(comp: *Compilation) void {
@@ -3899,17 +3901,12 @@ pub fn obtainWin32ResourceCacheManifest(comp: *const Compilation) Cache.Manifest
38993901
return man;
39003902
}
39013903

3902-
test "cImport" {
3903-
_ = cImport;
3904-
}
3905-
39063904
pub const CImportResult = struct {
39073905
out_zig_path: []u8,
39083906
cache_hit: bool,
39093907
errors: std.zig.ErrorBundle,
39103908

39113909
pub fn deinit(result: *CImportResult, gpa: std.mem.Allocator) void {
3912-
gpa.free(result.out_zig_path);
39133910
result.errors.deinit(gpa);
39143911
}
39153912
};
@@ -4054,7 +4051,7 @@ pub fn cImport(comp: *Compilation, c_src: []const u8) !CImportResult {
40544051
};
40554052
}
40564053

4057-
const out_zig_path = try comp.local_cache_directory.join(comp.gpa, &[_][]const u8{
4054+
const out_zig_path = try comp.local_cache_directory.join(comp.arena.allocator(), &.{
40584055
"o", &digest, cimport_zig_basename,
40594056
});
40604057
if (comp.verbose_cimport) {

src/Sema.zig

+4-7
Original file line numberDiff line numberDiff line change
@@ -5733,6 +5733,7 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr
57335733
defer tracy.end();
57345734

57355735
const mod = sema.mod;
5736+
const comp = mod.comp;
57365737
const gpa = sema.gpa;
57375738
const pl_node = sema.code.instructions.items(.data)[inst].pl_node;
57385739
const src = pl_node.src();
@@ -5770,7 +5771,7 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr
57705771
// Ignore the result, all the relevant operations have written to c_import_buf already.
57715772
_ = try sema.analyzeBodyBreak(&child_block, body);
57725773

5773-
var c_import_res = mod.comp.cImport(c_import_buf.items) catch |err|
5774+
var c_import_res = comp.cImport(c_import_buf.items) catch |err|
57745775
return sema.fail(&child_block, src, "C import failed: {s}", .{@errorName(err)});
57755776
defer c_import_res.deinit(gpa);
57765777

@@ -5779,7 +5780,7 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr
57795780
const msg = try sema.errMsg(&child_block, src, "C import failed", .{});
57805781
errdefer msg.destroy(gpa);
57815782

5782-
if (!mod.comp.bin_file.options.link_libc)
5783+
if (!comp.bin_file.options.link_libc)
57835784
try sema.errNote(&child_block, src, msg, "libc headers not available; compilation does not link against libc", .{});
57845785

57855786
const gop = try mod.cimport_errors.getOrPut(gpa, sema.owner_decl_index);
@@ -5791,11 +5792,7 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr
57915792
};
57925793
return sema.failWithOwnedErrorMsg(&child_block, msg);
57935794
}
5794-
// All modules are intended to go into an arena with a lifetime >= the ZigUnit.
5795-
// After the other uses of `tmp_hack_arena` are eliminated, it should be
5796-
// renamed to something more appropriate such as simply `arena`.
5797-
const zu_arena = mod.tmp_hack_arena.allocator();
5798-
const c_import_mod = try Package.Module.create(zu_arena, .{
5795+
const c_import_mod = try Package.Module.create(comp.arena.allocator(), .{
57995796
.root = .{
58005797
.root_dir = Compilation.Directory.cwd(),
58015798
.sub_path = std.fs.path.dirname(c_import_res.out_zig_path) orelse "",

0 commit comments

Comments
 (0)