qdf_flex_mem.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
  3. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for
  6. * any purpose with or without fee is hereby granted, provided that the
  7. * above copyright notice and this permission notice appear in all
  8. * copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  11. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  12. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  13. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  14. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  15. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  17. * PERFORMANCE OF THIS SOFTWARE.
  18. */
  19. /**
  20. * DOC: qdf_flex_mem (flexibly sized memory allocator)
  21. * QCA driver framework (QDF) flex mem APIs
  22. *
  23. * A flex memory allocator is a memory pool which not only dynamically expands,
  24. * but also dynamically reduces as well. Benefits over full dynamic memory
  25. * allocation are amoritized allocation cost, and reduced memory fragmentation.
  26. *
  27. * The allocator consists of 3 parts: the pool, segments, and items. Items are
  28. * the smallest chunks of memory that are handed out via the alloc call, and
  29. * are all of a uniform size. Segments are groups of items, representing the
  30. * smallest amount of memory that can be dynamically allocated or freed. A pool
  31. * is simply a collection of segments.
  32. */
  33. #ifndef __QDF_FLEX_MEM_H
  34. #define __QDF_FLEX_MEM_H
  35. #include "qdf_list.h"
  36. #include "qdf_lock.h"
  37. #define QDF_FM_BITMAP uint32_t
  38. #define QDF_FM_BITMAP_BITS (sizeof(QDF_FM_BITMAP) * 8)
  39. /**
  40. * struct qdf_flex_mem_pool - a pool of memory segments
  41. * @seg_list: the list containing the memory segments
  42. * @lock: spinlock for protecting internal data structures
  43. * @reduction_limit: the minimum number of segments to keep during reduction
  44. * @item_size: the size of the items the pool will allocate
  45. */
  46. struct qdf_flex_mem_pool {
  47. qdf_list_t seg_list;
  48. struct qdf_spinlock lock;
  49. uint16_t reduction_limit;
  50. uint16_t item_size;
  51. };
  52. /**
  53. * struct qdf_flex_mem_segment - a memory pool segment
  54. * @node: the list node for membership in the memory pool
  55. * @dynamic: true if this segment was dynamically allocated
  56. * @used_bitmap: bitmap for tracking which items in the segment are in use
  57. * @bytes: raw memory for allocating items from
  58. */
  59. struct qdf_flex_mem_segment {
  60. qdf_list_node_t node;
  61. bool dynamic;
  62. QDF_FM_BITMAP used_bitmap;
  63. uint8_t *bytes;
  64. };
  65. /**
  66. * DEFINE_QDF_FLEX_MEM_POOL() - define a new flex mem pool with one segment
  67. * @name: the name of the pool variable
  68. * @size_of_item: size of the items the pool will allocate
  69. * @rm_limit: min number of segments to keep during reduction
  70. */
  71. #define DEFINE_QDF_FLEX_MEM_POOL(name, size_of_item, rm_limit) \
  72. struct qdf_flex_mem_pool name; \
  73. uint8_t __ ## name ## _head_bytes[QDF_FM_BITMAP_BITS * (size_of_item)];\
  74. struct qdf_flex_mem_segment __ ## name ## _head = { \
  75. .node = QDF_LIST_NODE_INIT_SINGLE( \
  76. QDF_LIST_ANCHOR(name.seg_list)), \
  77. .bytes = __ ## name ## _head_bytes, \
  78. }; \
  79. struct qdf_flex_mem_pool name = { \
  80. .seg_list = QDF_LIST_INIT_SINGLE(__ ## name ## _head.node), \
  81. .reduction_limit = (rm_limit), \
  82. .item_size = (size_of_item), \
  83. }
  84. /**
  85. * qdf_flex_mem_init() - initialize a qdf_flex_mem_pool
  86. * @pool: the pool to initialize
  87. *
  88. * Return: None
  89. */
  90. void qdf_flex_mem_init(struct qdf_flex_mem_pool *pool);
  91. /**
  92. * qdf_flex_mem_deinit() - deinitialize a qdf_flex_mem_pool
  93. * @pool: the pool to deinitialize
  94. *
  95. * Return: None
  96. */
  97. void qdf_flex_mem_deinit(struct qdf_flex_mem_pool *pool);
  98. /**
  99. * qdf_flex_mem_alloc() - logically allocate memory from the pool
  100. * @pool: the pool to allocate from
  101. *
  102. * This function returns any unused item from any existing segment in the pool.
  103. * If there are no unused items in the pool, a new segment is dynamically
  104. * allocated to service the request. The size of the allocated memory is the
  105. * size originally used to create the pool.
  106. *
  107. * Return: Point to newly allocated memory, NULL on failure
  108. */
  109. void *qdf_flex_mem_alloc(struct qdf_flex_mem_pool *pool);
  110. /**
  111. * qdf_flex_mem_free() - logically frees @ptr from the pool
  112. * @pool: the pool to return the memory to
  113. * @ptr: a pointer received via a call to qdf_flex_mem_alloc()
  114. *
  115. * This function marks the item corresponding to @ptr as unused. If that item
  116. * was the last used item in the segment it belongs to, and the segment was
  117. * dynamically allocated, the segment will be freed.
  118. *
  119. * Return: None
  120. */
  121. void qdf_flex_mem_free(struct qdf_flex_mem_pool *pool, void *ptr);
  122. #endif /* __QDF_FLEX_MEM_H */