heap.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. /**
  18. * \file heap.h
  19. * \author Ioannis Koutras (joko@microlab.ntua.gr)
  20. * \date September, 2011
  21. * Basic structures needed for the dmmlib allocator.
  22. */
  23. #ifndef HEAP_H
  24. #define HEAP_H
  25. #include "dmm_config.h"
  26. #include <stdbool.h>
  27. #ifndef LEON3
  28. #include <stdint.h>
  29. #include <stddef.h> /* For size_t support */
  30. #else
  31. #include <sys/types.h>
  32. #endif
  33. #ifdef HAVE_LOCKS
  34. #include <pthread.h>
  35. #endif /* HAVE_LOCKS */
  36. #ifdef WITH_KNOBS
  37. /**
  38. * A structure for knobs states (currently 5)
  39. *
  40. * FIXME Have to check them
  41. *
  42. * */
  43. typedef uint8_t knob_state_t;
  44. #endif /* WITH_KNOBS */
  45. /** A structure to represent a maptable node.
  46. * This is a single-linked list holoding blocks of specific size, thus acting
  47. * as a fixed list within the allocator.
  48. */
  49. typedef struct maptable_node_s {
  50. unsigned int size; /**< The size of the blocks of the fixed list. */
  51. void *fixed_list_head; /**< Pointer to the head node of the fixed list. */
  52. struct maptable_node_s *next; /**< Pointer to the next node of the
  53. maptable. */
  54. } maptable_node_t;
  55. #ifdef WITH_STATS
  56. /** Statistics data structure. */
  57. typedef struct dmmstats_s {
  58. size_t max_mem_allocated; /**< Maximum total memory allocated. */
  59. size_t max_mem_requested; /**< Maximum total memory requested. */
  60. size_t mem_allocated; /**< Total memory currently allocated. */
  61. size_t mem_requested; /**< Total memory currently requested. */
  62. uint32_t live_objects; /**< Number of the currently used blocks. */
  63. uint32_t read_mem_accesses; /**< Number of read accesses. */
  64. uint32_t write_mem_accesses; /**< Number of write accesses. */
  65. uint32_t num_malloc; /**< Number of malloc()'s served. */
  66. uint32_t num_free; /**< Number of free()'s served. */
  67. } dmmstats_t;
  68. #endif /* WITH_STATS */
  69. #ifdef WITH_KNOBS
  70. /** A structure to represent tunable parameters of a heap */
  71. typedef struct dmmknobs_s {
  72. float frag_threshold; /**< Fragmentation threshold to enable coalescing
  73. or not. */
  74. uint32_t mem_threshold; /**< Memory size threshold. */
  75. #ifdef GOOD_FIT
  76. float fit_percentage; /**< Fit percentage for good-fit searches. */
  77. #endif /* GOOD_FIT */
  78. #ifdef HEAP_VAR_FIT
  79. /** Current search policy on the allocator's free lists. */
  80. void *(*search_policy)(heap_t * heap, size_t requested_size);
  81. #endif /* HEAP_VAR_FIT */
  82. #ifdef COALESCING_VARIABLE
  83. size_t max_coalesce_size; /**< Maximum coalesce size; -1 if coalescing
  84. is not supported */
  85. #endif /* COALESCING_VARIABLE */
  86. #ifdef SPLITTING_VARIABLE
  87. size_t min_split_size; /**< Minimum split size. */
  88. #endif /* SPLITTING_VARIABLE */
  89. /* FIXME Need to find explanation */
  90. float empty_threshold; /**< Empty Threshold */
  91. uint32_t percentage; /**< Percentage value */
  92. knob_state_t frag_state; /**< The current state of fragmentation. */
  93. knob_state_t foot_state; /**< The current state of footprint. */
  94. } dmmknobs_t;
  95. #endif /* WITH_KNOBS */
  96. /** A structure to store heap information. */
  97. typedef struct heap_s {
  98. #ifdef WITH_FIXED_LISTS
  99. maptable_node_t *maptable_head; /**< The head of the maptable list. */
  100. #endif /* WITH_FIXED_LISTS */
  101. void *free_list_head; /**< The head of the free list. */
  102. #ifdef FUTURE_FEATURES
  103. void *used_blocks_head; /**< The head of the used blocks list. */
  104. void *rov_ptr; /**< Roving pointer. */
  105. #endif /* FUTURE_FEATURES */
  106. #ifndef LEON3
  107. uint64_t num_objects; /**< Number of objects in the heap. */
  108. #else
  109. unsigned long num_objects; /**< Number of objects in the heap. */
  110. #endif /* LEON3 */
  111. #ifdef WITH_STATS
  112. dmmstats_t dmm_stats; /**< Statistics of the heap. */
  113. #endif /* WITH_STATS */
  114. #ifdef WITH_KNOBS
  115. dmmknobs_t dmm_knobs; /**< Tunable parameters of the heap. */
  116. #endif /* WITH_KNOBS */
  117. #ifdef HAVE_LOCKS
  118. pthread_mutex_t mutex;/**< Mutex when POSIX Threads are used. */
  119. #endif /* HAVE_LOCKS */
  120. } heap_t;
  121. /** The allocator structure of dmmlib. */
  122. typedef struct allocator_s {
  123. heap_t heaps[NUM_HEAPS]; /**< The heaps that the allocator manages. */
  124. bool initialized; /**< Initialization flag of the allocator. */
  125. void *border_ptr; /**< Border pointer of the allocator. */
  126. size_t remaining_size; /**< The size of the remaining free space which
  127. is handled by the allocator. */
  128. #ifdef ALLOC_VAR_FIT
  129. /** Current search policy on the allocator's free lists. */
  130. void *(*search_policy)(heap_t * heap, size_t requested_size);
  131. #endif /* ALLOC_VAR_FIT */
  132. } allocator_t;
  133. #endif /* HEAP_H */