chry_mempool.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef CHRY_MEMPOOL_H
  7. #define CHRY_MEMPOOL_H
  8. #include <stdint.h>
  9. #include <string.h>
  10. #include <stdbool.h>
  11. typedef void *chry_mempool_osal_sem_t;
  12. #ifndef CONFIG_CHRY_MEMPOOL_MAX_BLOCK_COUNT
  13. #define CONFIG_CHRY_MEMPOOL_MAX_BLOCK_COUNT 128
  14. #endif
  15. typedef struct {
  16. uint32_t in; /*!< Define the write pointer. */
  17. uint32_t out; /*!< Define the read pointer. */
  18. uint32_t mask; /*!< Define the write and read pointer mask. */
  19. void *pool; /*!< Define the memory pointer. */
  20. } chry_mempool_ringbuffer_t;
  21. struct chry_mempool {
  22. chry_mempool_ringbuffer_t in;
  23. chry_mempool_ringbuffer_t out;
  24. chry_mempool_osal_sem_t out_sem;
  25. void *block;
  26. uint32_t block_size;
  27. uint32_t block_count;
  28. uint8_t in_buf[sizeof(uintptr_t) * CONFIG_CHRY_MEMPOOL_MAX_BLOCK_COUNT];
  29. uint8_t out_buf[sizeof(uintptr_t) * CONFIG_CHRY_MEMPOOL_MAX_BLOCK_COUNT];
  30. };
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. chry_mempool_osal_sem_t chry_mempool_osal_sem_create(uint32_t max_count);
  35. void chry_mempool_osal_sem_delete(chry_mempool_osal_sem_t sem);
  36. int chry_mempool_osal_sem_take(chry_mempool_osal_sem_t sem, uint32_t timeout);
  37. int chry_mempool_osal_sem_give(chry_mempool_osal_sem_t sem);
  38. int chry_mempool_create(struct chry_mempool *pool, void *block, uint32_t block_size, uint32_t block_count);
  39. uintptr_t *chry_mempool_alloc(struct chry_mempool *pool);
  40. int chry_mempool_free(struct chry_mempool *pool, uintptr_t *item);
  41. int chry_mempool_send(struct chry_mempool *pool, uintptr_t *item);
  42. int chry_mempool_recv(struct chry_mempool *pool, uintptr_t **item, uint32_t timeout);
  43. void chry_mempool_reset(struct chry_mempool *pool);
  44. #ifdef __cplusplus
  45. }
  46. #endif
  47. #endif