block_header.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. size_t requested_size; /**< The requested size of the data part */
  37. size_t previous_size; /**< The LSB represents the availability of the
  38. previous block, the rest the size of the data
  39. part of the previous block in the memory space */
  40. /* TODO Add an ifndef guard in case we have other block organizations */
  41. list_node_header_t pointers; /**< The necessary pointers for block
  42. organization. */
  43. #ifdef WITH_OWNERSHIP
  44. heap_t *heap_owner; /** < A pointer to the heap the current block belongs to */
  45. #endif /* WITH_OWNERSHIP */
  46. } block_header_t;
  47. /**
  48. * The size of the header in number of bytes
  49. */
  50. #define HEADER_SIZE sizeof(block_header_t)
  51. /**
  52. * \brief Get the address of the block header of a memory block.
  53. *
  54. * \param ptr The data part of the memory block.
  55. */
  56. block_header_t * get_header(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. #ifdef WITH_OWNERSHIP
  119. /**
  120. * Set the heap owner of a memory block
  121. *
  122. * \param ptr The pointer to the data part of the memory block.
  123. * \param heap_owner The pointer to the heap owner.
  124. */
  125. void set_owner(void *ptr, heap_t *heap_owner);
  126. /**
  127. * Get the heap owner of a memory block
  128. *
  129. * \param ptr The pointer to the data part of the memory block.
  130. * \return The pointer to the heap owner.
  131. */
  132. heap_t * get_owner(void *ptr);
  133. #endif /* WITH_OWNERSHIP */
  134. /**
  135. * Check if previous block (in the memory space) belongs to a free list
  136. */
  137. bool is_previous_free(void *ptr);
  138. /**
  139. * Get the size of the previous block (in the memory space)
  140. *
  141. * \param ptr The pointer to the data part of the current memory block.
  142. */
  143. size_t get_previous_size(void *ptr);
  144. /**
  145. * Get the size and the availability of the previous block (in the memory
  146. * space)
  147. *
  148. * \param ptr The pointer to the data part of the previous memory block.
  149. */
  150. size_t get_previous_size_availability(void *ptr);
  151. /**
  152. * Get the previous memory block on data layout level
  153. *
  154. * \param ptr The pointer to the data part of the current memory block.
  155. */
  156. void * get_dlprevious(void *ptr);
  157. #endif /* BLOCK_HEADER_H */