/// by calling shm_unlink() (on POSIX platforms) when the SharedMemory object is destroyed.
/// The server parameter is ignored on Windows platforms.
- SharedMemory(const File& file, AccessMode mode, const void* addrHint = 0);
+ SharedMemory(const File& file, AccessMode mode, const void* addrHint = 0, std::size_t size = 0);
/// Maps the entire contents of file into a shared memory segment.
///
/// An address hint can be passed to the system, specifying the desired
/// is actually honored is, however, up to the system. Windows platform
/// will generally ignore the hint.
- SharedMemoryImpl(const Poco::File& aFile, SharedMemory::AccessMode mode, const void* addr);
+ SharedMemoryImpl(const Poco::File& aFile, SharedMemory::AccessMode mode, const void* addr = NULL, std::size_t size = 0);
/// Maps the entire contents of file into a shared memory segment.
///
/// An address hint can be passed to the system, specifying the desired
/// If server is set to false, the shared memory region will be unlinked
/// by calling shm_unlink when the SharedMemory object is destroyed.
- SharedMemoryImpl(const Poco::File& file, SharedMemory::AccessMode mode, const void* addrHint);
+ SharedMemoryImpl(const Poco::File& file, SharedMemory::AccessMode mode, const void* addrHint = NULL, std::size_t size = 0);
/// Maps the entire contents of file into a shared memory segment.
///
/// An address hint can be passed to the system, specifying the desired
/// is actually honored is, however, up to the system. Windows platform
/// will generally ignore the hint.
- SharedMemoryImpl(const Poco::File& file, SharedMemory::AccessMode mode, const void* addrHint);
+ SharedMemoryImpl(const Poco::File& file, SharedMemory::AccessMode mode, const void* addrHint = NULL, std::size_t size = 0);
/// Maps the entire contents of file into a shared memory segment.
///
/// An address hint can be passed to the system, specifying the desired
}
-SharedMemory::SharedMemory(const Poco::File& file, AccessMode mode, const void* addrHint):
- _pImpl(new SharedMemoryImpl(file, mode, addrHint))
+SharedMemory::SharedMemory(const Poco::File& file, AccessMode mode, const void* addrHint, std::size_t size):
+ _pImpl(new SharedMemoryImpl(file, mode, addrHint, size))
{
}
}
-SharedMemoryImpl::SharedMemoryImpl(const Poco::File&, SharedMemory::AccessMode, const void*)
+SharedMemoryImpl::SharedMemoryImpl(const Poco::File&, SharedMemory::AccessMode, const void*, std::size_t size)
{
}
}
-SharedMemoryImpl::SharedMemoryImpl(const Poco::File& file, SharedMemory::AccessMode mode, const void* addrHint):
- _size(0),
+SharedMemoryImpl::SharedMemoryImpl(const Poco::File& file, SharedMemory::AccessMode mode, const void* addrHint, std::size_t size):
+ _size(size),
_fd(-1),
_address(0),
_access(mode),
if (!file.exists() || !file.isFile())
throw FileNotFoundException(file.path());
- _size = file.getSize();
+ size_t filesize = file.getSize();
+ if (_size == 0 || filesize < _size)
+ _size = filesize;
int flag = O_RDONLY;
if (mode == SharedMemory::AM_WRITE)
flag = O_RDWR;
}
-SharedMemoryImpl::SharedMemoryImpl(const Poco::File& file, SharedMemory::AccessMode mode, const void*):
+SharedMemoryImpl::SharedMemoryImpl(const Poco::File& file, SharedMemory::AccessMode mode, const void*, std::size_t size):
_name(file.path()),
_memHandle(INVALID_HANDLE_VALUE),
_fileHandle(INVALID_HANDLE_VALUE),
- _size(0),
+ _size(size),
_mode(PAGE_READONLY),
_address(0)
{
- if (!file.exists() || !file.isFile())
- throw FileNotFoundException(_name);
-
- _size = static_cast<DWORD>(file.getSize());
-
DWORD shareMode = FILE_SHARE_READ;
DWORD fileMode = GENERIC_READ;
#endif
if (_fileHandle == INVALID_HANDLE_VALUE)
+ {
+ DWORD dwError = GetLastError();
+ if (dwError == ERROR_FILE_NOT_FOUND || dwError == ERROR_PATH_NOT_FOUND)
+ throw FileNotFoundException(_name);
throw OpenFileException("Cannot open memory mapped file", _name);
+ }
- _memHandle = CreateFileMapping(_fileHandle, NULL, _mode, 0, 0, NULL);
+ LARGE_INTEGER liFileSize = {0};
+ GetFileSizeEx(_fileHandle, &liFileSize);
+ if (_size == 0 || _size > liFileSize.QuadPart)
+ _size = liFileSize.QuadPart;
+ else
+ liFileSize.QuadPart = _size;
+ _memHandle = CreateFileMapping(_fileHandle, NULL, _mode, liFileSize.HighPart, liFileSize.LowPart, NULL);
if (!_memHandle)
{
CloseHandle(_fileHandle);
try
{
TFile file(path);
-
- cbImage = (size_t)file.getSize();
-
- if (trunc && cbImage > trunc)
- {
- cbImage = trunc;
- }
- m_pSharedMemory = new SharedMemory(file, SharedMemory::AM_WRITE);
+ m_pSharedMemory = new SharedMemory(file, SharedMemory::AM_READ, 0, trunc);
pImage = m_pSharedMemory->begin();
+ cbImage = m_pSharedMemory->end() - m_pSharedMemory->begin();
}
catch (...)
{
if (pCopy)
{
for (int i = 0; i < cbImage / 2; ++i)
- *((UInt16 *)pImage + i) = Poco::ByteOrder::flipBytes(*((UInt16 *)pCopy + i));
+ *((UInt16 *)pCopy + i) = Poco::ByteOrder::flipBytes(*((UInt16 *)pImage + i));
}
delete m_pSharedMemory;
if (pCopy)
{
for (int i = 0; i < cbImage / 2; ++i)
- *((UInt16 *)pImage + i) = Poco::ByteOrder::flipBytes(*((UInt16 *)pCopy + i));
+ *((UInt16 *)pCopy + i) = Poco::ByteOrder::flipBytes(*((UInt16 *)pImage + i));
}
delete m_pSharedMemory;
m_pSharedMemory = NULL;