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.

321 lines
8.4KB

  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. }
  81. CARLA_SAFE_EXCEPTION_RETURN("carla_shm_create", gNullCarlaShm);
  82. #endif
  83. return ret;
  84. }
  85. /*
  86. * Attach to an existing shared memory object.
  87. */
  88. static inline
  89. shm_t carla_shm_attach(const char* const filename) noexcept
  90. {
  91. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', gNullCarlaShm);
  92. shm_t ret;
  93. #ifdef CARLA_OS_WIN
  94. ret.map = INVALID_HANDLE_VALUE;
  95. ret.isServer = false;
  96. ret.filename = carla_strdup_safe(filename);
  97. #else
  98. try {
  99. ret.fd = ::shm_open(filename, O_RDWR, 0);
  100. ret.filename = nullptr;
  101. ret.size = 0;
  102. }
  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(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(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. void* const ptr(::mmap(nullptr, size, PROT_READ|PROT_WRITE, MAP_SHARED, shm.fd, 0));
  170. if (ptr == nullptr)
  171. {
  172. carla_safe_assert("ptr != nullptr", __FILE__, __LINE__);
  173. return nullptr;
  174. }
  175. shm.size = size;
  176. return ptr;
  177. #endif
  178. } CARLA_SAFE_EXCEPTION_RETURN("carla_shm_map", nullptr);
  179. }
  180. /*
  181. * Unmap a shared memory object address.
  182. */
  183. static inline
  184. void carla_shm_unmap(shm_t& shm, void* const ptr) noexcept
  185. {
  186. CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm),);
  187. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr,);
  188. #ifdef CARLA_OS_WIN
  189. CARLA_SAFE_ASSERT_RETURN(shm.map != INVALID_HANDLE_VALUE,);
  190. #else
  191. CARLA_SAFE_ASSERT_RETURN(shm.size > 0,);
  192. #endif
  193. try {
  194. #ifdef CARLA_OS_WIN
  195. const HANDLE map(shm.map);
  196. shm.map = INVALID_HANDLE_VALUE;
  197. ::UnmapViewOfFile(ptr);
  198. ::CloseHandle(map);
  199. #else
  200. const std::size_t size(shm.size);
  201. shm.size = 0;
  202. const int ret(::munmap(ptr, size));
  203. CARLA_SAFE_ASSERT(ret == 0);
  204. #endif
  205. } CARLA_SAFE_EXCEPTION("carla_shm_unmap");
  206. }
  207. // -----------------------------------------------------------------------
  208. // advanced calls
  209. /*
  210. * Create and open a new shared memory object for a XXXXXX temp filename.
  211. * Will keep trying until a free random filename is obtained.
  212. */
  213. static inline
  214. shm_t carla_shm_create_temp(char* const fileBase) noexcept
  215. {
  216. // check if the fileBase name is valid
  217. CARLA_SAFE_ASSERT_RETURN(fileBase != nullptr, gNullCarlaShm);
  218. const std::size_t fileBaseLen(std::strlen(fileBase));
  219. CARLA_SAFE_ASSERT_RETURN(fileBaseLen > 6, gNullCarlaShm);
  220. CARLA_SAFE_ASSERT_RETURN(std::strcmp(fileBase + (fileBaseLen - 6), "XXXXXX") == 0, gNullCarlaShm);
  221. // character set to use randomly
  222. static const char charSet[] = "abcdefghijklmnopqrstuvwxyz"
  223. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  224. "0123456789";
  225. static const int charSetLen = static_cast<int>(std::strlen(charSet) - 1); // -1 to avoid trailing '\0'
  226. // try until getting a valid shm is obtained or an error occurs
  227. for (;;)
  228. {
  229. // fill the XXXXXX characters randomly
  230. for (std::size_t c = fileBaseLen - 6; c < fileBaseLen; ++c)
  231. fileBase[c] = charSet[std::rand() % charSetLen];
  232. // (try to) create new shm for this filename
  233. const shm_t shm = carla_shm_create(fileBase);
  234. // all ok!
  235. if (carla_is_shm_valid(shm))
  236. return shm;
  237. // file already exists, keep trying
  238. #ifdef CARLA_OS_WIN
  239. if (::GetLastError() == ERROR_ALREADY_EXISTS)
  240. continue;
  241. #else
  242. if (errno == EEXIST)
  243. continue;
  244. #endif
  245. // some unknown error occurred, return null
  246. return gNullCarlaShm;
  247. }
  248. }
  249. // -----------------------------------------------------------------------
  250. // shared memory, templated calls
  251. /*
  252. * Map a shared memory object, handling object type and size.
  253. */
  254. template<typename T>
  255. static inline
  256. T* carla_shm_map(shm_t& shm) noexcept
  257. {
  258. return (T*)carla_shm_map(shm, sizeof(T));
  259. }
  260. /*
  261. * Map a shared memory object and return if it's non-null.
  262. */
  263. template<typename T>
  264. static inline
  265. bool carla_shm_map(shm_t& shm, T*& value) noexcept
  266. {
  267. value = (T*)carla_shm_map(shm, sizeof(T));
  268. return (value != nullptr);
  269. }
  270. // -----------------------------------------------------------------------
  271. #endif // CARLA_SHM_UTILS_HPP_INCLUDED