sys_alloc.c 939 B

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