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

11 years ago
10 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 shm; HANDLE map; };
  22. # define shm_t_INIT {nullptr, nullptr}
  23. #else
  24. # include <fcntl.h>
  25. # include <sys/mman.h>
  26. struct shm_t { int fd; const char* filename; };
  27. # define shm_t_INIT {-1, nullptr}
  28. #endif
  29. // -----------------------------------------------------------------------
  30. // shared memory calls
  31. /*
  32. * Null object returned when a shared memory operation fails.
  33. */
  34. #ifdef CARLA_OS_WIN
  35. static const shm_t gNullCarlaShm = { nullptr, nullptr };
  36. #else
  37. static const shm_t gNullCarlaShm = { -1, nullptr };
  38. #endif
  39. /*
  40. * Check if a shared memory object is valid.
  41. */
  42. static inline
  43. bool carla_is_shm_valid(const shm_t& shm) noexcept
  44. {
  45. #ifdef CARLA_OS_WIN
  46. return (shm.shm != nullptr && shm.shm != INVALID_HANDLE_VALUE);
  47. #else
  48. return (shm.fd >= 0);
  49. #endif
  50. }
  51. /*
  52. * Initialize a shared memory object to an invalid state.
  53. */
  54. static inline
  55. void carla_shm_init(shm_t& shm) noexcept
  56. {
  57. #ifdef CARLA_OS_WIN
  58. shm.shm = nullptr;
  59. shm.map = nullptr;
  60. #else
  61. shm.fd = -1;
  62. shm.filename = nullptr;
  63. #endif
  64. }
  65. /*
  66. * Create and open a new shared memory object.
  67. * Returns an invalid object if the operation failed or the filename already exists.
  68. */
  69. static inline
  70. shm_t carla_shm_create(const char* const filename) noexcept
  71. {
  72. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', gNullCarlaShm);
  73. shm_t ret;
  74. try {
  75. #ifdef CARLA_OS_WIN
  76. ret.shm = nullptr; // TODO
  77. ret.map = nullptr;
  78. #else
  79. ret.fd = ::shm_open(filename, O_CREAT|O_EXCL|O_RDWR, 0600);
  80. ret.filename = (ret.fd >= 0) ? carla_strdup_safe(filename) : nullptr;
  81. if (ret.fd >= 0 && ret.filename == nullptr)
  82. {
  83. ::close(ret.fd);
  84. ::shm_unlink(filename);
  85. ret.fd = -1;
  86. }
  87. #endif
  88. }
  89. catch(...) {
  90. carla_safe_exception("carla_shm_create", __FILE__, __LINE__);
  91. ret = gNullCarlaShm;
  92. }
  93. return ret;
  94. }
  95. /*
  96. * Attach to an existing shared memory object.
  97. */
  98. static inline
  99. shm_t carla_shm_attach(const char* const filename) noexcept
  100. {
  101. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', gNullCarlaShm);
  102. shm_t ret;
  103. try {
  104. #ifdef CARLA_OS_WIN
  105. ret.shm = ::CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr);
  106. ret.map = nullptr;
  107. #else
  108. ret.fd = ::shm_open(filename, O_RDWR, 0);
  109. ret.filename = nullptr;
  110. #endif
  111. }
  112. catch(...) {
  113. carla_safe_exception("carla_shm_attach", __FILE__, __LINE__);
  114. ret = gNullCarlaShm;
  115. }
  116. return ret;
  117. }
  118. /*
  119. * Close a shared memory object and invalidate it.
  120. */
  121. static inline
  122. void carla_shm_close(shm_t& shm) noexcept
  123. {
  124. CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm),);
  125. #ifdef CARLA_OS_WIN
  126. CARLA_SAFE_ASSERT(shm.map == nullptr);
  127. #endif
  128. try {
  129. #ifdef CARLA_OS_WIN
  130. ::CloseHandle(shm.shm);
  131. #else
  132. ::close(shm.fd);
  133. if (shm.filename != nullptr)
  134. {
  135. ::shm_unlink(shm.filename);
  136. delete[] shm.filename;
  137. }
  138. #endif
  139. } CARLA_SAFE_EXCEPTION("carla_shm_close");
  140. shm = gNullCarlaShm;
  141. }
  142. /*
  143. * Map a shared memory object to @a size bytes and return the memory address.
  144. * @note One shared memory object can only have one mapping at a time.
  145. */
  146. static inline
  147. void* carla_shm_map(shm_t& shm, const std::size_t size) noexcept
  148. {
  149. CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm), nullptr);
  150. CARLA_SAFE_ASSERT_RETURN(size > 0, nullptr);
  151. #ifdef CARLA_OS_WIN
  152. CARLA_SAFE_ASSERT_RETURN(shm.map == nullptr, nullptr);
  153. #endif
  154. try {
  155. #ifdef CARLA_OS_WIN
  156. const HANDLE map(:CreateFileMapping(shm.shm, nullptr, PAGE_READWRITE, size, size, nullptr));
  157. CARLA_SAFE_ASSERT_RETURN(map != nullptr, nullptr);
  158. const HANDLE ptr(::MapViewOfFile(map, FILE_MAP_COPY, 0, 0, size));
  159. if (ptr == nullptr)
  160. {
  161. carla_safe_assert("ptr != nullptr", __FILE__, __LINE__);
  162. ::CloseHandle(map);
  163. return nullptr;
  164. }
  165. shm.map = map;
  166. return ptr;
  167. #else
  168. if (shm.filename != nullptr)
  169. {
  170. const int ret(::ftruncate(shm.fd, static_cast<off_t>(size)));
  171. CARLA_SAFE_ASSERT_RETURN(ret == 0, nullptr);
  172. }
  173. return ::mmap(nullptr, size, PROT_READ|PROT_WRITE, MAP_SHARED, shm.fd, 0);
  174. #endif
  175. } CARLA_SAFE_EXCEPTION_RETURN("carla_shm_map", nullptr);
  176. }
  177. /*
  178. * Unmap a shared memory object address.
  179. */
  180. static inline
  181. void carla_shm_unmap(shm_t& shm, void* const ptr, const std::size_t size) noexcept
  182. {
  183. CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm),);
  184. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr,);
  185. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  186. #ifdef CARLA_OS_WIN
  187. CARLA_SAFE_ASSERT_RETURN(shm.map != nullptr,);
  188. #endif
  189. try {
  190. #ifdef CARLA_OS_WIN
  191. const HANDLE map(shm.map);
  192. shm.map = nullptr;
  193. ::UnmapViewOfFile(ptr);
  194. ::CloseHandle(map);
  195. #else
  196. const int ret(::munmap(ptr, size));
  197. CARLA_SAFE_ASSERT(ret == 0);
  198. #endif
  199. } CARLA_SAFE_EXCEPTION("carla_shm_unmap");
  200. // unused depending on platform
  201. return; (void)shm; (void)size;
  202. }
  203. // -----------------------------------------------------------------------
  204. // shared memory, templated calls
  205. /*
  206. * Map a shared memory object, handling object type and size.
  207. */
  208. template<typename T>
  209. static inline
  210. T* carla_shm_map(shm_t& shm) noexcept
  211. {
  212. return (T*)carla_shm_map(shm, sizeof(T));
  213. }
  214. /*
  215. * Map a shared memory object and return if it's non-null.
  216. */
  217. template<typename T>
  218. static inline
  219. bool carla_shm_map(shm_t& shm, T*& value) noexcept
  220. {
  221. value = (T*)carla_shm_map(shm, sizeof(T));
  222. return (value != nullptr);
  223. }
  224. /*
  225. * Unmap a shared memory object address and set it as null.
  226. */
  227. template<typename T>
  228. static inline
  229. void carla_shm_unmap(shm_t& shm, T*& value) noexcept
  230. {
  231. carla_shm_unmap(shm, value, sizeof(T));
  232. value = nullptr;
  233. }
  234. // -----------------------------------------------------------------------
  235. #endif // CARLA_SHM_UTILS_HPP_INCLUDED