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.

208 lines
4.2KB

  1. /*
  2. * Carla shared memory utils
  3. * Copyright (C) 2013 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 GPL.txt file
  16. */
  17. #ifndef __CARLA_SHM_UTILS_HPP__
  18. #define __CARLA_SHM_UTILS_HPP__
  19. #include "CarlaUtils.hpp"
  20. #ifdef CARLA_OS_WIN
  21. struct shm_t { HANDLE shm; HANDLE map; };
  22. #else
  23. # include <fcntl.h>
  24. # include <sys/mman.h>
  25. typedef int shm_t;
  26. #endif
  27. // -------------------------------------------------
  28. // shared memory calls
  29. static inline
  30. bool carla_is_shm_valid(const shm_t& shm)
  31. {
  32. #ifdef CARLA_OS_WIN
  33. return (shm.shm != nullptr && shm.shm != INVALID_HANDLE_VALUE);
  34. #else
  35. return (shm >= 0);
  36. #endif
  37. }
  38. static inline
  39. void carla_shm_init(shm_t& shm)
  40. {
  41. #ifdef CARLA_OS_WIN
  42. shm.shm = nullptr;
  43. shm.map = nullptr;
  44. #else
  45. shm = -1;
  46. #endif
  47. }
  48. #ifdef CARLA_OS_WIN
  49. static inline
  50. shm_t carla_shm_create(const char* const name)
  51. {
  52. CARLA_ASSERT(name != nullptr);
  53. shm_t ret;
  54. ret.shm = nullptr; // TODO
  55. ret.map = nullptr;
  56. return ret;
  57. }
  58. static inline
  59. shm_t carla_shm_attach(const char* const name)
  60. {
  61. CARLA_ASSERT(name != nullptr);
  62. shm_t ret;
  63. ret.shm = CreateFileA(name, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  64. ret.map = nullptr;
  65. return ret;
  66. }
  67. static inline
  68. shm_t carla_shm_attach_linux(const char* const name)
  69. {
  70. CARLA_ASSERT(name != nullptr);
  71. char shmName[std::strlen(name)+10];
  72. std::strcpy(shmName, "/dev/shm/");
  73. std::strcat(shmName, name);
  74. shm_t ret;
  75. ret.shm = CreateFileA(shmName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  76. ret.map = nullptr;
  77. return ret;
  78. }
  79. #else
  80. static inline
  81. shm_t carla_shm_create(const char* const name)
  82. {
  83. CARLA_ASSERT(name != nullptr);
  84. return shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
  85. }
  86. static inline
  87. shm_t carla_shm_attach(const char* const name)
  88. {
  89. CARLA_ASSERT(name != nullptr);
  90. return shm_open(name, O_RDWR, 0);
  91. }
  92. #endif
  93. static inline
  94. void carla_shm_close(shm_t& shm)
  95. {
  96. CARLA_ASSERT(carla_is_shm_valid(shm));
  97. #ifdef CARLA_OS_WIN
  98. CARLA_ASSERT(shm.map == nullptr);
  99. CloseHandle(shm.shm);
  100. shm.shm = nullptr;
  101. #else
  102. close(shm);
  103. shm = -1;
  104. #endif
  105. }
  106. static inline
  107. void* carla_shm_map(shm_t& shm, const size_t size)
  108. {
  109. CARLA_ASSERT(carla_is_shm_valid(shm));
  110. CARLA_ASSERT(size > 0);
  111. #ifdef CARLA_OS_WIN
  112. CARLA_ASSERT(shm.map == nullptr);
  113. HANDLE map = CreateFileMapping(shm.shm, NULL, PAGE_READWRITE, size, size, NULL);
  114. if (map == nullptr)
  115. return nullptr;
  116. HANDLE ptr = MapViewOfFile(map, FILE_MAP_COPY, 0, 0, size);
  117. if (ptr == nullptr)
  118. {
  119. CloseHandle(map);
  120. return nullptr;
  121. }
  122. shm.map = map;
  123. return ptr;
  124. #else
  125. ftruncate(shm, size);
  126. return mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, shm, 0);
  127. #endif
  128. }
  129. static inline
  130. void carla_shm_unmap(shm_t& shm, void* const ptr, const size_t size)
  131. {
  132. CARLA_ASSERT(carla_is_shm_valid(shm));
  133. CARLA_ASSERT(ptr != nullptr);
  134. CARLA_ASSERT(size > 0);
  135. #ifdef CARLA_OS_WIN
  136. CARLA_ASSERT(shm.map != nullptr);
  137. UnmapViewOfFile(ptr);
  138. CloseHandle(shm.map);
  139. shm.map = nullptr;
  140. return;
  141. // unused
  142. (void)size;
  143. #else
  144. munmap(ptr, size);
  145. return;
  146. // unused
  147. (void)shm;
  148. #endif
  149. }
  150. // -------------------------------------------------
  151. // shared memory, templated calls
  152. template<typename T>
  153. static inline
  154. bool carla_shm_map(shm_t& shm, T*& value)
  155. {
  156. value = (T*)carla_shm_map(shm, sizeof(T));
  157. return (value != nullptr);
  158. }
  159. template<typename T>
  160. static inline
  161. void carla_shm_unmap(shm_t& shm, T*& value)
  162. {
  163. carla_shm_unmap(shm, value, sizeof(T));
  164. value = nullptr;
  165. }
  166. // -------------------------------------------------
  167. #endif // __CARLA_SHM_UTILS_HPP__