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.

227 lines
3.8 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. uint64_t
  37. lfmp_slab_count(lfmp_t *lfmp_)
  38. {
  39. uint64_t rv;
  40. pthread_mutex_lock(&lfmp_->lock);
  41. rv = fmp_slab_count(&lfmp_->fmp);
  42. pthread_mutex_unlock(&lfmp_->lock);
  43. return rv;
  44. }
  45. static
  46. inline
  47. int
  48. lfmp_slab_alloc(lfmp_t *lfmp_)
  49. {
  50. int rv;
  51. pthread_mutex_lock(&lfmp_->lock);
  52. rv = fmp_slab_alloc(&lfmp_->fmp);
  53. pthread_mutex_unlock(&lfmp_->lock);
  54. return rv;
  55. }
  56. static
  57. inline
  58. void*
  59. lfmp_alloc(lfmp_t *lfmp_)
  60. {
  61. void *rv;
  62. pthread_mutex_lock(&lfmp_->lock);
  63. rv = fmp_alloc(&lfmp_->fmp);
  64. pthread_mutex_unlock(&lfmp_->lock);
  65. return rv;
  66. }
  67. static
  68. inline
  69. void*
  70. lfmp_calloc(lfmp_t *lfmp_)
  71. {
  72. void *rv;
  73. pthread_mutex_lock(&lfmp_->lock);
  74. rv = fmp_calloc(&lfmp_->fmp);
  75. pthread_mutex_unlock(&lfmp_->lock);
  76. return rv;
  77. }
  78. static
  79. inline
  80. void
  81. lfmp_free(lfmp_t *lfmp_,
  82. void *obj_)
  83. {
  84. pthread_mutex_lock(&lfmp_->lock);
  85. fmp_free(&lfmp_->fmp,obj_);
  86. pthread_mutex_unlock(&lfmp_->lock);
  87. }
  88. static
  89. inline
  90. void
  91. lfmp_clear(lfmp_t *lfmp_)
  92. {
  93. pthread_mutex_lock(&lfmp_->lock);
  94. fmp_clear(&lfmp_->fmp);
  95. pthread_mutex_unlock(&lfmp_->lock);
  96. }
  97. static
  98. inline
  99. void
  100. lfmp_destroy(lfmp_t *lfmp_)
  101. {
  102. pthread_mutex_lock(&lfmp_->lock);
  103. fmp_destroy(&lfmp_->fmp);
  104. pthread_mutex_unlock(&lfmp_->lock);
  105. pthread_mutex_destroy(&lfmp_->lock);
  106. }
  107. static
  108. inline
  109. uint64_t
  110. lfmp_avail_objs(lfmp_t *lfmp_)
  111. {
  112. uint64_t rv;
  113. pthread_mutex_lock(&lfmp_->lock);
  114. rv = fmp_avail_objs(&lfmp_->fmp);
  115. pthread_mutex_unlock(&lfmp_->lock);
  116. return rv;
  117. }
  118. static
  119. inline
  120. uint64_t
  121. lfmp_objs_in_slab(lfmp_t *lfmp_,
  122. void *slab_)
  123. {
  124. uint64_t rv;
  125. pthread_mutex_lock(&lfmp_->lock);
  126. rv = fmp_objs_in_slab(&lfmp_->fmp,slab_);
  127. pthread_mutex_unlock(&lfmp_->lock);
  128. return rv;
  129. }
  130. static
  131. inline
  132. void
  133. lfmp_remove_objs_in_slab(lfmp_t *lfmp_,
  134. void *slab_)
  135. {
  136. pthread_mutex_lock(&lfmp_->lock);
  137. fmp_remove_objs_in_slab(&lfmp_->fmp,slab_);
  138. pthread_mutex_unlock(&lfmp_->lock);
  139. }
  140. static
  141. inline
  142. int
  143. lfmp_gc(lfmp_t *lfmp_)
  144. {
  145. int rv;
  146. pthread_mutex_lock(&lfmp_->lock);
  147. rv = fmp_gc(&lfmp_->fmp);
  148. pthread_mutex_unlock(&lfmp_->lock);
  149. return rv;
  150. }
  151. static
  152. inline
  153. uint64_t
  154. lfmp_objs_per_slab(lfmp_t *lfmp_)
  155. {
  156. uint64_t rv;
  157. pthread_mutex_lock(&lfmp_->lock);
  158. rv = fmp_objs_per_slab(&lfmp_->fmp);
  159. pthread_mutex_unlock(&lfmp_->lock);
  160. return rv;
  161. }
  162. static
  163. inline
  164. double
  165. lfmp_slab_usage_ratio(lfmp_t *lfmp_)
  166. {
  167. double rv;
  168. pthread_mutex_lock(&lfmp_->lock);
  169. rv = fmp_slab_usage_ratio(&lfmp_->fmp);
  170. pthread_mutex_unlock(&lfmp_->lock);
  171. return rv;
  172. }
  173. static
  174. inline
  175. uint64_t
  176. lfmp_total_allocated_memory(lfmp_t *lfmp_)
  177. {
  178. uint64_t rv;
  179. pthread_mutex_lock(&lfmp_->lock);
  180. rv = fmp_total_allocated_memory(&lfmp_->fmp);
  181. pthread_mutex_unlock(&lfmp_->lock);
  182. return rv;
  183. }