Skip to content

Update Zig toolchain: 0.14.0 #103

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 1 commit into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.{
.name = "pgzx",
.name = .pgzx,
.fingerprint = 0x8cedca5730bac13e,
.version = "0.1.0",
.paths = .{
"build.zig",
Expand Down
3 changes: 1 addition & 2 deletions devshell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ in {

pkgs.pkg-config

# pkgs.zigpkgs.master
pkgs.zigpkgs.master
pkgs.zigpkgs.stable
pkgs.zls
];

Expand Down
3 changes: 2 additions & 1 deletion examples/char_count_zig/build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.{
.name = "char_count_zig",
.name = .char_count_zig,
.fingerprint = 0x6969c22ee1da70c0,
.version = "0.1.0",
.paths = .{
"extension",
Expand Down
3 changes: 2 additions & 1 deletion examples/pgaudit_zig/build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.{
.name = "pgzx",
.name = .pgaudit_zig,
.fingerprint = 0xf8875009244b02f6,
.version = "0.1.0",
.paths = .{
"extension",
Expand Down
3 changes: 2 additions & 1 deletion examples/pghostname_zig/build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.{
.name = "pghostname_zig",
.name = .pghostname_zig,
.fingerprint = 0xb5ee05c8bdbc8686,
.version = "0.1.0",
.paths = .{
"extension",
Expand Down
3 changes: 2 additions & 1 deletion examples/spi_sql/build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.{
.name = "spi_sql",
.name = .spi_sql,
.fingerprint = 0x6815a10c7341ea27,
.version = "0.1.0",
.paths = .{
"extension",
Expand Down
3 changes: 2 additions & 1 deletion examples/sqlfns/build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.{
.name = "sqlfns",
.name = .sqlfns,
.fingerprint = 0xf71e8e3592a1c0e8,
.version = "0.1.0",
.paths = .{
"extension",
Expand Down
12 changes: 6 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 24 additions & 11 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,25 @@
self,
nixpkgs,
...
}:
}: let
zig-stable = "0.14.0";

zig-overlay = _final: prev: let
orig = inputs.zig-overlay.packages.${prev.system};
in {
zigpkgs =
orig
// {
stable = orig.${zig-stable};
};
};

zls-overlay = final: prev: {
zls = inputs.zls.packages.${prev.system}.zls.overrideAttrs (_oldAttrs: {
nativeBuildInputs = [final.zigpkgs.stable];
});
};
in
inputs.parts.lib.mkFlake {inherit inputs;} {
debug = true;

Expand All @@ -41,10 +59,8 @@
zls
pgzx_scripts
];
zigpkgs = inputs.zig-overlay.overlays.default;
zls = _final: prev: {
zls = inputs.zls.packages.${prev.system}.zls;
};
zigpkgs = zig-overlay;
zls = zls-overlay;
pgzx_scripts = _final: prev: {
pgzx_scripts = self.packages.${prev.system}.pgzx_scripts;
};
Expand All @@ -61,7 +77,6 @@
systems = ["x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin"];

perSystem = {
system,
config,
lib,
pkgs,
Expand All @@ -70,10 +85,8 @@
nixpkgs = {
config.allowBroken = true;
overlays = [
inputs.zig-overlay.overlays.default
(_final: _prev: {
zls = inputs.zls.packages.${system}.zls;
})
zig-overlay
zls-overlay
];
};

Expand Down Expand Up @@ -103,7 +116,7 @@
zigfmt = {
enable = true;
name = "Zig fmt";
entry = "${pkgs.zigpkgs.master}/bin/zig fmt --check";
entry = "${pkgs.zigpkgs.stable}/bin/zig fmt --check";
files = "\\.zig$|\\.zon$";
};
};
Expand Down
41 changes: 25 additions & 16 deletions src/pgzx/mem.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,46 @@ const err = @import("err.zig");
pub const PGCurrentContextAllocator: std.mem.Allocator = .{
.ptr = undefined,
.vtable = &.{
.alloc = &pg_alloc,
.free = &pg_free,
.resize = &pg_resize,
.alloc = &pgAlloc,
.free = &pgFree,
.resize = &pgResize,
.remap = &pgRemap,
},
};

fn pg_alloc(ctx: *anyopaque, len: usize, ptr_align: u8, ret_addr: usize) ?[*]u8 {
fn pgAlloc(ctx: *anyopaque, len: usize, ptr_align: std.mem.Alignment, ret_addr: usize) ?[*]u8 {
_ = ret_addr;
_ = ctx;
return @ptrCast(pg.palloc_aligned(len, ptr_align, pg.MCXT_ALLOC_NO_OOM));
return @ptrCast(pg.palloc_aligned(len, @intFromEnum(ptr_align), pg.MCXT_ALLOC_NO_OOM));
}

fn pg_free(ctx: *anyopaque, buf: []u8, buf_align: u8, ret_addr: usize) void {
fn pgFree(ctx: *anyopaque, buf: []u8, buf_align: std.mem.Alignment, ret_addr: usize) void {
_ = ret_addr;
_ = buf_align;
_ = ctx;
pg.pfree(@ptrCast(buf));
}

fn pg_resize(ctx: *anyopaque, buf: []u8, buf_align: u8, new_len: usize, ret_addr: usize) bool {
fn pgResize(ctx: *anyopaque, buf: []u8, buf_align: std.mem.Alignment, new_len: usize, ret_addr: usize) bool {
_ = ret_addr;
_ = new_len;
_ = buf_align;
_ = buf;
_ = ctx;

// Postgres API only support realloc and might therefore relocate the buffer.
// resize is not allowed to relocate the buffer, so we have to return false
// and force the zig allocator to allocate a new buffer.
// allow shrinking only. We set all unused bytes to 0.
if (new_len < buf.len) {
@memset(buf[new_len..], 0);
return true;
}
return false;
}

fn pgRemap(ctx: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, ret_addr: usize) ?[*]u8 {
_ = ctx;
_ = alignment;
_ = ret_addr;
return @ptrCast(pg.repalloc(memory.ptr, new_len));
}

pub fn getErrorContext() MemoryContextAllocator {
return MemoryContextAllocator.init(pg.ErrorContext, .{});
}
Expand Down Expand Up @@ -260,15 +268,16 @@ pub const MemoryContextAllocator = struct {

const vtable = std.mem.Allocator.VTable{
.alloc = context_alloc,
.free = pg_free, // pg_free ignores the pointer.
.resize = pg_resize, // pg_resize ignores the pointer.
.free = pgFree, // pg_free ignores the pointer.
.resize = pgResize, // pg_resize ignores the pointer.
.remap = pgRemap, // pg_remap ignores the pointer.
};

fn context_alloc(ctx: *anyopaque, len: usize, ptr_align: u8, ret_addr: usize) ?[*]u8 {
fn context_alloc(ctx: *anyopaque, len: usize, ptr_align: std.mem.Alignment, ret_addr: usize) ?[*]u8 {
_ = ret_addr;
const self: *MemoryContextAllocator = @ptrCast(@alignCast(ctx));
const memctx = self.ctx;
const ptr = pg.MemoryContextAllocAligned(memctx, len, ptr_align, self.flags);
const ptr = pg.MemoryContextAllocAligned(memctx, len, @intFromEnum(ptr_align), self.flags);
return @ptrCast(ptr);
}
};
Expand Down
Loading