block_header.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 ptr The pointer to the data part of the current memory block.
  91. * \param size The size of the data part of the current memory block.
  92. */
  93. void set_size_and_free(void *ptr, size_t size);
  94. /**
  95. * Set the size of the memory block's data and mark it used
  96. *
  97. * \param ptr The pointer to the data part of the current memory block.
  98. * \param size The size of the data part of the current memory block.
  99. */
  100. void set_size_and_used(void *ptr, size_t size);
  101. #ifdef FUTURE_FEATURES
  102. /**
  103. * Set the requested size of memory block's data
  104. *
  105. * \param ptr The pointer to the data part of the current memory block.
  106. * \param size The requested size for the data part of the current memory
  107. * block.
  108. */
  109. void set_requested_size(void *ptr, size_t size);
  110. #endif /* FUTURE_FEATURES */
  111. /**
  112. * Mark the memory block as used
  113. *
  114. * \param ptr The pointer to the data part of the memory block.
  115. */
  116. void mark_used(void *ptr);
  117. /**
  118. * Mark the memory block as free
  119. *
  120. * \param ptr The pointer to the data part of the memory block.
  121. */
  122. void mark_free(void *ptr);
  123. /**
  124. * Set the availability and the size of the previous memory block
  125. *
  126. * \param ptr The pointer to the data part of the previous memory block.
  127. * \param previous_size_availability The size for the data part of the previous
  128. * memory block on data layout level.
  129. */
  130. void set_previous_size_availability(void *ptr, size_t previous_size_availability);
  131. #ifdef WITH_OWNERSHIP
  132. /**
  133. * Set the heap owner of a memory block
  134. *
  135. * \param ptr The pointer to the data part of the memory block.
  136. * \param heap_owner The pointer to the heap owner.
  137. */
  138. void set_owner(void *ptr, heap_t *heap_owner);
  139. /**
  140. * Get the heap owner of a memory block
  141. *
  142. * \param ptr The pointer to the data part of the memory block.
  143. * \return The pointer to the heap owner.
  144. */
  145. heap_t * get_owner(void *ptr);
  146. #endif /* WITH_OWNERSHIP */
  147. /**
  148. * Check if previous block (in the memory space) belongs to a free list
  149. */
  150. bool is_previous_free(void *ptr);
  151. /**
  152. * Get the size of the previous block (in the memory space)
  153. *
  154. * \param ptr The pointer to the data part of the current memory block.
  155. */
  156. size_t get_previous_size(void *ptr);
  157. /**
  158. * Get the size and the availability of the previous block (in the memory
  159. * space)
  160. *
  161. * \param ptr The pointer to the data part of the previous memory block.
  162. */
  163. size_t get_previous_size_availability(void *ptr);
  164. /**
  165. * Get the previous memory block on data layout level
  166. *
  167. * \param ptr The pointer to the data part of the current memory block.
  168. */
  169. void * get_dlprevious(void *ptr);
  170. #endif /* BLOCK_HEADER_H */