da_mem_pool.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef DA_MEM_POOL_H_
  17. #define DA_MEM_POOL_H_
  18. #include "da_queue.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #define MEM_F_SHARED 0x01
  22. struct pool_head {
  23. void **free_list;
  24. CIRCLEQ_ENTRY(pool_head) pool_circqe;
  25. unsigned int used;
  26. unsigned int allocated;
  27. unsigned int limit;
  28. unsigned int minavail;
  29. unsigned int size;
  30. unsigned int flags;
  31. unsigned int users;
  32. char name[12];
  33. };
  34. CIRCLEQ_HEAD(pool_circqh, pool_head);
  35. extern char mem_poison_byte;
  36. /*
  37. * init pools
  38. */
  39. void init_pools();
  40. /* Try to find an existing shared pool with the same characteristics and
  41. * returns it, otherwise creates this one. NULL is returned if no memory
  42. * is available for a new creation.
  43. */
  44. struct pool_head *create_pool(char *name, unsigned int size,
  45. unsigned int flags);
  46. /* Allocate a new entry for pool <pool>, and return it for immediate use.
  47. * NULL is returned if no memory is available for a new creation.
  48. */
  49. void *pool_refill_alloc(struct pool_head *pool);
  50. /*
  51. * This function frees whatever can be freed in pool <pool>.
  52. */
  53. void pool_flush(struct pool_head *pool);
  54. /*
  55. * This function frees whatever can be freed in all pools, but respecting
  56. * the minimum thresholds imposed by owners.
  57. */
  58. void pool_gc();
  59. /*
  60. * This function destroys a pool by freeing it completely, unless it's still
  61. * in use. This should be called only under extreme circumstances. It always
  62. * returns NULL if the resulting pool is empty, easing the clearing of the old
  63. * pointer, otherwise it returns the pool.
  64. * .
  65. */
  66. void *pool_destroy(struct pool_head *pool);
  67. /*
  68. * dump pools to log
  69. */
  70. void dump_pools(void);
  71. /*
  72. * Returns a pointer to type <type> taken from the
  73. * pool <pool_type> or dynamically allocated. In the
  74. * first case, <pool_type> is updated to point to the
  75. * next element in the list.
  76. */
  77. #define pool_alloc(pool) \
  78. ({ \
  79. void *__p; \
  80. if ((__p = (pool)->free_list) == NULL) \
  81. __p = pool_refill_alloc(pool); \
  82. else { \
  83. (pool)->free_list = *(void **)(pool)->free_list; \
  84. (pool)->used++; \
  85. } \
  86. __p; \
  87. })
  88. /*
  89. * Puts a memory area back to the corresponding pool.
  90. * Items are chained directly through a pointer that
  91. * is written in the beginning of the memory area, so
  92. * there's no need for any carrier cell. This implies
  93. * that each memory area is at least as big as one
  94. * pointer. Just like with the libc's free(), nothing
  95. * is done if <ptr> is NULL.
  96. */
  97. #define pool_free(pool, ptr) \
  98. ({ \
  99. if (likely((ptr) != NULL)) { \
  100. *(void **)(ptr) = (void *)(pool)->free_list; \
  101. (pool)->free_list = (void *)(ptr); \
  102. (pool)->used--; \
  103. } \
  104. })
  105. #endif /* DA_MEM_H_ */