tc_shm.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #ifndef __TC_SHM_H__
  17. #define __TC_SHM_H__
  18. #include "util/tc_platform.h"
  19. #if TARGET_PLATFORM_LINUX || TARGET_PLATFORM_IOS
  20. #include <unistd.h>
  21. #include <sys/types.h>
  22. #include <sys/shm.h>
  23. #endif
  24. #include "util/tc_ex.h"
  25. namespace tars
  26. {
  27. /////////////////////////////////////////////////
  28. /**
  29. * @file tc_shm.h
  30. * @brief 共享内存封装类.
  31. *
  32. * @author jarodruan@tencent.com
  33. */
  34. /////////////////////////////////////////////////
  35. /**
  36. * @brief 共享内存异常类.
  37. */
  38. struct TC_Shm_Exception : public TC_Exception
  39. {
  40. TC_Shm_Exception(const string &buffer, int err) : TC_Exception(buffer, err){};
  41. ~TC_Shm_Exception() throw() {};
  42. };
  43. #if TARGET_PLATFORM_WINDOWS
  44. typedef int key_t;
  45. typedef HANDLE SHMID;
  46. #else
  47. typedef int SHMID;
  48. #endif
  49. /**
  50. * @brief 共享内存连接类,说明:
  51. * 1 用于连接共享内存, 共享内存的权限是 0666
  52. * 2 _bOwner=false: 析够时不detach共享内存
  53. * 3 _bOwner=true: 析够时detach共享内存
  54. */
  55. class TC_Shm
  56. {
  57. public:
  58. /**
  59. * @brief 构造函数.
  60. *
  61. * @param bOwner 是否拥有共享内存,默认为false
  62. */
  63. TC_Shm(bool bOwner = false) : _bOwner(bOwner),_shmSize(0),_shmKey(0),_bCreate(true), _pshm(NULL) {}
  64. /**
  65. * @brief 构造函数.
  66. *
  67. * @param iShmSize 共享内存大小
  68. * @param iKey 共享内存Key
  69. * @throws TC_Shm_Exception
  70. */
  71. TC_Shm(size_t iShmSize, key_t iKey, bool bOwner = false);
  72. /**
  73. * @brief 析构函数.
  74. */
  75. ~TC_Shm();
  76. /**
  77. * @brief 初始化.
  78. *
  79. * @param iShmSize 共享内存大小
  80. * @param iKey 共享内存Key
  81. * @param bOwner 是否拥有共享内存
  82. * @throws TC_Shm_Exception
  83. * @return Ξ
  84. */
  85. void init(size_t iShmSize, key_t iKey, bool bOwner = false);
  86. /**
  87. * @brief 判断共享内存的类型,生成的共享内存,还是连接上的共享内存
  88. * 如果是生成的共享内存,此时可以根据需要做初始化
  89. *
  90. * @return true,生成共享内存; false, 连接上的共享内存
  91. */
  92. bool iscreate() {return _bCreate;}
  93. /**
  94. * @brief 获取共享内存的指针.
  95. *
  96. * @return void* 共享内存指针
  97. */
  98. void *getPointer() {return _pshm;}
  99. /**
  100. * @brief 获取共享内存Key.
  101. *
  102. * @return key_t* ,共享内存key
  103. */
  104. key_t getkey() {return _shmKey;}
  105. /**
  106. * @brief 获取共享内存ID.
  107. *
  108. * @return int ,共享内存Id
  109. */
  110. SHMID getid() {return _shemID;}
  111. /**
  112. * @brief 获取共享内存大小.
  113. *
  114. * return size_t,共享内存大小
  115. */
  116. size_t size() {return _shmSize;}
  117. /**
  118. * @brief 解除共享内存,在当前进程中解除共享内存
  119. * 共享内存在当前进程中无效
  120. * @return int
  121. */
  122. int detach();
  123. /**
  124. * @brief 删除共享内存.
  125. *
  126. * 完全删除共享内存
  127. */
  128. int del();
  129. protected:
  130. /**
  131. * 是否拥有共享内存
  132. */
  133. bool _bOwner;
  134. /**
  135. * 共享内存大小
  136. */
  137. size_t _shmSize;
  138. /**
  139. * 共享内存key
  140. */
  141. key_t _shmKey;
  142. /**
  143. * 是否是生成的共享内存
  144. */
  145. bool _bCreate;
  146. /**
  147. * 共享内存
  148. */
  149. void *_pshm;
  150. /**
  151. * 共享内存id
  152. */
  153. SHMID _shemID;
  154. };
  155. }
  156. #endif