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 8.5KB

11 years ago
9 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. carla_stderr("carla_shm_create_temp(%s) - failed, error code %i", fileBase, errno);
  243. #endif
  244. // some unknown error occurred, return null
  245. return gNullCarlaShm;
  246. }
  247. }
  248. // -----------------------------------------------------------------------
  249. // shared memory, templated calls
  250. /*
  251. * Map a shared memory object, handling object type and size.
  252. */
  253. template<typename T>
  254. static inline
  255. T* carla_shm_map(shm_t& shm) noexcept
  256. {
  257. return (T*)carla_shm_map(shm, sizeof(T));
  258. }
  259. /*
  260. * Map a shared memory object and return if it's non-null.
  261. */
  262. template<typename T>
  263. static inline
  264. bool carla_shm_map(shm_t& shm, T*& value) noexcept
  265. {
  266. value = (T*)carla_shm_map(shm, sizeof(T));
  267. return (value != nullptr);
  268. }
  269. // -----------------------------------------------------------------------
  270. #endif // CARLA_SHM_UTILS_HPP_INCLUDED