Hello,
I have a module where I put all my arena allocators.
Then I have different modules where I import the arena module.
Do I only have one definition for my arena allocator?
Hello,
I have a module where I put all my arena allocators.
Then I have different modules where I import the arena module.
Do I only have one definition for my arena allocator?
This is incomprehensible
If I’m understanding you correctly, it sounds like you want to know if your arena allocators are valid once they’re imported to different modules.
The short answer is probably not, unfortunately.
While std.mem.Allocator implementations are valid to pass by value at runtime, they can’t be created in a valid way during comptime (as far as I know) which is a prerequisite for them being imported by other modules. You could get around that with global variables to an extent, but general guidance I’ve seen steers users away from making allocators as global variables.
Moreover, std.heap.Arena is not safe/valid to be passed by value.
Hope that helps. If it’s missing the point of your question, please try to rephrase.
I thought that the arena declaration was shared between all modules,
because it works in my project.
I’ll run a test so I can be sure.
Program “A” uses the arena.
Program “B” resets the arena.
And I’ll check if the shared data is correct and valid.
///-----------------------
/// build (library)
/// zig 0.15.0 dev
///-----------------------
const std = @import("std");
pub fn build(b: *std.Build) void {
const logger_mod = b.addModule("logger", .{
.root_source_file = b.path( "./log/logger.zig" ),
});
const logsrc_mod = b.addModule("logsrc", .{
.root_source_file = b.path( "./log/logsrc.zig" ),
});
const allocTui_mod = b.addModule("allocTui", .{
.root_source_file = b.path( "./curse/allocTui.zig" ),
});
const cursed_mod = b.addModule("cursed", .{
.root_source_file = b.path( "./curse/cursed.zig" ),
});
const utils_mod = b.addModule("utils", .{
.root_source_file = b.path( "./curse/utils.zig" ),
.imports= &.{
.{ .name = "allocTui", .module = allocTui_mod },
.{ .name = "cursed", .module = cursed_mod },
},
});
const mvzr_mod = b.addModule("mvzr", .{
.root_source_file = b.path( "./regex/mvzr.zig" ),
});
const forms_mod = b.addModule("forms", .{
.root_source_file = b.path( "./curse/forms.zig" ),
.imports= &.{
.{ .name = "allocTui", .module = allocTui_mod },
.{ .name = "cursed", .module = cursed_mod },
.{ .name = "utils", .module = utils_mod},
.{ .name = "mvzr", .module = mvzr_mod },
},
});
const grid_mod = b.addModule("grid", .{
.root_source_file = b.path( "./curse/grid.zig" ),
.imports = &.{
.{ .name = "allocTui", .module = allocTui_mod },
.{ .name = "cursed", .module = cursed_mod},
.{ .name = "utils", .module = utils_mod},
},
});
const menu_mod= b.addModule("menu", .{
.root_source_file = b.path( "./curse/menu.zig" ),
.imports= &.{
.{ .name = "allocTui", .module = allocTui_mod },
.{ .name = "cursed", .module = cursed_mod},
.{ .name = "utils", .module = utils_mod},
},
});
const callpgm_mod = b.addModule("callpgm", .{
.root_source_file = b.path( "./calling/callpgm.zig" ),
});
const crypto_mod= b.addModule("crypto", .{
.root_source_file = b.path( "./crypt/crypto.zig" ),
});
const zmmap_mod= b.addModule("zmmap", .{
.root_source_file = b.path( "./mmap/zmmap.zig" ),
.imports= &.{
.{ .name = "crypto", .module = crypto_mod},
.{ .name = "logger", .module = logger_mod},
},
});
const library_mod = b.addModule("library", .{
.root_source_file = b.path( "library.zig" ),
.imports = &.{
.{ .name = "allocTui", .module = allocTui_mod },
.{ .name = "cursed", .module = cursed_mod },
.{ .name = "utils", .module = utils_mod },
.{ .name = "mvzr", .module = mvzr_mod },
.{ .name = "forms", .module = forms_mod },
.{ .name = "grid", .module = grid_mod },
.{ .name = "menu", .module = menu_mod },
.{ .name = "callpgm", .module = callpgm_mod },
.{ .name = "zmmap", .module = zmmap_mod },
.{ .name = "crypto", .module = crypto_mod },
.{ .name = "logger", .module = logger_mod },
.{ .name = "logsrc", .module = logsrc_mod },
},
});
_ = library_mod;
}
Oh my! You don’t need to add the imports manually if you directly use the relative path of the file. Usually it goes like this:
src/
module1/
file1.zig
file2.zig
module2/
...
And from file1.zig you just @import("file2.zig"), while from module2 you need to declare in the build system and use @import("module1").file1.
Also it’s easier to help if we can take a look at the depo.
I have my answer; there is only one arena in my case.
So if in a I declare a variable constructed with arena
and in another program I use the same arena and deinit the arena, my variable jumps (bug if I print it) because it no longer exists.
I did my test, and this is the behavior I expected.