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.

378 lines
8.0KB

  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. #define CHECK_MLOCK(ptr, size) (VirtualLock((ptr), (size)) != 0)
  26. #define CHECK_MUNLOCK(ptr, size) (VirtualUnlock((ptr), (size)) != 0)
  27. #define CHECK_MLOCKALL()(false)
  28. #define CHECK_MUNLOCKALL()(false)
  29. #else
  30. #include <sys/types.h>
  31. #include <sys/mman.h>
  32. #define CHECK_MLOCK(ptr, size) (mlock((ptr), (size)) == 0)
  33. #define CHECK_MUNLOCK(ptr, size) (munlock((ptr), (size)) == 0)
  34. #define CHECK_MLOCKALL() (mlockall(MCL_CURRENT | MCL_FUTURE) == 0)
  35. #define CHECK_MUNLOCKALL() (munlockall() == 0)
  36. #endif
  37. namespace Jack
  38. {
  39. void LockMemoryImp(void* ptr, size_t size);
  40. void UnlockMemoryImp(void* ptr, size_t size);
  41. class JackMem
  42. {
  43. private:
  44. size_t fSize;
  45. static size_t gSize;
  46. public:
  47. void* operator new(size_t size)
  48. {
  49. gSize = size;
  50. return calloc(1, size);
  51. }
  52. void operator delete(void* ptr, size_t size)
  53. {
  54. free(ptr);
  55. }
  56. JackMem():fSize(gSize)
  57. {}
  58. virtual ~JackMem()
  59. {}
  60. void LockMemory()
  61. {
  62. LockMemoryImp(this, fSize);
  63. }
  64. void UnlockMemory()
  65. {
  66. UnlockMemoryImp(this, fSize);
  67. }
  68. };
  69. /*!
  70. \brief The base class for shared memory management.
  71. A class which objects need to be allocated in shared memory derives from this class.
  72. */
  73. class JackShmMem
  74. {
  75. protected:
  76. jack_shm_info_t fInfo;
  77. static unsigned long fSegmentNum;
  78. static unsigned long fSegmentCount;
  79. static jack_shm_info_t gInfo;
  80. public:
  81. void* operator new(size_t size);
  82. void operator delete(void* p, size_t size);
  83. JackShmMem()
  84. {
  85. fInfo.index = gInfo.index;
  86. fInfo.attached_at = gInfo.attached_at;
  87. fInfo.size = gInfo.size;
  88. }
  89. virtual ~JackShmMem()
  90. {}
  91. int GetShmIndex()
  92. {
  93. return fInfo.index;
  94. }
  95. char* GetShmAddress()
  96. {
  97. return (char*)fInfo.attached_at;
  98. }
  99. void LockMemory()
  100. {
  101. LockMemoryImp(this, fInfo.size);
  102. }
  103. void UnlockMemory()
  104. {
  105. UnlockMemoryImp(this, fInfo.size);
  106. }
  107. };
  108. /*!
  109. \brief Pointer on shared memory segment in the client side.
  110. */
  111. template <class T>
  112. class JackShmReadWritePtr
  113. {
  114. private:
  115. jack_shm_info_t fInfo;
  116. void Init(int index)
  117. {
  118. if (fInfo.index < 0 && index >= 0) {
  119. JackLog("JackShmReadWritePtr::Init %ld %ld\n", index, fInfo.index);
  120. if (jack_initialize_shm_client() < 0)
  121. throw - 1;
  122. fInfo.index = index;
  123. if (jack_attach_shm(&fInfo)) {
  124. //jack_error("cannot attach shared memory segment", strerror(errno));
  125. throw - 2;
  126. }
  127. }
  128. }
  129. public:
  130. JackShmReadWritePtr()
  131. {
  132. fInfo.index = -1;
  133. fInfo.attached_at = NULL;
  134. }
  135. JackShmReadWritePtr(int index)
  136. {
  137. Init(index);
  138. }
  139. virtual ~JackShmReadWritePtr()
  140. {
  141. if (fInfo.index >= 0) {
  142. JackLog("JackShmReadWritePtr::~JackShmReadWritePtr %ld\n", fInfo.index);
  143. jack_release_shm(&fInfo);
  144. fInfo.index = -1;
  145. }
  146. }
  147. T* operator->() const
  148. {
  149. return (T*)fInfo.attached_at;
  150. }
  151. operator T*() const
  152. {
  153. return (T*)fInfo.attached_at;
  154. }
  155. JackShmReadWritePtr& operator=(int index)
  156. {
  157. Init(index);
  158. return *this;
  159. }
  160. int GetShmIndex()
  161. {
  162. return fInfo.index;
  163. }
  164. T* GetShmAddress()
  165. {
  166. return (T*)fInfo.attached_at;
  167. }
  168. };
  169. /*!
  170. \brief Pointer on shared memory segment in the client side: destroy the segment (used client control)
  171. */
  172. template <class T>
  173. class JackShmReadWritePtr1
  174. {
  175. private:
  176. jack_shm_info_t fInfo;
  177. void Init(int index)
  178. {
  179. if (fInfo.index < 0 && index >= 0) {
  180. JackLog("JackShmReadWritePtr1::Init %ld %ld\n", index, fInfo.index);
  181. if (jack_initialize_shm_client() < 0)
  182. throw - 1;
  183. fInfo.index = index;
  184. if (jack_attach_shm(&fInfo)) {
  185. //jack_error("cannot attach shared memory segment", strerror(errno));
  186. throw - 2;
  187. }
  188. /*
  189. nobody else needs to access this shared memory any more, so
  190. destroy it. because we have our own attachment to it, it won't
  191. vanish till we exit (and release it).
  192. */
  193. jack_destroy_shm(&fInfo);
  194. }
  195. }
  196. public:
  197. JackShmReadWritePtr1()
  198. {
  199. fInfo.index = -1;
  200. fInfo.attached_at = NULL;
  201. }
  202. JackShmReadWritePtr1(int index)
  203. {
  204. Init(index);
  205. }
  206. virtual ~JackShmReadWritePtr1()
  207. {
  208. if (fInfo.index >= 0) {
  209. JackLog("JackShmReadWritePtr1::~JackShmReadWritePtr1 %ld\n", fInfo.index);
  210. jack_release_shm(&fInfo);
  211. fInfo.index = -1;
  212. }
  213. }
  214. T* operator->() const
  215. {
  216. return (T*)fInfo.attached_at;
  217. }
  218. operator T*() const
  219. {
  220. return (T*)fInfo.attached_at;
  221. }
  222. JackShmReadWritePtr1& operator=(int index)
  223. {
  224. Init(index);
  225. return *this;
  226. }
  227. int GetShmIndex()
  228. {
  229. return fInfo.index;
  230. }
  231. T* GetShmAddress()
  232. {
  233. return (T*)fInfo.attached_at;
  234. }
  235. };
  236. /*!
  237. \brief Pointer on shared memory segment in the client side.
  238. */
  239. template <class T>
  240. class JackShmReadPtr
  241. {
  242. private:
  243. jack_shm_info_t fInfo;
  244. void Init(int index)
  245. {
  246. if (fInfo.index < 0 && index >= 0) {
  247. JackLog("JackShmPtrRead::Init %ld %ld\n", index, fInfo.index);
  248. if (jack_initialize_shm_client() < 0)
  249. throw - 1;
  250. fInfo.index = index;
  251. if (jack_attach_shm_read(&fInfo)) {
  252. //jack_error("cannot attach shared memory segment", strerror(errno));
  253. throw - 2;
  254. }
  255. }
  256. }
  257. public:
  258. JackShmReadPtr()
  259. {
  260. fInfo.index = -1;
  261. fInfo.attached_at = NULL;
  262. }
  263. JackShmReadPtr(int index)
  264. {
  265. Init(index);
  266. }
  267. virtual ~JackShmReadPtr()
  268. {
  269. if (fInfo.index >= 0) {
  270. JackLog("JackShmPtrRead::~JackShmPtrRead %ld\n", fInfo.index);
  271. jack_release_shm(&fInfo);
  272. fInfo.index = -1;
  273. }
  274. }
  275. T* operator->() const
  276. {
  277. return (T*)fInfo.attached_at;
  278. }
  279. operator T*() const
  280. {
  281. return (T*)fInfo.attached_at;
  282. }
  283. JackShmReadPtr& operator=(int index)
  284. {
  285. Init(index);
  286. return *this;
  287. }
  288. int GetShmIndex()
  289. {
  290. return fInfo.index;
  291. }
  292. T* GetShmAddress()
  293. {
  294. return (T*)fInfo.attached_at;
  295. }
  296. };
  297. } // end of namespace
  298. #endif