block_header.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. /** The header structure of every memory block inside a heap. */
  30. typedef struct block_header_s {
  31. size_t size; /**< The LSB represents the availability of the block (1
  32. for used, 0 for free), the rest the size of the data
  33. part. */
  34. size_t requested_size; /**< The requested size of the data part */
  35. size_t previous_size; /**< The LSB represents the availability of the
  36. previous block, the rest the size of the data
  37. part of the previous block in the memory space */
  38. void *next; /**< The next memory block in the list that the current
  39. block belongs to */
  40. #ifdef WITH_OWNERSHIP
  41. heap_t *heap_owner; /** < A pointer to the heap the current block belongs to */
  42. #endif /* WITH_OWNERSHIP */
  43. } block_header_t;
  44. /**
  45. * The size of the header in number of bytes
  46. */
  47. #define HEADER_SIZE sizeof(block_header_t)
  48. /**
  49. * Get the next memory block.
  50. *
  51. * \param ptr The pointer to the data part of the current memory block.
  52. *
  53. * \return The pointer of the data part of the next (in list) memory block.
  54. * \retval NULL There is no next memory block in the list.
  55. */
  56. void * get_next(void *ptr);
  57. /**
  58. * Get the size of the memory block's data
  59. *
  60. * \param ptr The pointer to the data part of the current memory block.
  61. *
  62. * \return The size of the data part of the current memory block.
  63. */
  64. size_t get_size(void *ptr);
  65. /**
  66. * Get the requested size of the memory block's data
  67. *
  68. * \param ptr The pointer to the data part of the current memory block.
  69. *
  70. * \return The size of the data that was initialy requested for this memory
  71. * block.
  72. */
  73. size_t get_requested_size(void *ptr);
  74. /**
  75. * Get all information of the memory block header's size record
  76. *
  77. * \param ptr The pointer to the data part of the current memory block.
  78. *
  79. * \return The availability and the size of the data part of the current memory
  80. * block.
  81. */
  82. size_t get_size_availability(void *ptr);
  83. /**
  84. * Set the size of the memory block's data
  85. *
  86. * \param ptr The pointer to the data part of the current memory block.
  87. * \param size The size of the data part of the current memory block.
  88. */
  89. void set_size(void *ptr, size_t size);
  90. /**
  91. * Set the requested size of memory block's data
  92. *
  93. * \param ptr The pointer to the data part of the current memory block.
  94. * \param size The requested size for the data part of the current memory
  95. * block.
  96. */
  97. void set_requested_size(void *ptr, size_t size);
  98. /**
  99. * Mark the memory block as used
  100. *
  101. * \param ptr The pointer to the data part of the memory block.
  102. */
  103. void mark_used(void *ptr);
  104. /**
  105. * Mark the memory block as free
  106. *
  107. * \param ptr The pointer to the data part of the memory block.
  108. */
  109. void mark_free(void *ptr);
  110. /**
  111. * Set the availability and the size of the previous memory block
  112. *
  113. * \param ptr The pointer to the data part of the previous memory block.
  114. * \param previous_size_availability The size for the data part of the previous
  115. * memory block on data layout level.
  116. */
  117. void set_previous_size_availability(void *ptr, size_t previous_size_availability);
  118. /**
  119. * Set the next memory block of a block
  120. *
  121. * \param block The pointer to the data part of the current memory
  122. * block.
  123. * \param next_block The pointer to the data part of the next memory block.
  124. */
  125. void set_next(void *block, void *next_block);
  126. #ifdef WITH_OWNERSHIP
  127. /**
  128. * Set the heap owner of a memory block
  129. *
  130. * \param ptr The pointer to the data part of the memory block.
  131. * \param heap_owner The pointer to the heap owner.
  132. */
  133. void set_owner(void *ptr, heap_t *heap_owner);
  134. /**
  135. * Get the heap owner of a memory block
  136. *
  137. * \param ptr The pointer to the data part of the memory block.
  138. * \return The pointer to the heap owner.
  139. */
  140. heap_t * get_owner(void *ptr);
  141. #endif /* WITH_OWNERSHIP */
  142. /**
  143. * Check if previous block (in the memory space) belongs to a free list
  144. */
  145. bool is_previous_free(void *ptr);
  146. /**
  147. * Get the size of the previous block (in the memory space)
  148. *
  149. * \param ptr The pointer to the data part of the current memory block.
  150. */
  151. size_t get_previous_size(void *ptr);
  152. /**
  153. * Get the size and the availability of the previous block (in the memory
  154. * space)
  155. *
  156. * \param ptr The pointer to the data part of the previous memory block.
  157. */
  158. size_t get_previous_size_availability(void *ptr);
  159. /**
  160. * Get the previous memory block (in the memory space)
  161. *
  162. * \param ptr The pointer to the data part of the current memory block.
  163. */
  164. void * get_previous(void *ptr);
  165. /**
  166. * Removes a memory block from a singly linked list of memory blocks.
  167. *
  168. * \param *block The block to be removed.
  169. * \param *starting_node The starting memory block of the list.
  170. */
  171. void remove_block(void *block, void *starting_node);
  172. #endif /* BLOCK_HEADER_H */