block_header.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 block_header.h
  19. * \author Ioannis Koutras (joko@microlab.ntua.gr)
  20. * \date September, 2011
  21. *
  22. * \brief Block header structure and functions, and memory block functions.
  23. */
  24. #ifndef BLOCK_HEADER_H
  25. #define BLOCK_HEADER_H
  26. #include <stddef.h>
  27. #include <stdbool.h>
  28. #include <dmmlib/heap.h>
  29. /* TODO Add an ifndef guard in case we have other block organizations */
  30. #include "linked_lists/linked_lists.h"
  31. /** The header structure of every memory block inside a heap. */
  32. typedef struct block_header_s {
  33. size_t size; /**< The LSB represents the availability of the block (1
  34. for used, 0 for free), the rest the size of the data
  35. part. */
  36. #ifdef FUTURE_FEATURES
  37. size_t requested_size; /**< The requested size of the data part */
  38. #endif /* FUTURE_FEATURES */
  39. size_t previous_size; /**< The LSB represents the availability of the
  40. previous block, the rest the size of the data
  41. part of the previous block in the memory space */
  42. /* TODO Add an ifndef guard in case we have other block organizations */
  43. list_node_header_t pointers; /**< The necessary pointers for block
  44. organization. */
  45. #ifdef WITH_OWNERSHIP
  46. heap_t *heap_owner; /** < A pointer to the heap the current block belongs to */
  47. #endif /* WITH_OWNERSHIP */
  48. } block_header_t;
  49. /**
  50. * The size of the header in number of bytes
  51. */
  52. #define HEADER_SIZE sizeof(block_header_t)
  53. /**
  54. * \brief Get the address of the block header of a memory block.
  55. *
  56. * \param ptr The data part of the memory block.
  57. */
  58. block_header_t * get_header(void *ptr);
  59. /**
  60. * Get the size of the memory block's data
  61. *
  62. * \param ptr The pointer to the data part of the current memory block.
  63. *
  64. * \return The size of the data part of the current memory block.
  65. */
  66. size_t get_size(void *ptr);
  67. #ifdef FUTURE_FEATURES
  68. /**
  69. * Get the requested size of the memory block's data
  70. *
  71. * \param ptr The pointer to the data part of the current memory block.
  72. *
  73. * \return The size of the data that was initialy requested for this memory
  74. * block.
  75. */
  76. size_t get_requested_size(void *ptr);
  77. #endif /* FUTURE_FEATURES */
  78. /**
  79. * Get all information of the memory block header's size record
  80. *
  81. * \param ptr The pointer to the data part of the current memory block.
  82. *
  83. * \return The availability and the size of the data part of the current memory
  84. * block.
  85. */
  86. size_t get_size_availability(void *ptr);
  87. /**
  88. * Set the size of the memory block's data and mark it free
  89. *
  90. * \param allocator The pointer to the allocator who manages the block.
  91. * \param ptr The pointer to the data part of the current memory block.
  92. * \param size The size of the data part of the current memory block.
  93. */
  94. void set_size_and_free(allocator_t *allocator, void *ptr, size_t size);
  95. /**
  96. * Set the size of the memory block's data and mark it used
  97. *
  98. * \param allocator The pointer to the allocator who manages the block.
  99. * \param ptr The pointer to the data part of the current memory block.
  100. * \param size The size of the data part of the current memory block.
  101. */
  102. void set_size_and_used(allocator_t *allocator, void *ptr, size_t size);
  103. #ifdef FUTURE_FEATURES
  104. /**
  105. * Set the requested size of memory block's data
  106. *
  107. * \param ptr The pointer to the data part of the current memory block.
  108. * \param size The requested size for the data part of the current memory
  109. * block.
  110. */
  111. void set_requested_size(void *ptr, size_t size);
  112. #endif /* FUTURE_FEATURES */
  113. /**
  114. * Mark the memory block as used, as well as the previous_size element of the
  115. * next block if there is one.
  116. *
  117. * \param allocator The pointer to the allocator who manages the block.
  118. * \param ptr The pointer to the data part of the memory block.
  119. */
  120. void mark_used(allocator_t *allocator, void *ptr);
  121. /**
  122. * Mark the memory block as free, as well as the previous_size element of the
  123. * next block if there is one.
  124. *
  125. * \param allocator The pointer to the allocator who manages the block.
  126. * \param ptr The pointer to the data part of the memory block.
  127. */
  128. void mark_free(allocator_t *allocator, void *ptr);
  129. /**
  130. * Set the availability and the size of the previous memory block
  131. *
  132. * \param ptr The pointer to the data part of the previous memory block.
  133. * \param previous_size_availability The size for the data part of the previous
  134. * memory block on data layout level.
  135. */
  136. void set_previous_size_availability(void *ptr, size_t previous_size_availability);
  137. #ifdef WITH_OWNERSHIP
  138. /**
  139. * Set the heap owner of a memory block
  140. *
  141. * \param ptr The pointer to the data part of the memory block.
  142. * \param heap_owner The pointer to the heap owner.
  143. */
  144. void set_owner(void *ptr, heap_t *heap_owner);
  145. /**
  146. * Get the heap owner of a memory block
  147. *
  148. * \param ptr The pointer to the data part of the memory block.
  149. * \return The pointer to the heap owner.
  150. */
  151. heap_t * get_owner(void *ptr);
  152. #endif /* WITH_OWNERSHIP */
  153. /**
  154. * Check if a memory block is free
  155. */
  156. bool is_free(void *ptr);
  157. /**
  158. * Check if previous block (in the memory space) belongs to a free list
  159. */
  160. bool is_previous_free(void *ptr);
  161. /**
  162. * Get the size of the previous block (in the memory space)
  163. *
  164. * \param ptr The pointer to the data part of the current memory block.
  165. */
  166. size_t get_previous_size(void *ptr);
  167. /**
  168. * Get the size and the availability of the previous block (in the memory
  169. * space)
  170. *
  171. * \param ptr The pointer to the data part of the previous memory block.
  172. */
  173. size_t get_previous_size_availability(void *ptr);
  174. /**
  175. * Get the previous memory block on data layout level
  176. *
  177. * \param ptr The pointer to the data part of the current memory block.
  178. *
  179. * \return The pointer to the data part of the previous memory block on
  180. * data layout level.
  181. */
  182. void * get_dlprevious(void *ptr);
  183. /**
  184. * Get the next memory block on data layout level if there is one
  185. *
  186. * \param allocator The pointer to the allocator who manages the block.
  187. * \param ptr The pointer to the data part of the current memory block.
  188. *
  189. * \return The pointer to the next block.
  190. * \retval NULL There is no next block.
  191. */
  192. void * get_dlnext(allocator_t *allocator, void *ptr);
  193. #endif /* BLOCK_HEADER_H */