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.4KB

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