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.

319 lines
8.4KB

  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 map; bool isServer; const char* filename; };
  22. # define shm_t_INIT { INVALID_HANDLE_VALUE, true, 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.filename != nullptr);
  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. #ifdef CARLA_OS_WIN
  66. ret.map = INVALID_HANDLE_VALUE;
  67. ret.isServer = true;
  68. ret.filename = carla_strdup_safe(filename);
  69. #else
  70. try {
  71. ret.fd = ::shm_open(filename, O_CREAT|O_EXCL|O_RDWR, 0600);
  72. ret.filename = (ret.fd >= 0) ? carla_strdup_safe(filename) : nullptr;
  73. ret.size = 0;
  74. if (ret.fd >= 0 && ret.filename == nullptr)
  75. {
  76. ::close(ret.fd);
  77. ::shm_unlink(filename);
  78. ret.fd = -1;
  79. }
  80. } CARLA_SAFE_EXCEPTION_RETURN("carla_shm_create", gNullCarlaShm);
  81. #endif
  82. return ret;
  83. }
  84. /*
  85. * Attach to an existing shared memory object.
  86. */
  87. static inline
  88. shm_t carla_shm_attach(const char* const filename) noexcept
  89. {
  90. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', gNullCarlaShm);
  91. shm_t ret;
  92. #ifdef CARLA_OS_WIN
  93. ret.map = INVALID_HANDLE_VALUE;
  94. ret.isServer = false;
  95. ret.filename = carla_strdup_safe(filename);
  96. #else
  97. try {
  98. ret.fd = ::shm_open(filename, O_RDWR, 0);
  99. ret.filename = nullptr;
  100. ret.size = 0;
  101. } CARLA_SAFE_EXCEPTION_RETURN("carla_shm_attach", gNullCarlaShm);
  102. #endif
  103. return ret;
  104. }
  105. /*
  106. * Close a shared memory object and invalidate it.
  107. */
  108. static inline
  109. void carla_shm_close(shm_t& shm) noexcept
  110. {
  111. CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm),);
  112. #ifdef CARLA_OS_WIN
  113. CARLA_SAFE_ASSERT(shm.map == INVALID_HANDLE_VALUE);
  114. #endif
  115. #ifdef CARLA_OS_WIN
  116. if (shm.filename != nullptr)
  117. delete[] shm.filename;
  118. #else
  119. try {
  120. ::close(shm.fd);
  121. if (shm.filename != nullptr)
  122. {
  123. ::shm_unlink(shm.filename);
  124. delete[] shm.filename;
  125. }
  126. } CARLA_SAFE_EXCEPTION("carla_shm_close");
  127. #endif
  128. shm = gNullCarlaShm;
  129. }
  130. /*
  131. * Map a shared memory object to @a size bytes and return the memory address.
  132. * @note One shared memory object can only have one mapping at a time.
  133. */
  134. static inline
  135. void* carla_shm_map(shm_t& shm, const std::size_t size) noexcept
  136. {
  137. CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm), nullptr);
  138. CARLA_SAFE_ASSERT_RETURN(size > 0, nullptr);
  139. #ifdef CARLA_OS_WIN
  140. CARLA_SAFE_ASSERT_RETURN(shm.map == INVALID_HANDLE_VALUE, nullptr);
  141. #else
  142. CARLA_SAFE_ASSERT_RETURN(shm.size == 0, nullptr);
  143. #endif
  144. try {
  145. #ifdef CARLA_OS_WIN
  146. HANDLE map;
  147. if (shm.isServer)
  148. map = ::CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE|SEC_COMMIT, 0, size, shm.filename);
  149. else
  150. map = ::OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, shm.filename);
  151. CARLA_SAFE_ASSERT_RETURN(map != INVALID_HANDLE_VALUE, nullptr);
  152. void* const ptr(::MapViewOfFile(map, FILE_MAP_ALL_ACCESS, 0, 0, size));
  153. if (ptr == nullptr)
  154. {
  155. carla_safe_assert("ptr != nullptr", __FILE__, __LINE__);
  156. ::CloseHandle(map);
  157. return nullptr;
  158. }
  159. shm.map = map;
  160. return ptr;
  161. #else
  162. if (shm.filename != nullptr)
  163. {
  164. const int ret(::ftruncate(shm.fd, static_cast<off_t>(size)));
  165. CARLA_SAFE_ASSERT_RETURN(ret == 0, nullptr);
  166. }
  167. void* const ptr(::mmap(nullptr, size, PROT_READ|PROT_WRITE, MAP_SHARED, shm.fd, 0));
  168. if (ptr == nullptr)
  169. {
  170. carla_safe_assert("ptr != nullptr", __FILE__, __LINE__);
  171. return nullptr;
  172. }
  173. shm.size = size;
  174. return ptr;
  175. #endif
  176. } CARLA_SAFE_EXCEPTION_RETURN("carla_shm_map", nullptr);
  177. }
  178. /*
  179. * Unmap a shared memory object address.
  180. */
  181. static inline
  182. void carla_shm_unmap(shm_t& shm, void* const ptr) noexcept
  183. {
  184. CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm),);
  185. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr,);
  186. #ifdef CARLA_OS_WIN
  187. CARLA_SAFE_ASSERT_RETURN(shm.map != INVALID_HANDLE_VALUE,);
  188. #else
  189. CARLA_SAFE_ASSERT_RETURN(shm.size > 0,);
  190. #endif
  191. try {
  192. #ifdef CARLA_OS_WIN
  193. const HANDLE map(shm.map);
  194. shm.map = INVALID_HANDLE_VALUE;
  195. ::UnmapViewOfFile(ptr);
  196. ::CloseHandle(map);
  197. #else
  198. const std::size_t size(shm.size);
  199. shm.size = 0;
  200. const int ret(::munmap(ptr, size));
  201. CARLA_SAFE_ASSERT(ret == 0);
  202. #endif
  203. } CARLA_SAFE_EXCEPTION("carla_shm_unmap");
  204. }
  205. // -----------------------------------------------------------------------
  206. // advanced calls
  207. /*
  208. * Create and open a new shared memory object for a XXXXXX temp filename.
  209. * Will keep trying until a free random filename is obtained.
  210. */
  211. static inline
  212. shm_t carla_shm_create_temp(char* const fileBase) noexcept
  213. {
  214. // check if the fileBase name is valid
  215. CARLA_SAFE_ASSERT_RETURN(fileBase != nullptr, gNullCarlaShm);
  216. const std::size_t fileBaseLen(std::strlen(fileBase));
  217. CARLA_SAFE_ASSERT_RETURN(fileBaseLen > 6, gNullCarlaShm);
  218. CARLA_SAFE_ASSERT_RETURN(std::strcmp(fileBase + (fileBaseLen - 6), "XXXXXX") == 0, gNullCarlaShm);
  219. // character set to use randomly
  220. static const char charSet[] = "abcdefghijklmnopqrstuvwxyz"
  221. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  222. "0123456789";
  223. static const int charSetLen = static_cast<int>(std::strlen(charSet) - 1); // -1 to avoid trailing '\0'
  224. // try until getting a valid shm is obtained or an error occurs
  225. for (;;)
  226. {
  227. // fill the XXXXXX characters randomly
  228. for (std::size_t c = fileBaseLen - 6; c < fileBaseLen; ++c)
  229. fileBase[c] = charSet[std::rand() % charSetLen];
  230. // (try to) create new shm for this filename
  231. const shm_t shm = carla_shm_create(fileBase);
  232. // all ok!
  233. if (carla_is_shm_valid(shm))
  234. return shm;
  235. // file already exists, keep trying
  236. #ifdef CARLA_OS_WIN
  237. if (::GetLastError() == ERROR_ALREADY_EXISTS)
  238. continue;
  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