block_header.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * \file block_header.h
  3. * \author Ioannis Koutras (joko@microlab.ntua.gr)
  4. * \date September, 2011
  5. * \brief Block header structure and functions, and memory block functions.
  6. */
  7. #ifndef BLOCK_HEADER_H
  8. #define BLOCK_HEADER_H
  9. #include <stddef.h>
  10. #include <stdbool.h>
  11. /**
  12. * \brief The header structure of every memory block inside a heap.
  13. */
  14. typedef struct block_header_s {
  15. size_t size; /**< \brief The size of the data part. */
  16. size_t requested_size; /**< \brief The requested size of the data part */
  17. size_t previous_size; /**< \brief The size of the data part of the
  18. previous block in the memory space */
  19. void *next; /**< \brief The next memory block in the list that the
  20. current block belongs to */
  21. } block_header_t;
  22. /**
  23. * \brief The size of the header in number of bytes
  24. */
  25. #define HEADER_SIZE sizeof(block_header_t)
  26. /**
  27. * \brief Get the next memory block.
  28. *
  29. * \param ptr The pointer to the data part of the current memory block.
  30. *
  31. * \return The pointer of the data part of the next (in list) memory block.
  32. * \retval NULL There is no next memory block in the list.
  33. */
  34. void * get_next(void *ptr);
  35. /**
  36. * \brief Get the size of the memory block's data
  37. *
  38. * \param ptr The pointer to the data part of the current memory block.
  39. *
  40. * \return The size of the data part of the current memory block.
  41. */
  42. size_t get_size(void *ptr);
  43. /**
  44. * \brief Set the size of the memory block's data
  45. *
  46. * \param ptr The pointer to the data part of the current memory block.
  47. * \param size The size of the data part of the current memory block.
  48. */
  49. void set_size(void *ptr, size_t size);
  50. /**
  51. * \brief Set the requested size of memory block's data
  52. *
  53. * \param ptr The pointer to the data part of the current memory block.
  54. * \param size The requested size for the data part of the current memory
  55. * block.
  56. */
  57. void set_requested_size(void *ptr, size_t size);
  58. /**
  59. * \brief Set the next memory block of a block
  60. *
  61. * \param block The pointer to the data part of the current memory
  62. * block.
  63. * \param next_block The pointer to the data part of the next memory block.
  64. */
  65. void set_next(void *block, void *next_block);
  66. /**
  67. * \brief Check if previous block (in the memory space) belongs to a free list
  68. */
  69. bool is_previous_free(void *ptr);
  70. /**
  71. * \brief Get the size of the previous block (in the memory space)
  72. *
  73. * \param ptr The pointer to the data part of the current memory block.
  74. */
  75. size_t get_previous_size(void *ptr);
  76. /**
  77. * \brief Get the previous memory block (in the memory space)
  78. *
  79. * \param ptr The pointer to the data part of the current memory block.
  80. */
  81. void * get_previous(void *ptr);
  82. /**
  83. * \brief Removes a memory block from a singly linked list of memory blocks.
  84. *
  85. * \param *block The block to be removed.
  86. * \param *starting_node The starting memory block of the list.
  87. */
  88. void remove_block(void *block, void *starting_node);
  89. #endif /* BLOCK_HEADER_H */