sys_alloc.c 558 B

123456789101112131415161718192021222324252627282930
  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. ptr = (void *) ((char *) ptr + HEADER_SIZE);
  15. set_size(ptr, req_padding(size));
  16. set_requested_size(ptr, size);
  17. set_next(ptr, heap->used_blocks_head);
  18. heap->used_blocks_head = ptr;
  19. sbrk_unlock();
  20. return ptr;
  21. }