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.

123 lines
2.5 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.h"
  16. #include <assert.h>
  17. #include <fcntl.h>
  18. #include <unistd.h>
  19. #include <mutex>
  20. #include <stack>
  21. static std::size_t g_BUFSIZE = (1024 * 1024 * 2);
  22. static std::mutex g_MUTEX;
  23. static std::stack<fuse_msgbuf_t*> g_MSGBUF_STACK;
  24. static
  25. __attribute__((destructor))
  26. void
  27. msgbuf_destroy()
  28. {
  29. // TODO: cleanup?
  30. }
  31. std::size_t
  32. msgbuf_bufsize()
  33. {
  34. return g_BUFSIZE;
  35. }
  36. void
  37. msgbuf_bufsize(const std::size_t size_)
  38. {
  39. g_BUFSIZE = size_;
  40. }
  41. fuse_msgbuf_t*
  42. msgbuf_alloc()
  43. {
  44. int rv;
  45. fuse_msgbuf_t *msgbuf;
  46. g_MUTEX.lock();
  47. if(g_MSGBUF_STACK.empty())
  48. {
  49. g_MUTEX.unlock();
  50. msgbuf = (fuse_msgbuf_t*)malloc(sizeof(fuse_msgbuf_t));
  51. if(msgbuf == NULL)
  52. return NULL;
  53. rv = pipe(msgbuf->pipefd);
  54. assert(rv == 0);
  55. rv = fcntl(msgbuf->pipefd[0],F_SETPIPE_SZ,g_BUFSIZE);
  56. assert(rv > 0);
  57. msgbuf->mem = (char*)malloc(rv);
  58. msgbuf->size = rv;
  59. msgbuf->used = 0;
  60. }
  61. else
  62. {
  63. msgbuf = g_MSGBUF_STACK.top();
  64. g_MSGBUF_STACK.pop();
  65. g_MUTEX.unlock();
  66. }
  67. return msgbuf;
  68. }
  69. fuse_msgbuf_t*
  70. msgbuf_alloc_memonly()
  71. {
  72. fuse_msgbuf_t *msgbuf;
  73. g_MUTEX.lock();
  74. if(g_MSGBUF_STACK.empty())
  75. {
  76. g_MUTEX.unlock();
  77. msgbuf = (fuse_msgbuf_t*)malloc(sizeof(fuse_msgbuf_t));
  78. if(msgbuf == NULL)
  79. return NULL;
  80. msgbuf->pipefd[0] = -1;
  81. msgbuf->pipefd[1] = -1;
  82. msgbuf->mem = (char*)malloc(g_BUFSIZE);
  83. msgbuf->size = g_BUFSIZE;
  84. msgbuf->used = 0;
  85. }
  86. else
  87. {
  88. msgbuf = g_MSGBUF_STACK.top();
  89. g_MSGBUF_STACK.pop();
  90. g_MUTEX.unlock();
  91. }
  92. return msgbuf;
  93. }
  94. void
  95. msgbuf_free(fuse_msgbuf_t *msgbuf_)
  96. {
  97. std::lock_guard<std::mutex> lck(g_MUTEX);
  98. g_MSGBUF_STACK.push(msgbuf_);
  99. }