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.

392 lines
8.8KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2008 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 int fSegmentNum;
  78. static jack_shm_info_t gInfo;
  79. public:
  80. void* operator new(size_t size);
  81. void operator delete(void* p, size_t size);
  82. JackShmMem()
  83. {
  84. fInfo.index = gInfo.index;
  85. fInfo.attached_at = gInfo.attached_at;
  86. fInfo.size = gInfo.size;
  87. }
  88. virtual ~JackShmMem()
  89. {}
  90. int GetShmIndex()
  91. {
  92. return fInfo.index;
  93. }
  94. char* GetShmAddress()
  95. {
  96. return (char*)fInfo.attached_at;
  97. }
  98. void LockMemory()
  99. {
  100. LockMemoryImp(this, fInfo.size);
  101. }
  102. void UnlockMemory()
  103. {
  104. UnlockMemoryImp(this, fInfo.size);
  105. }
  106. };
  107. /*!
  108. \brief Pointer on shared memory segment in the client side.
  109. */
  110. template <class T>
  111. class JackShmReadWritePtr
  112. {
  113. private:
  114. jack_shm_info_t fInfo;
  115. void Init(int index, const char* server_name = "default")
  116. {
  117. if (fInfo.index < 0 && index >= 0) {
  118. JackLog("JackShmReadWritePtr::Init %ld %ld\n", index, fInfo.index);
  119. if (jack_initialize_shm(server_name) < 0)
  120. throw - 1;
  121. fInfo.index = index;
  122. if (jack_attach_shm(&fInfo)) {
  123. //jack_error("cannot attach shared memory segment", strerror(errno));
  124. throw - 2;
  125. }
  126. }
  127. }
  128. public:
  129. JackShmReadWritePtr()
  130. {
  131. fInfo.index = -1;
  132. fInfo.attached_at = NULL;
  133. }
  134. JackShmReadWritePtr(int index, const char* server_name)
  135. {
  136. Init(index, server_name);
  137. }
  138. virtual ~JackShmReadWritePtr()
  139. {
  140. if (fInfo.index >= 0) {
  141. JackLog("JackShmReadWritePtr::~JackShmReadWritePtr %ld\n", fInfo.index);
  142. jack_release_shm(&fInfo);
  143. fInfo.index = -1;
  144. }
  145. }
  146. T* operator->() const
  147. {
  148. return (T*)fInfo.attached_at;
  149. }
  150. operator T*() const
  151. {
  152. return (T*)fInfo.attached_at;
  153. }
  154. JackShmReadWritePtr& operator=(int index)
  155. {
  156. Init(index);
  157. return *this;
  158. }
  159. void SetShmIndex(int index, const char* server_name)
  160. {
  161. Init(index, server_name);
  162. }
  163. int GetShmIndex()
  164. {
  165. return fInfo.index;
  166. }
  167. T* GetShmAddress()
  168. {
  169. return (T*)fInfo.attached_at;
  170. }
  171. };
  172. /*!
  173. \brief Pointer on shared memory segment in the client side: destroy the segment (used client control)
  174. */
  175. template <class T>
  176. class JackShmReadWritePtr1
  177. {
  178. private:
  179. jack_shm_info_t fInfo;
  180. void Init(int index, const char* server_name = "default")
  181. {
  182. if (fInfo.index < 0 && index >= 0) {
  183. JackLog("JackShmReadWritePtr1::Init %ld %ld\n", index, fInfo.index);
  184. if (jack_initialize_shm(server_name) < 0)
  185. throw - 1;
  186. fInfo.index = index;
  187. if (jack_attach_shm(&fInfo)) {
  188. //jack_error("cannot attach shared memory segment", strerror(errno));
  189. throw - 2;
  190. }
  191. /*
  192. nobody else needs to access this shared memory any more, so
  193. destroy it. because we have our own attachment to it, it won't
  194. vanish till we exit (and release it).
  195. */
  196. jack_destroy_shm(&fInfo);
  197. }
  198. }
  199. public:
  200. JackShmReadWritePtr1()
  201. {
  202. fInfo.index = -1;
  203. fInfo.attached_at = NULL;
  204. }
  205. JackShmReadWritePtr1(int index, const char* server_name)
  206. {
  207. Init(index, server_name);
  208. }
  209. virtual ~JackShmReadWritePtr1()
  210. {
  211. if (fInfo.index >= 0) {
  212. JackLog("JackShmReadWritePtr1::~JackShmReadWritePtr1 %ld\n", fInfo.index);
  213. jack_release_shm(&fInfo);
  214. fInfo.index = -1;
  215. }
  216. }
  217. T* operator->() const
  218. {
  219. return (T*)fInfo.attached_at;
  220. }
  221. operator T*() const
  222. {
  223. return (T*)fInfo.attached_at;
  224. }
  225. JackShmReadWritePtr1& operator=(int index)
  226. {
  227. Init(index);
  228. return *this;
  229. }
  230. void SetShmIndex(int index, const char* server_name)
  231. {
  232. Init(index, server_name);
  233. }
  234. int GetShmIndex()
  235. {
  236. return fInfo.index;
  237. }
  238. T* GetShmAddress()
  239. {
  240. return (T*)fInfo.attached_at;
  241. }
  242. };
  243. /*!
  244. \brief Pointer on shared memory segment in the client side.
  245. */
  246. template <class T>
  247. class JackShmReadPtr
  248. {
  249. private:
  250. jack_shm_info_t fInfo;
  251. void Init(int index, const char* server_name = "default")
  252. {
  253. if (fInfo.index < 0 && index >= 0) {
  254. JackLog("JackShmPtrRead::Init %ld %ld\n", index, fInfo.index);
  255. if (jack_initialize_shm(server_name) < 0)
  256. throw - 1;
  257. fInfo.index = index;
  258. if (jack_attach_shm_read(&fInfo)) {
  259. //jack_error("cannot attach shared memory segment", strerror(errno));
  260. throw - 2;
  261. }
  262. }
  263. }
  264. public:
  265. JackShmReadPtr()
  266. {
  267. fInfo.index = -1;
  268. fInfo.attached_at = NULL;
  269. }
  270. JackShmReadPtr(int index, const char* server_name)
  271. {
  272. Init(index, server_name);
  273. }
  274. virtual ~JackShmReadPtr()
  275. {
  276. if (fInfo.index >= 0) {
  277. JackLog("JackShmPtrRead::~JackShmPtrRead %ld\n", fInfo.index);
  278. jack_release_shm(&fInfo);
  279. fInfo.index = -1;
  280. }
  281. }
  282. T* operator->() const
  283. {
  284. return (T*)fInfo.attached_at;
  285. }
  286. operator T*() const
  287. {
  288. return (T*)fInfo.attached_at;
  289. }
  290. JackShmReadPtr& operator=(int index)
  291. {
  292. Init(index);
  293. return *this;
  294. }
  295. void SetShmIndex(int index, const char* server_name)
  296. {
  297. Init(index, server_name);
  298. }
  299. int GetShmIndex()
  300. {
  301. return fInfo.index;
  302. }
  303. T* GetShmAddress()
  304. {
  305. return (T*)fInfo.attached_at;
  306. }
  307. };
  308. } // end of namespace
  309. #endif