tc_mmap.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_MMAP_H
  17. #define __TC_MMAP_H
  18. #include "util/tc_platform.h"
  19. #if TARGET_PLATFORM_LINUX || TARGET_PLATFORM_IOS
  20. #include <sys/mman.h>
  21. #endif
  22. #include <string>
  23. #include "util/tc_ex.h"
  24. using namespace std;
  25. namespace tars
  26. {
  27. /////////////////////////////////////////////////
  28. /**
  29. * @file tc_mmap.h
  30. * @brief mmap封装类.
  31. * windows下采用文件映射实现(似乎不回会写文件!!??)
  32. *
  33. * @author jarodruan@tencent.com
  34. */
  35. /////////////////////////////////////////////////
  36. /**
  37. * @brief MMap异常
  38. */
  39. struct TC_Mmap_Exception : public TC_Exception
  40. {
  41. TC_Mmap_Exception(const string &buffer) : TC_Exception(buffer){};
  42. TC_Mmap_Exception(const string &buffer, int err) : TC_Exception(buffer, err){};
  43. ~TC_Mmap_Exception() throw() {};
  44. };
  45. /**
  46. * @brief mmap的操作类.
  47. *
  48. * 说明:
  49. * 1: 创建map时,文件一定需要有length的长度, 否则可能导致越界
  50. * 2: 2中说的情况一般通过产生空洞文件避免,int mmap(const char *file, size_t length);
  51. *
  52. * 实现了类似的封装, 推荐直接使用
  53. */
  54. class TC_Mmap
  55. {
  56. public:
  57. /**
  58. * @brief 构造函数.
  59. *
  60. * @param bOwner, 如果拥有, 则析够的时候unmap
  61. */
  62. TC_Mmap(bool bOwner = true);
  63. /**
  64. * @brief 析够
  65. */
  66. ~TC_Mmap();
  67. /**
  68. * @brief 映射到进程空间,采用:
  69. *
  70. * PROT_READ|PROT_WRITE,MAP_SHARED方式
  71. *
  72. * 注意的文件大小会比length大一个字节(初始化时生成空洞文件的原因)
  73. * @param file 文件名
  74. * @param length 映射文件的长度
  75. * @throws TC_Mmap_Exception
  76. * @return
  77. */
  78. void mmap(const char *file, size_t length);
  79. /**
  80. * @brief 解除映射关系, 解除后不能在访问这段空间了.
  81. *
  82. * @throws TC_Mmap_Exception
  83. * @return
  84. */
  85. void munmap();
  86. /**
  87. * @brief 把共享内存中的改变写回磁盘中.
  88. *
  89. * @param bSync true:同步写回, false:异步写回
  90. * @throws TC_Mmap_Exception
  91. * @return
  92. */
  93. void msync(bool bSync = false);
  94. /**
  95. * @brief 获取映射的指针地址.
  96. *
  97. * @return char* 映射的指针地址
  98. */
  99. char *getPointer() const { return _pAddr; }
  100. /**
  101. * @brief 获取映射的空间大小.
  102. *
  103. * @return size_t 映射的空间大小
  104. */
  105. size_t getSize() const { return _iLength; }
  106. /**
  107. * @brief 是否创建出来的,文件的存在可分为两种形式:
  108. *
  109. * 一种是创建的,一种是开始就存在的
  110. * @return 如果是创建出来的就返回true,否则返回false
  111. */
  112. bool iscreate() const { return _bCreate; }
  113. /**
  114. * @brief 设置是否拥有.
  115. *
  116. * @param bOwner ture or fale
  117. */
  118. void setOwner(bool bOwner) { _bOwner = bOwner; }
  119. protected:
  120. /**
  121. * 是否拥有
  122. */
  123. bool _bOwner;
  124. /**
  125. * 映射到进程空间的地址
  126. */
  127. char *_pAddr;
  128. /**
  129. * 映射的空间大小
  130. */
  131. size_t _iLength;
  132. /**
  133. * 是否创建出来的
  134. */
  135. bool _bCreate;
  136. #if TARGET_PLATFORM_WINDOWS
  137. HANDLE _hFile;
  138. HANDLE _hMap;
  139. #endif
  140. };
  141. }
  142. #endif