Skip to content

Allocgate #10055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Dec 1, 2021
4 changes: 2 additions & 2 deletions ci/srht/update-download-page.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();

const allocator = &arena.allocator;
const allocator = arena.allocator();

const out_dir = "out";
try std.fs.cwd().makePath(out_dir);
Expand All @@ -18,7 +18,7 @@ pub fn main() !void {
}

fn render(
allocator: *mem.Allocator,
allocator: mem.Allocator,
in_file: []const u8,
out_file: []const u8,
fmt: enum {
Expand Down
22 changes: 11 additions & 11 deletions doc/docgen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();

const allocator = &arena.allocator;
const allocator = arena.allocator();

var args_it = process.args();

Expand Down Expand Up @@ -342,7 +342,7 @@ const Action = enum {
Close,
};

fn genToc(allocator: *Allocator, tokenizer: *Tokenizer) !Toc {
fn genToc(allocator: Allocator, tokenizer: *Tokenizer) !Toc {
var urls = std.StringHashMap(Token).init(allocator);
errdefer urls.deinit();

Expand Down Expand Up @@ -708,7 +708,7 @@ fn genToc(allocator: *Allocator, tokenizer: *Tokenizer) !Toc {
};
}

fn urlize(allocator: *Allocator, input: []const u8) ![]u8 {
fn urlize(allocator: Allocator, input: []const u8) ![]u8 {
var buf = std.ArrayList(u8).init(allocator);
defer buf.deinit();

Expand All @@ -727,7 +727,7 @@ fn urlize(allocator: *Allocator, input: []const u8) ![]u8 {
return buf.toOwnedSlice();
}

fn escapeHtml(allocator: *Allocator, input: []const u8) ![]u8 {
fn escapeHtml(allocator: Allocator, input: []const u8) ![]u8 {
var buf = std.ArrayList(u8).init(allocator);
defer buf.deinit();

Expand Down Expand Up @@ -773,7 +773,7 @@ test "term color" {
try testing.expectEqualSlices(u8, "A<span class=\"t32_1\">green</span>B", result);
}

fn termColor(allocator: *Allocator, input: []const u8) ![]u8 {
fn termColor(allocator: Allocator, input: []const u8) ![]u8 {
var buf = std.ArrayList(u8).init(allocator);
defer buf.deinit();

Expand Down Expand Up @@ -883,7 +883,7 @@ fn writeEscapedLines(out: anytype, text: []const u8) !void {
}

fn tokenizeAndPrintRaw(
allocator: *Allocator,
allocator: Allocator,
docgen_tokenizer: *Tokenizer,
out: anytype,
source_token: Token,
Expand Down Expand Up @@ -1137,7 +1137,7 @@ fn tokenizeAndPrintRaw(
}

fn tokenizeAndPrint(
allocator: *Allocator,
allocator: Allocator,
docgen_tokenizer: *Tokenizer,
out: anytype,
source_token: Token,
Expand All @@ -1146,7 +1146,7 @@ fn tokenizeAndPrint(
return tokenizeAndPrintRaw(allocator, docgen_tokenizer, out, source_token, raw_src);
}

fn printSourceBlock(allocator: *Allocator, docgen_tokenizer: *Tokenizer, out: anytype, syntax_block: SyntaxBlock) !void {
fn printSourceBlock(allocator: Allocator, docgen_tokenizer: *Tokenizer, out: anytype, syntax_block: SyntaxBlock) !void {
const source_type = @tagName(syntax_block.source_type);

try out.print("<figure><figcaption class=\"{s}-cap\"><cite class=\"file\">{s}</cite></figcaption><pre>", .{ source_type, syntax_block.name });
Expand Down Expand Up @@ -1188,7 +1188,7 @@ fn printShell(out: anytype, shell_content: []const u8) !void {
}

fn genHtml(
allocator: *Allocator,
allocator: Allocator,
tokenizer: *Tokenizer,
toc: *Toc,
out: anytype,
Expand Down Expand Up @@ -1687,7 +1687,7 @@ fn genHtml(
}
}

fn exec(allocator: *Allocator, env_map: *std.BufMap, args: []const []const u8) !ChildProcess.ExecResult {
fn exec(allocator: Allocator, env_map: *std.BufMap, args: []const []const u8) !ChildProcess.ExecResult {
const result = try ChildProcess.exec(.{
.allocator = allocator,
.argv = args,
Expand All @@ -1711,7 +1711,7 @@ fn exec(allocator: *Allocator, env_map: *std.BufMap, args: []const []const u8) !
return result;
}

fn getBuiltinCode(allocator: *Allocator, env_map: *std.BufMap, zig_exe: []const u8) ![]const u8 {
fn getBuiltinCode(allocator: Allocator, env_map: *std.BufMap, zig_exe: []const u8) ![]const u8 {
const result = try exec(allocator, env_map, &[_][]const u8{ zig_exe, "build-obj", "--show-builtin" });
return result.stdout;
}
Expand Down
28 changes: 14 additions & 14 deletions doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -7362,7 +7362,7 @@ fn amain() !void {
}

var global_download_frame: anyframe = undefined;
fn fetchUrl(allocator: *Allocator, url: []const u8) ![]u8 {
fn fetchUrl(allocator: Allocator, url: []const u8) ![]u8 {
_ = url; // this is just an example, we don't actually do it!
const result = try allocator.dupe(u8, "this is the downloaded url contents");
errdefer allocator.free(result);
Expand All @@ -7374,7 +7374,7 @@ fn fetchUrl(allocator: *Allocator, url: []const u8) ![]u8 {
}

var global_file_frame: anyframe = undefined;
fn readFile(allocator: *Allocator, filename: []const u8) ![]u8 {
fn readFile(allocator: Allocator, filename: []const u8) ![]u8 {
_ = filename; // this is just an example, we don't actually do it!
const result = try allocator.dupe(u8, "this is the file contents");
errdefer allocator.free(result);
Expand Down Expand Up @@ -7433,15 +7433,15 @@ fn amain() !void {
std.debug.print("file_text: {s}\n", .{file_text});
}

fn fetchUrl(allocator: *Allocator, url: []const u8) ![]u8 {
fn fetchUrl(allocator: Allocator, url: []const u8) ![]u8 {
_ = url; // this is just an example, we don't actually do it!
const result = try allocator.dupe(u8, "this is the downloaded url contents");
errdefer allocator.free(result);
std.debug.print("fetchUrl returning\n", .{});
return result;
}

fn readFile(allocator: *Allocator, filename: []const u8) ![]u8 {
fn readFile(allocator: Allocator, filename: []const u8) ![]u8 {
_ = filename; // this is just an example, we don't actually do it!
const result = try allocator.dupe(u8, "this is the file contents");
errdefer allocator.free(result);
Expand Down Expand Up @@ -10050,8 +10050,8 @@ pub fn main() void {
C has a default allocator - <code>malloc</code>, <code>realloc</code>, and <code>free</code>.
When linking against libc, Zig exposes this allocator with {#syntax#}std.heap.c_allocator{#endsyntax#}.
However, by convention, there is no default allocator in Zig. Instead, functions which need to
allocate accept an {#syntax#}*Allocator{#endsyntax#} parameter. Likewise, data structures such as
{#syntax#}std.ArrayList{#endsyntax#} accept an {#syntax#}*Allocator{#endsyntax#} parameter in
allocate accept an {#syntax#}Allocator{#endsyntax#} parameter. Likewise, data structures such as
{#syntax#}std.ArrayList{#endsyntax#} accept an {#syntax#}Allocator{#endsyntax#} parameter in
their initialization functions:
</p>
{#code_begin|test|allocator#}
Expand All @@ -10061,12 +10061,12 @@ const expect = std.testing.expect;

test "using an allocator" {
var buffer: [100]u8 = undefined;
const allocator = &std.heap.FixedBufferAllocator.init(&buffer).allocator;
const allocator = std.heap.FixedBufferAllocator.init(&buffer).allocator();
const result = try concat(allocator, "foo", "bar");
try expect(std.mem.eql(u8, "foobar", result));
}

fn concat(allocator: *Allocator, a: []const u8, b: []const u8) ![]u8 {
fn concat(allocator: Allocator, a: []const u8, b: []const u8) ![]u8 {
const result = try allocator.alloc(u8, a.len + b.len);
std.mem.copy(u8, result, a);
std.mem.copy(u8, result[a.len..], b);
Expand All @@ -10091,7 +10091,7 @@ fn concat(allocator: *Allocator, a: []const u8, b: []const u8) ![]u8 {
</p>
<ol>
<li>
Are you making a library? In this case, best to accept an {#syntax#}*Allocator{#endsyntax#}
Are you making a library? In this case, best to accept an {#syntax#}Allocator{#endsyntax#}
as a parameter and allow your library's users to decide what allocator to use.
</li>
<li>Are you linking libc? In this case, {#syntax#}std.heap.c_allocator{#endsyntax#} is likely
Expand All @@ -10114,7 +10114,7 @@ pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();

const allocator = &arena.allocator;
const allocator = arena.allocator();

const ptr = try allocator.create(i32);
std.debug.print("ptr={*}\n", .{ptr});
Expand Down Expand Up @@ -10200,7 +10200,7 @@ test "string literal to constant slice" {
{#header_open|Implementing an Allocator#}
<p>Zig programmers can implement their own allocators by fulfilling the Allocator interface.
In order to do this one must read carefully the documentation comments in std/mem.zig and
then supply a {#syntax#}reallocFn{#endsyntax#} and a {#syntax#}shrinkFn{#endsyntax#}.
then supply a {#syntax#}allocFn{#endsyntax#} and a {#syntax#}resizeFn{#endsyntax#}.
</p>
<p>
There are many example allocators to look at for inspiration. Look at std/heap.zig and
Expand Down Expand Up @@ -10281,7 +10281,7 @@ test "string literal to constant slice" {
<p>
For example, the function's documentation may say "caller owns the returned memory", in which case
the code that calls the function must have a plan for when to free that memory. Probably in this situation,
the function will accept an {#syntax#}*Allocator{#endsyntax#} parameter.
the function will accept an {#syntax#}Allocator{#endsyntax#} parameter.
</p>
<p>
Sometimes the lifetime of a pointer may be more complicated. For example, the
Expand Down Expand Up @@ -10820,7 +10820,7 @@ const std = @import("std");

pub fn main() !void {
var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){};
const gpa = &general_purpose_allocator.allocator;
const gpa = general_purpose_allocator.allocator();
const args = try std.process.argsAlloc(gpa);
defer std.process.argsFree(gpa, args);

Expand All @@ -10842,7 +10842,7 @@ const PreopenList = std.fs.wasi.PreopenList;

pub fn main() !void {
var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){};
const gpa = &general_purpose_allocator.allocator;
const gpa = general_purpose_allocator.allocator();

var preopens = PreopenList.init(gpa);
defer preopens.deinit();
Expand Down
2 changes: 1 addition & 1 deletion lib/std/Thread.zig
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ const WindowsThreadImpl = struct {
errdefer assert(windows.kernel32.HeapFree(heap_handle, 0, alloc_ptr) != 0);

const instance_bytes = @ptrCast([*]u8, alloc_ptr)[0..alloc_bytes];
const instance = std.heap.FixedBufferAllocator.init(instance_bytes).allocator.create(Instance) catch unreachable;
const instance = std.heap.FixedBufferAllocator.init(instance_bytes).allocator().create(Instance) catch unreachable;
instance.* = .{
.fn_args = args,
.thread = .{
Expand Down
Loading