26 #ifndef _MMAPSTOREBUFFER_H_ 27 #define _MMAPSTOREBUFFER_H_ 32 #include <sys/types.h> 59 size_t initialSize_ = AMPS_MEMORYBUFFER_DEFAULT_LENGTH)
61 , _mapFile(INVALID_HANDLE_VALUE), _file(INVALID_HANDLE_VALUE)
63 _file = CreateFileA(fileName_.c_str(), GENERIC_READ | GENERIC_WRITE, 0,
64 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
65 if ( _file == INVALID_HANDLE_VALUE )
67 std::ostringstream os;
68 os <<
"Failed to create file " << fileName_ <<
" for MMapStoreBuffer";
71 LARGE_INTEGER liFileSize;
72 if (GetFileSizeEx(_file, &liFileSize) == 0)
74 std::ostringstream os;
75 os <<
"Failure getting file size of " << fileName_ <<
" for MMapStoreBuffer";
79 size_t fileSize = liFileSize.QuadPart;
81 size_t fileSize = liFileSize.LowPart;
83 _setSize( initialSize_ > fileSize ?
84 initialSize_ : fileSize);
88 size_t initialSize_ = AMPS_MEMORYBUFFER_DEFAULT_LENGTH)
91 _fd = ::open(fileName_.c_str(), O_CREAT | O_RDWR, (mode_t)0644);
93 memset(&statBuf, 0,
sizeof(statBuf));
94 if (fstat(_fd, &statBuf) == -1)
96 std::ostringstream os;
97 os <<
"Failed to stat file " << fileName_ <<
" for MMapStoreBuffer";
100 bool recovery = (size_t)statBuf.st_size >= initialSize_;
101 _setSize(recovery ? (
size_t)statBuf.st_size
119 FlushFileBuffers(_file);
120 UnmapViewOfFile(_buffer);
121 CloseHandle(_mapFile);
123 _mapFile = INVALID_HANDLE_VALUE;
124 _file = INVALID_HANDLE_VALUE;
126 munmap(_buffer, _bufferLen);
136 if (_buffer != NULL && _bufferLen)
139 if (!FlushViewOfFile(_buffer, _bufferPos))
141 if (msync(_buffer, _bufferPos, MS_ASYNC) != 0)
144 std::ostringstream os;
145 os <<
"Failed to sync mapped memory; buffer: " << (size_t)_buffer
146 <<
" pos: " << _bufferPos;
157 void _setSize(
size_t newSize_,
bool recovery_ =
false)
164 static const size_t pageSize = getPageSize();
165 size_t sz = (newSize_ + pageSize - 1) / pageSize * pageSize;
167 if (_mapFile != INVALID_HANDLE_VALUE && _mapFile != NULL)
169 FlushFileBuffers(_file);
170 UnmapViewOfFile(_buffer);
171 CloseHandle(_mapFile);
174 _mapFile = CreateFileMapping( _file, NULL, PAGE_READWRITE, (DWORD)((sz >> 32) & 0xffffffff), (DWORD)sz, NULL);
176 _mapFile = CreateFileMapping( _file, NULL, PAGE_READWRITE, 0, (DWORD)sz, NULL);
178 if (_mapFile == INVALID_HANDLE_VALUE || _mapFile == NULL)
182 error(
"Failed to create map of log file");
186 _buffer = (
char*)MapViewOfFile(_mapFile, FILE_MAP_ALL_ACCESS, 0, 0, sz);
189 std::ostringstream os;
190 os <<
"Failed to map log file to memory; buffer: " << (size_t)_buffer <<
" length: " << sz <<
" previous size: " << _bufferLen;
200 if (lseek(_fd, (off_t)sz - 1, SEEK_SET) == -1)
202 std::ostringstream os;
203 os <<
"Seek failed for buffer extension; buffer: " << (size_t)_buffer
204 <<
" length: " << _bufferLen <<
" pos: " << _bufferPos
205 <<
" requested new size: " << newSize_;
210 if (::write(_fd,
"", 1) == -1)
212 std::ostringstream os;
213 os <<
"Failed to grow buffer; buffer: " << (size_t)_buffer <<
" length: " 214 << _bufferLen <<
" pos: " << _bufferPos <<
" requested new size: " 224 result = mmap(0, sz, PROT_WRITE | PROT_READ, MAP_SHARED, _fd, 0);
226 else if (_bufferLen < sz)
229 result = mremap(_buffer, _bufferLen, sz, MREMAP_MAYMOVE);
231 munmap(_buffer, _bufferLen);
232 result = mmap(0, sz, PROT_WRITE | PROT_READ, MAP_SHARED, _fd, 0);
235 if (result == MAP_FAILED || result == NULL)
237 std::ostringstream os;
238 os <<
"Failed to map log file to memory; buffer: " 239 << (size_t)_buffer <<
" length: " << sz
240 <<
" previous size: " << _bufferLen;
247 _buffer = (
char*)result;
257 void error(
const std::string & message)
259 std::ostringstream os;
261 const size_t errorBufferSize = 1024;
262 char errorBuffer[errorBufferSize];
263 memset(errorBuffer, 0, errorBufferSize);
264 strerror_s(errorBuffer, errorBufferSize, errno);
265 os << message <<
". Error is " << errorBuffer;
267 os << message <<
". Error is " << strerror(errno);
269 throw StoreException(os.str());
278 static size_t getPageSize()
280 static size_t pageSize;
284 SYSTEM_INFO SYS_INFO;
285 GetSystemInfo(&SYS_INFO);
286 pageSize = SYS_INFO.dwPageSize;
288 pageSize = (size_t)sysconf(_SC_PAGESIZE);
290 if (pageSize == (
size_t)-1)
303 #endif //_MMAPSTOREBUFFER_H_ virtual void setSize(size_t newSize_)
Set the size for the buffer.
Definition: MMapStoreBuffer.hpp:152
MMapStoreBuffer(const std::string &fileName_, size_t initialSize_=AMPS_MEMORYBUFFER_DEFAULT_LENGTH)
Create an MMapStoreBuffer using fileName_ as the name of the memory mapped file used for storage...
Definition: MMapStoreBuffer.hpp:87
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
Provides AMPS::MemoryStoreBuffer, used by an AMPS::HAClient to store messages in memory.
Definition: ampsplusplus.hpp:103