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.

309 lines
6.8KB

  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 <new> // GCC 4.0
  20. #include "JackError.h"
  21. #include <errno.h>
  22. namespace Jack
  23. {
  24. /*!
  25. \brief The base class for shared memory management.
  26. A class which objects need to be allocated in shared memory derives from this class.
  27. */
  28. class JackShmMem
  29. {
  30. private:
  31. jack_shm_info_t fInfo;
  32. static unsigned long fSegmentNum;
  33. static unsigned long fSegmentCount;
  34. static jack_shm_info_t gInfo;
  35. public:
  36. void* operator new(size_t size);
  37. void operator delete(void* p, size_t size);
  38. JackShmMem()
  39. {
  40. fInfo.index = gInfo.index;
  41. fInfo.attached_at = gInfo.attached_at;
  42. }
  43. virtual ~JackShmMem()
  44. {}
  45. int GetShmIndex()
  46. {
  47. return fInfo.index;
  48. }
  49. char* GetShmAddress()
  50. {
  51. return (char*)fInfo.attached_at;
  52. }
  53. };
  54. /*!
  55. \brief Pointer on shared memory segment in the client side.
  56. */
  57. template <class T>
  58. class JackShmReadWritePtr
  59. {
  60. private:
  61. jack_shm_info_t fInfo;
  62. void Init(int index)
  63. {
  64. if (fInfo.index < 0 && index >= 0) {
  65. JackLog("JackShmReadWritePtr::Init %ld %ld\n", index, fInfo.index);
  66. if (jack_initialize_shm_client() < 0)
  67. throw - 1;
  68. fInfo.index = index;
  69. if (jack_attach_shm(&fInfo)) {
  70. //jack_error("cannot attach shared memory segment", strerror(errno));
  71. throw - 2;
  72. }
  73. }
  74. }
  75. public:
  76. JackShmReadWritePtr()
  77. {
  78. fInfo.index = -1;
  79. fInfo.attached_at = NULL;
  80. }
  81. JackShmReadWritePtr(int index)
  82. {
  83. Init(index);
  84. }
  85. virtual ~JackShmReadWritePtr()
  86. {
  87. if (fInfo.index >= 0) {
  88. JackLog("JackShmReadWritePtr::~JackShmReadWritePtr %ld\n", fInfo.index);
  89. jack_release_shm(&fInfo);
  90. fInfo.index = -1;
  91. }
  92. }
  93. T* operator->() const
  94. {
  95. return (T*)fInfo.attached_at;
  96. }
  97. operator T*() const
  98. {
  99. return (T*)fInfo.attached_at;
  100. }
  101. JackShmReadWritePtr& operator=(int index)
  102. {
  103. Init(index);
  104. return *this;
  105. }
  106. int GetShmIndex()
  107. {
  108. return fInfo.index;
  109. }
  110. T* GetShmAddress()
  111. {
  112. return (T*)fInfo.attached_at;
  113. }
  114. };
  115. /*!
  116. \brief Pointer on shared memory segment in the client side: destroy the segment (used client control)
  117. */
  118. template <class T>
  119. class JackShmReadWritePtr1
  120. {
  121. private:
  122. jack_shm_info_t fInfo;
  123. void Init(int index)
  124. {
  125. if (fInfo.index < 0 && index >= 0) {
  126. JackLog("JackShmReadWritePtr1::Init %ld %ld\n", index, fInfo.index);
  127. if (jack_initialize_shm_client() < 0)
  128. throw - 1;
  129. fInfo.index = index;
  130. if (jack_attach_shm(&fInfo)) {
  131. //jack_error("cannot attach shared memory segment", strerror(errno));
  132. throw - 2;
  133. }
  134. /*
  135. nobody else needs to access this shared memory any more, so
  136. destroy it. because we have our own attachment to it, it won't
  137. vanish till we exit (and release it).
  138. */
  139. jack_destroy_shm(&fInfo);
  140. }
  141. }
  142. public:
  143. JackShmReadWritePtr1()
  144. {
  145. fInfo.index = -1;
  146. fInfo.attached_at = NULL;
  147. }
  148. JackShmReadWritePtr1(int index)
  149. {
  150. Init(index);
  151. }
  152. virtual ~JackShmReadWritePtr1()
  153. {
  154. if (fInfo.index >= 0) {
  155. JackLog("JackShmReadWritePtr1::~JackShmReadWritePtr1 %ld\n", fInfo.index);
  156. jack_release_shm(&fInfo);
  157. fInfo.index = -1;
  158. }
  159. }
  160. T* operator->() const
  161. {
  162. return (T*)fInfo.attached_at;
  163. }
  164. operator T*() const
  165. {
  166. return (T*)fInfo.attached_at;
  167. }
  168. JackShmReadWritePtr1& operator=(int index)
  169. {
  170. Init(index);
  171. return *this;
  172. }
  173. int GetShmIndex()
  174. {
  175. return fInfo.index;
  176. }
  177. T* GetShmAddress()
  178. {
  179. return (T*)fInfo.attached_at;
  180. }
  181. };
  182. /*!
  183. \brief Pointer on shared memory segment in the client side.
  184. */
  185. template <class T>
  186. class JackShmReadPtr
  187. {
  188. private:
  189. jack_shm_info_t fInfo;
  190. void Init(int index)
  191. {
  192. if (fInfo.index < 0 && index >= 0) {
  193. JackLog("JackShmPtrRead::Init %ld %ld\n", index, fInfo.index);
  194. if (jack_initialize_shm_client() < 0)
  195. throw - 1;
  196. fInfo.index = index;
  197. if (jack_attach_shm_read(&fInfo)) {
  198. //jack_error("cannot attach shared memory segment", strerror(errno));
  199. throw - 2;
  200. }
  201. }
  202. }
  203. public:
  204. JackShmReadPtr()
  205. {
  206. fInfo.index = -1;
  207. fInfo.attached_at = NULL;
  208. }
  209. JackShmReadPtr(int index)
  210. {
  211. Init(index);
  212. }
  213. virtual ~JackShmReadPtr()
  214. {
  215. if (fInfo.index >= 0) {
  216. JackLog("JackShmPtrRead::~JackShmPtrRead %ld\n", fInfo.index);
  217. jack_release_shm(&fInfo);
  218. fInfo.index = -1;
  219. }
  220. }
  221. T* operator->() const
  222. {
  223. return (T*)fInfo.attached_at;
  224. }
  225. operator T*() const
  226. {
  227. return (T*)fInfo.attached_at;
  228. }
  229. JackShmReadPtr& operator=(int index)
  230. {
  231. Init(index);
  232. return *this;
  233. }
  234. int GetShmIndex()
  235. {
  236. return fInfo.index;
  237. }
  238. T* GetShmAddress()
  239. {
  240. return (T*)fInfo.attached_at;
  241. }
  242. };
  243. } // end of namespace
  244. #endif