Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ziglang/zig
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 55193cb13bbc
Choose a base ref
...
head repository: ziglang/zig
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 4eca75c53b46
Choose a head ref
  • 7 commits
  • 16 files changed
  • 2 contributors

Commits on Jun 20, 2018

  1. std.mem: remove allocator create in favor of construct; ref #733

    kristopher tate committed Jun 20, 2018
    Copy the full SHA
    457c0f0 View commit details
  2. Copy the full SHA
    71db8df View commit details
  3. std.mem.Allocator.construct: remove deprecation warning;

    kristopher tate committed Jun 20, 2018
    Copy the full SHA
    4b46af4 View commit details
  4. std.mem.Allocator.construct: improve formatting;

    kristopher tate committed Jun 20, 2018
    Copy the full SHA
    6bd8610 View commit details
  5. zig fmt

    andrewrk committed Jun 20, 2018
    Copy the full SHA
    e891f9c View commit details
  6. Copy the full SHA
    85f928f View commit details
  7. Copy the full SHA
    4eca75c View commit details
Showing with 191 additions and 216 deletions.
  1. +1 −1 src-self-hosted/errmsg.zig
  2. +1 −1 src-self-hosted/main.zig
  3. +21 −14 src-self-hosted/module.zig
  4. +4 −2 std/atomic/queue.zig
  5. +4 −2 std/atomic/stack.zig
  6. +20 −35 std/build.zig
  7. +5 −10 std/debug/index.zig
  8. +1 −2 std/heap.zig
  9. +3 −5 std/io.zig
  10. +1 −1 std/linked_list.zig
  11. +3 −11 std/mem.zig
  12. +3 −6 std/os/child_process.zig
  13. +11 −5 std/os/index.zig
  14. +75 −75 std/zig/parse.zig
  15. +1 −1 test/cases/null.zig
  16. +37 −45 test/tests.zig
2 changes: 1 addition & 1 deletion src-self-hosted/errmsg.zig
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ pub fn createFromParseError(
var out_stream = &std.io.BufferOutStream.init(&text_buf).stream;
try parse_error.render(&tree.tokens, out_stream);

const msg = try allocator.construct(Msg{
const msg = try allocator.create(Msg{
.tree = tree,
.path = path,
.text = text_buf.toOwnedSlice(),
2 changes: 1 addition & 1 deletion src-self-hosted/main.zig
Original file line number Diff line number Diff line change
@@ -707,7 +707,7 @@ const Fmt = struct {

// file_path must outlive Fmt
fn addToQueue(self: *Fmt, file_path: []const u8) !void {
const new_node = try self.seen.allocator.construct(std.LinkedList([]const u8).Node{
const new_node = try self.seen.allocator.create(std.LinkedList([]const u8).Node{
.prev = undefined,
.next = undefined,
.data = file_path,
35 changes: 21 additions & 14 deletions src-self-hosted/module.zig
Original file line number Diff line number Diff line change
@@ -110,11 +110,12 @@ pub const Module = struct {
parent: ?*CliPkg,

pub fn init(allocator: *mem.Allocator, name: []const u8, path: []const u8, parent: ?*CliPkg) !*CliPkg {
var pkg = try allocator.create(CliPkg);
pkg.name = name;
pkg.path = path;
pkg.children = ArrayList(*CliPkg).init(allocator);
pkg.parent = parent;
var pkg = try allocator.create(CliPkg{
.name = name,
.path = path,
.children = ArrayList(*CliPkg).init(allocator),
.parent = parent,
});
return pkg;
}

@@ -126,7 +127,16 @@ pub const Module = struct {
}
};

pub fn create(allocator: *mem.Allocator, name: []const u8, root_src_path: ?[]const u8, target: *const Target, kind: Kind, build_mode: builtin.Mode, zig_lib_dir: []const u8, cache_dir: []const u8) !*Module {
pub fn create(
allocator: *mem.Allocator,
name: []const u8,
root_src_path: ?[]const u8,
target: *const Target,
kind: Kind,
build_mode: builtin.Mode,
zig_lib_dir: []const u8,
cache_dir: []const u8,
) !*Module {
var name_buffer = try Buffer.init(allocator, name);
errdefer name_buffer.deinit();

@@ -139,10 +149,7 @@ pub const Module = struct {
const builder = c.LLVMCreateBuilderInContext(context) orelse return error.OutOfMemory;
errdefer c.LLVMDisposeBuilder(builder);

const module_ptr = try allocator.create(Module);
errdefer allocator.destroy(module_ptr);

module_ptr.* = Module{
const module_ptr = try allocator.create(Module{
.allocator = allocator,
.name = name_buffer,
.root_src_path = root_src_path,
@@ -196,7 +203,8 @@ pub const Module = struct {
.test_filters = [][]const u8{},
.test_name_prefix = null,
.emit_file_type = Emit.Binary,
};
});
errdefer allocator.destroy(module_ptr);
return module_ptr;
}

@@ -279,13 +287,12 @@ pub const Module = struct {
}
}

const link_lib = try self.allocator.create(LinkLib);
link_lib.* = LinkLib{
const link_lib = try self.allocator.create(LinkLib{
.name = name,
.path = null,
.provided_explicitly = provided_explicitly,
.symbols = ArrayList([]u8).init(self.allocator),
};
});
try self.link_libs_list.append(link_lib);
if (is_libc) {
self.libc_link_lib = link_lib;
6 changes: 4 additions & 2 deletions std/atomic/queue.zig
Original file line number Diff line number Diff line change
@@ -114,8 +114,10 @@ fn startPuts(ctx: *Context) u8 {
while (put_count != 0) : (put_count -= 1) {
std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
const x = @bitCast(i32, r.random.scalar(u32));
const node = ctx.allocator.create(Queue(i32).Node) catch unreachable;
node.data = x;
const node = ctx.allocator.create(Queue(i32).Node{
.next = undefined,
.data = x,
}) catch unreachable;
ctx.queue.put(node);
_ = @atomicRmw(isize, &ctx.put_sum, builtin.AtomicRmwOp.Add, x, AtomicOrder.SeqCst);
}
6 changes: 4 additions & 2 deletions std/atomic/stack.zig
Original file line number Diff line number Diff line change
@@ -117,8 +117,10 @@ fn startPuts(ctx: *Context) u8 {
while (put_count != 0) : (put_count -= 1) {
std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
const x = @bitCast(i32, r.random.scalar(u32));
const node = ctx.allocator.create(Stack(i32).Node) catch unreachable;
node.data = x;
const node = ctx.allocator.create(Stack(i32).Node{
.next = undefined,
.data = x,
}) catch unreachable;
ctx.stack.push(node);
_ = @atomicRmw(isize, &ctx.put_sum, builtin.AtomicRmwOp.Add, x, AtomicOrder.SeqCst);
}
55 changes: 20 additions & 35 deletions std/build.zig
Original file line number Diff line number Diff line change
@@ -158,8 +158,7 @@ pub const Builder = struct {
}

pub fn addTest(self: *Builder, root_src: []const u8) *TestStep {
const test_step = self.allocator.create(TestStep) catch unreachable;
test_step.* = TestStep.init(self, root_src);
const test_step = self.allocator.create(TestStep.init(self, root_src)) catch unreachable;
return test_step;
}

@@ -191,21 +190,18 @@ pub const Builder = struct {
}

pub fn addWriteFile(self: *Builder, file_path: []const u8, data: []const u8) *WriteFileStep {
const write_file_step = self.allocator.create(WriteFileStep) catch unreachable;
write_file_step.* = WriteFileStep.init(self, file_path, data);
const write_file_step = self.allocator.create(WriteFileStep.init(self, file_path, data)) catch unreachable;
return write_file_step;
}

pub fn addLog(self: *Builder, comptime format: []const u8, args: ...) *LogStep {
const data = self.fmt(format, args);
const log_step = self.allocator.create(LogStep) catch unreachable;
log_step.* = LogStep.init(self, data);
const log_step = self.allocator.create(LogStep.init(self, data)) catch unreachable;
return log_step;
}

pub fn addRemoveDirTree(self: *Builder, dir_path: []const u8) *RemoveDirStep {
const remove_dir_step = self.allocator.create(RemoveDirStep) catch unreachable;
remove_dir_step.* = RemoveDirStep.init(self, dir_path);
const remove_dir_step = self.allocator.create(RemoveDirStep.init(self, dir_path)) catch unreachable;
return remove_dir_step;
}

@@ -404,11 +400,10 @@ pub const Builder = struct {
}

pub fn step(self: *Builder, name: []const u8, description: []const u8) *Step {
const step_info = self.allocator.create(TopLevelStep) catch unreachable;
step_info.* = TopLevelStep{
const step_info = self.allocator.create(TopLevelStep{
.step = Step.initNoOp(name, self.allocator),
.description = description,
};
}) catch unreachable;
self.top_level_steps.append(step_info) catch unreachable;
return &step_info.step;
}
@@ -598,8 +593,7 @@ pub const Builder = struct {
const full_dest_path = os.path.resolve(self.allocator, self.prefix, dest_rel_path) catch unreachable;
self.pushInstalledFile(full_dest_path);

const install_step = self.allocator.create(InstallFileStep) catch unreachable;
install_step.* = InstallFileStep.init(self, src_path, full_dest_path);
const install_step = self.allocator.create(InstallFileStep.init(self, src_path, full_dest_path)) catch unreachable;
return install_step;
}

@@ -837,51 +831,43 @@ pub const LibExeObjStep = struct {
};

pub fn createSharedLibrary(builder: *Builder, name: []const u8, root_src: ?[]const u8, ver: *const Version) *LibExeObjStep {
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
self.* = initExtraArgs(builder, name, root_src, Kind.Lib, false, ver);
const self = builder.allocator.create(initExtraArgs(builder, name, root_src, Kind.Lib, false, ver)) catch unreachable;
return self;
}

pub fn createCSharedLibrary(builder: *Builder, name: []const u8, version: *const Version) *LibExeObjStep {
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
self.* = initC(builder, name, Kind.Lib, version, false);
const self = builder.allocator.create(initC(builder, name, Kind.Lib, version, false)) catch unreachable;
return self;
}

pub fn createStaticLibrary(builder: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep {
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
self.* = initExtraArgs(builder, name, root_src, Kind.Lib, true, builder.version(0, 0, 0));
const self = builder.allocator.create(initExtraArgs(builder, name, root_src, Kind.Lib, true, builder.version(0, 0, 0))) catch unreachable;
return self;
}

pub fn createCStaticLibrary(builder: *Builder, name: []const u8) *LibExeObjStep {
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
self.* = initC(builder, name, Kind.Lib, builder.version(0, 0, 0), true);
const self = builder.allocator.create(initC(builder, name, Kind.Lib, builder.version(0, 0, 0), true)) catch unreachable;
return self;
}

pub fn createObject(builder: *Builder, name: []const u8, root_src: []const u8) *LibExeObjStep {
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
self.* = initExtraArgs(builder, name, root_src, Kind.Obj, false, builder.version(0, 0, 0));
const self = builder.allocator.create(initExtraArgs(builder, name, root_src, Kind.Obj, false, builder.version(0, 0, 0))) catch unreachable;
return self;
}

pub fn createCObject(builder: *Builder, name: []const u8, src: []const u8) *LibExeObjStep {
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
self.* = initC(builder, name, Kind.Obj, builder.version(0, 0, 0), false);
const self = builder.allocator.create(initC(builder, name, Kind.Obj, builder.version(0, 0, 0), false)) catch unreachable;
self.object_src = src;
return self;
}

pub fn createExecutable(builder: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep {
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
self.* = initExtraArgs(builder, name, root_src, Kind.Exe, false, builder.version(0, 0, 0));
const self = builder.allocator.create(initExtraArgs(builder, name, root_src, Kind.Exe, false, builder.version(0, 0, 0))) catch unreachable;
return self;
}

pub fn createCExecutable(builder: *Builder, name: []const u8) *LibExeObjStep {
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
self.* = initC(builder, name, Kind.Exe, builder.version(0, 0, 0), false);
const self = builder.allocator.create(initC(builder, name, Kind.Exe, builder.version(0, 0, 0), false)) catch unreachable;
return self;
}

@@ -1748,14 +1734,14 @@ pub const CommandStep = struct {

/// ::argv is copied.
pub fn create(builder: *Builder, cwd: ?[]const u8, env_map: *const BufMap, argv: []const []const u8) *CommandStep {
const self = builder.allocator.create(CommandStep) catch unreachable;
self.* = CommandStep{
const self = builder.allocator.create(CommandStep{
.builder = builder,
.step = Step.init(argv[0], builder.allocator, make),
.argv = builder.allocator.alloc([]u8, argv.len) catch unreachable,
.cwd = cwd,
.env_map = env_map,
};
}) catch unreachable;

mem.copy([]const u8, self.argv, argv);
self.step.name = self.argv[0];
return self;
@@ -1778,18 +1764,17 @@ const InstallArtifactStep = struct {
const Self = this;

pub fn create(builder: *Builder, artifact: *LibExeObjStep) *Self {
const self = builder.allocator.create(Self) catch unreachable;
const dest_dir = switch (artifact.kind) {
LibExeObjStep.Kind.Obj => unreachable,
LibExeObjStep.Kind.Exe => builder.exe_dir,
LibExeObjStep.Kind.Lib => builder.lib_dir,
};
self.* = Self{
const self = builder.allocator.create(Self{
.builder = builder,
.step = Step.init(builder.fmt("install {}", artifact.step.name), builder.allocator, make),
.artifact = artifact,
.dest_file = os.path.join(builder.allocator, dest_dir, artifact.out_filename) catch unreachable,
};
}) catch unreachable;
self.step.dependOn(&artifact.step);
builder.pushInstalledFile(self.dest_file);
if (self.artifact.kind == LibExeObjStep.Kind.Lib and !self.artifact.static) {
15 changes: 5 additions & 10 deletions std/debug/index.zig
Original file line number Diff line number Diff line change
@@ -249,9 +249,7 @@ fn printSourceAtAddress(debug_info: *ElfStackTrace, out_stream: var, address: us
pub fn openSelfDebugInfo(allocator: *mem.Allocator) !*ElfStackTrace {
switch (builtin.object_format) {
builtin.ObjectFormat.elf => {
const st = try allocator.create(ElfStackTrace);
errdefer allocator.destroy(st);
st.* = ElfStackTrace{
const st = try allocator.create(ElfStackTrace{
.self_exe_file = undefined,
.elf = undefined,
.debug_info = undefined,
@@ -261,7 +259,8 @@ pub fn openSelfDebugInfo(allocator: *mem.Allocator) !*ElfStackTrace {
.debug_ranges = null,
.abbrev_table_list = ArrayList(AbbrevTableHeader).init(allocator),
.compile_unit_list = ArrayList(CompileUnit).init(allocator),
};
});
errdefer allocator.destroy(st);
st.self_exe_file = try os.openSelfExe();
errdefer st.self_exe_file.close();

@@ -280,11 +279,8 @@ pub fn openSelfDebugInfo(allocator: *mem.Allocator) !*ElfStackTrace {
var exe_file = try os.openSelfExe();
defer exe_file.close();

const st = try allocator.create(ElfStackTrace);
const st = try allocator.create(ElfStackTrace{ .symbol_table = try macho.loadSymbols(allocator, &io.FileInStream.init(&exe_file)) });
errdefer allocator.destroy(st);

st.* = ElfStackTrace{ .symbol_table = try macho.loadSymbols(allocator, &io.FileInStream.init(&exe_file)) };

return st;
},
builtin.ObjectFormat.coff => {
@@ -974,8 +970,7 @@ fn scanAllCompileUnits(st: *ElfStackTrace) !void {

try st.self_exe_file.seekTo(compile_unit_pos);

const compile_unit_die = try st.allocator().create(Die);
compile_unit_die.* = try parseDie(st, abbrev_table, is_64);
const compile_unit_die = try st.allocator().create(try parseDie(st, abbrev_table, is_64));

if (compile_unit_die.tag_id != DW.TAG_compile_unit) return error.InvalidDebugInfo;

3 changes: 1 addition & 2 deletions std/heap.zig
Original file line number Diff line number Diff line change
@@ -407,8 +407,7 @@ fn testAllocator(allocator: *mem.Allocator) !void {
var slice = try allocator.alloc(*i32, 100);

for (slice) |*item, i| {
item.* = try allocator.create(i32);
item.*.* = @intCast(i32, i);
item.* = try allocator.create(@intCast(i32, i));
}

for (slice) |item, i| {
8 changes: 3 additions & 5 deletions std/io.zig
Original file line number Diff line number Diff line change
@@ -414,14 +414,12 @@ pub const BufferedAtomicFile = struct {

pub fn create(allocator: *mem.Allocator, dest_path: []const u8) !*BufferedAtomicFile {
// TODO with well defined copy elision we don't need this allocation
var self = try allocator.create(BufferedAtomicFile);
errdefer allocator.destroy(self);

self.* = BufferedAtomicFile{
var self = try allocator.create(BufferedAtomicFile{
.atomic_file = undefined,
.file_stream = undefined,
.buffered_stream = undefined,
};
});
errdefer allocator.destroy(self);

self.atomic_file = try os.AtomicFile.init(allocator, dest_path, os.default_file_mode);
errdefer self.atomic_file.deinit();
2 changes: 1 addition & 1 deletion std/linked_list.zig
Original file line number Diff line number Diff line change
@@ -193,7 +193,7 @@ fn BaseLinkedList(comptime T: type, comptime ParentType: type, comptime field_na
/// A pointer to the new node.
pub fn allocateNode(list: *Self, allocator: *Allocator) !*Node {
comptime assert(!isIntrusive());
return allocator.create(Node);
return allocator.create(Node(undefined));
}

/// Deallocate a node.
14 changes: 3 additions & 11 deletions std/mem.zig
Original file line number Diff line number Diff line change
@@ -31,16 +31,8 @@ pub const Allocator = struct {
/// Guaranteed: `old_mem.len` is the same as what was returned from `allocFn` or `reallocFn`
freeFn: fn (self: *Allocator, old_mem: []u8) void,

/// Call destroy with the result
pub fn create(self: *Allocator, comptime T: type) !*T {
if (@sizeOf(T) == 0) return *{};
const slice = try self.alloc(T, 1);
return &slice[0];
}

/// Call destroy with the result
/// TODO once #733 is solved, this will replace create
pub fn construct(self: *Allocator, init: var) Error!*@typeOf(init) {
/// Call `destroy` with the result
pub fn create(self: *Allocator, init: var) Error!*@typeOf(init) {
const T = @typeOf(init);
if (@sizeOf(T) == 0) return &{};
const slice = try self.alloc(T, 1);
@@ -49,7 +41,7 @@ pub const Allocator = struct {
return ptr;
}

/// `ptr` should be the return value of `construct` or `create`
/// `ptr` should be the return value of `create`
pub fn destroy(self: *Allocator, ptr: var) void {
const non_const_ptr = @intToPtr([*]u8, @ptrToInt(ptr));
self.freeFn(self, non_const_ptr[0..@sizeOf(@typeOf(ptr).Child)]);
Loading