dblockpool.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. \ingroup lub
  3. \defgroup lub_dblockpool dblockpool
  4. @{
  5. \brief This interface provides a dynamic facility to manage the allocation and
  6. deallocation of fixed sized blocks of memory.
  7. It dynamically allocates chunks of memory each of which is used as a "blockpool".
  8. This dynamic memory is taken from the standard heap via "malloc()".
  9. This type of memory manager is very fast and doesn't suffer from
  10. any memory fragmentation problems.
  11. This allocator can reuse blocks in the order in which they are freed,
  12. this is significant for some applications where you don't want to
  13. re-use a particular freed block too soon. e.g. hardware timing
  14. limits on re-use.
  15. \author Graeme McKerrell
  16. \date Created On : Fri Feb 24 14:50:18 2005
  17. \version TESTED
  18. */
  19. /*---------------------------------------------------------------
  20. * HISTORY
  21. * 24-Feb-2006 Graeme McKerrell
  22. * Initial version
  23. *---------------------------------------------------------------
  24. * Copyright (C) 2006 Newport Networks. All Rights Reserved.
  25. *--------------------------------------------------------------- */
  26. #ifndef _lub_dblockpool_h
  27. #define _lub_dblockpool_h
  28. #include <stddef.h>
  29. #include "c_decl.h"
  30. _BEGIN_C_DECL
  31. /****************************************************************
  32. * TYPE DEFINITIONS
  33. **************************************************************** */
  34. /**
  35. * This type represents a "dblockpool" instance.
  36. */
  37. typedef struct _lub_dblockpool lub_dblockpool_t;
  38. /**
  39. * CLIENTS MUST NOT USE THESE FIELDS DIRECTLY
  40. */
  41. struct _lub_dblockpool
  42. {
  43. struct _lub_dblockpool_chunk *first_chunk;
  44. size_t block_size;
  45. unsigned chunk_size;
  46. unsigned max_chunks;
  47. };
  48. /****************************************************************
  49. * DBLOCKPOOL OPERATIONS
  50. **************************************************************** */
  51. /**
  52. * This operation initialises an instance of a dblockpool.
  53. *
  54. * \pre 'blocksize' must be an multiple of 'sizeof(void *)'.
  55. * (If the client declares a structure for the block this should be handled
  56. * automatically by the compiler)
  57. *
  58. * \post If the size constraint is not met an assert will fire.
  59. * \post Following initialisation the allocation of memory can be performed.
  60. */
  61. extern void
  62. lub_dblockpool_init(
  63. /**
  64. * the "dblockpool" instance to initialise.
  65. */
  66. lub_dblockpool_t *instance,
  67. /**
  68. * The size in bytes of each block.
  69. */
  70. size_t blocksize,
  71. /**
  72. * The number of blocks to be managed in a chunk.
  73. */
  74. unsigned chunksize,
  75. /**
  76. * The maximum number of chunks to be allocated.
  77. * (a value of zero means unlimited...)
  78. */
  79. unsigned max_chunks
  80. );
  81. /**
  82. * This operation finalises an instance of a dblockpool.
  83. *
  84. * \pre 'instance' must have been initialised first.
  85. *
  86. * \post All the dynamic memory allocated will be released.
  87. */
  88. extern void
  89. lub_dblockpool_fini(
  90. /**
  91. * the "dblockpool" instance to finalise.
  92. */
  93. lub_dblockpool_t *instance
  94. );
  95. /**
  96. * This operation allocates a "block" of memory from a "dblockpool"
  97. *
  98. *
  99. * \pre
  100. * The dblockpool must have been initialised.
  101. *
  102. * \return
  103. * A pointer to a "block" of memory, or NULL if there is none left for
  104. * allocation.
  105. *
  106. * \post
  107. * The behaviour is undefined if the "dblockpool" is uninitialised.
  108. */
  109. extern void *
  110. lub_dblockpool_alloc(
  111. /**
  112. * the "dblockpool" instance to operate on
  113. */
  114. lub_dblockpool_t *instance
  115. );
  116. /**
  117. * This operation de-allocates a "block" of memory back into a "blockpool"
  118. *
  119. * \pre
  120. * The specified block must have been previously allocated using
  121. * lub_blockpool_alloc()
  122. *
  123. * \post
  124. * The de-allocated block become available for subsequent
  125. * lub_blockpool_alloc() requests.
  126. */
  127. extern void
  128. lub_dblockpool_free(
  129. /**
  130. * the "blockpool" instance to invoke this operation upon
  131. */
  132. lub_dblockpool_t *instance,
  133. /**
  134. * the "block" to release back to the pool
  135. */
  136. void *block
  137. );
  138. _END_C_DECL
  139. #endif /* _lub_dblockpool_h */
  140. /** @} lub_dblockpool */