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.3KB

11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. #endif
  82. }
  83. catch(...) {
  84. carla_safe_exception("carla_shm_create", __FILE__, __LINE__);
  85. ret = gNullCarlaShm;
  86. }
  87. return ret;
  88. }
  89. /*
  90. * Attach to an existing shared memory object.
  91. */
  92. static inline
  93. shm_t carla_shm_attach(const char* const filename) noexcept
  94. {
  95. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', gNullCarlaShm);
  96. shm_t ret;
  97. try {
  98. #ifdef CARLA_OS_WIN
  99. ret.shm = ::CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  100. ret.map = nullptr;
  101. #else
  102. ret.fd = ::shm_open(filename, O_RDWR, 0);
  103. ret.filename = nullptr;
  104. #endif
  105. }
  106. catch(...) {
  107. ret = gNullCarlaShm;
  108. }
  109. return ret;
  110. }
  111. /*
  112. * Close a shared memory object and invalidate it.
  113. */
  114. static inline
  115. void carla_shm_close(shm_t& shm) noexcept
  116. {
  117. CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm),);
  118. #ifdef CARLA_OS_WIN
  119. CARLA_SAFE_ASSERT(shm.map == nullptr);
  120. #endif
  121. try {
  122. #ifdef CARLA_OS_WIN
  123. ::CloseHandle(shm.shm);
  124. #else
  125. ::close(shm.fd);
  126. if (shm.filename != nullptr)
  127. {
  128. ::shm_unlink(shm.filename);
  129. delete[] shm.filename;
  130. }
  131. #endif
  132. } CARLA_SAFE_EXCEPTION("carla_shm_close");
  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(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 == nullptr, nullptr);
  146. #endif
  147. try {
  148. #ifdef CARLA_OS_WIN
  149. const HANDLE map = ::CreateFileMapping(shm.shm, NULL, PAGE_READWRITE, size, size, NULL);
  150. CARLA_SAFE_ASSERT_RETURN(map != nullptr, nullptr);
  151. HANDLE ptr = ::MapViewOfFile(map, FILE_MAP_COPY, 0, 0, size);
  152. if (ptr == nullptr)
  153. {
  154. carla_safe_assert("ptr != nullptr", __FILE__, __LINE__);
  155. ::CloseHandle(map);
  156. return nullptr;
  157. }
  158. shm.map = map;
  159. return ptr;
  160. #else
  161. const int ret = ::ftruncate(shm.fd, static_cast<off_t>(size));
  162. CARLA_SAFE_ASSERT_RETURN(ret == 0, nullptr);
  163. return ::mmap(nullptr, size, PROT_READ|PROT_WRITE, MAP_SHARED, shm.fd, 0);
  164. #endif
  165. } CARLA_SAFE_EXCEPTION_RETURN("carla_shm_map", nullptr);
  166. }
  167. /*
  168. * Unmap a shared memory object address.
  169. */
  170. static inline
  171. void carla_shm_unmap(shm_t& shm, void* const ptr, const std::size_t size) noexcept
  172. {
  173. CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm),);
  174. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr,);
  175. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  176. #ifdef CARLA_OS_WIN
  177. CARLA_SAFE_ASSERT_RETURN(shm.map != nullptr,);
  178. #endif
  179. try {
  180. #ifdef CARLA_OS_WIN
  181. const HANDLE map = shm.map;
  182. shm.map = nullptr;
  183. ::UnmapViewOfFile(ptr);
  184. ::CloseHandle(map);
  185. #else
  186. const int ret = ::munmap(ptr, size);
  187. CARLA_SAFE_ASSERT(ret == 0);
  188. #endif
  189. } CARLA_SAFE_EXCEPTION("carla_shm_unmap");
  190. // unused depending on platform
  191. return; (void)shm; (void)size;
  192. }
  193. // -----------------------------------------------------------------------
  194. // shared memory, templated calls
  195. /*
  196. * Map a shared memory object, handling object type and size.
  197. */
  198. template<typename T>
  199. static inline
  200. T* carla_shm_map(shm_t& shm) noexcept
  201. {
  202. return (T*)carla_shm_map(shm, sizeof(T));
  203. }
  204. /*
  205. * Map a shared memory object and return if it's non-null.
  206. */
  207. template<typename T>
  208. static inline
  209. bool carla_shm_map(shm_t& shm, T*& value) noexcept
  210. {
  211. value = (T*)carla_shm_map(shm, sizeof(T));
  212. return (value != nullptr);
  213. }
  214. /*
  215. * Unmap a shared memory object address and set it as null.
  216. */
  217. template<typename T>
  218. static inline
  219. void carla_shm_unmap(shm_t& shm, T*& value) noexcept
  220. {
  221. carla_shm_unmap(shm, value, sizeof(T));
  222. value = nullptr;
  223. }
  224. // -----------------------------------------------------------------------
  225. #endif // CARLA_SHM_UTILS_HPP_INCLUDED