sys_alloc.c 908 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. heap->dmm_stats.mem_allocated += req_padding(size);
  27. heap->dmm_stats.mem_requested += size;
  28. #ifdef HAVE_LOCKS
  29. sbrk_unlock();
  30. #endif /* HAVE_LOCKS */
  31. return ptr;
  32. }