sys_alloc.c 742 B

123456789101112131415161718192021222324252627282930313233343536
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include "posix_lock.h"
  6. #include "other.h"
  7. #include "sys_alloc.h"
  8. #include "block_header.h"
  9. #include "heap.h"
  10. void *sys_alloc(heap_t *heap, size_t size) {
  11. size_t allocation_size;
  12. void *ptr;
  13. sbrk_lock();
  14. allocation_size = req_padding(size) + HEADER_SIZE;
  15. ptr = sbrk((int) allocation_size);
  16. if(ptr == (void *) -1) {
  17. printf("sbrk problem for size of: %zu\n", allocation_size);
  18. printf( "Error on sbrk: %s\n", strerror( errno ) );
  19. }
  20. ptr = (void *) ((char *) ptr + HEADER_SIZE);
  21. set_size(ptr, req_padding(size));
  22. set_requested_size(ptr, size);
  23. set_next(ptr, heap->used_blocks_head);
  24. heap->used_blocks_head = ptr;
  25. sbrk_unlock();
  26. return ptr;
  27. }