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.

317 lines
8.2KB

  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 <cerrno>
  25. # include <fcntl.h>
  26. # include <sys/mman.h>
  27. struct shm_t { int fd; const char* filename; std::size_t size; };
  28. # define shm_t_INIT { -1, nullptr, 0 }
  29. #endif
  30. // -----------------------------------------------------------------------
  31. // shared memory calls
  32. /*
  33. * Null object returned when a shared memory operation fails.
  34. */
  35. static const shm_t gNullCarlaShm = shm_t_INIT;
  36. /*
  37. * Check if a shared memory object is valid.
  38. */
  39. static inline
  40. bool carla_is_shm_valid(const shm_t& shm) noexcept
  41. {
  42. #ifdef CARLA_OS_WIN
  43. return (shm.shm != nullptr && shm.shm != INVALID_HANDLE_VALUE);
  44. #else
  45. return (shm.fd >= 0);
  46. #endif
  47. }
  48. /*
  49. * Initialize a shared memory object to an invalid state.
  50. */
  51. static inline
  52. void carla_shm_init(shm_t& shm) noexcept
  53. {
  54. shm = gNullCarlaShm;
  55. }
  56. /*
  57. * Create and open a new shared memory object.
  58. * Returns an invalid object if the operation failed or the filename already exists.
  59. */
  60. static inline
  61. shm_t carla_shm_create(const char* const filename) noexcept
  62. {
  63. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', gNullCarlaShm);
  64. shm_t ret;
  65. try {
  66. #ifdef CARLA_OS_WIN
  67. ret.shm = nullptr; // TODO
  68. ret.map = nullptr;
  69. #else
  70. ret.fd = ::shm_open(filename, O_CREAT|O_EXCL|O_RDWR, 0600);
  71. ret.filename = (ret.fd >= 0) ? carla_strdup_safe(filename) : nullptr;
  72. ret.size = 0;
  73. if (ret.fd >= 0 && ret.filename == nullptr)
  74. {
  75. ::close(ret.fd);
  76. ::shm_unlink(filename);
  77. ret.fd = -1;
  78. }
  79. #endif
  80. }
  81. catch(...) {
  82. carla_safe_exception("carla_shm_create", __FILE__, __LINE__);
  83. ret = gNullCarlaShm;
  84. }
  85. return ret;
  86. }
  87. /*
  88. * Attach to an existing shared memory object.
  89. */
  90. static inline
  91. shm_t carla_shm_attach(const char* const filename) noexcept
  92. {
  93. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', gNullCarlaShm);
  94. shm_t ret;
  95. try {
  96. #ifdef CARLA_OS_WIN
  97. ret.shm = ::CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr);
  98. ret.map = nullptr;
  99. #else
  100. ret.fd = ::shm_open(filename, O_RDWR, 0);
  101. ret.filename = nullptr;
  102. ret.size = 0;
  103. #endif
  104. }
  105. catch(...) {
  106. carla_safe_exception("carla_shm_attach", __FILE__, __LINE__);
  107. ret = gNullCarlaShm;
  108. }
  109. return ret;
  110. }
  111. /*
  112. * Close a shared memory object and invalidate it.
  113. */
  114. static inline
  115. void carla_shm_close(shm_t& shm) noexcept
  116. {
  117. CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm),);
  118. #ifdef CARLA_OS_WIN
  119. CARLA_SAFE_ASSERT(shm.map == nullptr);
  120. #endif
  121. try {
  122. #ifdef CARLA_OS_WIN
  123. ::CloseHandle(shm.shm);
  124. #else
  125. ::close(shm.fd);
  126. if (shm.filename != nullptr)
  127. {
  128. ::shm_unlink(shm.filename);
  129. delete[] shm.filename;
  130. }
  131. #endif
  132. } CARLA_SAFE_EXCEPTION("carla_shm_close");
  133. shm = gNullCarlaShm;
  134. }
  135. /*
  136. * Map a shared memory object to @a size bytes and return the memory address.
  137. * @note One shared memory object can only have one mapping at a time.
  138. */
  139. static inline
  140. void* carla_shm_map(shm_t& shm, const std::size_t size) noexcept
  141. {
  142. CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm), nullptr);
  143. CARLA_SAFE_ASSERT_RETURN(size > 0, nullptr);
  144. #ifdef CARLA_OS_WIN
  145. CARLA_SAFE_ASSERT_RETURN(shm.map == nullptr, nullptr);
  146. #else
  147. CARLA_SAFE_ASSERT_RETURN(shm.size == 0, nullptr);
  148. #endif
  149. try {
  150. #ifdef CARLA_OS_WIN
  151. const HANDLE map(::CreateFileMapping(shm.shm, nullptr, PAGE_READWRITE, size, size, nullptr));
  152. CARLA_SAFE_ASSERT_RETURN(map != nullptr, nullptr);
  153. void* const ptr(::MapViewOfFile(map, FILE_MAP_COPY, 0, 0, size));
  154. if (ptr == nullptr)
  155. {
  156. carla_safe_assert("ptr != nullptr", __FILE__, __LINE__);
  157. ::CloseHandle(map);
  158. return nullptr;
  159. }
  160. shm.map = map;
  161. return ptr;
  162. #else
  163. if (shm.filename != nullptr)
  164. {
  165. const int ret(::ftruncate(shm.fd, static_cast<off_t>(size)));
  166. CARLA_SAFE_ASSERT_RETURN(ret == 0, nullptr);
  167. }
  168. void* const ptr(::mmap(nullptr, size, PROT_READ|PROT_WRITE, MAP_SHARED, shm.fd, 0));
  169. if (ptr == nullptr)
  170. {
  171. carla_safe_assert("ptr != nullptr", __FILE__, __LINE__);
  172. return nullptr;
  173. }
  174. shm.size = size;
  175. return ptr;
  176. #endif
  177. } CARLA_SAFE_EXCEPTION_RETURN("carla_shm_map", nullptr);
  178. }
  179. /*
  180. * Unmap a shared memory object address.
  181. */
  182. static inline
  183. void carla_shm_unmap(shm_t& shm, void* const ptr) noexcept
  184. {
  185. CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm),);
  186. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr,);
  187. #ifdef CARLA_OS_WIN
  188. CARLA_SAFE_ASSERT_RETURN(shm.map != nullptr,);
  189. #else
  190. CARLA_SAFE_ASSERT_RETURN(shm.size > 0,);
  191. #endif
  192. try {
  193. #ifdef CARLA_OS_WIN
  194. const HANDLE map(shm.map);
  195. shm.map = nullptr;
  196. ::UnmapViewOfFile(ptr);
  197. ::CloseHandle(map);
  198. #else
  199. const std::size_t size(shm.size);
  200. shm.size = 0;
  201. const int ret(::munmap(ptr, size));
  202. CARLA_SAFE_ASSERT(ret == 0);
  203. #endif
  204. } CARLA_SAFE_EXCEPTION("carla_shm_unmap");
  205. }
  206. // -----------------------------------------------------------------------
  207. // advanced calls
  208. /*
  209. * Create and open a new shared memory object for a XXXXXX temp filename.
  210. * Will keep trying until a free random filename is obtained.
  211. */
  212. static inline
  213. shm_t carla_shm_create_temp(char* const fileBase) noexcept
  214. {
  215. // check if the fileBase name is valid
  216. CARLA_SAFE_ASSERT_RETURN(fileBase != nullptr, gNullCarlaShm);
  217. const std::size_t fileBaseLen(std::strlen(fileBase));
  218. CARLA_SAFE_ASSERT_RETURN(fileBaseLen > 6, gNullCarlaShm);
  219. CARLA_SAFE_ASSERT_RETURN(std::strcmp(fileBase + (fileBaseLen - 6), "XXXXXX") == 0, gNullCarlaShm);
  220. // character set to use randomly
  221. static const char charSet[] = "abcdefghijklmnopqrstuvwxyz"
  222. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  223. "0123456789";
  224. static const int charSetLen = static_cast<int>(std::strlen(charSet) - 1); // -1 to avoid trailing '\0'
  225. // try until getting a valid shm is obtained or an error occurs
  226. for (;;)
  227. {
  228. // fill the XXXXXX characters randomly
  229. for (std::size_t c = fileBaseLen - 6; c < fileBaseLen; ++c)
  230. fileBase[c] = charSet[std::rand() % charSetLen];
  231. // (try to) create new shm for this filename
  232. const shm_t shm = carla_shm_create(fileBase);
  233. // all ok!
  234. if (carla_is_shm_valid(shm))
  235. return shm;
  236. // file already exists, keep trying
  237. #ifdef CARLA_OS_WIN
  238. // TODO
  239. #else
  240. if (errno == EEXIST)
  241. continue;
  242. #endif
  243. // some unknown error occurred, return null
  244. return gNullCarlaShm;
  245. }
  246. }
  247. // -----------------------------------------------------------------------
  248. // shared memory, templated calls
  249. /*
  250. * Map a shared memory object, handling object type and size.
  251. */
  252. template<typename T>
  253. static inline
  254. T* carla_shm_map(shm_t& shm) noexcept
  255. {
  256. return (T*)carla_shm_map(shm, sizeof(T));
  257. }
  258. /*
  259. * Map a shared memory object and return if it's non-null.
  260. */
  261. template<typename T>
  262. static inline
  263. bool carla_shm_map(shm_t& shm, T*& value) noexcept
  264. {
  265. value = (T*)carla_shm_map(shm, sizeof(T));
  266. return (value != nullptr);
  267. }
  268. // -----------------------------------------------------------------------
  269. #endif // CARLA_SHM_UTILS_HPP_INCLUDED