You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
4.0 KiB

  1. /*
  2. ISC License
  3. Copyright (c) 2020, Antonio SJ Musumeci <trapexit@spawn.link>
  4. Permission to use, copy, modify, and/or distribute this software for any
  5. purpose with or without fee is hereby granted, provided that the above
  6. copyright notice and this permission notice appear in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. #pragma once
  16. #include "fmp.h"
  17. #include <pthread.h>
  18. typedef struct lfmp_t lfmp_t;
  19. struct lfmp_t
  20. {
  21. fmp_t fmp;
  22. pthread_mutex_t lock;
  23. };
  24. static
  25. inline
  26. void
  27. lfmp_init(lfmp_t *lfmp_,
  28. const uint64_t obj_size_,
  29. const uint64_t page_multiple_)
  30. {
  31. fmp_init(&lfmp_->fmp,obj_size_,page_multiple_);
  32. pthread_mutex_init(&lfmp_->lock,NULL);
  33. }
  34. static
  35. inline
  36. void
  37. lfmp_lock(lfmp_t *lfmp_)
  38. {
  39. pthread_mutex_lock(&lfmp_->lock);
  40. }
  41. static
  42. inline
  43. void
  44. lfmp_unlock(lfmp_t *lfmp_)
  45. {
  46. pthread_mutex_unlock(&lfmp_->lock);
  47. }
  48. static
  49. inline
  50. uint64_t
  51. lfmp_slab_count(lfmp_t *lfmp_)
  52. {
  53. uint64_t rv;
  54. pthread_mutex_lock(&lfmp_->lock);
  55. rv = fmp_slab_count(&lfmp_->fmp);
  56. pthread_mutex_unlock(&lfmp_->lock);
  57. return rv;
  58. }
  59. static
  60. inline
  61. int
  62. lfmp_slab_alloc(lfmp_t *lfmp_)
  63. {
  64. int rv;
  65. pthread_mutex_lock(&lfmp_->lock);
  66. rv = fmp_slab_alloc(&lfmp_->fmp);
  67. pthread_mutex_unlock(&lfmp_->lock);
  68. return rv;
  69. }
  70. static
  71. inline
  72. void*
  73. lfmp_alloc(lfmp_t *lfmp_)
  74. {
  75. void *rv;
  76. pthread_mutex_lock(&lfmp_->lock);
  77. rv = fmp_alloc(&lfmp_->fmp);
  78. pthread_mutex_unlock(&lfmp_->lock);
  79. return rv;
  80. }
  81. static
  82. inline
  83. void*
  84. lfmp_calloc(lfmp_t *lfmp_)
  85. {
  86. void *rv;
  87. pthread_mutex_lock(&lfmp_->lock);
  88. rv = fmp_calloc(&lfmp_->fmp);
  89. pthread_mutex_unlock(&lfmp_->lock);
  90. return rv;
  91. }
  92. static
  93. inline
  94. void
  95. lfmp_free(lfmp_t *lfmp_,
  96. void *obj_)
  97. {
  98. pthread_mutex_lock(&lfmp_->lock);
  99. fmp_free(&lfmp_->fmp,obj_);
  100. pthread_mutex_unlock(&lfmp_->lock);
  101. }
  102. static
  103. inline
  104. void
  105. lfmp_clear(lfmp_t *lfmp_)
  106. {
  107. pthread_mutex_lock(&lfmp_->lock);
  108. fmp_clear(&lfmp_->fmp);
  109. pthread_mutex_unlock(&lfmp_->lock);
  110. }
  111. static
  112. inline
  113. void
  114. lfmp_destroy(lfmp_t *lfmp_)
  115. {
  116. pthread_mutex_lock(&lfmp_->lock);
  117. fmp_destroy(&lfmp_->fmp);
  118. pthread_mutex_unlock(&lfmp_->lock);
  119. pthread_mutex_destroy(&lfmp_->lock);
  120. }
  121. static
  122. inline
  123. uint64_t
  124. lfmp_avail_objs(lfmp_t *lfmp_)
  125. {
  126. uint64_t rv;
  127. pthread_mutex_lock(&lfmp_->lock);
  128. rv = fmp_avail_objs(&lfmp_->fmp);
  129. pthread_mutex_unlock(&lfmp_->lock);
  130. return rv;
  131. }
  132. static
  133. inline
  134. uint64_t
  135. lfmp_objs_in_slab(lfmp_t *lfmp_,
  136. void *slab_)
  137. {
  138. uint64_t rv;
  139. pthread_mutex_lock(&lfmp_->lock);
  140. rv = fmp_objs_in_slab(&lfmp_->fmp,slab_);
  141. pthread_mutex_unlock(&lfmp_->lock);
  142. return rv;
  143. }
  144. static
  145. inline
  146. void
  147. lfmp_remove_objs_in_slab(lfmp_t *lfmp_,
  148. void *slab_)
  149. {
  150. pthread_mutex_lock(&lfmp_->lock);
  151. fmp_remove_objs_in_slab(&lfmp_->fmp,slab_);
  152. pthread_mutex_unlock(&lfmp_->lock);
  153. }
  154. static
  155. inline
  156. int
  157. lfmp_gc(lfmp_t *lfmp_)
  158. {
  159. int rv;
  160. pthread_mutex_lock(&lfmp_->lock);
  161. rv = fmp_gc(&lfmp_->fmp);
  162. pthread_mutex_unlock(&lfmp_->lock);
  163. return rv;
  164. }
  165. static
  166. inline
  167. uint64_t
  168. lfmp_objs_per_slab(lfmp_t *lfmp_)
  169. {
  170. uint64_t rv;
  171. pthread_mutex_lock(&lfmp_->lock);
  172. rv = fmp_objs_per_slab(&lfmp_->fmp);
  173. pthread_mutex_unlock(&lfmp_->lock);
  174. return rv;
  175. }
  176. static
  177. inline
  178. double
  179. lfmp_slab_usage_ratio(lfmp_t *lfmp_)
  180. {
  181. double rv;
  182. pthread_mutex_lock(&lfmp_->lock);
  183. rv = fmp_slab_usage_ratio(&lfmp_->fmp);
  184. pthread_mutex_unlock(&lfmp_->lock);
  185. return rv;
  186. }
  187. static
  188. inline
  189. uint64_t
  190. lfmp_total_allocated_memory(lfmp_t *lfmp_)
  191. {
  192. uint64_t rv;
  193. pthread_mutex_lock(&lfmp_->lock);
  194. rv = fmp_total_allocated_memory(&lfmp_->fmp);
  195. pthread_mutex_unlock(&lfmp_->lock);
  196. return rv;
  197. }