jack2 codebase
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.

371 lines
7.6KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2006 Grame
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #ifndef __JackShmMem__
  17. #define __JackShmMem__
  18. #include "shm.h"
  19. #include "JackError.h"
  20. #include <new> // GCC 4.0
  21. #include <errno.h>
  22. #include <stdlib.h>
  23. #ifdef WIN32
  24. #include <windows.h>
  25. #else
  26. #include <sys/types.h>
  27. #include <sys/mman.h>
  28. #endif
  29. namespace Jack
  30. {
  31. class JackLockMem
  32. {
  33. private:
  34. size_t fSize;
  35. static size_t gSize;
  36. public:
  37. void* operator new(size_t size)
  38. {
  39. gSize = size;
  40. return malloc(size);
  41. }
  42. void operator delete(void* ptr, size_t size)
  43. {
  44. free(ptr);
  45. }
  46. JackLockMem():fSize(gSize)
  47. {}
  48. virtual ~JackLockMem()
  49. {
  50. UnlockMemory();
  51. }
  52. void LockMemory()
  53. {
  54. #ifdef __APPLE__
  55. mlock(this, fSize);
  56. #elif linux_
  57. mlock(this, fSize);
  58. #elif WIN32
  59. VirtualLock(this, fSize);
  60. #endif
  61. }
  62. void UnlockMemory()
  63. {
  64. #ifdef __APPLE__
  65. munlock(this, fSize);
  66. #elif linux_
  67. munlock(this, fSize);
  68. #elif WIN32
  69. VirtualUnlock(this, fSize);
  70. #endif
  71. }
  72. };
  73. /*!
  74. \brief The base class for shared memory management.
  75. A class which objects need to be allocated in shared memory derives from this class.
  76. */
  77. class JackShmMem
  78. {
  79. private:
  80. jack_shm_info_t fInfo;
  81. static unsigned long fSegmentNum;
  82. static unsigned long fSegmentCount;
  83. static jack_shm_info_t gInfo;
  84. public:
  85. void* operator new(size_t size);
  86. void operator delete(void* p, size_t size);
  87. JackShmMem()
  88. {
  89. fInfo.index = gInfo.index;
  90. fInfo.attached_at = gInfo.attached_at;
  91. }
  92. virtual ~JackShmMem()
  93. {}
  94. int GetShmIndex()
  95. {
  96. return fInfo.index;
  97. }
  98. char* GetShmAddress()
  99. {
  100. return (char*)fInfo.attached_at;
  101. }
  102. };
  103. /*!
  104. \brief Pointer on shared memory segment in the client side.
  105. */
  106. template <class T>
  107. class JackShmReadWritePtr
  108. {
  109. private:
  110. jack_shm_info_t fInfo;
  111. void Init(int index)
  112. {
  113. if (fInfo.index < 0 && index >= 0) {
  114. JackLog("JackShmReadWritePtr::Init %ld %ld\n", index, fInfo.index);
  115. if (jack_initialize_shm_client() < 0)
  116. throw - 1;
  117. fInfo.index = index;
  118. if (jack_attach_shm(&fInfo)) {
  119. //jack_error("cannot attach shared memory segment", strerror(errno));
  120. throw - 2;
  121. }
  122. }
  123. }
  124. public:
  125. JackShmReadWritePtr()
  126. {
  127. fInfo.index = -1;
  128. fInfo.attached_at = NULL;
  129. }
  130. JackShmReadWritePtr(int index)
  131. {
  132. Init(index);
  133. }
  134. virtual ~JackShmReadWritePtr()
  135. {
  136. if (fInfo.index >= 0) {
  137. JackLog("JackShmReadWritePtr::~JackShmReadWritePtr %ld\n", fInfo.index);
  138. jack_release_shm(&fInfo);
  139. fInfo.index = -1;
  140. }
  141. }
  142. T* operator->() const
  143. {
  144. return (T*)fInfo.attached_at;
  145. }
  146. operator T*() const
  147. {
  148. return (T*)fInfo.attached_at;
  149. }
  150. JackShmReadWritePtr& operator=(int index)
  151. {
  152. Init(index);
  153. return *this;
  154. }
  155. int GetShmIndex()
  156. {
  157. return fInfo.index;
  158. }
  159. T* GetShmAddress()
  160. {
  161. return (T*)fInfo.attached_at;
  162. }
  163. };
  164. /*!
  165. \brief Pointer on shared memory segment in the client side: destroy the segment (used client control)
  166. */
  167. template <class T>
  168. class JackShmReadWritePtr1
  169. {
  170. private:
  171. jack_shm_info_t fInfo;
  172. void Init(int index)
  173. {
  174. if (fInfo.index < 0 && index >= 0) {
  175. JackLog("JackShmReadWritePtr1::Init %ld %ld\n", index, fInfo.index);
  176. if (jack_initialize_shm_client() < 0)
  177. throw - 1;
  178. fInfo.index = index;
  179. if (jack_attach_shm(&fInfo)) {
  180. //jack_error("cannot attach shared memory segment", strerror(errno));
  181. throw - 2;
  182. }
  183. /*
  184. nobody else needs to access this shared memory any more, so
  185. destroy it. because we have our own attachment to it, it won't
  186. vanish till we exit (and release it).
  187. */
  188. jack_destroy_shm(&fInfo);
  189. }
  190. }
  191. public:
  192. JackShmReadWritePtr1()
  193. {
  194. fInfo.index = -1;
  195. fInfo.attached_at = NULL;
  196. }
  197. JackShmReadWritePtr1(int index)
  198. {
  199. Init(index);
  200. }
  201. virtual ~JackShmReadWritePtr1()
  202. {
  203. if (fInfo.index >= 0) {
  204. JackLog("JackShmReadWritePtr1::~JackShmReadWritePtr1 %ld\n", fInfo.index);
  205. jack_release_shm(&fInfo);
  206. fInfo.index = -1;
  207. }
  208. }
  209. T* operator->() const
  210. {
  211. return (T*)fInfo.attached_at;
  212. }
  213. operator T*() const
  214. {
  215. return (T*)fInfo.attached_at;
  216. }
  217. JackShmReadWritePtr1& operator=(int index)
  218. {
  219. Init(index);
  220. return *this;
  221. }
  222. int GetShmIndex()
  223. {
  224. return fInfo.index;
  225. }
  226. T* GetShmAddress()
  227. {
  228. return (T*)fInfo.attached_at;
  229. }
  230. };
  231. /*!
  232. \brief Pointer on shared memory segment in the client side.
  233. */
  234. template <class T>
  235. class JackShmReadPtr
  236. {
  237. private:
  238. jack_shm_info_t fInfo;
  239. void Init(int index)
  240. {
  241. if (fInfo.index < 0 && index >= 0) {
  242. JackLog("JackShmPtrRead::Init %ld %ld\n", index, fInfo.index);
  243. if (jack_initialize_shm_client() < 0)
  244. throw - 1;
  245. fInfo.index = index;
  246. if (jack_attach_shm_read(&fInfo)) {
  247. //jack_error("cannot attach shared memory segment", strerror(errno));
  248. throw - 2;
  249. }
  250. }
  251. }
  252. public:
  253. JackShmReadPtr()
  254. {
  255. fInfo.index = -1;
  256. fInfo.attached_at = NULL;
  257. }
  258. JackShmReadPtr(int index)
  259. {
  260. Init(index);
  261. }
  262. virtual ~JackShmReadPtr()
  263. {
  264. if (fInfo.index >= 0) {
  265. JackLog("JackShmPtrRead::~JackShmPtrRead %ld\n", fInfo.index);
  266. jack_release_shm(&fInfo);
  267. fInfo.index = -1;
  268. }
  269. }
  270. T* operator->() const
  271. {
  272. return (T*)fInfo.attached_at;
  273. }
  274. operator T*() const
  275. {
  276. return (T*)fInfo.attached_at;
  277. }
  278. JackShmReadPtr& operator=(int index)
  279. {
  280. Init(index);
  281. return *this;
  282. }
  283. int GetShmIndex()
  284. {
  285. return fInfo.index;
  286. }
  287. T* GetShmAddress()
  288. {
  289. return (T*)fInfo.attached_at;
  290. }
  291. };
  292. } // end of namespace
  293. #endif