1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_store.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include "lockbyte.hxx" 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir #include "sal/types.h" 34*cdf0e10cSrcweir #include "osl/diagnose.h" 35*cdf0e10cSrcweir #include "osl/file.h" 36*cdf0e10cSrcweir #include "osl/process.h" 37*cdf0e10cSrcweir #include "rtl/alloc.h" 38*cdf0e10cSrcweir #include "rtl/ustring.hxx" 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir #include "object.hxx" 41*cdf0e10cSrcweir #include "storbase.hxx" 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir #ifndef INCLUDED_STRING_H 44*cdf0e10cSrcweir #include <string.h> 45*cdf0e10cSrcweir #define INCLUDED_STRING_H 46*cdf0e10cSrcweir #endif 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir using namespace store; 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir /*======================================================================== 51*cdf0e10cSrcweir * 52*cdf0e10cSrcweir * ILockBytes (non-virtual interface) implementation. 53*cdf0e10cSrcweir * 54*cdf0e10cSrcweir *======================================================================*/ 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir storeError ILockBytes::initialize (rtl::Reference< PageData::Allocator > & rxAllocator, sal_uInt16 nPageSize) 57*cdf0e10cSrcweir { 58*cdf0e10cSrcweir OSL_PRECOND((STORE_MINIMUM_PAGESIZE <= nPageSize) && (nPageSize <= STORE_MAXIMUM_PAGESIZE), "invalid PageSize"); 59*cdf0e10cSrcweir return initialize_Impl (rxAllocator, nPageSize); 60*cdf0e10cSrcweir } 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir storeError ILockBytes::readPageAt (PageHolder & rPage, sal_uInt32 nOffset) 63*cdf0e10cSrcweir { 64*cdf0e10cSrcweir OSL_PRECOND(!(nOffset == STORE_PAGE_NULL), "store::ILockBytes::readPageAt(): invalid Offset"); 65*cdf0e10cSrcweir if (nOffset == STORE_PAGE_NULL) 66*cdf0e10cSrcweir return store_E_CantSeek; 67*cdf0e10cSrcweir 68*cdf0e10cSrcweir return readPageAt_Impl (rPage, nOffset); 69*cdf0e10cSrcweir } 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir storeError ILockBytes::writePageAt (PageHolder const & rPage, sal_uInt32 nOffset) 72*cdf0e10cSrcweir { 73*cdf0e10cSrcweir // [SECURITY:ValInput] 74*cdf0e10cSrcweir PageData const * pagedata = rPage.get(); 75*cdf0e10cSrcweir OSL_PRECOND(!(pagedata == 0), "store::ILockBytes::writePageAt(): invalid Page"); 76*cdf0e10cSrcweir if (pagedata == 0) 77*cdf0e10cSrcweir return store_E_InvalidParameter; 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir sal_uInt32 const offset = pagedata->location(); 80*cdf0e10cSrcweir OSL_PRECOND(!(nOffset != offset), "store::ILockBytes::writePageAt(): inconsistent Offset"); 81*cdf0e10cSrcweir if (nOffset != offset) 82*cdf0e10cSrcweir return store_E_InvalidParameter; 83*cdf0e10cSrcweir 84*cdf0e10cSrcweir OSL_PRECOND(!(nOffset == STORE_PAGE_NULL), "store::ILockBytes::writePageAt(): invalid Offset"); 85*cdf0e10cSrcweir if (nOffset == STORE_PAGE_NULL) 86*cdf0e10cSrcweir return store_E_CantSeek; 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir return writePageAt_Impl (rPage, nOffset); 89*cdf0e10cSrcweir } 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir storeError ILockBytes::readAt (sal_uInt32 nOffset, void * pBuffer, sal_uInt32 nBytes) 92*cdf0e10cSrcweir { 93*cdf0e10cSrcweir // [SECURITY:ValInput] 94*cdf0e10cSrcweir sal_uInt8 * dst_lo = static_cast<sal_uInt8*>(pBuffer); 95*cdf0e10cSrcweir if (!(dst_lo != 0)) 96*cdf0e10cSrcweir return store_E_InvalidParameter; 97*cdf0e10cSrcweir 98*cdf0e10cSrcweir sal_uInt8 * dst_hi = dst_lo + nBytes; 99*cdf0e10cSrcweir if (!(dst_lo < dst_hi)) 100*cdf0e10cSrcweir return (dst_lo > dst_hi) ? store_E_InvalidParameter : store_E_None; 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir OSL_PRECOND(!(nOffset == STORE_PAGE_NULL), "store::ILockBytes::readAt(): invalid Offset"); 103*cdf0e10cSrcweir if (nOffset == STORE_PAGE_NULL) 104*cdf0e10cSrcweir return store_E_CantSeek; 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir sal_uInt64 const src_size = nOffset + nBytes; 107*cdf0e10cSrcweir if (src_size > SAL_MAX_UINT32) 108*cdf0e10cSrcweir return store_E_CantSeek; 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir return readAt_Impl (nOffset, dst_lo, (dst_hi - dst_lo)); 111*cdf0e10cSrcweir } 112*cdf0e10cSrcweir 113*cdf0e10cSrcweir storeError ILockBytes::writeAt (sal_uInt32 nOffset, void const * pBuffer, sal_uInt32 nBytes) 114*cdf0e10cSrcweir { 115*cdf0e10cSrcweir // [SECURITY:ValInput] 116*cdf0e10cSrcweir sal_uInt8 const * src_lo = static_cast<sal_uInt8 const*>(pBuffer); 117*cdf0e10cSrcweir if (!(src_lo != 0)) 118*cdf0e10cSrcweir return store_E_InvalidParameter; 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir sal_uInt8 const * src_hi = src_lo + nBytes; 121*cdf0e10cSrcweir if (!(src_lo < src_hi)) 122*cdf0e10cSrcweir return (src_lo > src_hi) ? store_E_InvalidParameter : store_E_None; 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir OSL_PRECOND(!(nOffset == STORE_PAGE_NULL), "store::ILockBytes::writeAt(): invalid Offset"); 125*cdf0e10cSrcweir if (nOffset == STORE_PAGE_NULL) 126*cdf0e10cSrcweir return store_E_CantSeek; 127*cdf0e10cSrcweir 128*cdf0e10cSrcweir sal_uInt64 const dst_size = nOffset + nBytes; 129*cdf0e10cSrcweir if (dst_size > SAL_MAX_UINT32) 130*cdf0e10cSrcweir return store_E_CantSeek; 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir return writeAt_Impl (nOffset, src_lo, (src_hi - src_lo)); 133*cdf0e10cSrcweir } 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir storeError ILockBytes::getSize (sal_uInt32 & rnSize) 136*cdf0e10cSrcweir { 137*cdf0e10cSrcweir rnSize = 0; 138*cdf0e10cSrcweir return getSize_Impl (rnSize); 139*cdf0e10cSrcweir } 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir storeError ILockBytes::setSize (sal_uInt32 nSize) 142*cdf0e10cSrcweir { 143*cdf0e10cSrcweir return setSize_Impl (nSize); 144*cdf0e10cSrcweir } 145*cdf0e10cSrcweir 146*cdf0e10cSrcweir storeError ILockBytes::flush() 147*cdf0e10cSrcweir { 148*cdf0e10cSrcweir return flush_Impl(); 149*cdf0e10cSrcweir } 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir /*======================================================================== 152*cdf0e10cSrcweir * 153*cdf0e10cSrcweir * FileLockBytes implementation. 154*cdf0e10cSrcweir * 155*cdf0e10cSrcweir *======================================================================*/ 156*cdf0e10cSrcweir namespace store 157*cdf0e10cSrcweir { 158*cdf0e10cSrcweir 159*cdf0e10cSrcweir struct FileHandle 160*cdf0e10cSrcweir { 161*cdf0e10cSrcweir oslFileHandle m_handle; 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir FileHandle() : m_handle(0) {} 164*cdf0e10cSrcweir 165*cdf0e10cSrcweir bool operator != (FileHandle const & rhs) 166*cdf0e10cSrcweir { 167*cdf0e10cSrcweir return (m_handle != rhs.m_handle); 168*cdf0e10cSrcweir } 169*cdf0e10cSrcweir 170*cdf0e10cSrcweir static storeError errorFromNative (oslFileError eErrno) 171*cdf0e10cSrcweir { 172*cdf0e10cSrcweir switch (eErrno) 173*cdf0e10cSrcweir { 174*cdf0e10cSrcweir case osl_File_E_None: 175*cdf0e10cSrcweir return store_E_None; 176*cdf0e10cSrcweir 177*cdf0e10cSrcweir case osl_File_E_NOENT: 178*cdf0e10cSrcweir return store_E_NotExists; 179*cdf0e10cSrcweir 180*cdf0e10cSrcweir case osl_File_E_ACCES: 181*cdf0e10cSrcweir case osl_File_E_PERM: 182*cdf0e10cSrcweir return store_E_AccessViolation; 183*cdf0e10cSrcweir 184*cdf0e10cSrcweir case osl_File_E_AGAIN: 185*cdf0e10cSrcweir case osl_File_E_DEADLK: 186*cdf0e10cSrcweir return store_E_LockingViolation; 187*cdf0e10cSrcweir 188*cdf0e10cSrcweir case osl_File_E_BADF: 189*cdf0e10cSrcweir return store_E_InvalidHandle; 190*cdf0e10cSrcweir 191*cdf0e10cSrcweir case osl_File_E_INVAL: 192*cdf0e10cSrcweir return store_E_InvalidParameter; 193*cdf0e10cSrcweir 194*cdf0e10cSrcweir case osl_File_E_NOMEM: 195*cdf0e10cSrcweir return store_E_OutOfMemory; 196*cdf0e10cSrcweir 197*cdf0e10cSrcweir case osl_File_E_NOSPC: 198*cdf0e10cSrcweir return store_E_OutOfSpace; 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir case osl_File_E_OVERFLOW: 201*cdf0e10cSrcweir return store_E_CantSeek; 202*cdf0e10cSrcweir 203*cdf0e10cSrcweir default: 204*cdf0e10cSrcweir return store_E_Unknown; 205*cdf0e10cSrcweir } 206*cdf0e10cSrcweir } 207*cdf0e10cSrcweir 208*cdf0e10cSrcweir static sal_uInt32 modeToNative (storeAccessMode eAccessMode) 209*cdf0e10cSrcweir { 210*cdf0e10cSrcweir sal_uInt32 nFlags = 0; 211*cdf0e10cSrcweir switch (eAccessMode) 212*cdf0e10cSrcweir { 213*cdf0e10cSrcweir case store_AccessCreate: 214*cdf0e10cSrcweir case store_AccessReadCreate: 215*cdf0e10cSrcweir nFlags |= osl_File_OpenFlag_Create; 216*cdf0e10cSrcweir // fall through 217*cdf0e10cSrcweir case store_AccessReadWrite: 218*cdf0e10cSrcweir nFlags |= osl_File_OpenFlag_Write; 219*cdf0e10cSrcweir // fall through 220*cdf0e10cSrcweir case store_AccessReadOnly: 221*cdf0e10cSrcweir nFlags |= osl_File_OpenFlag_Read; 222*cdf0e10cSrcweir break; 223*cdf0e10cSrcweir default: 224*cdf0e10cSrcweir OSL_PRECOND(0, "store::FileHandle: unknown storeAccessMode"); 225*cdf0e10cSrcweir } 226*cdf0e10cSrcweir return nFlags; 227*cdf0e10cSrcweir } 228*cdf0e10cSrcweir 229*cdf0e10cSrcweir storeError initialize (rtl_uString * pFilename, storeAccessMode eAccessMode) 230*cdf0e10cSrcweir { 231*cdf0e10cSrcweir // Verify arguments. 232*cdf0e10cSrcweir sal_uInt32 nFlags = modeToNative (eAccessMode); 233*cdf0e10cSrcweir if (!pFilename || !nFlags) 234*cdf0e10cSrcweir return store_E_InvalidParameter; 235*cdf0e10cSrcweir 236*cdf0e10cSrcweir // Convert into FileUrl. 237*cdf0e10cSrcweir rtl::OUString aFileUrl; 238*cdf0e10cSrcweir if (osl_getFileURLFromSystemPath (pFilename, &(aFileUrl.pData)) != osl_File_E_None) 239*cdf0e10cSrcweir { 240*cdf0e10cSrcweir // Not system path. Assume file url. 241*cdf0e10cSrcweir rtl_uString_assign (&(aFileUrl.pData), pFilename); 242*cdf0e10cSrcweir } 243*cdf0e10cSrcweir if (aFileUrl.compareToAscii("file://", 7) != 0) 244*cdf0e10cSrcweir { 245*cdf0e10cSrcweir // Not file url. Assume relative path. 246*cdf0e10cSrcweir rtl::OUString aCwdUrl; 247*cdf0e10cSrcweir (void) osl_getProcessWorkingDir (&(aCwdUrl.pData)); 248*cdf0e10cSrcweir 249*cdf0e10cSrcweir // Absolute file url. 250*cdf0e10cSrcweir (void) osl_getAbsoluteFileURL (aCwdUrl.pData, aFileUrl.pData, &(aFileUrl.pData)); 251*cdf0e10cSrcweir } 252*cdf0e10cSrcweir 253*cdf0e10cSrcweir // Acquire handle. 254*cdf0e10cSrcweir oslFileError result = osl_openFile (aFileUrl.pData, &m_handle, nFlags); 255*cdf0e10cSrcweir if (result == osl_File_E_EXIST) 256*cdf0e10cSrcweir { 257*cdf0e10cSrcweir // Already existing (O_CREAT | O_EXCL). 258*cdf0e10cSrcweir result = osl_openFile (aFileUrl.pData, &m_handle, osl_File_OpenFlag_Read | osl_File_OpenFlag_Write); 259*cdf0e10cSrcweir if ((result == osl_File_E_None) && (eAccessMode == store_AccessCreate)) 260*cdf0e10cSrcweir { 261*cdf0e10cSrcweir // Truncate existing file. 262*cdf0e10cSrcweir result = osl_setFileSize (m_handle, 0); 263*cdf0e10cSrcweir } 264*cdf0e10cSrcweir } 265*cdf0e10cSrcweir if (result != osl_File_E_None) 266*cdf0e10cSrcweir return errorFromNative(result); 267*cdf0e10cSrcweir return store_E_None; 268*cdf0e10cSrcweir } 269*cdf0e10cSrcweir 270*cdf0e10cSrcweir /** @see FileLockBytes destructor 271*cdf0e10cSrcweir */ 272*cdf0e10cSrcweir static void closeFile (oslFileHandle hFile) 273*cdf0e10cSrcweir { 274*cdf0e10cSrcweir (void) osl_closeFile (hFile); 275*cdf0e10cSrcweir } 276*cdf0e10cSrcweir 277*cdf0e10cSrcweir /** @see ResourceHolder<T>::destructor_type 278*cdf0e10cSrcweir */ 279*cdf0e10cSrcweir struct CloseFile 280*cdf0e10cSrcweir { 281*cdf0e10cSrcweir void operator()(FileHandle & rFile) const 282*cdf0e10cSrcweir { 283*cdf0e10cSrcweir // Release handle. 284*cdf0e10cSrcweir closeFile (rFile.m_handle); 285*cdf0e10cSrcweir rFile.m_handle = 0; 286*cdf0e10cSrcweir } 287*cdf0e10cSrcweir }; 288*cdf0e10cSrcweir typedef CloseFile destructor_type; 289*cdf0e10cSrcweir }; 290*cdf0e10cSrcweir 291*cdf0e10cSrcweir class FileLockBytes : 292*cdf0e10cSrcweir public store::OStoreObject, 293*cdf0e10cSrcweir public store::ILockBytes 294*cdf0e10cSrcweir { 295*cdf0e10cSrcweir /** Representation. 296*cdf0e10cSrcweir */ 297*cdf0e10cSrcweir oslFileHandle m_hFile; 298*cdf0e10cSrcweir sal_uInt32 m_nSize; 299*cdf0e10cSrcweir rtl::Reference< PageData::Allocator > m_xAllocator; 300*cdf0e10cSrcweir 301*cdf0e10cSrcweir storeError initSize_Impl (sal_uInt32 & rnSize); 302*cdf0e10cSrcweir 303*cdf0e10cSrcweir /** ILockBytes implementation. 304*cdf0e10cSrcweir */ 305*cdf0e10cSrcweir virtual storeError initialize_Impl (rtl::Reference< PageData::Allocator > & rxAllocator, sal_uInt16 nPageSize); 306*cdf0e10cSrcweir 307*cdf0e10cSrcweir virtual storeError readPageAt_Impl (PageHolder & rPage, sal_uInt32 nOffset); 308*cdf0e10cSrcweir virtual storeError writePageAt_Impl (PageHolder const & rPage, sal_uInt32 nOffset); 309*cdf0e10cSrcweir 310*cdf0e10cSrcweir virtual storeError readAt_Impl (sal_uInt32 nOffset, void * pBuffer, sal_uInt32 nBytes); 311*cdf0e10cSrcweir virtual storeError writeAt_Impl (sal_uInt32 nOffset, void const * pBuffer, sal_uInt32 nBytes); 312*cdf0e10cSrcweir 313*cdf0e10cSrcweir virtual storeError getSize_Impl (sal_uInt32 & rnSize); 314*cdf0e10cSrcweir virtual storeError setSize_Impl (sal_uInt32 nSize); 315*cdf0e10cSrcweir 316*cdf0e10cSrcweir virtual storeError flush_Impl(); 317*cdf0e10cSrcweir 318*cdf0e10cSrcweir /** Not implemented. 319*cdf0e10cSrcweir */ 320*cdf0e10cSrcweir FileLockBytes (FileLockBytes const &); 321*cdf0e10cSrcweir FileLockBytes & operator= (FileLockBytes const &); 322*cdf0e10cSrcweir 323*cdf0e10cSrcweir public: 324*cdf0e10cSrcweir /** Construction. 325*cdf0e10cSrcweir */ 326*cdf0e10cSrcweir explicit FileLockBytes (FileHandle & rFile); 327*cdf0e10cSrcweir 328*cdf0e10cSrcweir /** Delegate multiple inherited IReference. 329*cdf0e10cSrcweir */ 330*cdf0e10cSrcweir virtual oslInterlockedCount SAL_CALL acquire(); 331*cdf0e10cSrcweir virtual oslInterlockedCount SAL_CALL release(); 332*cdf0e10cSrcweir 333*cdf0e10cSrcweir protected: 334*cdf0e10cSrcweir /** Destruction. 335*cdf0e10cSrcweir */ 336*cdf0e10cSrcweir virtual ~FileLockBytes(); 337*cdf0e10cSrcweir }; 338*cdf0e10cSrcweir 339*cdf0e10cSrcweir } // namespace store 340*cdf0e10cSrcweir 341*cdf0e10cSrcweir FileLockBytes::FileLockBytes (FileHandle & rFile) 342*cdf0e10cSrcweir : m_hFile (rFile.m_handle), m_nSize (SAL_MAX_UINT32), m_xAllocator() 343*cdf0e10cSrcweir { 344*cdf0e10cSrcweir } 345*cdf0e10cSrcweir 346*cdf0e10cSrcweir FileLockBytes::~FileLockBytes() 347*cdf0e10cSrcweir { 348*cdf0e10cSrcweir FileHandle::closeFile (m_hFile); 349*cdf0e10cSrcweir } 350*cdf0e10cSrcweir 351*cdf0e10cSrcweir oslInterlockedCount SAL_CALL FileLockBytes::acquire() 352*cdf0e10cSrcweir { 353*cdf0e10cSrcweir return OStoreObject::acquire(); 354*cdf0e10cSrcweir } 355*cdf0e10cSrcweir 356*cdf0e10cSrcweir oslInterlockedCount SAL_CALL FileLockBytes::release() 357*cdf0e10cSrcweir { 358*cdf0e10cSrcweir return OStoreObject::release(); 359*cdf0e10cSrcweir } 360*cdf0e10cSrcweir 361*cdf0e10cSrcweir storeError FileLockBytes::initSize_Impl (sal_uInt32 & rnSize) 362*cdf0e10cSrcweir { 363*cdf0e10cSrcweir /* osl_getFileSize() uses slow 'fstat(h, &size)', 364*cdf0e10cSrcweir * instead of fast 'size = lseek(h, 0, SEEK_END)'. 365*cdf0e10cSrcweir * so, init size here, and track changes. 366*cdf0e10cSrcweir */ 367*cdf0e10cSrcweir sal_uInt64 uSize = 0; 368*cdf0e10cSrcweir oslFileError result = osl_getFileSize (m_hFile, &uSize); 369*cdf0e10cSrcweir if (result != osl_File_E_None) 370*cdf0e10cSrcweir return FileHandle::errorFromNative(result); 371*cdf0e10cSrcweir if (uSize > SAL_MAX_UINT32) 372*cdf0e10cSrcweir return store_E_CantSeek; 373*cdf0e10cSrcweir 374*cdf0e10cSrcweir rnSize = sal::static_int_cast<sal_uInt32>(uSize); 375*cdf0e10cSrcweir return store_E_None; 376*cdf0e10cSrcweir } 377*cdf0e10cSrcweir 378*cdf0e10cSrcweir storeError FileLockBytes::initialize_Impl (rtl::Reference< PageData::Allocator > & rxAllocator, sal_uInt16 nPageSize) 379*cdf0e10cSrcweir { 380*cdf0e10cSrcweir storeError result = initSize_Impl (m_nSize); 381*cdf0e10cSrcweir if (result != store_E_None) 382*cdf0e10cSrcweir return (result); 383*cdf0e10cSrcweir 384*cdf0e10cSrcweir result = PageData::Allocator::createInstance (rxAllocator, nPageSize); 385*cdf0e10cSrcweir if (result != store_E_None) 386*cdf0e10cSrcweir return (result); 387*cdf0e10cSrcweir 388*cdf0e10cSrcweir // @see readPageAt_Impl(). 389*cdf0e10cSrcweir m_xAllocator = rxAllocator; 390*cdf0e10cSrcweir return store_E_None; 391*cdf0e10cSrcweir } 392*cdf0e10cSrcweir 393*cdf0e10cSrcweir storeError FileLockBytes::readPageAt_Impl (PageHolder & rPage, sal_uInt32 nOffset) 394*cdf0e10cSrcweir { 395*cdf0e10cSrcweir if (m_xAllocator.is()) 396*cdf0e10cSrcweir { 397*cdf0e10cSrcweir PageHolder page (m_xAllocator->construct<PageData>(), m_xAllocator); 398*cdf0e10cSrcweir page.swap (rPage); 399*cdf0e10cSrcweir } 400*cdf0e10cSrcweir 401*cdf0e10cSrcweir if (!m_xAllocator.is()) 402*cdf0e10cSrcweir return store_E_InvalidAccess; 403*cdf0e10cSrcweir if (!rPage.get()) 404*cdf0e10cSrcweir return store_E_OutOfMemory; 405*cdf0e10cSrcweir 406*cdf0e10cSrcweir PageData * pagedata = rPage.get(); 407*cdf0e10cSrcweir return readAt_Impl (nOffset, pagedata, pagedata->size()); 408*cdf0e10cSrcweir } 409*cdf0e10cSrcweir 410*cdf0e10cSrcweir storeError FileLockBytes::writePageAt_Impl (PageHolder const & rPage, sal_uInt32 nOffset) 411*cdf0e10cSrcweir { 412*cdf0e10cSrcweir PageData const * pagedata = rPage.get(); 413*cdf0e10cSrcweir OSL_PRECOND(pagedata != 0, "contract violation"); 414*cdf0e10cSrcweir return writeAt_Impl (nOffset, pagedata, pagedata->size()); 415*cdf0e10cSrcweir } 416*cdf0e10cSrcweir 417*cdf0e10cSrcweir storeError FileLockBytes::readAt_Impl (sal_uInt32 nOffset, void * pBuffer, sal_uInt32 nBytes) 418*cdf0e10cSrcweir { 419*cdf0e10cSrcweir sal_uInt64 nDone = 0; 420*cdf0e10cSrcweir oslFileError result = osl_readFileAt (m_hFile, nOffset, pBuffer, nBytes, &nDone); 421*cdf0e10cSrcweir if (result != osl_File_E_None) 422*cdf0e10cSrcweir return FileHandle::errorFromNative(result); 423*cdf0e10cSrcweir if (nDone != nBytes) 424*cdf0e10cSrcweir return (nDone != 0) ? store_E_CantRead : store_E_NotExists; 425*cdf0e10cSrcweir return store_E_None; 426*cdf0e10cSrcweir } 427*cdf0e10cSrcweir 428*cdf0e10cSrcweir storeError FileLockBytes::writeAt_Impl (sal_uInt32 nOffset, void const * pBuffer, sal_uInt32 nBytes) 429*cdf0e10cSrcweir { 430*cdf0e10cSrcweir sal_uInt64 nDone = 0; 431*cdf0e10cSrcweir oslFileError result = osl_writeFileAt (m_hFile, nOffset, pBuffer, nBytes, &nDone); 432*cdf0e10cSrcweir if (result != osl_File_E_None) 433*cdf0e10cSrcweir return FileHandle::errorFromNative(result); 434*cdf0e10cSrcweir if (nDone != nBytes) 435*cdf0e10cSrcweir return store_E_CantWrite; 436*cdf0e10cSrcweir 437*cdf0e10cSrcweir sal_uInt64 const uSize = nOffset + nBytes; 438*cdf0e10cSrcweir OSL_PRECOND(uSize < SAL_MAX_UINT32, "store::ILockBytes::writeAt() contract violation"); 439*cdf0e10cSrcweir if (uSize > m_nSize) 440*cdf0e10cSrcweir m_nSize = sal::static_int_cast<sal_uInt32>(uSize); 441*cdf0e10cSrcweir return store_E_None; 442*cdf0e10cSrcweir } 443*cdf0e10cSrcweir 444*cdf0e10cSrcweir storeError FileLockBytes::getSize_Impl (sal_uInt32 & rnSize) 445*cdf0e10cSrcweir { 446*cdf0e10cSrcweir rnSize = m_nSize; 447*cdf0e10cSrcweir return store_E_None; 448*cdf0e10cSrcweir } 449*cdf0e10cSrcweir 450*cdf0e10cSrcweir storeError FileLockBytes::setSize_Impl (sal_uInt32 nSize) 451*cdf0e10cSrcweir { 452*cdf0e10cSrcweir oslFileError result = osl_setFileSize (m_hFile, nSize); 453*cdf0e10cSrcweir if (result != osl_File_E_None) 454*cdf0e10cSrcweir return FileHandle::errorFromNative(result); 455*cdf0e10cSrcweir 456*cdf0e10cSrcweir m_nSize = nSize; 457*cdf0e10cSrcweir return store_E_None; 458*cdf0e10cSrcweir } 459*cdf0e10cSrcweir 460*cdf0e10cSrcweir storeError FileLockBytes::flush_Impl() 461*cdf0e10cSrcweir { 462*cdf0e10cSrcweir oslFileError result = osl_syncFile (m_hFile); 463*cdf0e10cSrcweir if (result != osl_File_E_None) 464*cdf0e10cSrcweir return FileHandle::errorFromNative(result); 465*cdf0e10cSrcweir return store_E_None; 466*cdf0e10cSrcweir } 467*cdf0e10cSrcweir 468*cdf0e10cSrcweir /*======================================================================== 469*cdf0e10cSrcweir * 470*cdf0e10cSrcweir * MappedLockBytes implementation. 471*cdf0e10cSrcweir * 472*cdf0e10cSrcweir *======================================================================*/ 473*cdf0e10cSrcweir namespace store 474*cdf0e10cSrcweir { 475*cdf0e10cSrcweir 476*cdf0e10cSrcweir struct FileMapping 477*cdf0e10cSrcweir { 478*cdf0e10cSrcweir sal_uInt8 * m_pAddr; 479*cdf0e10cSrcweir sal_uInt32 m_nSize; 480*cdf0e10cSrcweir 481*cdf0e10cSrcweir FileMapping() : m_pAddr(0), m_nSize(0) {} 482*cdf0e10cSrcweir 483*cdf0e10cSrcweir bool operator != (FileMapping const & rhs) const 484*cdf0e10cSrcweir { 485*cdf0e10cSrcweir return ((m_pAddr != rhs.m_pAddr) || (m_nSize != rhs.m_nSize)); 486*cdf0e10cSrcweir } 487*cdf0e10cSrcweir 488*cdf0e10cSrcweir oslFileError initialize (oslFileHandle hFile) 489*cdf0e10cSrcweir { 490*cdf0e10cSrcweir // Determine mapping size. 491*cdf0e10cSrcweir sal_uInt64 uSize = 0; 492*cdf0e10cSrcweir oslFileError result = osl_getFileSize (hFile, &uSize); 493*cdf0e10cSrcweir if (result != osl_File_E_None) 494*cdf0e10cSrcweir return result; 495*cdf0e10cSrcweir 496*cdf0e10cSrcweir // [SECURITY:IntOver] 497*cdf0e10cSrcweir if (uSize > SAL_MAX_UINT32) 498*cdf0e10cSrcweir return osl_File_E_OVERFLOW; 499*cdf0e10cSrcweir m_nSize = sal::static_int_cast<sal_uInt32>(uSize); 500*cdf0e10cSrcweir 501*cdf0e10cSrcweir // Acquire mapping. 502*cdf0e10cSrcweir return osl_mapFile (hFile, reinterpret_cast<void**>(&m_pAddr), m_nSize, 0, osl_File_MapFlag_RandomAccess); 503*cdf0e10cSrcweir } 504*cdf0e10cSrcweir 505*cdf0e10cSrcweir /** @see MappedLockBytes::destructor. 506*cdf0e10cSrcweir */ 507*cdf0e10cSrcweir static void unmapFile (sal_uInt8 * pAddr, sal_uInt32 nSize) 508*cdf0e10cSrcweir { 509*cdf0e10cSrcweir (void) osl_unmapFile (pAddr, nSize); 510*cdf0e10cSrcweir } 511*cdf0e10cSrcweir 512*cdf0e10cSrcweir /** @see ResourceHolder<T>::destructor_type 513*cdf0e10cSrcweir */ 514*cdf0e10cSrcweir struct UnmapFile 515*cdf0e10cSrcweir { 516*cdf0e10cSrcweir void operator ()(FileMapping & rMapping) const 517*cdf0e10cSrcweir { 518*cdf0e10cSrcweir // Release mapping. 519*cdf0e10cSrcweir unmapFile (rMapping.m_pAddr, rMapping.m_nSize); 520*cdf0e10cSrcweir rMapping.m_pAddr = 0, rMapping.m_nSize = 0; 521*cdf0e10cSrcweir } 522*cdf0e10cSrcweir }; 523*cdf0e10cSrcweir typedef UnmapFile destructor_type; 524*cdf0e10cSrcweir }; 525*cdf0e10cSrcweir 526*cdf0e10cSrcweir class MappedLockBytes : 527*cdf0e10cSrcweir public store::OStoreObject, 528*cdf0e10cSrcweir public store::PageData::Allocator, 529*cdf0e10cSrcweir public store::ILockBytes 530*cdf0e10cSrcweir { 531*cdf0e10cSrcweir /** Representation. 532*cdf0e10cSrcweir */ 533*cdf0e10cSrcweir sal_uInt8 * m_pData; 534*cdf0e10cSrcweir sal_uInt32 m_nSize; 535*cdf0e10cSrcweir sal_uInt16 m_nPageSize; 536*cdf0e10cSrcweir 537*cdf0e10cSrcweir /** PageData::Allocator implementation. 538*cdf0e10cSrcweir */ 539*cdf0e10cSrcweir virtual void allocate_Impl (void ** ppPage, sal_uInt16 * pnSize); 540*cdf0e10cSrcweir virtual void deallocate_Impl (void * pPage); 541*cdf0e10cSrcweir 542*cdf0e10cSrcweir /** ILockBytes implementation. 543*cdf0e10cSrcweir */ 544*cdf0e10cSrcweir virtual storeError initialize_Impl (rtl::Reference< PageData::Allocator > & rxAllocator, sal_uInt16 nPageSize); 545*cdf0e10cSrcweir 546*cdf0e10cSrcweir virtual storeError readPageAt_Impl (PageHolder & rPage, sal_uInt32 nOffset); 547*cdf0e10cSrcweir virtual storeError writePageAt_Impl (PageHolder const & rPage, sal_uInt32 nOffset); 548*cdf0e10cSrcweir 549*cdf0e10cSrcweir virtual storeError readAt_Impl (sal_uInt32 nOffset, void * pBuffer, sal_uInt32 nBytes); 550*cdf0e10cSrcweir virtual storeError writeAt_Impl (sal_uInt32 nOffset, const void * pBuffer, sal_uInt32 nBytes); 551*cdf0e10cSrcweir 552*cdf0e10cSrcweir virtual storeError getSize_Impl (sal_uInt32 & rnSize); 553*cdf0e10cSrcweir virtual storeError setSize_Impl (sal_uInt32 nSize); 554*cdf0e10cSrcweir 555*cdf0e10cSrcweir virtual storeError flush_Impl(); 556*cdf0e10cSrcweir 557*cdf0e10cSrcweir /** Not implemented. 558*cdf0e10cSrcweir */ 559*cdf0e10cSrcweir MappedLockBytes (MappedLockBytes const &); 560*cdf0e10cSrcweir MappedLockBytes & operator= (MappedLockBytes const &); 561*cdf0e10cSrcweir 562*cdf0e10cSrcweir public: 563*cdf0e10cSrcweir /** Construction. 564*cdf0e10cSrcweir */ 565*cdf0e10cSrcweir explicit MappedLockBytes (FileMapping & rMapping); 566*cdf0e10cSrcweir 567*cdf0e10cSrcweir /** Delegate multiple inherited IReference. 568*cdf0e10cSrcweir */ 569*cdf0e10cSrcweir virtual oslInterlockedCount SAL_CALL acquire(); 570*cdf0e10cSrcweir virtual oslInterlockedCount SAL_CALL release(); 571*cdf0e10cSrcweir 572*cdf0e10cSrcweir protected: 573*cdf0e10cSrcweir /* Destruction. 574*cdf0e10cSrcweir */ 575*cdf0e10cSrcweir virtual ~MappedLockBytes(); 576*cdf0e10cSrcweir }; 577*cdf0e10cSrcweir 578*cdf0e10cSrcweir } // namespace store 579*cdf0e10cSrcweir 580*cdf0e10cSrcweir MappedLockBytes::MappedLockBytes (FileMapping & rMapping) 581*cdf0e10cSrcweir : m_pData (rMapping.m_pAddr), m_nSize (rMapping.m_nSize), m_nPageSize(0) 582*cdf0e10cSrcweir { 583*cdf0e10cSrcweir } 584*cdf0e10cSrcweir 585*cdf0e10cSrcweir MappedLockBytes::~MappedLockBytes() 586*cdf0e10cSrcweir { 587*cdf0e10cSrcweir FileMapping::unmapFile (m_pData, m_nSize); 588*cdf0e10cSrcweir } 589*cdf0e10cSrcweir 590*cdf0e10cSrcweir oslInterlockedCount SAL_CALL MappedLockBytes::acquire() 591*cdf0e10cSrcweir { 592*cdf0e10cSrcweir return OStoreObject::acquire(); 593*cdf0e10cSrcweir } 594*cdf0e10cSrcweir 595*cdf0e10cSrcweir oslInterlockedCount SAL_CALL MappedLockBytes::release() 596*cdf0e10cSrcweir { 597*cdf0e10cSrcweir return OStoreObject::release(); 598*cdf0e10cSrcweir } 599*cdf0e10cSrcweir 600*cdf0e10cSrcweir void MappedLockBytes::allocate_Impl (void ** ppPage, sal_uInt16 * pnSize) 601*cdf0e10cSrcweir { 602*cdf0e10cSrcweir OSL_PRECOND((ppPage != 0) && (pnSize != 0), "contract violation"); 603*cdf0e10cSrcweir if ((ppPage != 0) && (pnSize != 0)) 604*cdf0e10cSrcweir *ppPage = 0, *pnSize = m_nPageSize; 605*cdf0e10cSrcweir } 606*cdf0e10cSrcweir 607*cdf0e10cSrcweir void MappedLockBytes::deallocate_Impl (void * pPage) 608*cdf0e10cSrcweir { 609*cdf0e10cSrcweir OSL_PRECOND((m_pData <= pPage) && (pPage < m_pData + m_nSize), "contract violation"); 610*cdf0e10cSrcweir (void)pPage; // UNUSED 611*cdf0e10cSrcweir } 612*cdf0e10cSrcweir 613*cdf0e10cSrcweir storeError MappedLockBytes::initialize_Impl (rtl::Reference< PageData::Allocator > & rxAllocator, sal_uInt16 nPageSize) 614*cdf0e10cSrcweir { 615*cdf0e10cSrcweir rxAllocator = this; 616*cdf0e10cSrcweir m_nPageSize = nPageSize; 617*cdf0e10cSrcweir return store_E_None; 618*cdf0e10cSrcweir } 619*cdf0e10cSrcweir 620*cdf0e10cSrcweir storeError MappedLockBytes::readPageAt_Impl (PageHolder & rPage, sal_uInt32 nOffset) 621*cdf0e10cSrcweir { 622*cdf0e10cSrcweir sal_uInt8 * src_lo = m_pData + nOffset; 623*cdf0e10cSrcweir if ((m_pData > src_lo) || (src_lo >= m_pData + m_nSize)) 624*cdf0e10cSrcweir return store_E_NotExists; 625*cdf0e10cSrcweir 626*cdf0e10cSrcweir sal_uInt8 * src_hi = src_lo + m_nPageSize; 627*cdf0e10cSrcweir if ((m_pData > src_hi) || (src_hi > m_pData + m_nSize)) 628*cdf0e10cSrcweir return store_E_CantRead; 629*cdf0e10cSrcweir 630*cdf0e10cSrcweir PageHolder page (reinterpret_cast< PageData* >(src_lo), static_cast< PageData::Allocator* >(this)); 631*cdf0e10cSrcweir page.swap (rPage); 632*cdf0e10cSrcweir 633*cdf0e10cSrcweir return store_E_None; 634*cdf0e10cSrcweir } 635*cdf0e10cSrcweir 636*cdf0e10cSrcweir storeError MappedLockBytes::writePageAt_Impl (PageHolder const & /*rPage*/, sal_uInt32 /*nOffset*/) 637*cdf0e10cSrcweir { 638*cdf0e10cSrcweir return store_E_AccessViolation; 639*cdf0e10cSrcweir } 640*cdf0e10cSrcweir 641*cdf0e10cSrcweir storeError MappedLockBytes::readAt_Impl (sal_uInt32 nOffset, void * pBuffer, sal_uInt32 nBytes) 642*cdf0e10cSrcweir { 643*cdf0e10cSrcweir sal_uInt8 const * src_lo = m_pData + nOffset; 644*cdf0e10cSrcweir if ((m_pData > src_lo) || (src_lo >= m_pData + m_nSize)) 645*cdf0e10cSrcweir return store_E_NotExists; 646*cdf0e10cSrcweir 647*cdf0e10cSrcweir sal_uInt8 const * src_hi = src_lo + nBytes; 648*cdf0e10cSrcweir if ((m_pData > src_hi) || (src_hi > m_pData + m_nSize)) 649*cdf0e10cSrcweir return store_E_CantRead; 650*cdf0e10cSrcweir 651*cdf0e10cSrcweir memcpy (pBuffer, src_lo, (src_hi - src_lo)); 652*cdf0e10cSrcweir return store_E_None; 653*cdf0e10cSrcweir } 654*cdf0e10cSrcweir 655*cdf0e10cSrcweir storeError MappedLockBytes::writeAt_Impl (sal_uInt32 /*nOffset*/, void const * /*pBuffer*/, sal_uInt32 /*nBytes*/) 656*cdf0e10cSrcweir { 657*cdf0e10cSrcweir return store_E_AccessViolation; 658*cdf0e10cSrcweir } 659*cdf0e10cSrcweir 660*cdf0e10cSrcweir storeError MappedLockBytes::getSize_Impl (sal_uInt32 & rnSize) 661*cdf0e10cSrcweir { 662*cdf0e10cSrcweir rnSize = m_nSize; 663*cdf0e10cSrcweir return store_E_None; 664*cdf0e10cSrcweir } 665*cdf0e10cSrcweir 666*cdf0e10cSrcweir storeError MappedLockBytes::setSize_Impl (sal_uInt32 /*nSize*/) 667*cdf0e10cSrcweir { 668*cdf0e10cSrcweir return store_E_AccessViolation; 669*cdf0e10cSrcweir } 670*cdf0e10cSrcweir 671*cdf0e10cSrcweir storeError MappedLockBytes::flush_Impl() 672*cdf0e10cSrcweir { 673*cdf0e10cSrcweir return store_E_None; 674*cdf0e10cSrcweir } 675*cdf0e10cSrcweir 676*cdf0e10cSrcweir /*======================================================================== 677*cdf0e10cSrcweir * 678*cdf0e10cSrcweir * MemoryLockBytes implementation. 679*cdf0e10cSrcweir * 680*cdf0e10cSrcweir *======================================================================*/ 681*cdf0e10cSrcweir namespace store 682*cdf0e10cSrcweir { 683*cdf0e10cSrcweir 684*cdf0e10cSrcweir class MemoryLockBytes : 685*cdf0e10cSrcweir public store::OStoreObject, 686*cdf0e10cSrcweir public store::ILockBytes 687*cdf0e10cSrcweir { 688*cdf0e10cSrcweir /** Representation. 689*cdf0e10cSrcweir */ 690*cdf0e10cSrcweir sal_uInt8 * m_pData; 691*cdf0e10cSrcweir sal_uInt32 m_nSize; 692*cdf0e10cSrcweir rtl::Reference< PageData::Allocator > m_xAllocator; 693*cdf0e10cSrcweir 694*cdf0e10cSrcweir /** ILockBytes implementation. 695*cdf0e10cSrcweir */ 696*cdf0e10cSrcweir virtual storeError initialize_Impl (rtl::Reference< PageData::Allocator > & rxAllocator, sal_uInt16 nPageSize); 697*cdf0e10cSrcweir 698*cdf0e10cSrcweir virtual storeError readPageAt_Impl (PageHolder & rPage, sal_uInt32 nOffset); 699*cdf0e10cSrcweir virtual storeError writePageAt_Impl (PageHolder const & rPage, sal_uInt32 nOffset); 700*cdf0e10cSrcweir 701*cdf0e10cSrcweir virtual storeError readAt_Impl (sal_uInt32 nOffset, void * pBuffer, sal_uInt32 nBytes); 702*cdf0e10cSrcweir virtual storeError writeAt_Impl (sal_uInt32 nOffset, const void * pBuffer, sal_uInt32 nBytes); 703*cdf0e10cSrcweir 704*cdf0e10cSrcweir virtual storeError getSize_Impl (sal_uInt32 & rnSize); 705*cdf0e10cSrcweir virtual storeError setSize_Impl (sal_uInt32 nSize); 706*cdf0e10cSrcweir 707*cdf0e10cSrcweir virtual storeError flush_Impl(); 708*cdf0e10cSrcweir 709*cdf0e10cSrcweir /** Not implemented. 710*cdf0e10cSrcweir */ 711*cdf0e10cSrcweir MemoryLockBytes (MemoryLockBytes const &); 712*cdf0e10cSrcweir MemoryLockBytes& operator= (MemoryLockBytes const &); 713*cdf0e10cSrcweir 714*cdf0e10cSrcweir public: 715*cdf0e10cSrcweir /** Construction. 716*cdf0e10cSrcweir */ 717*cdf0e10cSrcweir MemoryLockBytes(); 718*cdf0e10cSrcweir 719*cdf0e10cSrcweir /** Delegate multiple inherited IReference. 720*cdf0e10cSrcweir */ 721*cdf0e10cSrcweir virtual oslInterlockedCount SAL_CALL acquire(); 722*cdf0e10cSrcweir virtual oslInterlockedCount SAL_CALL release(); 723*cdf0e10cSrcweir 724*cdf0e10cSrcweir protected: 725*cdf0e10cSrcweir /** Destruction. 726*cdf0e10cSrcweir */ 727*cdf0e10cSrcweir virtual ~MemoryLockBytes(); 728*cdf0e10cSrcweir }; 729*cdf0e10cSrcweir 730*cdf0e10cSrcweir } // namespace store 731*cdf0e10cSrcweir 732*cdf0e10cSrcweir MemoryLockBytes::MemoryLockBytes() 733*cdf0e10cSrcweir : m_pData (0), m_nSize (0), m_xAllocator() 734*cdf0e10cSrcweir {} 735*cdf0e10cSrcweir 736*cdf0e10cSrcweir MemoryLockBytes::~MemoryLockBytes() 737*cdf0e10cSrcweir { 738*cdf0e10cSrcweir rtl_freeMemory (m_pData); 739*cdf0e10cSrcweir } 740*cdf0e10cSrcweir 741*cdf0e10cSrcweir oslInterlockedCount SAL_CALL MemoryLockBytes::acquire (void) 742*cdf0e10cSrcweir { 743*cdf0e10cSrcweir return OStoreObject::acquire(); 744*cdf0e10cSrcweir } 745*cdf0e10cSrcweir 746*cdf0e10cSrcweir oslInterlockedCount SAL_CALL MemoryLockBytes::release (void) 747*cdf0e10cSrcweir { 748*cdf0e10cSrcweir return OStoreObject::release(); 749*cdf0e10cSrcweir } 750*cdf0e10cSrcweir 751*cdf0e10cSrcweir storeError MemoryLockBytes::initialize_Impl (rtl::Reference< PageData::Allocator > & rxAllocator, sal_uInt16 nPageSize) 752*cdf0e10cSrcweir { 753*cdf0e10cSrcweir storeError result = PageData::Allocator::createInstance (rxAllocator, nPageSize); 754*cdf0e10cSrcweir if (result == store_E_None) 755*cdf0e10cSrcweir { 756*cdf0e10cSrcweir // @see readPageAt_Impl(). 757*cdf0e10cSrcweir m_xAllocator = rxAllocator; 758*cdf0e10cSrcweir } 759*cdf0e10cSrcweir return result; 760*cdf0e10cSrcweir } 761*cdf0e10cSrcweir 762*cdf0e10cSrcweir storeError MemoryLockBytes::readPageAt_Impl (PageHolder & rPage, sal_uInt32 nOffset) 763*cdf0e10cSrcweir { 764*cdf0e10cSrcweir if (m_xAllocator.is()) 765*cdf0e10cSrcweir { 766*cdf0e10cSrcweir PageHolder page (m_xAllocator->construct<PageData>(), m_xAllocator); 767*cdf0e10cSrcweir page.swap (rPage); 768*cdf0e10cSrcweir } 769*cdf0e10cSrcweir 770*cdf0e10cSrcweir if (!m_xAllocator.is()) 771*cdf0e10cSrcweir return store_E_InvalidAccess; 772*cdf0e10cSrcweir if (!rPage.get()) 773*cdf0e10cSrcweir return store_E_OutOfMemory; 774*cdf0e10cSrcweir 775*cdf0e10cSrcweir PageData * pagedata = rPage.get(); 776*cdf0e10cSrcweir return readAt_Impl (nOffset, pagedata, pagedata->size()); 777*cdf0e10cSrcweir } 778*cdf0e10cSrcweir 779*cdf0e10cSrcweir storeError MemoryLockBytes::writePageAt_Impl (PageHolder const & rPage, sal_uInt32 nOffset) 780*cdf0e10cSrcweir { 781*cdf0e10cSrcweir PageData const * pagedata = rPage.get(); 782*cdf0e10cSrcweir OSL_PRECOND(!(pagedata == 0), "contract violation"); 783*cdf0e10cSrcweir return writeAt_Impl (nOffset, pagedata, pagedata->size()); 784*cdf0e10cSrcweir } 785*cdf0e10cSrcweir 786*cdf0e10cSrcweir storeError MemoryLockBytes::readAt_Impl (sal_uInt32 nOffset, void * pBuffer, sal_uInt32 nBytes) 787*cdf0e10cSrcweir { 788*cdf0e10cSrcweir sal_uInt8 const * src_lo = m_pData + nOffset; 789*cdf0e10cSrcweir if ((m_pData > src_lo) || (src_lo >= m_pData + m_nSize)) 790*cdf0e10cSrcweir return store_E_NotExists; 791*cdf0e10cSrcweir 792*cdf0e10cSrcweir sal_uInt8 const * src_hi = src_lo + nBytes; 793*cdf0e10cSrcweir if ((m_pData > src_hi) || (src_hi > m_pData + m_nSize)) 794*cdf0e10cSrcweir return store_E_CantRead; 795*cdf0e10cSrcweir 796*cdf0e10cSrcweir memcpy (pBuffer, src_lo, (src_hi - src_lo)); 797*cdf0e10cSrcweir return store_E_None; 798*cdf0e10cSrcweir } 799*cdf0e10cSrcweir 800*cdf0e10cSrcweir storeError MemoryLockBytes::writeAt_Impl (sal_uInt32 nOffset, const void * pBuffer, sal_uInt32 nBytes) 801*cdf0e10cSrcweir { 802*cdf0e10cSrcweir sal_uInt64 const dst_size = nOffset + nBytes; 803*cdf0e10cSrcweir OSL_PRECOND(dst_size < SAL_MAX_UINT32, "store::ILockBytes::writeAt() contract violation"); 804*cdf0e10cSrcweir if (dst_size > m_nSize) 805*cdf0e10cSrcweir { 806*cdf0e10cSrcweir storeError eErrCode = setSize_Impl (sal::static_int_cast<sal_uInt32>(dst_size)); 807*cdf0e10cSrcweir if (eErrCode != store_E_None) 808*cdf0e10cSrcweir return eErrCode; 809*cdf0e10cSrcweir } 810*cdf0e10cSrcweir OSL_POSTCOND(dst_size <= m_nSize, "store::MemoryLockBytes::setSize_Impl() contract violation"); 811*cdf0e10cSrcweir 812*cdf0e10cSrcweir sal_uInt8 * dst_lo = m_pData + nOffset; 813*cdf0e10cSrcweir if (dst_lo >= m_pData + m_nSize) 814*cdf0e10cSrcweir return store_E_CantSeek; 815*cdf0e10cSrcweir 816*cdf0e10cSrcweir sal_uInt8 * dst_hi = dst_lo + nBytes; 817*cdf0e10cSrcweir if (dst_hi > m_pData + m_nSize) 818*cdf0e10cSrcweir return store_E_CantWrite; 819*cdf0e10cSrcweir 820*cdf0e10cSrcweir memcpy (dst_lo, pBuffer, (dst_hi - dst_lo)); 821*cdf0e10cSrcweir return store_E_None; 822*cdf0e10cSrcweir } 823*cdf0e10cSrcweir 824*cdf0e10cSrcweir storeError MemoryLockBytes::getSize_Impl (sal_uInt32 & rnSize) 825*cdf0e10cSrcweir { 826*cdf0e10cSrcweir rnSize = m_nSize; 827*cdf0e10cSrcweir return store_E_None; 828*cdf0e10cSrcweir } 829*cdf0e10cSrcweir 830*cdf0e10cSrcweir storeError MemoryLockBytes::setSize_Impl (sal_uInt32 nSize) 831*cdf0e10cSrcweir { 832*cdf0e10cSrcweir if (nSize != m_nSize) 833*cdf0e10cSrcweir { 834*cdf0e10cSrcweir sal_uInt8 * pData = reinterpret_cast<sal_uInt8*>(rtl_reallocateMemory (m_pData, nSize)); 835*cdf0e10cSrcweir if (pData != 0) 836*cdf0e10cSrcweir { 837*cdf0e10cSrcweir if (nSize > m_nSize) 838*cdf0e10cSrcweir memset (pData + m_nSize, 0, sal::static_int_cast<size_t>(nSize - m_nSize)); 839*cdf0e10cSrcweir } 840*cdf0e10cSrcweir else 841*cdf0e10cSrcweir { 842*cdf0e10cSrcweir if (nSize != 0) 843*cdf0e10cSrcweir return store_E_OutOfMemory; 844*cdf0e10cSrcweir } 845*cdf0e10cSrcweir m_pData = pData, m_nSize = nSize; 846*cdf0e10cSrcweir } 847*cdf0e10cSrcweir return store_E_None; 848*cdf0e10cSrcweir } 849*cdf0e10cSrcweir 850*cdf0e10cSrcweir storeError MemoryLockBytes::flush_Impl() 851*cdf0e10cSrcweir { 852*cdf0e10cSrcweir return store_E_None; 853*cdf0e10cSrcweir } 854*cdf0e10cSrcweir 855*cdf0e10cSrcweir /*======================================================================== 856*cdf0e10cSrcweir * 857*cdf0e10cSrcweir * ILockBytes factory implementations. 858*cdf0e10cSrcweir * 859*cdf0e10cSrcweir *======================================================================*/ 860*cdf0e10cSrcweir namespace store 861*cdf0e10cSrcweir { 862*cdf0e10cSrcweir 863*cdf0e10cSrcweir template< class T > struct ResourceHolder 864*cdf0e10cSrcweir { 865*cdf0e10cSrcweir typedef typename T::destructor_type destructor_type; 866*cdf0e10cSrcweir 867*cdf0e10cSrcweir T m_value; 868*cdf0e10cSrcweir 869*cdf0e10cSrcweir explicit ResourceHolder (T const & value = T()) : m_value (value) {} 870*cdf0e10cSrcweir ~ResourceHolder() { reset(); } 871*cdf0e10cSrcweir 872*cdf0e10cSrcweir T & get() { return m_value; } 873*cdf0e10cSrcweir T const & get() const { return m_value; } 874*cdf0e10cSrcweir 875*cdf0e10cSrcweir void set (T const & value) { m_value = value; } 876*cdf0e10cSrcweir void reset (T const & value = T()) 877*cdf0e10cSrcweir { 878*cdf0e10cSrcweir T tmp (m_value); 879*cdf0e10cSrcweir if (tmp != value) 880*cdf0e10cSrcweir destructor_type()(tmp); 881*cdf0e10cSrcweir set (value); 882*cdf0e10cSrcweir } 883*cdf0e10cSrcweir T release() 884*cdf0e10cSrcweir { 885*cdf0e10cSrcweir T tmp (m_value); 886*cdf0e10cSrcweir set (T()); 887*cdf0e10cSrcweir return tmp; 888*cdf0e10cSrcweir } 889*cdf0e10cSrcweir 890*cdf0e10cSrcweir ResourceHolder (ResourceHolder & rhs) 891*cdf0e10cSrcweir { 892*cdf0e10cSrcweir set (rhs.release()); 893*cdf0e10cSrcweir } 894*cdf0e10cSrcweir ResourceHolder & operator= (ResourceHolder & rhs) 895*cdf0e10cSrcweir { 896*cdf0e10cSrcweir reset (rhs.release()); 897*cdf0e10cSrcweir return *this; 898*cdf0e10cSrcweir } 899*cdf0e10cSrcweir }; 900*cdf0e10cSrcweir 901*cdf0e10cSrcweir storeError 902*cdf0e10cSrcweir FileLockBytes_createInstance ( 903*cdf0e10cSrcweir rtl::Reference< ILockBytes > & rxLockBytes, 904*cdf0e10cSrcweir rtl_uString * pFilename, 905*cdf0e10cSrcweir storeAccessMode eAccessMode 906*cdf0e10cSrcweir ) 907*cdf0e10cSrcweir { 908*cdf0e10cSrcweir // Acquire file handle. 909*cdf0e10cSrcweir ResourceHolder<FileHandle> xFile; 910*cdf0e10cSrcweir storeError result = xFile.get().initialize (pFilename, eAccessMode); 911*cdf0e10cSrcweir if (result != store_E_None) 912*cdf0e10cSrcweir return (result); 913*cdf0e10cSrcweir 914*cdf0e10cSrcweir if (eAccessMode == store_AccessReadOnly) 915*cdf0e10cSrcweir { 916*cdf0e10cSrcweir ResourceHolder<FileMapping> xMapping; 917*cdf0e10cSrcweir if (xMapping.get().initialize (xFile.get().m_handle) == osl_File_E_None) 918*cdf0e10cSrcweir { 919*cdf0e10cSrcweir rxLockBytes = new MappedLockBytes (xMapping.get()); 920*cdf0e10cSrcweir if (!rxLockBytes.is()) 921*cdf0e10cSrcweir return store_E_OutOfMemory; 922*cdf0e10cSrcweir (void) xMapping.release(); 923*cdf0e10cSrcweir } 924*cdf0e10cSrcweir } 925*cdf0e10cSrcweir if (!rxLockBytes.is()) 926*cdf0e10cSrcweir { 927*cdf0e10cSrcweir rxLockBytes = new FileLockBytes (xFile.get()); 928*cdf0e10cSrcweir if (!rxLockBytes.is()) 929*cdf0e10cSrcweir return store_E_OutOfMemory; 930*cdf0e10cSrcweir (void) xFile.release(); 931*cdf0e10cSrcweir } 932*cdf0e10cSrcweir 933*cdf0e10cSrcweir return store_E_None; 934*cdf0e10cSrcweir } 935*cdf0e10cSrcweir 936*cdf0e10cSrcweir storeError 937*cdf0e10cSrcweir MemoryLockBytes_createInstance ( 938*cdf0e10cSrcweir rtl::Reference< ILockBytes > & rxLockBytes 939*cdf0e10cSrcweir ) 940*cdf0e10cSrcweir { 941*cdf0e10cSrcweir rxLockBytes = new MemoryLockBytes(); 942*cdf0e10cSrcweir if (!rxLockBytes.is()) 943*cdf0e10cSrcweir return store_E_OutOfMemory; 944*cdf0e10cSrcweir 945*cdf0e10cSrcweir return store_E_None; 946*cdf0e10cSrcweir } 947*cdf0e10cSrcweir 948*cdf0e10cSrcweir } // namespace store 949