sys_alloc.c 512 B

1234567891011121314151617181920212223242526272829
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include "posix_lock.h"
  4. #include "other.h"
  5. #include "sys_alloc.h"
  6. #include "block_header.h"
  7. #include "heap.h"
  8. void *sys_alloc(heap_t *heap, size_t size) {
  9. size_t allocation_size;
  10. void *ptr;
  11. sbrk_lock();
  12. allocation_size = req_padding(size) + HEADER_SIZE;
  13. ptr = sbrk((int) allocation_size);
  14. set_size(ptr, req_padding(size));
  15. set_requested_size(ptr, size);
  16. set_next(ptr, heap->used_blocks_head);
  17. heap->used_blocks_head = ptr;
  18. sbrk_unlock();
  19. return ptr;
  20. }