Skip to content

Instantly share code, notes, and snippets.

@fengb
Created May 28, 2019 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fengb/1bcf4e784ebfff337d1a02edd646412b to your computer and use it in GitHub Desktop.
Save fengb/1bcf4e784ebfff337d1a02edd646412b to your computer and use it in GitHub Desktop.
const FreeList = linked_list.SinglyLinkedList([]u8);
fn consumeUnusedNode(self: *Self) !*FreeList.Node {
if (self.unused_nodes.first == null) {
// var bytes = try self.backing_allocator.alignedAlloc(u8, page_size, self.page_size);
var bytes = try self.backing_allocator.reallocFn(self.backing_allocator, []u8{}, 1, self.page_size, page_size);
const node_memsize = bytes.len - (bytes.len % @sizeOf(FreeList.Node));
// Small leak due to misalignment. Can't do anything about it...
bytes = bytes[0..node_memsize];
const buffer = @bytesToSlice(FreeList.Node, bytes);
std.debug.assert(buffer.len > 0);
for (buffer) |*node| {
self.unused_nodes.prepend(node); // this breaks
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment