qdf_talloc.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2018 The Linux Foundation. All rights reserved.
  3. * Copyright (c) 2022-2024 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_talloc.h - Public APIs for t(ree) alloc(ate) memory management
  21. *
  22. * These APIs allocate memory like malloc, but track those allocations via a
  23. * parent-child relationship, or tree. If the parent is freed while it still has
  24. * children, a panic will be triggered. This effectively gives you the ability
  25. * to limit the lifetime of an allocation by ensuring the child allocation
  26. * lifetime will be strictly less than the parent allocation lifetime.
  27. */
  28. #ifndef __QDF_TALLOC_H
  29. #define __QDF_TALLOC_H
  30. #include "i_qdf_talloc.h"
  31. #include "qdf_status.h"
  32. /**
  33. * qdf_talloc() - t(ree) alloc(ate) memory
  34. * @parent: the parent memory of the new allocation
  35. * @size: requested size of the newly allocated memory
  36. *
  37. * Return: pointer to the newly allocated memory
  38. */
  39. #define qdf_talloc(parent, size) \
  40. qdf_talloc_fl(parent, size, __func__, __LINE__)
  41. /**
  42. * qdf_talloc_type() - t(ree) alloc(ate) memory for a type
  43. * @parent: the parent memory of the new allocation
  44. * @cursor: pointer to the type of memory to allocate
  45. *
  46. * This API automatically determines the correct size needed or an allocation
  47. * based on the type of @cursor. If you need to allocate an arbitrary number
  48. * of bytes, use qdf_talloc() instead.
  49. *
  50. * Return: pointer to the newly allocated memory
  51. */
  52. #define qdf_talloc_type(parent, cursor) \
  53. qdf_talloc(parent, sizeof(*(cursor)))
  54. /**
  55. * qdf_talloc_fl() - t(ree) alloc(ate) memory with function and line info
  56. * @parent: the parent memory of the new allocation
  57. * @size: requested size of the newly allocated memory
  58. * @func: name of the function requesting the allocation
  59. * @line: line number of the call site in @func
  60. *
  61. * Return: pointer to the newly allocated memory
  62. */
  63. #define qdf_talloc_fl(parent, size, func, line) \
  64. __qdf_talloc_fl(parent, size, func, line)
  65. /**
  66. * qdf_tfree() - free memory allocated using the *_talloc() function family
  67. * @ptr: double point to memory to free, set to NULL
  68. *
  69. * Return: None
  70. */
  71. #define qdf_tfree(ptr) \
  72. qdf_tfree_fl(ptr, __func__, __LINE__)
  73. /**
  74. * qdf_tfree_fl() - free memory allocated using the *_talloc() function family
  75. * with function and line info
  76. * @ptr: pointer to memory to free
  77. * @func: name of the function requesting the free
  78. * @line: line number of the call site in @func
  79. *
  80. * Return: None
  81. */
  82. #define qdf_tfree_fl(ptr, func, line) \
  83. do { \
  84. __qdf_tfree_fl(ptr, func, line); \
  85. ptr = (void *)1; \
  86. } while (false)
  87. /**
  88. * qdf_talloc_assert_no_children() - assert @parent has not child allocations
  89. * @parent: the parent memory pointer to check
  90. *
  91. * Return: None
  92. */
  93. #define qdf_talloc_assert_no_children(parent) \
  94. qdf_talloc_assert_no_children_fl(parent, __func__, __LINE__)
  95. #ifdef WLAN_TALLOC_DEBUG
  96. /**
  97. * qdf_talloc_feature_init() - initialize the QDF talloc feature
  98. *
  99. * Must be called before allocating memory via a qdf_talloc API.
  100. *
  101. * Return: None
  102. */
  103. QDF_STATUS qdf_talloc_feature_init(void);
  104. /**
  105. * qdf_talloc_feature_deinit() - deinitialize the QDF talloc feature
  106. *
  107. * Memory must not be allocated via a qdf_talloc API after this is called. This
  108. * API asserts that the parent/child relationship table is empty in order to
  109. * catch memory leaks.
  110. *
  111. * Return: None
  112. */
  113. void qdf_talloc_feature_deinit(void);
  114. void *__qdf_talloc_fl(const void *parent, const size_t size,
  115. const char *func, const uint16_t line);
  116. void __qdf_tfree_fl(void *ptr, const char *func, const uint16_t line);
  117. /**
  118. * qdf_talloc_assert_no_children_fl() - assert @parent has not child allocations
  119. * @parent: the parent memory pointer to check
  120. * @func: name of the function requesting the assert
  121. * @line: line number of the call site in @func
  122. *
  123. * Return: None
  124. */
  125. void qdf_talloc_assert_no_children_fl(const void *parent,
  126. const char *func, const uint16_t line);
  127. #else /* WLAN_TALLOC_DEBUG */
  128. static inline QDF_STATUS qdf_talloc_feature_init(void)
  129. {
  130. return QDF_STATUS_SUCCESS;
  131. }
  132. static inline void qdf_talloc_feature_deinit(void) { }
  133. static inline void *__qdf_talloc_fl(const void *parent, const size_t size,
  134. const char *func, const uint16_t line)
  135. {
  136. return __zalloc_auto(size);
  137. }
  138. static inline void
  139. __qdf_tfree_fl(void *ptr, const char *func, const uint16_t line)
  140. {
  141. __k_free(ptr);
  142. }
  143. static inline void
  144. qdf_talloc_assert_no_children_fl(const void *parent,
  145. const char *func, const uint16_t line) { }
  146. #endif /* WLAN_TALLOC_DEBUG */
  147. #endif /* __QDF_TALLOC_H */