tc_mmap.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #include "util/tc_mmap.h"
  17. #if TARGET_PLATFORM_LINUX || TARGET_PLATFORM_IOS
  18. #include <sys/types.h>
  19. #include <unistd.h>
  20. #include <sys/stat.h>
  21. #include <fcntl.h>
  22. #endif
  23. #include <cerrno>
  24. #include <cassert>
  25. #include <iostream>
  26. #include "util/tc_file.h"
  27. namespace tars
  28. {
  29. TC_Mmap::TC_Mmap(bool bOwner)
  30. : _bOwner(bOwner)
  31. , _pAddr(NULL)
  32. , _iLength(0)
  33. , _bCreate(false)
  34. {
  35. #if TARGET_PLATFORM_WINDOWS
  36. _hFile = NULL;
  37. _hMap = NULL;
  38. #endif
  39. }
  40. TC_Mmap::~TC_Mmap()
  41. {
  42. if(_bOwner)
  43. {
  44. munmap();
  45. }
  46. }
  47. void TC_Mmap::mmap(const char *file, size_t length)
  48. {
  49. assert(length > 0);
  50. if(_bOwner)
  51. {
  52. munmap();
  53. }
  54. #if TARGET_PLATFORM_WINDOWS
  55. if(_hFile != NULL)
  56. {
  57. CloseHandle(_hFile);
  58. }
  59. if(_hMap != NULL)
  60. {
  61. CloseHandle(_hMap);
  62. }
  63. _hFile = CreateFile(file, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ| FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_FLAG_SEQUENTIAL_SCAN|FILE_FLAG_WRITE_THROUGH|FILE_FLAG_NO_BUFFERING, NULL);
  64. if(_hFile == INVALID_HANDLE_VALUE)
  65. {
  66. THROW_EXCEPTION_SYSCODE(TC_Mmap_Exception, "[TC_Mmap::mmap] fopen file '" + string(file) + "' error");
  67. }
  68. _hMap = CreateFileMapping(_hFile, NULL, PAGE_READWRITE, 0, length, NULL);
  69. if (_hMap != NULL)
  70. {
  71. if (GetLastError() == ERROR_ALREADY_EXISTS)
  72. _bCreate = false;
  73. else
  74. _bCreate = true;
  75. }
  76. else
  77. {
  78. THROW_EXCEPTION_SYSCODE(TC_Mmap_Exception, "[TC_Mmap::mmap] fopen file '" + string(file) + "' error");
  79. }
  80. _pAddr = (char*)MapViewOfFile(_hMap, FILE_MAP_ALL_ACCESS, 0, 0, length);
  81. // void *pAddr = MapViewOfFile(_hMap, FILE_MAP_ALL_ACCESS, 0, 0, length);
  82. if (_pAddr == NULL)
  83. {
  84. CloseHandle(_hFile);
  85. CloseHandle(_hMap);
  86. THROW_EXCEPTION_SYSCODE(TC_Mmap_Exception, "[TC_Mmap::mmap] fopen file '" + string(file) + "' error");
  87. }
  88. #else
  89. //注意_bCreate的赋值位置:保证多线程用一个对象的时候也不会有问题
  90. int fd = open(file, O_CREAT|O_EXCL|O_RDWR, 0666);
  91. if(fd == -1)
  92. {
  93. if(errno != EEXIST)
  94. {
  95. THROW_EXCEPTION_SYSCODE(TC_Mmap_Exception, "[TC_Mmap::mmap] fopen file '" + string(file) + "' error");
  96. }
  97. else
  98. {
  99. fd = open(file, O_CREAT|O_RDWR, 0666);
  100. if(fd == -1)
  101. {
  102. THROW_EXCEPTION_SYSCODE(TC_Mmap_Exception, "[TC_Mmap::mmap] fopen file '" + string(file) + "' error");
  103. }
  104. _bCreate = false;
  105. }
  106. }
  107. else
  108. {
  109. _bCreate = true;
  110. }
  111. if(_bCreate)
  112. {
  113. //避免空洞文件
  114. lseek(fd, length-1, SEEK_SET);
  115. write(fd,"\0",1);
  116. }
  117. _pAddr = static_cast<char*>(::mmap(NULL, length, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0));
  118. if(_pAddr == (void*)-1)
  119. {
  120. _pAddr = NULL;
  121. close(fd);
  122. THROW_EXCEPTION_SYSCODE(TC_Mmap_Exception, "[TC_Mmap::mmap] mmap file '" + string(file) + "' error");
  123. }
  124. if(fd != -1)
  125. {
  126. close(fd);
  127. }
  128. #endif
  129. _iLength = length;
  130. if(_bCreate)
  131. {
  132. _pAddr[length-1] = '\0';
  133. }
  134. }
  135. void TC_Mmap::munmap()
  136. {
  137. if(_pAddr == NULL)
  138. {
  139. return;
  140. }
  141. #if TARGET_PLATFORM_WINDOWS
  142. BOOL ret = FlushViewOfFile(_pAddr, 0);
  143. if(!ret)
  144. {
  145. THROW_EXCEPTION_SYSCODE(TC_Mmap_Exception, "[TC_Mmap::munmap] munmap error");
  146. }
  147. UnmapViewOfFile(_pAddr);
  148. CloseHandle(_hFile);
  149. _hFile = NULL;
  150. CloseHandle(_hMap);
  151. _hMap = NULL;
  152. #else
  153. int ret = ::munmap(_pAddr, _iLength);
  154. if(ret == -1)
  155. {
  156. THROW_EXCEPTION_SYSCODE(TC_Mmap_Exception, "[TC_Mmap::munmap] munmap error");
  157. }
  158. #endif
  159. _pAddr = NULL;
  160. _iLength = 0;
  161. _bCreate = false;
  162. }
  163. void TC_Mmap::msync(bool bSync)
  164. {
  165. int ret = 0;
  166. #if TARGET_PLATFORM_WINDOWS
  167. FlushViewOfFile(_pAddr, _iLength);
  168. #else
  169. if(bSync)
  170. {
  171. ret = ::msync(_pAddr, _iLength, MS_SYNC | MS_INVALIDATE);
  172. }
  173. else
  174. {
  175. ret = ::msync(_pAddr, _iLength, MS_ASYNC | MS_INVALIDATE);
  176. }
  177. #endif
  178. if(ret != 0)
  179. {
  180. THROW_EXCEPTION_SYSCODE(TC_Mmap_Exception, "[TC_Mmap::msync] msync error");
  181. }
  182. }
  183. }