26 #ifndef _MMAPSTOREBUFFER_H_ 27 #define _MMAPSTOREBUFFER_H_ 32 #include <sys/types.h> 60 , _mapFile(INVALID_HANDLE_VALUE), _file(INVALID_HANDLE_VALUE)
62 _file = CreateFileA(fileName_.c_str(), GENERIC_READ | GENERIC_WRITE, 0,
63 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
64 if ( _file == INVALID_HANDLE_VALUE )
66 std::ostringstream os;
67 os <<
"Failed to create file " << fileName_ <<
" for MMapStoreBuffer";
70 LARGE_INTEGER liFileSize;
71 if (GetFileSizeEx(_file, &liFileSize) == 0)
73 std::ostringstream os;
74 os <<
"Failure getting file size of " << fileName_ <<
" for MMapStoreBuffer";
78 size_t fileSize = liFileSize.QuadPart;
80 size_t fileSize = liFileSize.LowPart;
82 _setSize(AMPS_MEMORYBUFFER_DEFAULT_LENGTH > fileSize
83 ? AMPS_MEMORYBUFFER_DEFAULT_LENGTH : fileSize);
86 _fd = ::open(fileName_.c_str(), O_CREAT | O_RDWR, (mode_t)0644);
88 memset(&statBuf, 0,
sizeof(statBuf));
89 if (fstat(_fd, &statBuf) == -1)
91 std::ostringstream os;
92 os <<
"Failed to stat file " << fileName_ <<
" for MMapStoreBuffer";
95 bool recovery = statBuf.st_size >= (ssize_t)AMPS_MEMORYBUFFER_DEFAULT_LENGTH;
96 _setSize(recovery ? (
size_t)statBuf.st_size
97 : AMPS_MEMORYBUFFER_DEFAULT_LENGTH,
114 FlushFileBuffers(_file);
115 UnmapViewOfFile(_buffer);
116 CloseHandle(_mapFile);
118 _mapFile = INVALID_HANDLE_VALUE;
119 _file = INVALID_HANDLE_VALUE;
121 munmap(_buffer, _bufferLen);
131 if (_buffer != NULL && _bufferLen)
134 if (!FlushViewOfFile(_buffer, _bufferPos))
136 if (msync(_buffer, _bufferPos, MS_ASYNC) != 0)
139 std::ostringstream os;
140 os <<
"Failed to sync mapped memory; buffer: " << (size_t)_buffer
141 <<
" pos: " << _bufferPos;
152 void _setSize(
size_t newSize_,
bool recovery_ =
false)
159 size_t sz = newSize_ & (size_t)(~(getPageSize() - 1));
165 if (_mapFile != INVALID_HANDLE_VALUE && _mapFile != NULL)
167 FlushFileBuffers(_file);
168 UnmapViewOfFile(_buffer);
169 CloseHandle(_mapFile);
172 _mapFile = CreateFileMapping( _file, NULL, PAGE_READWRITE, (DWORD)((sz >> 32) & 0xffffffff), (DWORD)sz, NULL);
174 _mapFile = CreateFileMapping( _file, NULL, PAGE_READWRITE, 0, (DWORD)sz, NULL);
176 if (_mapFile == INVALID_HANDLE_VALUE || _mapFile == NULL)
180 error(
"Failed to create map of log file");
184 _buffer = (
char*)MapViewOfFile(_mapFile, FILE_MAP_ALL_ACCESS, 0, 0, sz);
187 std::ostringstream os;
188 os <<
"Failed to map log file to memory; buffer: " << (size_t)_buffer <<
" length: " << sz <<
" previous size: " << _bufferLen;
198 if (lseek(_fd, (off_t)sz - 1, SEEK_SET) == -1)
200 std::ostringstream os;
201 os <<
"Seek failed for buffer extension; buffer: " << (size_t)_buffer
202 <<
" length: " << _bufferLen <<
" pos: " << _bufferPos
203 <<
" requested new size: " << newSize_;
208 if (::write(_fd,
"", 1) == -1)
210 std::ostringstream os;
211 os <<
"Failed to grow buffer; buffer: " << (size_t)_buffer <<
" length: " 212 << _bufferLen <<
" pos: " << _bufferPos <<
" requested new size: " 222 result = mmap(0, sz, PROT_WRITE | PROT_READ, MAP_SHARED, _fd, 0);
224 else if (_bufferLen < sz)
227 result = mremap(_buffer, _bufferLen, sz, MREMAP_MAYMOVE);
229 munmap(_buffer, _bufferLen);
230 result = mmap(0, sz, PROT_WRITE | PROT_READ, MAP_SHARED, _fd, 0);
233 if (result == MAP_FAILED || result == NULL)
235 std::ostringstream os;
236 os <<
"Failed to map log file to memory; buffer: " 237 << (size_t)_buffer <<
" length: " << sz
238 <<
" previous size: " << _bufferLen;
245 _buffer = (
char*)result;
255 void error(
const std::string & message)
257 std::ostringstream os;
259 const size_t errorBufferSize = 1024;
260 char errorBuffer[errorBufferSize];
261 memset(errorBuffer, 0, errorBufferSize);
262 strerror_s(errorBuffer, errorBufferSize, errno);
263 os << message <<
". Error is " << errorBuffer;
265 os << message <<
". Error is " << strerror(errno);
267 throw StoreException(os.str());
276 static size_t getPageSize()
278 static size_t pageSize;
282 SYSTEM_INFO SYS_INFO;
283 GetSystemInfo(&SYS_INFO);
284 pageSize = SYS_INFO.dwPageSize;
286 pageSize = (size_t)sysconf(_SC_PAGESIZE);
296 #endif //_MMAPSTOREBUFFER_H_ virtual void setSize(size_t newSize_)
Set the size for the buffer.
Definition: MMapStoreBuffer.hpp:147
A Buffer implementation that uses memory for storage.
Definition: MemoryStoreBuffer.hpp:50
Core type, function, and class declarations for the AMPS C++ client.
A Buffer implementation that uses a memory mapped file as its storage.
Definition: MMapStoreBuffer.hpp:49
MMapStoreBuffer(const std::string &fileName_)
Create an MMapStoreBuffer using fileName_ as the name of the memory mapped file used for storage...
Definition: MMapStoreBuffer.hpp:57
Provides AMPS::MemoryStoreBuffer, used by an AMPS::HAClient to store messages in memory.
Definition: ampsplusplus.hpp:102