23#include <osl/diagnose.h>
25#include <osl/process.h>
26#include <rtl/ustring.hxx>
45 OSL_PRECOND(!(nOffset ==
STORE_PAGE_NULL),
"store::ILockBytes::readPageAt(): invalid Offset");
55 PageData const * pagedata = rPage.get();
56 OSL_PRECOND(!(pagedata ==
nullptr),
"store::ILockBytes::writePageAt(): invalid Page");
57 if (pagedata ==
nullptr)
60 sal_uInt32
const offset = pagedata->
location();
61 OSL_PRECOND(!(nOffset != offset),
"store::ILockBytes::writePageAt(): inconsistent Offset");
62 if (nOffset != offset)
65 OSL_PRECOND(!(nOffset ==
STORE_PAGE_NULL),
"store::ILockBytes::writePageAt(): invalid Offset");
76 if (dst_lo ==
nullptr)
83 OSL_PRECOND(!(nOffset ==
STORE_PAGE_NULL),
"store::ILockBytes::readAt(): invalid Offset");
87 sal_uInt64
const src_size =
static_cast<sal_uInt64
>(nOffset) + nBytes;
98 if (src_lo ==
nullptr)
101 sal_uInt8 const * src_hi = src_lo + nBytes;
102 if (src_lo >= src_hi)
105 OSL_PRECOND(!(nOffset ==
STORE_PAGE_NULL),
"store::ILockBytes::writeAt(): invalid Offset");
109 sal_uInt64
const dst_size =
static_cast<sal_uInt64
>(nOffset) + nBytes;
143 bool operator != (FileHandle
const & rhs)
148 static storeError errorFromNative (oslFileError eErrno)
152 case osl_File_E_None:
155 case osl_File_E_NOENT:
158 case osl_File_E_ACCES:
159 case osl_File_E_PERM:
162 case osl_File_E_AGAIN:
163 case osl_File_E_DEADLK:
166 case osl_File_E_BADF:
169 case osl_File_E_INVAL:
172 case osl_File_E_NOMEM:
175 case osl_File_E_NOSPC:
178 case osl_File_E_OVERFLOW:
188 sal_uInt32 nFlags = 0;
192 nFlags |= osl_File_OpenFlag_Create;
195 nFlags |= osl_File_OpenFlag_Write;
198 nFlags |= osl_File_OpenFlag_Read;
201 OSL_PRECOND(
false,
"store::FileHandle: unknown storeAccessMode");
209 sal_uInt32 nFlags = modeToNative (eAccessMode);
210 if (!pFilename || !nFlags)
215 if (osl_getFileURLFromSystemPath (pFilename, &(aFileUrl.pData)) != osl_File_E_None)
218 rtl_uString_assign (&(aFileUrl.pData), pFilename);
220 if (!aFileUrl.startsWith(
"file://"))
224 (void) osl_getProcessWorkingDir (&(aCwdUrl.pData));
227 (void) osl_getAbsoluteFileURL (aCwdUrl.pData, aFileUrl.pData, &(aFileUrl.pData));
231 oslFileError
result = osl_openFile (aFileUrl.pData, &
m_handle, nFlags);
232 if (result == osl_File_E_EXIST)
235 result = osl_openFile (aFileUrl.pData, &
m_handle, osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
242 if (result != osl_File_E_None)
243 return errorFromNative(result);
249 static void closeFile (oslFileHandle hFile)
251 (void) osl_closeFile (hFile);
258 void operator()(FileHandle & rFile)
const
261 closeFile (rFile.m_handle);
262 rFile.m_handle =
nullptr;
265 typedef CloseFile destructor_type;
278 storeError initSize_Impl (sal_uInt32 & rnSize);
284 virtual storeError readPageAt_Impl (std::shared_ptr<PageData> & rPage, sal_uInt32 nOffset)
override;
285 virtual storeError writePageAt_Impl (std::shared_ptr<PageData>
const & rPage, sal_uInt32 nOffset)
override;
287 virtual storeError readAt_Impl (sal_uInt32 nOffset,
void * pBuffer, sal_uInt32 nBytes)
override;
288 virtual storeError writeAt_Impl (sal_uInt32 nOffset,
void const * pBuffer, sal_uInt32 nBytes)
override;
290 virtual storeError getSize_Impl (sal_uInt32 & rnSize)
override;
291 virtual storeError setSize_Impl (sal_uInt32 nSize)
override;
298 explicit FileLockBytes (FileHandle
const & rFile);
300 FileLockBytes(
const FileLockBytes&) =
delete;
301 FileLockBytes& operator=(
const FileLockBytes&) =
delete;
306 virtual ~FileLockBytes()
override;
313FileLockBytes::FileLockBytes (FileHandle
const & rFile)
318FileLockBytes::~FileLockBytes()
320 FileHandle::closeFile (
m_hFile);
323storeError FileLockBytes::initSize_Impl (sal_uInt32 & rnSize)
329 sal_uInt64 uSize = 0;
331 if (result != osl_File_E_None)
332 return FileHandle::errorFromNative(result);
333 if (uSize > SAL_MAX_UINT32)
336 rnSize = sal::static_int_cast<sal_uInt32>(uSize);
346 result = PageData::Allocator::createInstance (rxAllocator, nPageSize);
355storeError FileLockBytes::readPageAt_Impl (std::shared_ptr<PageData> & rPage, sal_uInt32 nOffset)
369 return readAt_Impl (nOffset, pagedata, pagedata->
size());
372storeError FileLockBytes::writePageAt_Impl (std::shared_ptr<PageData>
const & rPage, sal_uInt32 nOffset)
374 PageData const * pagedata = rPage.get();
375 OSL_PRECOND(pagedata !=
nullptr,
"contract violation");
376 return writeAt_Impl (nOffset, pagedata, pagedata->
size());
379storeError FileLockBytes::readAt_Impl (sal_uInt32 nOffset,
void * pBuffer, sal_uInt32 nBytes)
381 sal_uInt64 nDone = 0;
382 oslFileError
result = osl_readFileAt (
m_hFile, nOffset, pBuffer, nBytes, &nDone);
383 if (result != osl_File_E_None)
384 return FileHandle::errorFromNative(result);
390storeError FileLockBytes::writeAt_Impl (sal_uInt32 nOffset,
void const * pBuffer, sal_uInt32 nBytes)
392 sal_uInt64 nDone = 0;
393 oslFileError
result = osl_writeFileAt (
m_hFile, nOffset, pBuffer, nBytes, &nDone);
394 if (result != osl_File_E_None)
395 return FileHandle::errorFromNative(result);
399 sal_uInt64
const uSize = nOffset + nBytes;
400 OSL_PRECOND(uSize < SAL_MAX_UINT32,
"store::ILockBytes::writeAt() contract violation");
402 m_nSize = sal::static_int_cast<sal_uInt32>(uSize);
406storeError FileLockBytes::getSize_Impl (sal_uInt32 & rnSize)
412storeError FileLockBytes::setSize_Impl (sal_uInt32 nSize)
415 if (result != osl_File_E_None)
416 return FileHandle::errorFromNative(result);
425 if (result != osl_File_E_None)
426 return FileHandle::errorFromNative(result);
448 bool operator != (FileMapping
const & rhs)
const
453 oslFileError initialize (oslFileHandle hFile)
456 sal_uInt64 uSize = 0;
457 oslFileError
result = osl_getFileSize (hFile, &uSize);
458 if (result != osl_File_E_None)
462 if (uSize > SAL_MAX_UINT32)
463 return osl_File_E_OVERFLOW;
464 m_nSize = sal::static_int_cast<sal_uInt32>(uSize);
469 return osl_mapFile (hFile,
reinterpret_cast<void**
>(&
m_pAddr),
m_nSize, 0, osl_File_MapFlag_RandomAccess);
474 static void unmapFile (oslFileHandle hFile,
sal_uInt8 * pAddr, sal_uInt32 nSize)
476 (void) osl_unmapMappedFile (hFile, pAddr, nSize);
477 (void) osl_closeFile (hFile);
484 void operator ()(FileMapping & rMapping)
const
487 unmapFile (rMapping.m_hFile, rMapping.m_pAddr, rMapping.m_nSize);
488 rMapping.m_pAddr =
nullptr;
489 rMapping.m_nSize = 0;
492 typedef UnmapFile destructor_type;
495class MappedLockBytes :
509 virtual void allocate_Impl (
void ** ppPage, sal_uInt16 * pnSize)
override;
510 virtual void deallocate_Impl (
void * pPage)
override;
516 virtual storeError readPageAt_Impl (std::shared_ptr<PageData> & rPage, sal_uInt32 nOffset)
override;
517 virtual storeError writePageAt_Impl (std::shared_ptr<PageData>
const & rPage, sal_uInt32 nOffset)
override;
519 virtual storeError readAt_Impl (sal_uInt32 nOffset,
void * pBuffer, sal_uInt32 nBytes)
override;
520 virtual storeError writeAt_Impl (sal_uInt32 nOffset,
const void * pBuffer, sal_uInt32 nBytes)
override;
522 virtual storeError getSize_Impl (sal_uInt32 & rnSize)
override;
523 virtual storeError setSize_Impl (sal_uInt32 nSize)
override;
530 explicit MappedLockBytes (FileMapping
const & rMapping);
532 MappedLockBytes(
const MappedLockBytes&) =
delete;
533 MappedLockBytes& operator=(
const MappedLockBytes&) =
delete;
538 virtual ~MappedLockBytes()
override;
545MappedLockBytes::MappedLockBytes (FileMapping
const & rMapping)
550MappedLockBytes::~MappedLockBytes()
555void MappedLockBytes::allocate_Impl (
void ** ppPage, sal_uInt16 * pnSize)
557 OSL_PRECOND((ppPage !=
nullptr) && (pnSize !=
nullptr),
"contract violation");
558 if ((ppPage !=
nullptr) && (pnSize !=
nullptr))
565void MappedLockBytes::deallocate_Impl (
void * pPage)
577storeError MappedLockBytes::readPageAt_Impl (std::shared_ptr<PageData> & rPage, sal_uInt32 nOffset)
593storeError MappedLockBytes::writePageAt_Impl (std::shared_ptr<PageData>
const & , sal_uInt32 )
598storeError MappedLockBytes::readAt_Impl (sal_uInt32 nOffset,
void * pBuffer, sal_uInt32 nBytes)
604 sal_uInt8 const * src_hi = src_lo + nBytes;
608 memcpy (pBuffer, src_lo, (src_hi - src_lo));
612storeError MappedLockBytes::writeAt_Impl (sal_uInt32 ,
void const * , sal_uInt32 )
617storeError MappedLockBytes::getSize_Impl (sal_uInt32 & rnSize)
623storeError MappedLockBytes::setSize_Impl (sal_uInt32 )
638class MemoryLockBytes :
652 virtual storeError readPageAt_Impl (std::shared_ptr<PageData> & rPage, sal_uInt32 nOffset)
override;
653 virtual storeError writePageAt_Impl (std::shared_ptr<PageData>
const & rPage, sal_uInt32 nOffset)
override;
655 virtual storeError readAt_Impl (sal_uInt32 nOffset,
void * pBuffer, sal_uInt32 nBytes)
override;
656 virtual storeError writeAt_Impl (sal_uInt32 nOffset,
const void * pBuffer, sal_uInt32 nBytes)
override;
658 virtual storeError getSize_Impl (sal_uInt32 & rnSize)
override;
659 virtual storeError setSize_Impl (sal_uInt32 nSize)
override;
668 MemoryLockBytes(
const MemoryLockBytes&) =
delete;
669 MemoryLockBytes& operator=(
const MemoryLockBytes&) =
delete;
672 virtual ~MemoryLockBytes()
override;
679MemoryLockBytes::MemoryLockBytes()
683MemoryLockBytes::~MemoryLockBytes()
690 storeError result = PageData::Allocator::createInstance (rxAllocator, nPageSize);
699storeError MemoryLockBytes::readPageAt_Impl (std::shared_ptr<PageData> & rPage, sal_uInt32 nOffset)
713 return readAt_Impl (nOffset, pagedata, pagedata->
size());
716storeError MemoryLockBytes::writePageAt_Impl (std::shared_ptr<PageData>
const & rPage, sal_uInt32 nOffset)
718 PageData const * pagedata = rPage.get();
719 OSL_PRECOND(!(pagedata ==
nullptr),
"contract violation");
720 return writeAt_Impl (nOffset, pagedata, pagedata->
size());
723storeError MemoryLockBytes::readAt_Impl (sal_uInt32 nOffset,
void * pBuffer, sal_uInt32 nBytes)
729 sal_uInt8 const * src_hi = src_lo + nBytes;
733 memcpy (pBuffer, src_lo, (src_hi - src_lo));
737storeError MemoryLockBytes::writeAt_Impl (sal_uInt32 nOffset,
const void * pBuffer, sal_uInt32 nBytes)
739 sal_uInt64
const dst_size = nOffset + nBytes;
740 OSL_PRECOND(dst_size < SAL_MAX_UINT32,
"store::ILockBytes::writeAt() contract violation");
743 storeError eErrCode = setSize_Impl (sal::static_int_cast<sal_uInt32>(dst_size));
747 SAL_WARN_IF(dst_size >
m_nSize,
"store",
"store::MemoryLockBytes::setSize_Impl() contract violation");
757 memcpy (dst_lo, pBuffer, (dst_hi - dst_lo));
761storeError MemoryLockBytes::getSize_Impl (sal_uInt32 & rnSize)
767storeError MemoryLockBytes::setSize_Impl (sal_uInt32 nSize)
772 if (pData !=
nullptr)
775 memset (pData +
m_nSize, 0, sal::static_int_cast<size_t>(nSize -
m_nSize));
800template<
class T >
struct ResourceHolder
802 typedef typename T::destructor_type destructor_type;
806 explicit ResourceHolder (T
const & value = T()) :
m_value (
value) {}
807 ~ResourceHolder() { reset(T()); }
813 void reset (T
const & value)
817 destructor_type()(tmp);
827 ResourceHolder & operator= (ResourceHolder & rhs)
829 reset (rhs.release());
839 rtl_uString * pFilename,
844 ResourceHolder<FileHandle> xFile;
851 ResourceHolder<FileMapping> xMapping;
852 if (xMapping.get().initialize (xFile.get().m_handle) == osl_File_E_None)
854 rxLockBytes =
new MappedLockBytes (xMapping.get());
855 if (!rxLockBytes.is())
857 (void) xFile.release();
858 (void) xMapping.release();
861 if (!rxLockBytes.is())
863 rxLockBytes =
new FileLockBytes (xFile.get());
864 if (!rxLockBytes.is())
866 (void) xFile.release();
877 rxLockBytes =
new MemoryLockBytes();
878 if (!rxLockBytes.is())
virtual storeError writeAt_Impl(sal_uInt32 nOffset, const void *pBuffer, sal_uInt32 nBytes)=0
virtual storeError initialize_Impl(rtl::Reference< PageData::Allocator > &rxAllocator, sal_uInt16 nPageSize)=0
Implementation (abstract).
virtual storeError writePageAt_Impl(std::shared_ptr< PageData > const &rPage, sal_uInt32 nOffset)=0
virtual storeError flush_Impl()=0
storeError readPageAt(std::shared_ptr< PageData > &rPage, sal_uInt32 nOffset)
storeError writePageAt(std::shared_ptr< PageData > const &rPage, sal_uInt32 nOffset)
storeError writeAt(sal_uInt32 nOffset, const void *pBuffer, sal_uInt32 nBytes)
storeError setSize(sal_uInt32 nSize)
virtual storeError setSize_Impl(sal_uInt32 nSize)=0
virtual storeError readPageAt_Impl(std::shared_ptr< PageData > &rPage, sal_uInt32 nOffset)=0
virtual storeError getSize_Impl(sal_uInt32 &rnSize)=0
storeError getSize(sal_uInt32 &rnSize)
storeError readAt(sal_uInt32 nOffset, void *pBuffer, sal_uInt32 nBytes)
virtual storeError readAt_Impl(sal_uInt32 nOffset, void *pBuffer, sal_uInt32 nBytes)=0
sal_uInt8 * m_pData
Representation.
rtl::Reference< PageData::Allocator > m_xAllocator
oslFileHandle m_hFile
Representation.
#define SAL_WARN_IF(condition, area, stream)
std::unique_ptr< sal_Int32[]> pData
void set(css::uno::UnoInterfaceReference const &value)
Old OStorePageCache implementation.
storeError FileLockBytes_createInstance(rtl::Reference< ILockBytes > &rxLockBytes, rtl_uString *pFilename, storeAccessMode eAccessMode)
storeError MemoryLockBytes_createInstance(rtl::Reference< ILockBytes > &rxLockBytes)
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
sal_uInt16 size() const
size.
sal_uInt32 location() const
location.
storeAccessMode
Access Mode enumeration.
constexpr sal_uInt16 STORE_MINIMUM_PAGESIZE
PageSize (enforced) limits.
storeError
Error Code enumeration.
@ store_E_InvalidParameter
@ store_E_LockingViolation
@ store_E_AccessViolation
constexpr sal_uInt16 STORE_MAXIMUM_PAGESIZE