Skip to content

pixqc/zalloc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 

Repository files navigation

zalloc: zig allocators implemented in c

for educational purposes only, related blog post: https://pixqc.com/zigalloc

example usage:

// "Why do I have to pass allocators around in Zig?"
// because userland decides which allocation strategy to use
// and where the data should be placed
void alloc_hello(Allocator *allocator) {
  const char *str = "hello world\n";
  size_t str_size = strlen(str)   1;
  MemoryBlock str1 = allocator->vtable->alloc(allocator, str_size);
  memcpy(str1.ptr, str, str_size);
}

int main() {
  char buf[1000];
  memset(buf, 0xAA, 1000);
  Allocator *fba = create_fixed_buffer_allocator(buf, sizeof(buf));
  alloc_hello(fba);

  Allocator *arena = create_arena_allocator();
  alloc_hello(arena);

  Allocator *gpa = create_gpa_allocator();
  alloc_hello(gpa);

  return 0;
}

About

zig allocators implemented in c

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages