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.

CarlaShmUtils.hpp 9.6KB

11 years ago
7 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. # ifdef CARLA_OS_HAIKU
  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. # ifdef CARLA_OS_MAC
  173. void* const ptr(::mmap(nullptr, size, PROT_READ|PROT_WRITE, MAP_SHARED, shm.fd, 0));
  174. # else
  175. void* const ptr(::mmap(nullptr, size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_LOCKED, shm.fd, 0));
  176. # endif
  177. if (ptr == nullptr)
  178. {
  179. carla_safe_assert("ptr != nullptr", __FILE__, __LINE__);
  180. return nullptr;
  181. }
  182. shm.size = size;
  183. return ptr;
  184. #endif
  185. } CARLA_SAFE_EXCEPTION_RETURN("carla_shm_map", nullptr);
  186. }
  187. /*
  188. * Unmap a shared memory object address.
  189. */
  190. static inline
  191. void carla_shm_unmap(carla_shm_t& shm, void* const ptr) noexcept
  192. {
  193. CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm),);
  194. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr,);
  195. #ifdef CARLA_OS_WIN
  196. CARLA_SAFE_ASSERT_RETURN(shm.map != INVALID_HANDLE_VALUE,);
  197. #else
  198. CARLA_SAFE_ASSERT_RETURN(shm.size > 0,);
  199. #endif
  200. try {
  201. #ifdef CARLA_OS_WIN
  202. const HANDLE map(shm.map);
  203. shm.map = INVALID_HANDLE_VALUE;
  204. ::UnmapViewOfFile(ptr);
  205. ::CloseHandle(map);
  206. #else
  207. const std::size_t size(shm.size);
  208. shm.size = 0;
  209. const int ret(::munmap(ptr, size));
  210. CARLA_SAFE_ASSERT(ret == 0);
  211. #endif
  212. } CARLA_SAFE_EXCEPTION("carla_shm_unmap");
  213. }
  214. #ifndef __WINE__
  215. // -----------------------------------------------------------------------
  216. // advanced calls
  217. /*
  218. * Create and open a new shared memory object for a XXXXXX temp filename.
  219. * Will keep trying until a free random filename is obtained.
  220. */
  221. static inline
  222. carla_shm_t carla_shm_create_temp(char* const fileBase) noexcept
  223. {
  224. // check if the fileBase name is valid
  225. CARLA_SAFE_ASSERT_RETURN(fileBase != nullptr, gNullCarlaShm);
  226. const std::size_t fileBaseLen(std::strlen(fileBase));
  227. CARLA_SAFE_ASSERT_RETURN(fileBaseLen > 6, gNullCarlaShm);
  228. CARLA_SAFE_ASSERT_RETURN(std::strcmp(fileBase + (fileBaseLen - 6), "XXXXXX") == 0, gNullCarlaShm);
  229. // character set to use randomly
  230. static const char charSet[] = "abcdefghijklmnopqrstuvwxyz"
  231. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  232. "0123456789";
  233. static const int charSetLen = static_cast<int>(std::strlen(charSet) - 1); // -1 to avoid trailing '\0'
  234. // try until getting a valid shm is obtained or an error occurs
  235. for (;;)
  236. {
  237. // fill the XXXXXX characters randomly
  238. for (std::size_t c = fileBaseLen - 6; c < fileBaseLen; ++c)
  239. fileBase[c] = charSet[std::rand() % charSetLen];
  240. #ifdef CARLA_OS_WIN
  241. // Windows: check if file already exists
  242. const HANDLE h = ::CreateFileMapping(INVALID_HANDLE_VALUE, nullptr,
  243. PAGE_READWRITE|SEC_COMMIT, 0, 8, fileBase);
  244. if (h == INVALID_HANDLE_VALUE)
  245. {
  246. carla_stderr("carla_shm_create_temp(%s) - file mapping test error", fileBase);
  247. return gNullCarlaShm;
  248. }
  249. const DWORD error = ::GetLastError();
  250. ::CloseHandle(h);
  251. if (error == ERROR_ALREADY_EXISTS)
  252. {
  253. carla_stderr("carla_shm_create_temp(%s) - file exists, retrying", fileBase);
  254. continue;
  255. }
  256. #endif
  257. // (try to) create new shm for this filename
  258. const carla_shm_t shm = carla_shm_create(fileBase);
  259. // all ok!
  260. if (carla_is_shm_valid(shm))
  261. return shm;
  262. #ifndef CARLA_OS_WIN
  263. // Non-Windows: if file already exists, keep trying
  264. if (errno == EEXIST)
  265. {
  266. carla_stderr("carla_shm_create_temp(%s) - file exists, retrying", fileBase);
  267. continue;
  268. }
  269. const int localerrno = errno;
  270. carla_stderr("carla_shm_create_temp(%s) - failed, error code %i", fileBase, localerrno);
  271. #endif
  272. // some unknown error occurred, return null
  273. return gNullCarlaShm;
  274. }
  275. }
  276. // -----------------------------------------------------------------------
  277. // shared memory, templated calls
  278. /*
  279. * Map a shared memory object, handling object type and size.
  280. */
  281. template<typename T>
  282. static inline
  283. T* carla_shm_map(carla_shm_t& shm) noexcept
  284. {
  285. return (T*)carla_shm_map(shm, sizeof(T));
  286. }
  287. /*
  288. * Map a shared memory object and return if it's non-null.
  289. */
  290. template<typename T>
  291. static inline
  292. bool carla_shm_map(carla_shm_t& shm, T*& value) noexcept
  293. {
  294. value = (T*)carla_shm_map(shm, sizeof(T));
  295. return (value != nullptr);
  296. }
  297. // -----------------------------------------------------------------------
  298. #endif // __WINE__
  299. #endif // CARLA_SHM_UTILS_HPP_INCLUDED