sys_alloc.c 648 B

123456789101112131415161718192021222324252627282930313233
  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. if(ptr == (void *) -1) {
  15. printf("sbrk problem for size of: %d\n", allocation_size);
  16. }
  17. ptr = (void *) ((char *) ptr + HEADER_SIZE);
  18. set_size(ptr, req_padding(size));
  19. set_requested_size(ptr, size);
  20. set_next(ptr, heap->used_blocks_head);
  21. heap->used_blocks_head = ptr;
  22. sbrk_unlock();
  23. return ptr;
  24. }