sys_alloc.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright 2011 Institute of Communication and Computer Systems (ICCS)
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. #include <unistd.h>
  18. #include <stdio.h>
  19. #include <errno.h>
  20. #include <string.h>
  21. #include <dmmlib/heap.h>
  22. #include "dmm_config.h"
  23. #ifdef HAVE_LOCKS
  24. #include "posix_lock.h"
  25. #endif /* HAVE_LOCKS */
  26. #include "other.h"
  27. #include "sys_alloc.h"
  28. #include "block_header.h"
  29. void *sys_alloc(allocator_t *allocator, heap_t *heap, size_t size) {
  30. size_t allocation_size, previous_size, previous_size_availability;
  31. void *ptr;
  32. #ifdef HAVE_LOCKS
  33. sbrk_lock();
  34. #endif /* HAVE_LOCKS */
  35. allocation_size = req_padding(size) + HEADER_SIZE;
  36. if(allocator->border_ptr != NULL) {
  37. previous_size = get_size(allocator->border_ptr);
  38. previous_size_availability = get_size_availability(allocator->border_ptr);
  39. } else {
  40. previous_size = 0;
  41. previous_size_availability = 1; /* Occupied and of 0 size */
  42. }
  43. #ifndef WITH_MEMORY_SPACE_AWARENESS
  44. ptr = sbrk((int) allocation_size);
  45. if(ptr == (void *) -1) {
  46. printf("sbrk problem for size of: %zu\n", allocation_size);
  47. printf("Error on sbrk: %s\n", strerror( errno ) );
  48. }
  49. if(allocator->border_ptr != NULL && ptr != (void *)
  50. ((char *) allocator->border_ptr + previous_size)) {
  51. printf("sbrk() does not return sequential space.\n");
  52. }
  53. #else
  54. if(allocator->remaining_size >= allocation_size) {
  55. ptr = (void *) ((char *) allocator->border_ptr + previous_size);
  56. allocator->remaining_size -= allocation_size;
  57. } else {
  58. printf("No more free space.\n");
  59. }
  60. #endif /* WITH_MEMORY_SPACE_AWARENESS */
  61. /* Go to the data part of the block */
  62. ptr = (void *) ((char *) ptr + HEADER_SIZE);
  63. /* Set the new border pointer */
  64. allocator->border_ptr = ptr;
  65. /* Set some values for block header */
  66. set_size(ptr, req_padding(size));
  67. set_previous_size_availability(ptr, previous_size_availability);
  68. /* Update stats */
  69. heap->dmm_stats.mem_allocated += req_padding(size);
  70. heap->dmm_stats.mem_requested += size;
  71. /* FIXME To be refactored - START */
  72. set_requested_size(ptr, size);
  73. mark_used(ptr);
  74. // Update the used blocks list
  75. #ifdef BLOCKS_IN_DLL
  76. set_previous(ptr, NULL);
  77. #endif /* BLOCKS_IN_DLL */
  78. set_next(ptr, heap->used_blocks_head);
  79. heap->used_blocks_head = ptr;
  80. // Begin of Stats
  81. heap->dmm_stats.live_objects += 1;
  82. heap->dmm_stats.num_malloc += 1;
  83. // End of Stats
  84. /* FIXME To be refactored - END */
  85. #ifdef HAVE_LOCKS
  86. sbrk_unlock();
  87. #endif /* HAVE_LOCKS */
  88. return ptr;
  89. }