Allocgate/Zig 0.9.1

This commit is contained in:
Cat /dev/Nulo 2022-07-18 16:22:31 -03:00
parent fadda3c05f
commit c12e89a3e5
2 changed files with 9 additions and 9 deletions

View File

@ -4,7 +4,7 @@ const aaronsw = @import("main.zig");
pub fn main() !void { pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit(); defer _ = gpa.deinit();
const allocator = &gpa.allocator; const allocator = gpa.allocator();
const str = try std.io.getStdIn().reader().readAllAlloc(allocator, 10241024); const str = try std.io.getStdIn().reader().readAllAlloc(allocator, 10241024);
defer allocator.free(str); defer allocator.free(str);

View File

@ -37,20 +37,20 @@ const Document = struct {
}; };
const Paragraph = struct { const Paragraph = struct {
text: []u8, text: []u8,
pub fn deinit(self: *Paragraph, allocator: *std.mem.Allocator) void { pub fn deinit(self: *Paragraph, allocator: std.mem.Allocator) void {
allocator.free(self.text); allocator.free(self.text);
} }
}; };
const Heading = struct { const Heading = struct {
level: u8, level: u8,
text: []u8, text: []u8,
pub fn deinit(self: *Heading, allocator: *std.mem.Allocator) void { pub fn deinit(self: *Heading, allocator: std.mem.Allocator) void {
allocator.free(self.text); allocator.free(self.text);
} }
}; };
const BlockQuote = struct { const BlockQuote = struct {
children: Children, children: Children,
pub fn deinit(self: *BlockQuote, _: *std.mem.Allocator) void { pub fn deinit(self: *BlockQuote, _: std.mem.Allocator) void {
deinitChildren(&self.children); deinitChildren(&self.children);
} }
}; };
@ -61,13 +61,13 @@ const List = struct {
kind: Kind, kind: Kind,
tight: bool, tight: bool,
children: Children, children: Children,
pub fn deinit(self: *List, _: *std.mem.Allocator) void { pub fn deinit(self: *List, _: std.mem.Allocator) void {
deinitChildren(&self.children); deinitChildren(&self.children);
} }
}; };
const ListItem = struct { const ListItem = struct {
children: Children, children: Children,
pub fn deinit(self: *ListItem, _: *std.mem.Allocator) void { pub fn deinit(self: *ListItem, _: std.mem.Allocator) void {
deinitChildren(&self.children); deinitChildren(&self.children);
} }
}; };
@ -79,7 +79,7 @@ fn arrayListAppend(comptime T: type, list: *std.ArrayList(T), item: T) !*T {
} }
fn reallocAppend( fn reallocAppend(
allocator: *std.mem.Allocator, allocator: std.mem.Allocator,
alloc_str: []u8, alloc_str: []u8,
append_str: []const u8, append_str: []const u8,
) ![]u8 { ) ![]u8 {
@ -139,7 +139,7 @@ fn getHeadingLevel(line: []const u8) u8 {
} }
return level; return level;
} }
fn checkIfBlockStarts(allocator: *std.mem.Allocator, line: *[]const u8) !?Block { fn checkIfBlockStarts(allocator: std.mem.Allocator, line: *[]const u8) !?Block {
if (std.mem.startsWith(u8, line.*, ">")) { if (std.mem.startsWith(u8, line.*, ">")) {
line.* = line.*[1..]; line.* = line.*[1..];
const indent = detectIndent(line.*); const indent = detectIndent(line.*);
@ -193,7 +193,7 @@ fn verifyStack(stack: Stack) void {
} }
} }
pub fn parse(allocator: *std.mem.Allocator, str: []const u8) !Document { pub fn parse(allocator: std.mem.Allocator, str: []const u8) !Document {
var doc_block = Block{ .document = Document{ .children = Children.init(allocator) } }; var doc_block = Block{ .document = Document{ .children = Children.init(allocator) } };
errdefer doc_block.document.deinit(); errdefer doc_block.document.deinit();