Projektarbeit Line Following Robot bei Prof. Chowanetz im WS22/23
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vcos_blockpool.h 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. Copyright (c) 2012, Broadcom Europe Ltd
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of the copyright holder nor the
  12. names of its contributors may be used to endorse or promote products
  13. derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  18. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /*=============================================================================
  26. VideoCore OS Abstraction Layer - fixed size allocator support
  27. =============================================================================*/
  28. #ifndef VCOS_BLOCKPOOL_H
  29. #define VCOS_BLOCKPOOL_H
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. #include "vcos_types.h"
  34. #include "pthreads/vcos_platform.h"
  35. /** \file
  36. *
  37. * Thread safe, fixed size allocator API.
  38. *
  39. */
  40. /** Initialises a block pool to use already allocated (e.g. statically)
  41. * allocated memory.
  42. *
  43. * Different implementations will incur different overheads. Use
  44. * VCOS_BLOCKPOOL_SIZE(num_blocks, block_size) to calculate the number
  45. * of bytes required including overheads for the desired pools.
  46. *
  47. * @param pool Pointer to pool object
  48. * @param num_blocks The number of blocks required.
  49. * @param block_size The size of an individual block.
  50. * @param start The address of the start of the pool.
  51. * @param pool_size The size of the pool in bytes.
  52. * @param align Alignment for block data. Use VCOS_BLOCKPOOL_ALIGN_DEFAULT
  53. * for default word alignment.
  54. * @param flags Reserved for future use.
  55. * @param name Name of the pool. Used for diagnostics.
  56. *
  57. * @return VCOS_SUCCESS if the pool was created.
  58. */
  59. VCOS_INLINE_DECL
  60. VCOS_STATUS_T vcos_blockpool_init(VCOS_BLOCKPOOL_T *pool,
  61. VCOS_UNSIGNED num_blocks, VCOS_UNSIGNED block_size,
  62. void *start, VCOS_UNSIGNED pool_size, VCOS_UNSIGNED align,
  63. VCOS_UNSIGNED flags, const char *name);
  64. /** Creates a pool of blocks of a given size within a buffer allocated on
  65. * the heap.
  66. *
  67. * The heap memory is freed when the block pool is destroyed by
  68. * calling vcos_blockpool_delete.
  69. *
  70. * @param pool Pointer to pool object
  71. * @param num_blocks The number of blocks required.
  72. * @param block_size The size of an individual block.
  73. * @param align Alignment for block data. Use VCOS_BLOCKPOOL_ALIGN_DEFAULT
  74. * for default word alignment.
  75. * @param flags Reserved for future use.
  76. * @param name Name of the pool. Used for diagnostics.
  77. *
  78. * @return VCOS_SUCCESS if the pool was created.
  79. */
  80. VCOS_INLINE_DECL
  81. VCOS_STATUS_T vcos_blockpool_create_on_heap(VCOS_BLOCKPOOL_T *pool,
  82. VCOS_UNSIGNED num_blocks, VCOS_UNSIGNED block_size,
  83. VCOS_UNSIGNED align, VCOS_UNSIGNED flags,
  84. const char *name);
  85. /** Allocate a block from the pool
  86. *
  87. * @param pool Pointer to the pool to allocate from.
  88. * @return a pointer to the newly allocated block or NULL if no blocks were
  89. * available.
  90. */
  91. VCOS_INLINE_DECL
  92. void *vcos_blockpool_alloc(VCOS_BLOCKPOOL_T *pool);
  93. /** Allocate a block from the pool and zero it.
  94. *
  95. * @param pool Pointer to the pool to allocate from.
  96. * @return a pointer to the newly allocated block or NULL if no blocks were
  97. * available.
  98. */
  99. VCOS_INLINE_DECL
  100. void *vcos_blockpool_calloc(VCOS_BLOCKPOOL_T *pool);
  101. /** Returns a block to the pool.
  102. *
  103. * @param block The block to free.
  104. */
  105. VCOS_INLINE_DECL
  106. void vcos_blockpool_free(void *block);
  107. /** Queries the number of available blocks in the pool.
  108. * @param pool The pool to query.
  109. */
  110. VCOS_INLINE_IMPL
  111. VCOS_UNSIGNED vcos_blockpool_available_count(VCOS_BLOCKPOOL_T *pool);
  112. /** Queries the number of used blocks in the pool.
  113. * @param pool The pool to query.
  114. */
  115. VCOS_INLINE_IMPL
  116. VCOS_UNSIGNED vcos_blockpool_used_count(VCOS_BLOCKPOOL_T *pool);
  117. /** Deinitialize a memory pool.
  118. *
  119. * @param pool The pool to de-initialize.
  120. */
  121. VCOS_INLINE_DECL
  122. void vcos_blockpool_delete(VCOS_BLOCKPOOL_T *pool);
  123. /** Return an integer handle for a given allocated block. */
  124. VCOS_INLINE_DECL
  125. uint32_t vcos_blockpool_elem_to_handle(void *block);
  126. /** Convert an integer handle back into a pointer.
  127. * Returns NULL if invalid. */
  128. VCOS_INLINE_DECL
  129. void *vcos_blockpool_elem_from_handle(VCOS_BLOCKPOOL_T *pool, uint32_t handle);
  130. /** Checks whether a pointer is an allocated block within the specified pool.
  131. * Returns true if the block is valid, otherwise, false is returned. */
  132. VCOS_INLINE_DECL
  133. uint32_t vcos_blockpool_is_valid_elem(
  134. VCOS_BLOCKPOOL_T *pool, const void *block);
  135. /** May be called once to allow the block pool to be extended by dynamically
  136. * adding subpools. The block size cannot be altered.
  137. *
  138. * @param num_extensions The number of extensions that may be created.
  139. * The maximum is (VCOS_BLOCKPOOL_MAX_SUBPOOLS - 1)
  140. * @param num_blocks The number of blocks to allocate in each in each
  141. * dynamically allocated subpool.
  142. * @return VCOS_SUCCESS if successful.
  143. */
  144. VCOS_INLINE_DECL
  145. VCOS_STATUS_T vcos_blockpool_extend(VCOS_BLOCKPOOL_T *pool,
  146. VCOS_UNSIGNED num_extensions, VCOS_UNSIGNED num_blocks);
  147. #ifdef __cplusplus
  148. }
  149. #endif
  150. #endif