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.

354 lines
9.5KB

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