block_header.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. /** The header structure of every memory block inside a heap. */
  13. typedef struct block_header_s {
  14. size_t size; /**< The LSB represents the availability of the block (1
  15. for used, 0 for free), the rest the size of the data
  16. part. */
  17. size_t requested_size; /**< The requested size of the data part */
  18. size_t previous_size; /**< The LSB represents the availability of the
  19. previous block, the rest the size of the data
  20. part of the previous block in the memory space */
  21. void *next; /**< The next memory block in the list that the current
  22. block belongs to */
  23. } block_header_t;
  24. /**
  25. * The size of the header in number of bytes
  26. */
  27. #define HEADER_SIZE sizeof(block_header_t)
  28. /**
  29. * Get the next memory block.
  30. *
  31. * \param ptr The pointer to the data part of the current memory block.
  32. *
  33. * \return The pointer of the data part of the next (in list) memory block.
  34. * \retval NULL There is no next memory block in the list.
  35. */
  36. void * get_next(void *ptr);
  37. /**
  38. * Get the size of the memory block's data
  39. *
  40. * \param ptr The pointer to the data part of the current memory block.
  41. *
  42. * \return The size of the data part of the current memory block.
  43. */
  44. size_t get_size(void *ptr);
  45. /**
  46. * Get the requested size of the memory block's data
  47. *
  48. * \param ptr The pointer to the data part of the current memory block.
  49. *
  50. * \return The size of the data that was initialy requested for this memory
  51. * block.
  52. */
  53. size_t get_requested_size(void *ptr);
  54. /**
  55. * Get all information of the memory block header's size record
  56. *
  57. * \param ptr The pointer to the data part of the current memory block.
  58. *
  59. * \return The availability and the size of the data part of the current memory
  60. * block.
  61. */
  62. size_t get_size_availability(void *ptr);
  63. /**
  64. * Set the size of the memory block's data
  65. *
  66. * \param ptr The pointer to the data part of the current memory block.
  67. * \param size The size of the data part of the current memory block.
  68. */
  69. void set_size(void *ptr, size_t size);
  70. /**
  71. * Set the requested size of memory block's data
  72. *
  73. * \param ptr The pointer to the data part of the current memory block.
  74. * \param size The requested size for the data part of the current memory
  75. * block.
  76. */
  77. void set_requested_size(void *ptr, size_t size);
  78. /**
  79. * Mark the memory block as used
  80. *
  81. * \param ptr The pointer to the data part of the memory block.
  82. */
  83. void mark_used(void *ptr);
  84. /**
  85. * Mark the memory block as free
  86. *
  87. * \param ptr The pointer to the data part of the memory block.
  88. */
  89. void mark_free(void *ptr);
  90. /**
  91. * Set the availability and the size of the previous memory block
  92. *
  93. * \param ptr The pointer to the data part of the previous memory block.
  94. * \param previous_size_availability The size for the data part of the previous
  95. * memory block on data layout level.
  96. */
  97. void set_previous_size_availability(void *ptr, size_t previous_size_availability);
  98. /**
  99. * Set the next memory block of a block
  100. *
  101. * \param block The pointer to the data part of the current memory
  102. * block.
  103. * \param next_block The pointer to the data part of the next memory block.
  104. */
  105. void set_next(void *block, void *next_block);
  106. /**
  107. * Check if previous block (in the memory space) belongs to a free list
  108. */
  109. bool is_previous_free(void *ptr);
  110. /**
  111. * Get the size of the previous block (in the memory space)
  112. *
  113. * \param ptr The pointer to the data part of the current memory block.
  114. */
  115. size_t get_previous_size(void *ptr);
  116. /**
  117. * Get the size and the availability of the previous block (in the memory
  118. * space)
  119. *
  120. * \param ptr The pointer to the data part of the previous memory block.
  121. */
  122. size_t get_previous_size_availability(void *ptr);
  123. /**
  124. * Get the previous memory block (in the memory space)
  125. *
  126. * \param ptr The pointer to the data part of the current memory block.
  127. */
  128. void * get_previous(void *ptr);
  129. /**
  130. * Removes a memory block from a singly linked list of memory blocks.
  131. *
  132. * \param *block The block to be removed.
  133. * \param *starting_node The starting memory block of the list.
  134. */
  135. void remove_block(void *block, void *starting_node);
  136. #endif /* BLOCK_HEADER_H */