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.

381 lines
8.9KB

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