osi_mem_recycler.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef _OSI_MEM_RECYCLER_H_
  13. #define _OSI_MEM_RECYCLER_H_
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <stddef.h>
  17. #include "kernel_config.h"
  18. #include "osi_compiler.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /**
  23. * @brief opaque data structure of memory recycler
  24. */
  25. typedef struct osiMemRecycler osiMemRecycler_t;
  26. /**
  27. * @brief creat memory recycler
  28. *
  29. * To avoid dynamic memory complexity, static memory with specified
  30. * \p depth will be used.
  31. *
  32. * @param depth maximum pointer count
  33. * @return
  34. * - memory recycler pointer
  35. * - NULL if out of memory
  36. */
  37. osiMemRecycler_t *osiMemRecyclerCreate(size_t depth);
  38. /**
  39. * @brief delete the memory recycler
  40. *
  41. * When \p p is NULL, nothing will be done.
  42. *
  43. * @param d the memory recycler
  44. */
  45. void osiMemRecyclerDelete(osiMemRecycler_t *d);
  46. /**
  47. * @brief put a pointer into memory recycler
  48. *
  49. * To mimic free (3), \p p can be NULL. In this case, it will be
  50. * ignored silently.
  51. *
  52. * When the pointer is already in the memory recycler, it can't be
  53. * added again.
  54. *
  55. * @param d the memory recycler, must be valid
  56. * @param p pointer to be put to memory recycler
  57. * @return
  58. * - true on success
  59. * - false on fail
  60. * - invalid parameter
  61. * - the pointer already in memory recycler
  62. */
  63. bool osiMemRecyclerPut(osiMemRecycler_t *d, const void *p);
  64. /**
  65. * @brief undo put a pointer into memory recycler
  66. *
  67. * Though it is rare to undo put, this is also provided.
  68. *
  69. * @param d the memory recycler, must be valid
  70. * @param p pointer to be extract from memory recycler
  71. * @return
  72. * - true on success
  73. * - false on fail
  74. * - invalid parameter
  75. * - the pointer not in memory recycler
  76. */
  77. bool osiMemRecyclerUndoPut(osiMemRecycler_t *d, const void *p);
  78. /**
  79. * @brief free pointers into memory recycler
  80. *
  81. * After pointers are freed, they are extracted from the memory
  82. * recycler also.
  83. *
  84. * @param d the memory recycler, must be valid
  85. */
  86. void osiMemRecyclerEmpty(osiMemRecycler_t *d);
  87. #ifdef __cplusplus
  88. }
  89. #endif
  90. #endif