Audio plugin host https://kx.studio/carla
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.

271 lines
6.6KB

  1. /*
  2. * Carla shared memory utils
  3. * Copyright (C) 2013-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_SHM_UTILS_HPP_INCLUDED
  18. #define CARLA_SHM_UTILS_HPP_INCLUDED
  19. #include "CarlaUtils.hpp"
  20. #ifdef CARLA_OS_WIN
  21. struct shm_t { HANDLE shm; HANDLE map; };
  22. # define shm_t_INIT {nullptr, nullptr}
  23. #else
  24. # include <fcntl.h>
  25. # include <sys/mman.h>
  26. struct shm_t { int fd; const char* filename; };
  27. # define shm_t_INIT {-1, nullptr}
  28. #endif
  29. // -----------------------------------------------------------------------
  30. // shared memory calls
  31. /*
  32. * Null object returned when a shared memory operation fails.
  33. */
  34. #ifdef CARLA_OS_WIN
  35. static const shm_t gNullCarlaShm = { nullptr, nullptr };
  36. #else
  37. static const shm_t gNullCarlaShm = { -1, nullptr };
  38. #endif
  39. /*
  40. * Check if a shared memory object is valid.
  41. */
  42. static inline
  43. bool carla_is_shm_valid(const shm_t& shm) noexcept
  44. {
  45. #ifdef CARLA_OS_WIN
  46. return (shm.shm != nullptr && shm.shm != INVALID_HANDLE_VALUE);
  47. #else
  48. return (shm.fd >= 0);
  49. #endif
  50. }
  51. /*
  52. * Initialize a shared memory object to an invalid state.
  53. */
  54. static inline
  55. void carla_shm_init(shm_t& shm) noexcept
  56. {
  57. #ifdef CARLA_OS_WIN
  58. shm.shm = nullptr;
  59. shm.map = nullptr;
  60. #else
  61. shm.fd = -1;
  62. shm.filename = nullptr;
  63. #endif
  64. }
  65. /*
  66. * Create and open a new shared memory object.
  67. * Returns an invalid object if the operation failed or the filename already exists.
  68. */
  69. static inline
  70. shm_t carla_shm_create(const char* const filename) noexcept
  71. {
  72. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', gNullCarlaShm);
  73. shm_t ret;
  74. try {
  75. #ifdef CARLA_OS_WIN
  76. ret.shm = nullptr; // TODO
  77. ret.map = nullptr;
  78. #else
  79. ret.fd = ::shm_open(filename, O_CREAT|O_EXCL|O_RDWR, 0600);
  80. ret.filename = (ret.fd >= 0) ? carla_strdup_safe(filename) : nullptr;
  81. if (ret.fd >= 0 && ret.filename == nullptr)
  82. {
  83. ::close(ret.fd);
  84. ::shm_unlink(filename);
  85. ret.fd = -1;
  86. }
  87. #endif
  88. }
  89. catch(...) {
  90. carla_safe_exception("carla_shm_create", __FILE__, __LINE__);
  91. ret = gNullCarlaShm;
  92. }
  93. return ret;
  94. }
  95. /*
  96. * Attach to an existing shared memory object.
  97. */
  98. static inline
  99. shm_t carla_shm_attach(const char* const filename) noexcept
  100. {
  101. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', gNullCarlaShm);
  102. shm_t ret;
  103. try {
  104. #ifdef CARLA_OS_WIN
  105. ret.shm = ::CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  106. ret.map = nullptr;
  107. #else
  108. ret.fd = ::shm_open(filename, O_RDWR, 0);
  109. ret.filename = nullptr;
  110. #endif
  111. }
  112. catch(...) {
  113. ret = gNullCarlaShm;
  114. }
  115. return ret;
  116. }
  117. /*
  118. * Close a shared memory object and invalidate it.
  119. */
  120. static inline
  121. void carla_shm_close(shm_t& shm) noexcept
  122. {
  123. CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm),);
  124. #ifdef CARLA_OS_WIN
  125. CARLA_SAFE_ASSERT(shm.map == nullptr);
  126. #endif
  127. try {
  128. #ifdef CARLA_OS_WIN
  129. ::CloseHandle(shm.shm);
  130. #else
  131. ::close(shm.fd);
  132. if (shm.filename != nullptr)
  133. {
  134. ::shm_unlink(shm.filename);
  135. delete[] shm.filename;
  136. }
  137. #endif
  138. } CARLA_SAFE_EXCEPTION("carla_shm_close");
  139. shm = gNullCarlaShm;
  140. }
  141. /*
  142. * Map a shared memory object to @a size bytes and return the memory address.
  143. * @note One shared memory object can only have one mapping at a time.
  144. */
  145. static inline
  146. void* carla_shm_map(shm_t& shm, const std::size_t size) noexcept
  147. {
  148. CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm), nullptr);
  149. CARLA_SAFE_ASSERT_RETURN(size > 0, nullptr);
  150. #ifdef CARLA_OS_WIN
  151. CARLA_SAFE_ASSERT_RETURN(shm.map == nullptr, nullptr);
  152. #endif
  153. try {
  154. #ifdef CARLA_OS_WIN
  155. const HANDLE map = ::CreateFileMapping(shm.shm, NULL, PAGE_READWRITE, size, size, NULL);
  156. CARLA_SAFE_ASSERT_RETURN(map != nullptr, nullptr);
  157. HANDLE ptr = ::MapViewOfFile(map, FILE_MAP_COPY, 0, 0, size);
  158. if (ptr == nullptr)
  159. {
  160. carla_safe_assert("ptr != nullptr", __FILE__, __LINE__);
  161. ::CloseHandle(map);
  162. return nullptr;
  163. }
  164. shm.map = map;
  165. return ptr;
  166. #else
  167. if (shm.filename != nullptr)
  168. {
  169. const int ret = ::ftruncate(shm.fd, static_cast<off_t>(size));
  170. CARLA_SAFE_ASSERT_RETURN(ret == 0, nullptr);
  171. }
  172. return ::mmap(nullptr, size, PROT_READ|PROT_WRITE, MAP_SHARED, shm.fd, 0);
  173. #endif
  174. } CARLA_SAFE_EXCEPTION_RETURN("carla_shm_map", nullptr);
  175. }
  176. /*
  177. * Unmap a shared memory object address.
  178. */
  179. static inline
  180. void carla_shm_unmap(shm_t& shm, void* const ptr, const std::size_t size) noexcept
  181. {
  182. CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm),);
  183. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr,);
  184. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  185. #ifdef CARLA_OS_WIN
  186. CARLA_SAFE_ASSERT_RETURN(shm.map != nullptr,);
  187. #endif
  188. try {
  189. #ifdef CARLA_OS_WIN
  190. const HANDLE map = shm.map;
  191. shm.map = nullptr;
  192. ::UnmapViewOfFile(ptr);
  193. ::CloseHandle(map);
  194. #else
  195. const int ret = ::munmap(ptr, size);
  196. CARLA_SAFE_ASSERT(ret == 0);
  197. #endif
  198. } CARLA_SAFE_EXCEPTION("carla_shm_unmap");
  199. // unused depending on platform
  200. return; (void)shm; (void)size;
  201. }
  202. // -----------------------------------------------------------------------
  203. // shared memory, templated calls
  204. /*
  205. * Map a shared memory object, handling object type and size.
  206. */
  207. template<typename T>
  208. static inline
  209. T* carla_shm_map(shm_t& shm) noexcept
  210. {
  211. return (T*)carla_shm_map(shm, sizeof(T));
  212. }
  213. /*
  214. * Map a shared memory object and return if it's non-null.
  215. */
  216. template<typename T>
  217. static inline
  218. bool carla_shm_map(shm_t& shm, T*& value) noexcept
  219. {
  220. value = (T*)carla_shm_map(shm, sizeof(T));
  221. return (value != nullptr);
  222. }
  223. /*
  224. * Unmap a shared memory object address and set it as null.
  225. */
  226. template<typename T>
  227. static inline
  228. void carla_shm_unmap(shm_t& shm, T*& value) noexcept
  229. {
  230. carla_shm_unmap(shm, value, sizeof(T));
  231. value = nullptr;
  232. }
  233. // -----------------------------------------------------------------------
  234. #endif // CARLA_SHM_UTILS_HPP_INCLUDED