Invalid free error with version 0.14 that did not appear in 0.13, for aligned memory that is assigned via `memcpy`

This doesn’t do anything different than:

const buffer = 

If you wanted to declare a slice with an alignment you would have to do this:

const buffer: []align(page_size) const u8 = 

But this isn’t necessary, because the called function already returns the slice with that alignment.


This @memcpy(buffer[0..bytes.len], bytes); is equivalent to @memcpy(buffer, bytes); because you asked alignedAlloc to give you a slice of length bytes.len.


The main problem with the code is that you don’t call free with the same pointer you received from alignedAlloc, one way you could do that is to declare the bytes field with the correct alignment so that it doesn’t get lost by the time free is called:

    bytes: []align(page_size) u8,

    pub fn init(allocator: std.mem.Allocator, bytes: []const u8) !@This() {
        const buffer = try allocator.alignedAlloc(u8, page_size, bytes.len);
        @memcpy(buffer, bytes);
        return .{
            .allocator = allocator,
            .bytes = buffer,
        };
    }

However this only works on targets that have a compile time known pageSize().
Since:

Zig supports runtime known page sizes.

To make it also work there, we need a function that can dispatch between runtime known alignments, I tried to adapt a solution from an older topic to recent Zig here: