OSDN Git Service

Add size parameter to Poco::SharedMemory() constructor for markdown.cpp performance
authorsdottaka <none@none>
Mon, 28 Jan 2013 01:39:49 +0000 (17:39 -0800)
committersdottaka <none@none>
Mon, 28 Jan 2013 01:39:49 +0000 (17:39 -0800)
Externals/poco/Foundation/include/Poco/SharedMemory.h
Externals/poco/Foundation/include/Poco/SharedMemory_DUMMY.h
Externals/poco/Foundation/include/Poco/SharedMemory_POSIX.h
Externals/poco/Foundation/include/Poco/SharedMemory_WIN32.h
Externals/poco/Foundation/src/SharedMemory.cpp
Externals/poco/Foundation/src/SharedMemory_DUMMY.cpp
Externals/poco/Foundation/src/SharedMemory_POSIX.cpp
Externals/poco/Foundation/src/SharedMemory_WIN32.cpp
Src/markdown.cpp

index dcb4fc2..7c5e36d 100644 (file)
@@ -86,7 +86,7 @@ public:
                /// 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
index d363a37..ec0fe28 100644 (file)
@@ -64,7 +64,7 @@ public:
                /// 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
index 91a8d73..0ff8e2a 100644 (file)
@@ -66,7 +66,7 @@ public:
                /// 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
index f4cc4ea..c3d08b4 100644 (file)
@@ -63,7 +63,7 @@ public:
                /// 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
index 4fb8a8a..7778390 100644 (file)
@@ -68,8 +68,8 @@ SharedMemory::SharedMemory(const std::string& name, std::size_t size, AccessMode
 }
 
 
-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))
 {
 }
 
index 881444f..8a304c7 100644 (file)
@@ -45,7 +45,7 @@ SharedMemoryImpl::SharedMemoryImpl(const std::string&, std::size_t, SharedMemory
 }
 
 
-SharedMemoryImpl::SharedMemoryImpl(const Poco::File&, SharedMemory::AccessMode, const void*)
+SharedMemoryImpl::SharedMemoryImpl(const Poco::File&, SharedMemory::AccessMode, const void*, std::size_t size)
 {
 }
 
index 77395f1..cda474f 100644 (file)
@@ -85,8 +85,8 @@ SharedMemoryImpl::SharedMemoryImpl(const std::string& name, std::size_t size, Sh
 }
 
 
-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),
@@ -97,7 +97,9 @@ SharedMemoryImpl::SharedMemoryImpl(const Poco::File& file, SharedMemory::AccessM
        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;
index cc11165..4f9b465 100644 (file)
@@ -72,19 +72,14 @@ SharedMemoryImpl::SharedMemoryImpl(const std::string& name, std::size_t size, Sh
 }
 
 
-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;
 
@@ -101,9 +96,20 @@ SharedMemoryImpl::SharedMemoryImpl(const Poco::File& file, SharedMemory::AccessM
 #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);
index 62c92a5..2387859 100644 (file)
@@ -776,15 +776,9 @@ CMarkdown::FileImage::FileImage(const TCHAR *path, size_t trunc, unsigned flags)
                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 (...)
                {
@@ -806,7 +800,7 @@ CMarkdown::FileImage::FileImage(const TCHAR *path, size_t trunc, unsigned flags)
                        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;
@@ -847,7 +841,7 @@ CMarkdown::FileImage::FileImage(const TCHAR *path, size_t trunc, unsigned flags)
                        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;