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.

124 lines
2.4 KiB

  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 <stack>
  22. static std::uint32_t g_PAGESIZE = 0;
  23. static std::uint32_t g_BUFSIZE = 0;
  24. static std::mutex g_MUTEX;
  25. static std::stack<fuse_msgbuf_t*> g_MSGBUF_STACK;
  26. static
  27. __attribute__((constructor))
  28. void
  29. msgbuf_constructor()
  30. {
  31. g_PAGESIZE = sysconf(_SC_PAGESIZE);
  32. g_BUFSIZE = (g_PAGESIZE * (FUSE_MAX_MAX_PAGES + 2));
  33. }
  34. static
  35. __attribute__((destructor))
  36. void
  37. msgbuf_destroy()
  38. {
  39. // TODO: cleanup?
  40. }
  41. uint32_t
  42. msgbuf_get_bufsize()
  43. {
  44. return g_BUFSIZE;
  45. }
  46. void
  47. msgbuf_set_bufsize(const uint32_t size_in_pages_)
  48. {
  49. g_BUFSIZE = (size_in_pages_ * g_PAGESIZE);
  50. }
  51. static
  52. void*
  53. page_aligned_malloc(const uint64_t size_)
  54. {
  55. int rv;
  56. void *buf = NULL;
  57. rv = posix_memalign(&buf,g_PAGESIZE,size_);
  58. if(rv != 0)
  59. return NULL;
  60. return buf;
  61. }
  62. fuse_msgbuf_t*
  63. msgbuf_alloc()
  64. {
  65. fuse_msgbuf_t *msgbuf;
  66. g_MUTEX.lock();
  67. if(g_MSGBUF_STACK.empty())
  68. {
  69. g_MUTEX.unlock();
  70. msgbuf = (fuse_msgbuf_t*)malloc(sizeof(fuse_msgbuf_t));
  71. if(msgbuf == NULL)
  72. return NULL;
  73. msgbuf->mem = (char*)page_aligned_malloc(g_BUFSIZE);
  74. if(msgbuf->mem == NULL)
  75. {
  76. free(msgbuf);
  77. return NULL;
  78. }
  79. msgbuf->size = g_BUFSIZE;
  80. }
  81. else
  82. {
  83. msgbuf = g_MSGBUF_STACK.top();
  84. g_MSGBUF_STACK.pop();
  85. g_MUTEX.unlock();
  86. }
  87. return msgbuf;
  88. }
  89. void
  90. msgbuf_free(fuse_msgbuf_t *msgbuf_)
  91. {
  92. std::lock_guard<std::mutex> lck(g_MUTEX);
  93. if(msgbuf_->size != g_BUFSIZE)
  94. {
  95. free(msgbuf_->mem);
  96. free(msgbuf_);
  97. return;
  98. }
  99. g_MSGBUF_STACK.push(msgbuf_);
  100. }