heap_allocator_unix.odin 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #+build linux, darwin, freebsd, openbsd, netbsd, haiku
  2. #+private
  3. package runtime
  4. when ODIN_OS == .Darwin {
  5. foreign import libc "system:System.framework"
  6. } else {
  7. foreign import libc "system:c"
  8. }
  9. @(default_calling_convention="c")
  10. foreign libc {
  11. @(link_name="malloc") _unix_malloc :: proc(size: int) -> rawptr ---
  12. @(link_name="calloc") _unix_calloc :: proc(num, size: int) -> rawptr ---
  13. @(link_name="free") _unix_free :: proc(ptr: rawptr) ---
  14. @(link_name="realloc") _unix_realloc :: proc(ptr: rawptr, size: int) -> rawptr ---
  15. }
  16. _heap_alloc :: proc "contextless" (size: int, zero_memory := true) -> rawptr {
  17. if size <= 0 {
  18. return nil
  19. }
  20. if zero_memory {
  21. return _unix_calloc(1, size)
  22. } else {
  23. return _unix_malloc(size)
  24. }
  25. }
  26. _heap_resize :: proc "contextless" (ptr: rawptr, new_size: int) -> rawptr {
  27. // NOTE: _unix_realloc doesn't guarantee new memory will be zeroed on
  28. // POSIX platforms. Ensure your caller takes this into account.
  29. return _unix_realloc(ptr, new_size)
  30. }
  31. _heap_free :: proc "contextless" (ptr: rawptr) {
  32. _unix_free(ptr)
  33. }