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.

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