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.

160 lines
3.1 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. /*
  2. ISC License
  3. Copyright (c) 2022, 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. #include "fuse_msgbuf.hpp"
  16. #include "fuse.h"
  17. #include <unistd.h>
  18. #include <cstdint>
  19. #include <cstdlib>
  20. #include <mutex>
  21. #include <vector>
  22. #include <unordered_set>
  23. #include <atomic>
  24. static std::uint32_t g_PAGESIZE = 0;
  25. static std::uint32_t g_BUFSIZE = 0;
  26. static std::uint32_t g_MAX_ALLOCS = 128;
  27. static std::mutex g_MUTEX;
  28. static std::vector<fuse_msgbuf_t*> g_MSGBUF_STACK;
  29. static std::unordered_set<fuse_msgbuf_t*> g_MSGBUF_ALLOCED;
  30. static
  31. __attribute__((constructor))
  32. void
  33. msgbuf_constructor()
  34. {
  35. g_PAGESIZE = sysconf(_SC_PAGESIZE);
  36. // +2 because to do O_DIRECT we need to offset the buffer to align
  37. g_BUFSIZE = (g_PAGESIZE * (FUSE_MAX_MAX_PAGES + 2));
  38. }
  39. static
  40. __attribute__((destructor))
  41. void
  42. msgbuf_destroy()
  43. {
  44. }
  45. uint32_t
  46. msgbuf_get_bufsize()
  47. {
  48. return g_BUFSIZE;
  49. }
  50. void
  51. msgbuf_set_bufsize(const uint32_t size_in_pages_)
  52. {
  53. g_BUFSIZE = (size_in_pages_ * g_PAGESIZE);
  54. }
  55. static
  56. void*
  57. page_aligned_malloc(const uint64_t size_)
  58. {
  59. int rv;
  60. void *buf = NULL;
  61. rv = posix_memalign(&buf,g_PAGESIZE,size_);
  62. if(rv != 0)
  63. return NULL;
  64. return buf;
  65. }
  66. fuse_msgbuf_t*
  67. msgbuf_alloc()
  68. {
  69. fuse_msgbuf_t *msgbuf;
  70. std::lock_guard<std::mutex> lck(g_MUTEX);
  71. if(g_MSGBUF_STACK.empty())
  72. {
  73. msgbuf = (fuse_msgbuf_t*)malloc(sizeof(fuse_msgbuf_t));
  74. if(msgbuf == NULL)
  75. return NULL;
  76. msgbuf->mem = (char*)page_aligned_malloc(g_BUFSIZE);
  77. if(msgbuf->mem == NULL)
  78. {
  79. free(msgbuf);
  80. return NULL;
  81. }
  82. msgbuf->size = g_BUFSIZE;
  83. g_MSGBUF_ALLOCED.emplace(msgbuf);
  84. }
  85. else
  86. {
  87. msgbuf = g_MSGBUF_STACK.back();
  88. g_MSGBUF_STACK.pop_back();
  89. }
  90. return msgbuf;
  91. }
  92. void
  93. msgbuf_free(fuse_msgbuf_t *msgbuf_)
  94. {
  95. std::lock_guard<std::mutex> lck(g_MUTEX);
  96. if(msgbuf_->size != g_BUFSIZE)
  97. {
  98. g_MSGBUF_ALLOCED.erase(msgbuf_);
  99. free(msgbuf_->mem);
  100. free(msgbuf_);
  101. return;
  102. }
  103. g_MSGBUF_STACK.emplace_back(msgbuf_);
  104. }
  105. uint64_t
  106. msgbuf_alloc_count()
  107. {
  108. return g_MSGBUF_ALLOCED.size();
  109. }
  110. uint64_t
  111. msgbuf_avail_count()
  112. {
  113. return g_MSGBUF_STACK.size();
  114. }
  115. void
  116. msgbuf_gc()
  117. {
  118. std::vector<fuse_msgbuf_t*> oldstack;
  119. {
  120. std::lock_guard<std::mutex> lck(g_MUTEX);
  121. oldstack.swap(g_MSGBUF_STACK);
  122. }
  123. fprintf(stderr,"freeing %lu msgbufs\n",oldstack.size());
  124. for(auto msgbuf: oldstack)
  125. {
  126. g_MSGBUF_ALLOCED.erase(msgbuf);
  127. free(msgbuf->mem);
  128. free(msgbuf);
  129. }
  130. }