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.

396 lines
8.6KB

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