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 _bufferLen = (size_t)statBuf.st_size;
96 _setSize(statBuf.st_size > (ssize_t)AMPS_MEMORYBUFFER_DEFAULT_LENGTH
97 ? (size_t)statBuf.st_size : AMPS_MEMORYBUFFER_DEFAULT_LENGTH);
113 FlushFileBuffers(_file);
114 UnmapViewOfFile(_buffer);
115 CloseHandle(_mapFile);
117 _mapFile = INVALID_HANDLE_VALUE;
118 _file = INVALID_HANDLE_VALUE;
120 munmap(_buffer, _bufferLen);
133 if (!FlushViewOfFile(_buffer, _bufferPos))
135 if (msync(_buffer, _bufferPos, MS_ASYNC) != 0)
138 std::ostringstream os;
139 os <<
"Failed to sync mapped memory; buffer: " << (size_t)_buffer
140 <<
" pos: " << _bufferPos;
151 void _setSize(
size_t newSize_)
158 size_t sz = newSize_ & (size_t)(~(getPageSize() - 1));
164 if (_mapFile != INVALID_HANDLE_VALUE && _mapFile != NULL)
166 FlushFileBuffers(_file);
167 UnmapViewOfFile(_buffer);
168 CloseHandle(_mapFile);
171 _mapFile = CreateFileMapping( _file, NULL, PAGE_READWRITE, (DWORD)((sz >> 32) & 0xffffffff), (DWORD)sz, NULL);
173 _mapFile = CreateFileMapping( _file, NULL, PAGE_READWRITE, 0, (DWORD)sz, NULL);
175 if (_mapFile == INVALID_HANDLE_VALUE || _mapFile == NULL)
179 error(
"Failed to create map of log file");
183 _buffer = (
char*)MapViewOfFile(_mapFile, FILE_MAP_ALL_ACCESS, 0, 0, sz);
186 std::ostringstream os;
187 os <<
"Failed to map log file to memory; buffer: " << (size_t)_buffer <<
" length: " << sz <<
" previous size: " << _bufferLen;
197 if (lseek(_fd, (off_t)sz - 1, SEEK_SET) == -1)
199 std::ostringstream os;
200 os <<
"Seek failed for buffer extension; buffer: " << (size_t)_buffer
201 <<
" length: " << _bufferLen <<
" pos: " << _bufferPos
202 <<
" requested new size: " << newSize_;
205 if (::write(_fd,
"", 1) == -1)
207 std::ostringstream os;
208 os <<
"Failed to grow buffer; buffer: " << (size_t)_buffer <<
" length: " 209 << _bufferLen <<
" pos: " << _bufferPos <<
" requested new size: " 218 result = mmap(0, sz, PROT_WRITE | PROT_READ, MAP_SHARED, _fd, 0);
220 else if (_bufferLen < sz)
223 result = mremap(_buffer, _bufferLen, sz, MREMAP_MAYMOVE);
225 munmap(_buffer, _bufferLen);
226 result = mmap(0, sz, PROT_WRITE | PROT_READ, MAP_SHARED, _fd, 0);
229 if (result == MAP_FAILED || result == NULL)
231 std::ostringstream os;
232 os <<
"Failed to map log file to memory; buffer: " 233 << (size_t)_buffer <<
" length: " << sz
234 <<
" previous size: " << _bufferLen;
239 _buffer = (
char*)result;
248 void error(
const std::string & message)
250 std::ostringstream os;
252 const size_t errorBufferSize = 1024;
253 char errorBuffer[errorBufferSize];
254 memset(errorBuffer, 0, errorBufferSize);
255 strerror_s(errorBuffer, errorBufferSize, errno);
256 os << message <<
". Error is " << errorBuffer;
258 os << message <<
". Error is " << strerror(errno);
260 throw StoreException(os.str());
269 static size_t getPageSize()
271 static size_t pageSize;
275 SYSTEM_INFO SYS_INFO;
276 GetSystemInfo(&SYS_INFO);
277 pageSize = SYS_INFO.dwPageSize;
279 pageSize = (size_t)sysconf(_SC_PAGESIZE);
289 #endif //_MMAPSTOREBUFFER_H_ virtual void setSize(size_t newSize_)
Set the size for the buffer.
Definition: MMapStoreBuffer.hpp:146
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:106