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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. # ifndef MAP_LOCKED
  30. # define MAP_LOCKED 0x0
  31. # endif
  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. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, nullptr);
  174. if (ptr == MAP_FAILED)
  175. {
  176. carla_stderr2("carla_shm_map() - mmap failed: %s", std::strerror(errno));
  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. #endif // __WINE__
  295. // -----------------------------------------------------------------------
  296. #ifndef CARLA_OS_LINUX
  297. # undef MAP_LOCKED
  298. #endif
  299. #endif // CARLA_SHM_UTILS_HPP_INCLUDED