sys_alloc.c 922 B

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