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.

351 lines
9.6KB

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