block_header.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * \file block_header.h
  3. * \author Ioannis Koutras (joko@microlab.ntua.gr)
  4. * \date September, 2011
  5. *
  6. * \brief Block header structure and functions, and memory block functions.
  7. */
  8. #ifndef BLOCK_HEADER_H
  9. #define BLOCK_HEADER_H
  10. #include <stddef.h>
  11. #include <stdbool.h>
  12. #include <dmmlib/heap.h>
  13. /** The header structure of every memory block inside a heap. */
  14. typedef struct block_header_s {
  15. size_t size; /**< The LSB represents the availability of the block (1
  16. for used, 0 for free), the rest the size of the data
  17. part. */
  18. size_t requested_size; /**< The requested size of the data part */
  19. size_t previous_size; /**< The LSB represents the availability of the
  20. previous block, the rest the size of the data
  21. part of the previous block in the memory space */
  22. void *next; /**< The next memory block in the list that the current
  23. block belongs to */
  24. #ifdef WITH_OWNERSHIP
  25. heap_t *heap_owner; /** < A pointer to the heap the current block belongs to */
  26. #endif /* WITH_OWNERSHIP */
  27. } block_header_t;
  28. /**
  29. * The size of the header in number of bytes
  30. */
  31. #define HEADER_SIZE sizeof(block_header_t)
  32. /**
  33. * Get the next memory block.
  34. *
  35. * \param ptr The pointer to the data part of the current memory block.
  36. *
  37. * \return The pointer of the data part of the next (in list) memory block.
  38. * \retval NULL There is no next memory block in the list.
  39. */
  40. void * get_next(void *ptr);
  41. /**
  42. * Get the size of the memory block's data
  43. *
  44. * \param ptr The pointer to the data part of the current memory block.
  45. *
  46. * \return The size of the data part of the current memory block.
  47. */
  48. size_t get_size(void *ptr);
  49. /**
  50. * Get the requested size of the memory block's data
  51. *
  52. * \param ptr The pointer to the data part of the current memory block.
  53. *
  54. * \return The size of the data that was initialy requested for this memory
  55. * block.
  56. */
  57. size_t get_requested_size(void *ptr);
  58. /**
  59. * Get all information of the memory block header's size record
  60. *
  61. * \param ptr The pointer to the data part of the current memory block.
  62. *
  63. * \return The availability and the size of the data part of the current memory
  64. * block.
  65. */
  66. size_t get_size_availability(void *ptr);
  67. /**
  68. * Set the size of the memory block's data
  69. *
  70. * \param ptr The pointer to the data part of the current memory block.
  71. * \param size The size of the data part of the current memory block.
  72. */
  73. void set_size(void *ptr, size_t size);
  74. /**
  75. * Set the requested size of memory block's data
  76. *
  77. * \param ptr The pointer to the data part of the current memory block.
  78. * \param size The requested size for the data part of the current memory
  79. * block.
  80. */
  81. void set_requested_size(void *ptr, size_t size);
  82. /**
  83. * Mark the memory block as used
  84. *
  85. * \param ptr The pointer to the data part of the memory block.
  86. */
  87. void mark_used(void *ptr);
  88. /**
  89. * Mark the memory block as free
  90. *
  91. * \param ptr The pointer to the data part of the memory block.
  92. */
  93. void mark_free(void *ptr);
  94. /**
  95. * Set the availability and the size of the previous memory block
  96. *
  97. * \param ptr The pointer to the data part of the previous memory block.
  98. * \param previous_size_availability The size for the data part of the previous
  99. * memory block on data layout level.
  100. */
  101. void set_previous_size_availability(void *ptr, size_t previous_size_availability);
  102. /**
  103. * Set the next memory block of a block
  104. *
  105. * \param block The pointer to the data part of the current memory
  106. * block.
  107. * \param next_block The pointer to the data part of the next memory block.
  108. */
  109. void set_next(void *block, void *next_block);
  110. #ifdef WITH_OWNERSHIP
  111. /**
  112. * Set the heap owner of a memory block
  113. *
  114. * \param ptr The pointer to the data part of the memory block.
  115. * \param heap_owner The pointer to the heap owner.
  116. */
  117. void set_owner(void *ptr, heap_t *heap_owner);
  118. /**
  119. * Get the heap owner of a memory block
  120. *
  121. * \param ptr The pointer to the data part of the memory block.
  122. * \return The pointer to the heap owner.
  123. */
  124. heap_t * get_owner(void *ptr);
  125. #endif /* WITH_OWNERSHIP */
  126. /**
  127. * Check if previous block (in the memory space) belongs to a free list
  128. */
  129. bool is_previous_free(void *ptr);
  130. /**
  131. * Get the size of the previous block (in the memory space)
  132. *
  133. * \param ptr The pointer to the data part of the current memory block.
  134. */
  135. size_t get_previous_size(void *ptr);
  136. /**
  137. * Get the size and the availability of the previous block (in the memory
  138. * space)
  139. *
  140. * \param ptr The pointer to the data part of the previous memory block.
  141. */
  142. size_t get_previous_size_availability(void *ptr);
  143. /**
  144. * Get the previous memory block (in the memory space)
  145. *
  146. * \param ptr The pointer to the data part of the current memory block.
  147. */
  148. void * get_previous(void *ptr);
  149. /**
  150. * Removes a memory block from a singly linked list of memory blocks.
  151. *
  152. * \param *block The block to be removed.
  153. * \param *starting_node The starting memory block of the list.
  154. */
  155. void remove_block(void *block, void *starting_node);
  156. #endif /* BLOCK_HEADER_H */