self-hosted test: use C allocator since we depend on libc

This commit is contained in:
Andrew Kelley 2018-07-10 20:27:15 -04:00
parent 574e31f0a0
commit 8197a14ceb

View File

@ -21,12 +21,11 @@ test "compile errors" {
} }
const file1 = "1.zig"; const file1 = "1.zig";
const allocator = std.heap.c_allocator;
const TestContext = struct { const TestContext = struct {
loop: std.event.Loop, loop: std.event.Loop,
zig_lib_dir: []u8, zig_lib_dir: []u8,
direct_allocator: std.heap.DirectAllocator,
arena: std.heap.ArenaAllocator,
zig_cache_dir: []u8, zig_cache_dir: []u8,
file_index: std.atomic.Int(usize), file_index: std.atomic.Int(usize),
group: std.event.Group(error!void), group: std.event.Group(error!void),
@ -37,8 +36,6 @@ const TestContext = struct {
fn init(self: *TestContext) !void { fn init(self: *TestContext) !void {
self.* = TestContext{ self.* = TestContext{
.any_err = {}, .any_err = {},
.direct_allocator = undefined,
.arena = undefined,
.loop = undefined, .loop = undefined,
.zig_lib_dir = undefined, .zig_lib_dir = undefined,
.zig_cache_dir = undefined, .zig_cache_dir = undefined,
@ -46,36 +43,27 @@ const TestContext = struct {
.file_index = std.atomic.Int(usize).init(0), .file_index = std.atomic.Int(usize).init(0),
}; };
self.direct_allocator = std.heap.DirectAllocator.init(); try self.loop.initMultiThreaded(allocator);
errdefer self.direct_allocator.deinit();
self.arena = std.heap.ArenaAllocator.init(&self.direct_allocator.allocator);
errdefer self.arena.deinit();
// TODO faster allocator for coroutines that is thread-safe/lock-free
try self.loop.initMultiThreaded(&self.direct_allocator.allocator);
errdefer self.loop.deinit(); errdefer self.loop.deinit();
self.group = std.event.Group(error!void).init(&self.loop); self.group = std.event.Group(error!void).init(&self.loop);
errdefer self.group.cancelAll(); errdefer self.group.cancelAll();
self.zig_lib_dir = try introspect.resolveZigLibDir(&self.arena.allocator); self.zig_lib_dir = try introspect.resolveZigLibDir(allocator);
errdefer self.arena.allocator.free(self.zig_lib_dir); errdefer allocator.free(self.zig_lib_dir);
self.zig_cache_dir = try introspect.resolveZigCacheDir(&self.arena.allocator); self.zig_cache_dir = try introspect.resolveZigCacheDir(allocator);
errdefer self.arena.allocator.free(self.zig_cache_dir); errdefer allocator.free(self.zig_cache_dir);
try std.os.makePath(&self.arena.allocator, tmp_dir_name); try std.os.makePath(allocator, tmp_dir_name);
errdefer std.os.deleteTree(&self.arena.allocator, tmp_dir_name) catch {}; errdefer std.os.deleteTree(allocator, tmp_dir_name) catch {};
} }
fn deinit(self: *TestContext) void { fn deinit(self: *TestContext) void {
std.os.deleteTree(&self.arena.allocator, tmp_dir_name) catch {}; std.os.deleteTree(allocator, tmp_dir_name) catch {};
self.arena.allocator.free(self.zig_cache_dir); allocator.free(self.zig_cache_dir);
self.arena.allocator.free(self.zig_lib_dir); allocator.free(self.zig_lib_dir);
self.loop.deinit(); self.loop.deinit();
self.arena.deinit();
self.direct_allocator.deinit();
} }
fn run(self: *TestContext) !void { fn run(self: *TestContext) !void {
@ -99,14 +87,14 @@ const TestContext = struct {
) !void { ) !void {
var file_index_buf: [20]u8 = undefined; var file_index_buf: [20]u8 = undefined;
const file_index = try std.fmt.bufPrint(file_index_buf[0..], "{}", self.file_index.next()); const file_index = try std.fmt.bufPrint(file_index_buf[0..], "{}", self.file_index.next());
const file1_path = try std.os.path.join(&self.arena.allocator, tmp_dir_name, file_index, file1); const file1_path = try std.os.path.join(allocator, tmp_dir_name, file_index, file1);
if (std.os.path.dirname(file1_path)) |dirname| { if (std.os.path.dirname(file1_path)) |dirname| {
try std.os.makePath(&self.arena.allocator, dirname); try std.os.makePath(allocator, dirname);
} }
// TODO async I/O // TODO async I/O
try std.io.writeFile(&self.arena.allocator, file1_path, source); try std.io.writeFile(allocator, file1_path, source);
var module = try Module.create( var module = try Module.create(
&self.loop, &self.loop,