1*cdf0e10cSrcweir /* 2*cdf0e10cSrcweir * t_page.cxx 3*cdf0e10cSrcweir */ 4*cdf0e10cSrcweir 5*cdf0e10cSrcweir #include "osl/diagnose.h" 6*cdf0e10cSrcweir #include "rtl/alloc.h" 7*cdf0e10cSrcweir #include "rtl/ref.hxx" 8*cdf0e10cSrcweir 9*cdf0e10cSrcweir #include "storbase.hxx" 10*cdf0e10cSrcweir 11*cdf0e10cSrcweir #include "osl/file.h" 12*cdf0e10cSrcweir #include "rtl/ustring.hxx" 13*cdf0e10cSrcweir 14*cdf0e10cSrcweir /*======================================================================== 15*cdf0e10cSrcweir * 16*cdf0e10cSrcweir * OTest... 17*cdf0e10cSrcweir * 18*cdf0e10cSrcweir *======================================================================*/ 19*cdf0e10cSrcweir 20*cdf0e10cSrcweir template< class T > void swap (T & lhs, T & rhs) 21*cdf0e10cSrcweir { 22*cdf0e10cSrcweir T tmp = rhs; rhs = lhs; lhs = tmp; 23*cdf0e10cSrcweir } 24*cdf0e10cSrcweir 25*cdf0e10cSrcweir /*======================================================================*/ 26*cdf0e10cSrcweir 27*cdf0e10cSrcweir class SharedCount 28*cdf0e10cSrcweir { 29*cdf0e10cSrcweir long * m_pCount; 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir class Allocator 32*cdf0e10cSrcweir { 33*cdf0e10cSrcweir rtl_cache_type * m_cache; 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir public: 36*cdf0e10cSrcweir static Allocator & get(); 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir long * alloc() 39*cdf0e10cSrcweir { 40*cdf0e10cSrcweir return static_cast<long*>(rtl_cache_alloc (m_cache)); 41*cdf0e10cSrcweir } 42*cdf0e10cSrcweir void free (long * pCount) 43*cdf0e10cSrcweir { 44*cdf0e10cSrcweir rtl_cache_free (m_cache, pCount); 45*cdf0e10cSrcweir } 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir protected: 48*cdf0e10cSrcweir Allocator(); 49*cdf0e10cSrcweir ~Allocator(); 50*cdf0e10cSrcweir }; 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir public: 53*cdf0e10cSrcweir SharedCount() 54*cdf0e10cSrcweir : m_pCount(Allocator::get().alloc()) 55*cdf0e10cSrcweir { 56*cdf0e10cSrcweir if (m_pCount != 0) (*m_pCount) = 1; 57*cdf0e10cSrcweir } 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir ~SharedCount() 60*cdf0e10cSrcweir { 61*cdf0e10cSrcweir if (m_pCount != 0) 62*cdf0e10cSrcweir { 63*cdf0e10cSrcweir long new_count = --(*m_pCount); 64*cdf0e10cSrcweir if (new_count == 0) 65*cdf0e10cSrcweir Allocator::get().free(m_pCount); 66*cdf0e10cSrcweir } 67*cdf0e10cSrcweir } 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir bool operator== (long count) const 70*cdf0e10cSrcweir { 71*cdf0e10cSrcweir return (m_pCount != 0) ? *m_pCount == count : false; 72*cdf0e10cSrcweir } 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir friend void swap<> (SharedCount & lhs, SharedCount & rhs); // nothrow 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir SharedCount (SharedCount const & rhs); // nothrow 77*cdf0e10cSrcweir SharedCount & operator= (SharedCount const & rhs); // nothrow 78*cdf0e10cSrcweir }; 79*cdf0e10cSrcweir 80*cdf0e10cSrcweir template<> 81*cdf0e10cSrcweir inline void swap (SharedCount & lhs, SharedCount & rhs) // nothrow 82*cdf0e10cSrcweir { 83*cdf0e10cSrcweir swap<long*>(lhs.m_pCount, rhs.m_pCount); 84*cdf0e10cSrcweir } 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir SharedCount::SharedCount (SharedCount const & rhs) // nothrow 87*cdf0e10cSrcweir : m_pCount (rhs.m_pCount) 88*cdf0e10cSrcweir { 89*cdf0e10cSrcweir if (m_pCount != 0) ++(*m_pCount); 90*cdf0e10cSrcweir } 91*cdf0e10cSrcweir 92*cdf0e10cSrcweir SharedCount & 93*cdf0e10cSrcweir SharedCount::operator= (SharedCount const & rhs) // nothrow 94*cdf0e10cSrcweir { 95*cdf0e10cSrcweir SharedCount tmp(rhs); 96*cdf0e10cSrcweir swap<SharedCount>(tmp, *this); 97*cdf0e10cSrcweir return *this; 98*cdf0e10cSrcweir } 99*cdf0e10cSrcweir 100*cdf0e10cSrcweir SharedCount::Allocator & 101*cdf0e10cSrcweir SharedCount::Allocator::get() 102*cdf0e10cSrcweir { 103*cdf0e10cSrcweir static Allocator g_aSharedCountAllocator; 104*cdf0e10cSrcweir return g_aSharedCountAllocator; 105*cdf0e10cSrcweir } 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir SharedCount::Allocator::Allocator() 108*cdf0e10cSrcweir { 109*cdf0e10cSrcweir m_cache = rtl_cache_create ( 110*cdf0e10cSrcweir "store_shared_count_cache", 111*cdf0e10cSrcweir sizeof(long), 112*cdf0e10cSrcweir 0, // objalign 113*cdf0e10cSrcweir 0, // constructor 114*cdf0e10cSrcweir 0, // destructor 115*cdf0e10cSrcweir 0, // reclaim 116*cdf0e10cSrcweir 0, // userarg 117*cdf0e10cSrcweir 0, // default source 118*cdf0e10cSrcweir 0 // flags 119*cdf0e10cSrcweir ); 120*cdf0e10cSrcweir } 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir SharedCount::Allocator::~Allocator() 123*cdf0e10cSrcweir { 124*cdf0e10cSrcweir rtl_cache_destroy (m_cache), m_cache = 0; 125*cdf0e10cSrcweir } 126*cdf0e10cSrcweir 127*cdf0e10cSrcweir /*======================================================================*/ 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir #if 0 /* OLD */ 130*cdf0e10cSrcweir 131*cdf0e10cSrcweir typedef store::OStorePageData PageData; 132*cdf0e10cSrcweir 133*cdf0e10cSrcweir #else /* NEW */ 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir #if defined(OSL_BIGENDIAN) 136*cdf0e10cSrcweir #define STORE_DWORD(dword) OSL_SWAPDWORD((dword)) 137*cdf0e10cSrcweir #else 138*cdf0e10cSrcweir #define STORE_DWORD(dword) (dword) 139*cdf0e10cSrcweir #endif 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir struct PageData 142*cdf0e10cSrcweir { 143*cdf0e10cSrcweir typedef store::OStorePageGuard G; 144*cdf0e10cSrcweir typedef store::OStorePageDescriptor D; 145*cdf0e10cSrcweir typedef store::OStorePageLink L; 146*cdf0e10cSrcweir 147*cdf0e10cSrcweir /** Representation. 148*cdf0e10cSrcweir */ 149*cdf0e10cSrcweir G m_aGuard; 150*cdf0e10cSrcweir D m_aDescr; 151*cdf0e10cSrcweir L m_aMarked; 152*cdf0e10cSrcweir L m_aUnused; 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir /** theSize. 155*cdf0e10cSrcweir */ 156*cdf0e10cSrcweir static const size_t theSize = sizeof(G) + sizeof(D) + 2 * sizeof(L); 157*cdf0e10cSrcweir static const sal_uInt16 thePageSize = theSize; 158*cdf0e10cSrcweir STORE_STATIC_ASSERT(STORE_MINIMUM_PAGESIZE >= thePageSize); 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir /** type. 161*cdf0e10cSrcweir */ 162*cdf0e10cSrcweir sal_uInt32 type() const { return m_aGuard.m_nMagic; /* @@@ */ } 163*cdf0e10cSrcweir 164*cdf0e10cSrcweir /** offset. 165*cdf0e10cSrcweir */ 166*cdf0e10cSrcweir sal_uInt32 offset() const { return m_aDescr.m_nAddr; /* @@@ */ } 167*cdf0e10cSrcweir void offset (sal_uInt32 nOffset) { m_aDescr.m_nAddr = nOffset; } 168*cdf0e10cSrcweir 169*cdf0e10cSrcweir /** size. 170*cdf0e10cSrcweir */ 171*cdf0e10cSrcweir sal_uInt16 size() const { return m_aDescr.m_nSize; /* @@@ */ } 172*cdf0e10cSrcweir 173*cdf0e10cSrcweir /** Allocation. 174*cdf0e10cSrcweir */ 175*cdf0e10cSrcweir class Allocator : public rtl::IReference 176*cdf0e10cSrcweir { 177*cdf0e10cSrcweir public: 178*cdf0e10cSrcweir template< class T > T * construct() 179*cdf0e10cSrcweir { 180*cdf0e10cSrcweir void * page = 0; sal_uInt16 size = 0; 181*cdf0e10cSrcweir if (allocate (&page, &size)) 182*cdf0e10cSrcweir { 183*cdf0e10cSrcweir return new(page) T(size); 184*cdf0e10cSrcweir } 185*cdf0e10cSrcweir return 0; 186*cdf0e10cSrcweir } 187*cdf0e10cSrcweir 188*cdf0e10cSrcweir virtual bool allocate (void ** ppPage, sal_uInt16 * pnSize) = 0; 189*cdf0e10cSrcweir virtual void deallocate (void * pPage) = 0; 190*cdf0e10cSrcweir }; 191*cdf0e10cSrcweir 192*cdf0e10cSrcweir static void * operator new (size_t, void * p) { return p; } 193*cdf0e10cSrcweir static void operator delete (void *, void *) {} 194*cdf0e10cSrcweir 195*cdf0e10cSrcweir /** Construction. 196*cdf0e10cSrcweir */ 197*cdf0e10cSrcweir explicit PageData (sal_uInt16 nPageSize = thePageSize) 198*cdf0e10cSrcweir : m_aDescr (STORE_PAGE_NULL, nPageSize, thePageSize) 199*cdf0e10cSrcweir {} 200*cdf0e10cSrcweir 201*cdf0e10cSrcweir /** ... 202*cdf0e10cSrcweir */ 203*cdf0e10cSrcweir void guard() 204*cdf0e10cSrcweir {} 205*cdf0e10cSrcweir 206*cdf0e10cSrcweir storeError verify() const 207*cdf0e10cSrcweir { 208*cdf0e10cSrcweir return store_E_None; 209*cdf0e10cSrcweir } 210*cdf0e10cSrcweir }; 211*cdf0e10cSrcweir 212*cdf0e10cSrcweir #endif /* NEW */ 213*cdf0e10cSrcweir 214*cdf0e10cSrcweir class IPageAllocator 215*cdf0e10cSrcweir { 216*cdf0e10cSrcweir public: 217*cdf0e10cSrcweir virtual void deallocate (void * p) = 0; 218*cdf0e10cSrcweir }; 219*cdf0e10cSrcweir 220*cdf0e10cSrcweir class PageAllocator 221*cdf0e10cSrcweir { 222*cdf0e10cSrcweir rtl_cache_type * m_cache; 223*cdf0e10cSrcweir SharedCount m_refcount; 224*cdf0e10cSrcweir 225*cdf0e10cSrcweir public: 226*cdf0e10cSrcweir PageAllocator() 227*cdf0e10cSrcweir : m_cache(0), m_refcount() 228*cdf0e10cSrcweir {} 229*cdf0e10cSrcweir 230*cdf0e10cSrcweir ~PageAllocator() 231*cdf0e10cSrcweir { 232*cdf0e10cSrcweir // NYI 233*cdf0e10cSrcweir if (m_refcount == 1) 234*cdf0e10cSrcweir { 235*cdf0e10cSrcweir } 236*cdf0e10cSrcweir } 237*cdf0e10cSrcweir 238*cdf0e10cSrcweir friend void swap<>(PageAllocator & lhs, PageAllocator & rhs); 239*cdf0e10cSrcweir 240*cdf0e10cSrcweir PageAllocator (PageAllocator const & rhs); 241*cdf0e10cSrcweir PageAllocator & operator= (PageAllocator const & rhs); 242*cdf0e10cSrcweir }; 243*cdf0e10cSrcweir 244*cdf0e10cSrcweir template<> 245*cdf0e10cSrcweir inline void swap (PageAllocator & lhs, PageAllocator & rhs) 246*cdf0e10cSrcweir { 247*cdf0e10cSrcweir swap<rtl_cache_type*>(lhs.m_cache, rhs.m_cache); 248*cdf0e10cSrcweir swap<SharedCount>(lhs.m_refcount, rhs.m_refcount); 249*cdf0e10cSrcweir } 250*cdf0e10cSrcweir 251*cdf0e10cSrcweir PageAllocator::PageAllocator (PageAllocator const & rhs) 252*cdf0e10cSrcweir : m_cache (rhs.m_cache), 253*cdf0e10cSrcweir m_refcount (rhs.m_refcount) 254*cdf0e10cSrcweir { 255*cdf0e10cSrcweir } 256*cdf0e10cSrcweir 257*cdf0e10cSrcweir PageAllocator & 258*cdf0e10cSrcweir PageAllocator::operator= (PageAllocator const & rhs) 259*cdf0e10cSrcweir { 260*cdf0e10cSrcweir PageAllocator tmp (rhs); 261*cdf0e10cSrcweir swap<PageAllocator>(tmp, *this); 262*cdf0e10cSrcweir return *this; 263*cdf0e10cSrcweir } 264*cdf0e10cSrcweir 265*cdf0e10cSrcweir /*======================================================================*/ 266*cdf0e10cSrcweir 267*cdf0e10cSrcweir class PageHolder 268*cdf0e10cSrcweir { 269*cdf0e10cSrcweir SharedCount m_refcount; 270*cdf0e10cSrcweir PageData * m_pagedata; 271*cdf0e10cSrcweir 272*cdf0e10cSrcweir typedef rtl::Reference< PageData::Allocator > allocator_type; 273*cdf0e10cSrcweir allocator_type m_allocator; 274*cdf0e10cSrcweir 275*cdf0e10cSrcweir public: 276*cdf0e10cSrcweir explicit PageHolder (PageData * pagedata = 0, allocator_type const & allocator = allocator_type()) 277*cdf0e10cSrcweir : m_refcount (), 278*cdf0e10cSrcweir m_pagedata (pagedata), 279*cdf0e10cSrcweir m_allocator(allocator) 280*cdf0e10cSrcweir {} 281*cdf0e10cSrcweir 282*cdf0e10cSrcweir ~PageHolder() 283*cdf0e10cSrcweir { 284*cdf0e10cSrcweir if ((m_refcount == 1) && (m_pagedata != 0) && m_allocator.is()) 285*cdf0e10cSrcweir { 286*cdf0e10cSrcweir // free pagedata. 287*cdf0e10cSrcweir m_allocator->deallocate (m_pagedata); 288*cdf0e10cSrcweir } 289*cdf0e10cSrcweir } 290*cdf0e10cSrcweir 291*cdf0e10cSrcweir PageData * get() { return m_pagedata; } 292*cdf0e10cSrcweir PageData const * get() const { return m_pagedata; } 293*cdf0e10cSrcweir 294*cdf0e10cSrcweir PageData * operator->() { return m_pagedata; } 295*cdf0e10cSrcweir PageData const * operator->() const { return m_pagedata; } 296*cdf0e10cSrcweir 297*cdf0e10cSrcweir friend void swap<> (PageHolder & lhs, PageHolder & rhs); // nothrow 298*cdf0e10cSrcweir 299*cdf0e10cSrcweir PageHolder (PageHolder const & rhs); // nothrow 300*cdf0e10cSrcweir PageHolder & operator= (PageHolder const & rhs); // nothrow 301*cdf0e10cSrcweir }; 302*cdf0e10cSrcweir 303*cdf0e10cSrcweir template<> 304*cdf0e10cSrcweir inline void swap (PageHolder & lhs, PageHolder & rhs) // nothrow 305*cdf0e10cSrcweir { 306*cdf0e10cSrcweir swap<SharedCount>(lhs.m_refcount, rhs.m_refcount); 307*cdf0e10cSrcweir swap<PageData*>(lhs.m_pagedata, rhs.m_pagedata); 308*cdf0e10cSrcweir swap<PageHolder::allocator_type>(lhs.m_allocator, rhs.m_allocator); 309*cdf0e10cSrcweir } 310*cdf0e10cSrcweir 311*cdf0e10cSrcweir PageHolder::PageHolder (PageHolder const & rhs) // nothrow 312*cdf0e10cSrcweir : m_refcount (rhs.m_refcount), 313*cdf0e10cSrcweir m_pagedata (rhs.m_pagedata), 314*cdf0e10cSrcweir m_allocator(rhs.m_allocator) 315*cdf0e10cSrcweir {} 316*cdf0e10cSrcweir 317*cdf0e10cSrcweir PageHolder & 318*cdf0e10cSrcweir PageHolder::operator= (PageHolder const & rhs) // nothrow 319*cdf0e10cSrcweir { 320*cdf0e10cSrcweir PageHolder tmp (rhs); 321*cdf0e10cSrcweir swap<PageHolder>(tmp, *this); 322*cdf0e10cSrcweir return *this; 323*cdf0e10cSrcweir } 324*cdf0e10cSrcweir 325*cdf0e10cSrcweir /*======================================================================*/ 326*cdf0e10cSrcweir 327*cdf0e10cSrcweir template< class T > 328*cdf0e10cSrcweir class PageHolderObject 329*cdf0e10cSrcweir { 330*cdf0e10cSrcweir protected: 331*cdf0e10cSrcweir /** Representation. 332*cdf0e10cSrcweir */ 333*cdf0e10cSrcweir PageHolder m_xPage; 334*cdf0e10cSrcweir 335*cdf0e10cSrcweir /** Checked cast. 336*cdf0e10cSrcweir */ 337*cdf0e10cSrcweir template< class U > 338*cdf0e10cSrcweir static bool isA (PageData const * p) 339*cdf0e10cSrcweir { 340*cdf0e10cSrcweir return ((p != 0) && (p->type() == U::theTypeId)); 341*cdf0e10cSrcweir } 342*cdf0e10cSrcweir 343*cdf0e10cSrcweir template< class U > 344*cdf0e10cSrcweir static U * dynamic_page_cast (PageData * p) 345*cdf0e10cSrcweir { 346*cdf0e10cSrcweir return isA<U>(p) ? static_cast<U*>(p) : 0; 347*cdf0e10cSrcweir } 348*cdf0e10cSrcweir 349*cdf0e10cSrcweir template< class U > 350*cdf0e10cSrcweir static U const * dynamic_page_cast (PageData const * p) 351*cdf0e10cSrcweir { 352*cdf0e10cSrcweir return isA<U>(p) ? static_cast<U const *>(p) : 0; 353*cdf0e10cSrcweir } 354*cdf0e10cSrcweir 355*cdf0e10cSrcweir public: 356*cdf0e10cSrcweir static PageHolderObject<T> construct (rtl::Reference< PageData::Allocator > const & rxAllocator) 357*cdf0e10cSrcweir { 358*cdf0e10cSrcweir PageHolderObject<T> tmp; 359*cdf0e10cSrcweir if (rxAllocator.is()) 360*cdf0e10cSrcweir { 361*cdf0e10cSrcweir PageHolder xPage (rxAllocator->construct<T>(), rxAllocator); 362*cdf0e10cSrcweir store::swap<PageHolder>(tmp.m_xPage, xPage); 363*cdf0e10cSrcweir } 364*cdf0e10cSrcweir return tmp; 365*cdf0e10cSrcweir } 366*cdf0e10cSrcweir 367*cdf0e10cSrcweir explicit PageHolderObject (PageHolder const & rxPage = PageHolder()) 368*cdf0e10cSrcweir : m_xPage (rxPage) 369*cdf0e10cSrcweir {} 370*cdf0e10cSrcweir 371*cdf0e10cSrcweir void swap (PageHolderObject<T> & rhs) 372*cdf0e10cSrcweir { 373*cdf0e10cSrcweir store::swap<PageHolder>(m_xPage, rhs.m_xPage); 374*cdf0e10cSrcweir } 375*cdf0e10cSrcweir 376*cdf0e10cSrcweir PageHolderObject (PageHolderObject<T> const & rhs) 377*cdf0e10cSrcweir : m_xPage (rhs.m_xPage) 378*cdf0e10cSrcweir { 379*cdf0e10cSrcweir } 380*cdf0e10cSrcweir 381*cdf0e10cSrcweir PageHolderObject<T> & operator= (PageHolderObject<T> const & rhs) 382*cdf0e10cSrcweir { 383*cdf0e10cSrcweir PageHolderObject<T> tmp (rhs); 384*cdf0e10cSrcweir this->swap(tmp); 385*cdf0e10cSrcweir return *this; 386*cdf0e10cSrcweir } 387*cdf0e10cSrcweir 388*cdf0e10cSrcweir T * operator->() 389*cdf0e10cSrcweir { 390*cdf0e10cSrcweir T * pImpl = dynamic_page_cast<T>(m_xPage.get()); 391*cdf0e10cSrcweir OSL_PRECOND(pImpl != 0, "store::PageHolder<T>::operator->(): Null pointer"); 392*cdf0e10cSrcweir return pImpl; 393*cdf0e10cSrcweir } 394*cdf0e10cSrcweir T const * operator->() const 395*cdf0e10cSrcweir { 396*cdf0e10cSrcweir T const * pImpl = dynamic_page_cast<T>(m_xPage.get()); 397*cdf0e10cSrcweir OSL_PRECOND(pImpl != 0, "store::PageHolder<T>::operator->(): Null pointer"); 398*cdf0e10cSrcweir return pImpl; 399*cdf0e10cSrcweir } 400*cdf0e10cSrcweir 401*cdf0e10cSrcweir T & operator*() 402*cdf0e10cSrcweir { 403*cdf0e10cSrcweir T * pImpl = dynamic_page_cast<T>(m_xPage.get()); 404*cdf0e10cSrcweir OSL_PRECOND(pImpl != 0, "store::PageHolder<T>::operator*(): Null pointer"); 405*cdf0e10cSrcweir return *pImpl; 406*cdf0e10cSrcweir } 407*cdf0e10cSrcweir T const & operator*() const 408*cdf0e10cSrcweir { 409*cdf0e10cSrcweir T const * pImpl = dynamic_page_cast<T>(m_xPage.get()); 410*cdf0e10cSrcweir OSL_PRECOND(pImpl != 0, "store::PageHolder<T>::operator*(): Null pointer"); 411*cdf0e10cSrcweir return *pImpl; 412*cdf0e10cSrcweir } 413*cdf0e10cSrcweir 414*cdf0e10cSrcweir static storeError guard (PageHolder & rxPage) 415*cdf0e10cSrcweir { 416*cdf0e10cSrcweir T * pImpl = dynamic_page_cast<T>(rxPage.get()); 417*cdf0e10cSrcweir if (pImpl != 0) 418*cdf0e10cSrcweir { pImpl->guard(); return store_E_None; } 419*cdf0e10cSrcweir else if (rxPage.get() != 0) 420*cdf0e10cSrcweir return store_E_WrongVersion; 421*cdf0e10cSrcweir else 422*cdf0e10cSrcweir return store_E_InvalidAccess; 423*cdf0e10cSrcweir } 424*cdf0e10cSrcweir static storeError verify (PageHolder const & rxPage) 425*cdf0e10cSrcweir { 426*cdf0e10cSrcweir T const * pImpl = dynamic_page_cast<T>(rxPage.get()); 427*cdf0e10cSrcweir if (pImpl != 0) 428*cdf0e10cSrcweir return pImpl->verify(); 429*cdf0e10cSrcweir else if (rxPage.get() != 0) 430*cdf0e10cSrcweir return store_E_WrongVersion; 431*cdf0e10cSrcweir else 432*cdf0e10cSrcweir return store_E_InvalidAccess; 433*cdf0e10cSrcweir } 434*cdf0e10cSrcweir }; 435*cdf0e10cSrcweir 436*cdf0e10cSrcweir /*======================================================================*/ 437*cdf0e10cSrcweir 438*cdf0e10cSrcweir class PageObject 439*cdf0e10cSrcweir { 440*cdf0e10cSrcweir public: 441*cdf0e10cSrcweir explicit PageObject (PageHolder const & rxPage = PageHolder()) 442*cdf0e10cSrcweir : m_xPage (rxPage) 443*cdf0e10cSrcweir {} 444*cdf0e10cSrcweir 445*cdf0e10cSrcweir virtual ~PageObject(); 446*cdf0e10cSrcweir 447*cdf0e10cSrcweir PageHolder & get() { return m_xPage; } 448*cdf0e10cSrcweir PageHolder const & get() const { return m_xPage; } 449*cdf0e10cSrcweir 450*cdf0e10cSrcweir PageData * operator->() 451*cdf0e10cSrcweir { 452*cdf0e10cSrcweir PageData * pImpl = m_xPage.get(); 453*cdf0e10cSrcweir OSL_PRECOND(pImpl != 0, "store::PageObject::operator->(): Null pointer"); 454*cdf0e10cSrcweir return pImpl; 455*cdf0e10cSrcweir } 456*cdf0e10cSrcweir PageData & operator*() 457*cdf0e10cSrcweir { 458*cdf0e10cSrcweir PageData * pImpl = m_xPage.get(); 459*cdf0e10cSrcweir OSL_PRECOND(pImpl != 0, "store::PageObject::operator*(): Null pointer"); 460*cdf0e10cSrcweir return *pImpl; 461*cdf0e10cSrcweir } 462*cdf0e10cSrcweir 463*cdf0e10cSrcweir virtual void guard(); 464*cdf0e10cSrcweir virtual storeError verify() const; 465*cdf0e10cSrcweir 466*cdf0e10cSrcweir protected: 467*cdf0e10cSrcweir PageHolder m_xPage; 468*cdf0e10cSrcweir }; 469*cdf0e10cSrcweir 470*cdf0e10cSrcweir PageObject::~PageObject() 471*cdf0e10cSrcweir {} 472*cdf0e10cSrcweir void PageObject::guard() 473*cdf0e10cSrcweir { 474*cdf0e10cSrcweir PageData * p = m_xPage.get(); 475*cdf0e10cSrcweir p->guard(); 476*cdf0e10cSrcweir } 477*cdf0e10cSrcweir storeError PageObject::verify() const 478*cdf0e10cSrcweir { 479*cdf0e10cSrcweir PageData const * p = m_xPage.get(); 480*cdf0e10cSrcweir return p->verify(); 481*cdf0e10cSrcweir } 482*cdf0e10cSrcweir 483*cdf0e10cSrcweir /*======================================================================*/ 484*cdf0e10cSrcweir 485*cdf0e10cSrcweir template< class T > 486*cdf0e10cSrcweir T * dynamic_page_cast (PageData * pagedata) 487*cdf0e10cSrcweir { 488*cdf0e10cSrcweir if ((pagedata != 0) && (pagedata->type() == T::theTypeId)) 489*cdf0e10cSrcweir return static_cast<T*>(pagedata); 490*cdf0e10cSrcweir return 0; 491*cdf0e10cSrcweir } 492*cdf0e10cSrcweir 493*cdf0e10cSrcweir template< class T > 494*cdf0e10cSrcweir T * dynamic_page_cast (PageData const * pagedata) 495*cdf0e10cSrcweir { 496*cdf0e10cSrcweir if ((pagedata != 0) && (pagedata->type() == T::theTypeId)) 497*cdf0e10cSrcweir return static_cast<T*>(pagedata); 498*cdf0e10cSrcweir return 0; 499*cdf0e10cSrcweir } 500*cdf0e10cSrcweir 501*cdf0e10cSrcweir /*======================================================================*/ 502*cdf0e10cSrcweir 503*cdf0e10cSrcweir class TestBIOS 504*cdf0e10cSrcweir { 505*cdf0e10cSrcweir public: 506*cdf0e10cSrcweir storeError loadPageAt (PageHolder & rPage, storeError (*pfnVerify)(PageHolder const &)) 507*cdf0e10cSrcweir { 508*cdf0e10cSrcweir return (pfnVerify)(rPage); 509*cdf0e10cSrcweir } 510*cdf0e10cSrcweir 511*cdf0e10cSrcweir storeError allocate (PageHolder & rxPage, ...) 512*cdf0e10cSrcweir { 513*cdf0e10cSrcweir // NYI: PageObject.save(nAddr, *this); 514*cdf0e10cSrcweir (void)rxPage; // NYI 515*cdf0e10cSrcweir return store_E_Unknown; // NYI 516*cdf0e10cSrcweir } 517*cdf0e10cSrcweir 518*cdf0e10cSrcweir storeError loadAt (PageHolder & rPage, sal_uInt32 nOffset) 519*cdf0e10cSrcweir { 520*cdf0e10cSrcweir (void)rPage; // NYI 521*cdf0e10cSrcweir (void)nOffset; // NYI 522*cdf0e10cSrcweir return store_E_Unknown; // NYI 523*cdf0e10cSrcweir } 524*cdf0e10cSrcweir storeError saveAt (PageHolder const & rPage, sal_uInt32 nOffset) 525*cdf0e10cSrcweir { 526*cdf0e10cSrcweir (void)rPage; // NYI 527*cdf0e10cSrcweir (void)nOffset; // NYI 528*cdf0e10cSrcweir return store_E_Unknown; // NYI 529*cdf0e10cSrcweir } 530*cdf0e10cSrcweir 531*cdf0e10cSrcweir template< class T > 532*cdf0e10cSrcweir storeError save (PageHolder & rxPage, sal_uInt32 nOffset) 533*cdf0e10cSrcweir { 534*cdf0e10cSrcweir storeError result = PageHolderObject<T>::guard (rxPage); 535*cdf0e10cSrcweir if (result != store_E_None) 536*cdf0e10cSrcweir return result; 537*cdf0e10cSrcweir return saveAt (rxPage, nOffset); 538*cdf0e10cSrcweir } 539*cdf0e10cSrcweir 540*cdf0e10cSrcweir storeError lookupAt (PageHolder & rPage, sal_uInt32 nOffset) 541*cdf0e10cSrcweir { 542*cdf0e10cSrcweir (void)rPage; // NYI 543*cdf0e10cSrcweir (void)nOffset; // NYI 544*cdf0e10cSrcweir return store_E_NotExists; 545*cdf0e10cSrcweir } 546*cdf0e10cSrcweir storeError replaceAt (PageHolder const & rPage, sal_uInt32 nOffset) 547*cdf0e10cSrcweir { 548*cdf0e10cSrcweir (void)rPage; // NYI 549*cdf0e10cSrcweir (void)nOffset; // NYI 550*cdf0e10cSrcweir return store_E_None; 551*cdf0e10cSrcweir } 552*cdf0e10cSrcweir }; 553*cdf0e10cSrcweir 554*cdf0e10cSrcweir struct TestDataV1 : public PageData 555*cdf0e10cSrcweir { 556*cdf0e10cSrcweir static const sal_uInt32 theTypeId = 6 * 9; 557*cdf0e10cSrcweir }; 558*cdf0e10cSrcweir struct TestData : public PageData 559*cdf0e10cSrcweir { 560*cdf0e10cSrcweir typedef PageData base; 561*cdf0e10cSrcweir typedef TestData self; 562*cdf0e10cSrcweir 563*cdf0e10cSrcweir static const sal_uInt32 theTypeId = 42; 564*cdf0e10cSrcweir 565*cdf0e10cSrcweir void guard() 566*cdf0e10cSrcweir { 567*cdf0e10cSrcweir base::guard(); 568*cdf0e10cSrcweir // self::m_aGuard = ...; 569*cdf0e10cSrcweir } 570*cdf0e10cSrcweir storeError verify() const 571*cdf0e10cSrcweir { 572*cdf0e10cSrcweir storeError result = base::verify(); 573*cdf0e10cSrcweir if (result != store_E_None) 574*cdf0e10cSrcweir return result; 575*cdf0e10cSrcweir if (!(base::type() == self::theTypeId)) 576*cdf0e10cSrcweir return store_E_WrongVersion; 577*cdf0e10cSrcweir return store_E_None; 578*cdf0e10cSrcweir } 579*cdf0e10cSrcweir 580*cdf0e10cSrcweir storeError dwim() const 581*cdf0e10cSrcweir { 582*cdf0e10cSrcweir return store_E_None; 583*cdf0e10cSrcweir } 584*cdf0e10cSrcweir }; 585*cdf0e10cSrcweir class TestObject : public PageObject 586*cdf0e10cSrcweir { 587*cdf0e10cSrcweir typedef PageObject base; 588*cdf0e10cSrcweir 589*cdf0e10cSrcweir public: 590*cdf0e10cSrcweir 591*cdf0e10cSrcweir void dwim() 592*cdf0e10cSrcweir { 593*cdf0e10cSrcweir PageHolderObject< TestData > xPage (m_xPage); 594*cdf0e10cSrcweir xPage->guard(); 595*cdf0e10cSrcweir } 596*cdf0e10cSrcweir 597*cdf0e10cSrcweir virtual void guard() 598*cdf0e10cSrcweir { 599*cdf0e10cSrcweir TestData * pagedata = dynamic_page_cast< TestData >(m_xPage.get()); 600*cdf0e10cSrcweir if (pagedata != 0) 601*cdf0e10cSrcweir {} 602*cdf0e10cSrcweir } 603*cdf0e10cSrcweir virtual storeError verify() const 604*cdf0e10cSrcweir { 605*cdf0e10cSrcweir storeError result = base::verify(); 606*cdf0e10cSrcweir if (result != store_E_None) 607*cdf0e10cSrcweir return result; 608*cdf0e10cSrcweir 609*cdf0e10cSrcweir TestData const * pagedata = dynamic_page_cast< TestData const >(m_xPage.get()); 610*cdf0e10cSrcweir if (!pagedata) 611*cdf0e10cSrcweir return store_E_WrongVersion; 612*cdf0e10cSrcweir 613*cdf0e10cSrcweir return pagedata->verify(); 614*cdf0e10cSrcweir } 615*cdf0e10cSrcweir 616*cdf0e10cSrcweir static storeError verify (PageHolder const & rPage) 617*cdf0e10cSrcweir { 618*cdf0e10cSrcweir return PageHolderObject< TestData >::verify (rPage); 619*cdf0e10cSrcweir } 620*cdf0e10cSrcweir 621*cdf0e10cSrcweir storeError loadAt (sal_uInt32 nOffset, TestBIOS & rBIOS) 622*cdf0e10cSrcweir { 623*cdf0e10cSrcweir storeError result = rBIOS.lookupAt (m_xPage, nOffset); // cache lookup 624*cdf0e10cSrcweir if (result == store_E_NotExists) 625*cdf0e10cSrcweir { 626*cdf0e10cSrcweir result = rBIOS.loadAt (m_xPage, nOffset); 627*cdf0e10cSrcweir if (result != store_E_None) 628*cdf0e10cSrcweir return result; 629*cdf0e10cSrcweir 630*cdf0e10cSrcweir result = PageHolderObject< TestData >::verify (m_xPage); 631*cdf0e10cSrcweir if (result != store_E_None) 632*cdf0e10cSrcweir return result; 633*cdf0e10cSrcweir 634*cdf0e10cSrcweir result = rBIOS.replaceAt (m_xPage, nOffset); // cache insert 635*cdf0e10cSrcweir } 636*cdf0e10cSrcweir return result; 637*cdf0e10cSrcweir } 638*cdf0e10cSrcweir storeError saveAt (sal_uInt32 nOffset, TestBIOS & rBIOS) 639*cdf0e10cSrcweir { 640*cdf0e10cSrcweir if (!m_xPage.get()) 641*cdf0e10cSrcweir return store_E_InvalidAccess; 642*cdf0e10cSrcweir m_xPage->m_aDescr.m_nAddr = store::htonl(nOffset); // m_xPage->location (nOffset); 643*cdf0e10cSrcweir 644*cdf0e10cSrcweir storeError result = PageHolderObject< TestData >::guard (m_xPage); 645*cdf0e10cSrcweir if (result != store_E_None) 646*cdf0e10cSrcweir return result; 647*cdf0e10cSrcweir 648*cdf0e10cSrcweir result = rBIOS.saveAt (m_xPage, nOffset); 649*cdf0e10cSrcweir if (result != store_E_None) 650*cdf0e10cSrcweir return result; 651*cdf0e10cSrcweir 652*cdf0e10cSrcweir return rBIOS.replaceAt (m_xPage, nOffset); // cache update 653*cdf0e10cSrcweir } 654*cdf0e10cSrcweir }; 655*cdf0e10cSrcweir 656*cdf0e10cSrcweir class TestObjectV2 : public PageHolderObject< TestData > 657*cdf0e10cSrcweir { 658*cdf0e10cSrcweir typedef PageHolderObject< TestData > base; 659*cdf0e10cSrcweir 660*cdf0e10cSrcweir public: 661*cdf0e10cSrcweir storeError saveAt (sal_uInt32 nOffset, TestBIOS & rBIOS) 662*cdf0e10cSrcweir { 663*cdf0e10cSrcweir m_xPage->offset(nOffset); 664*cdf0e10cSrcweir 665*cdf0e10cSrcweir storeError result = PageHolderObject< TestData >::guard (m_xPage); 666*cdf0e10cSrcweir if (result != store_E_None) 667*cdf0e10cSrcweir return result; 668*cdf0e10cSrcweir 669*cdf0e10cSrcweir result = rBIOS.saveAt (m_xPage, nOffset); 670*cdf0e10cSrcweir if (result != store_E_None) 671*cdf0e10cSrcweir return result; 672*cdf0e10cSrcweir 673*cdf0e10cSrcweir return rBIOS.replaceAt (m_xPage, nOffset); 674*cdf0e10cSrcweir } 675*cdf0e10cSrcweir #if 1 676*cdf0e10cSrcweir storeError dwim() const 677*cdf0e10cSrcweir { 678*cdf0e10cSrcweir TestData const * pImpl1 = operator->(); 679*cdf0e10cSrcweir 680*cdf0e10cSrcweir PageHolderObject< TestData > xImpl (m_xPage); 681*cdf0e10cSrcweir 682*cdf0e10cSrcweir TestData const * pImpl2 = &*xImpl; 683*cdf0e10cSrcweir OSL_ASSERT(pImpl1 == pImpl2); 684*cdf0e10cSrcweir 685*cdf0e10cSrcweir return xImpl->dwim(); 686*cdf0e10cSrcweir } 687*cdf0e10cSrcweir #endif 688*cdf0e10cSrcweir }; 689*cdf0e10cSrcweir 690*cdf0e10cSrcweir class TestClient 691*cdf0e10cSrcweir { 692*cdf0e10cSrcweir public: 693*cdf0e10cSrcweir void dwim(TestBIOS & rBIOS) 694*cdf0e10cSrcweir { 695*cdf0e10cSrcweir TestObject aObj; 696*cdf0e10cSrcweir 697*cdf0e10cSrcweir rBIOS.loadPageAt(aObj.get(), aObj.verify); 698*cdf0e10cSrcweir rBIOS.loadPageAt(aObj.get(), TestObject::verify); 699*cdf0e10cSrcweir rBIOS.loadPageAt(aObj.get(), PageHolderObject<TestData>::verify); 700*cdf0e10cSrcweir 701*cdf0e10cSrcweir aObj.loadAt (1024, rBIOS); 702*cdf0e10cSrcweir 703*cdf0e10cSrcweir TestObjectV2 aObj2; 704*cdf0e10cSrcweir aObj2.dwim(); 705*cdf0e10cSrcweir aObj2->dwim(); 706*cdf0e10cSrcweir } 707*cdf0e10cSrcweir }; 708*cdf0e10cSrcweir 709*cdf0e10cSrcweir /*======================================================================*/ 710*cdf0e10cSrcweir #if 0 /* NYI */ 711*cdf0e10cSrcweir BIOS::load (PageObject & rPage, sal_uInt32 nOffset) 712*cdf0e10cSrcweir { 713*cdf0e10cSrcweir result = m_xCache->readPageAt (rPage.get(), nOffset); 714*cdf0e10cSrcweir if (result == NotExists) 715*cdf0e10cSrcweir { 716*cdf0e10cSrcweir result = m_xLockBytes->readPageAt (rPage.get(), nOffset); 717*cdf0e10cSrcweir if (result != None) 718*cdf0e10cSrcweir return result; 719*cdf0e10cSrcweir 720*cdf0e10cSrcweir result = rPage.verify(); 721*cdf0e10cSrcweir if (result != None) 722*cdf0e10cSrcweir return result; 723*cdf0e10cSrcweir 724*cdf0e10cSrcweir result = m_xCache->writePageAt (rPage.get(), nOffset); 725*cdf0e10cSrcweir } 726*cdf0e10cSrcweir return result; 727*cdf0e10cSrcweir } 728*cdf0e10cSrcweir BIOS::save (PageObject & rPage, sal_uInt32 nOffset) 729*cdf0e10cSrcweir { 730*cdf0e10cSrcweir rPage.guard(); 731*cdf0e10cSrcweir result = m_xLockBytes->writePageAt (rPage.get(), nOffset); 732*cdf0e10cSrcweir if (result != None) 733*cdf0e10cSrcweir return result; 734*cdf0e10cSrcweir 735*cdf0e10cSrcweir return m_xCache->writePageAt (rPage.get(), nOffset); 736*cdf0e10cSrcweir } 737*cdf0e10cSrcweir BIOS::init (rxLockBytes, eAccessMode, nPageSize) 738*cdf0e10cSrcweir { 739*cdf0e10cSrcweir SuperPage super; 740*cdf0e10cSrcweir if (eAccessMode == store_AccessCreate) 741*cdf0e10cSrcweir { 742*cdf0e10cSrcweir sal_uInt16 pagesize = nPageSize; 743*cdf0e10cSrcweir if ((STORE_MINIMUM_PAGESIZE > pagesize) || (pagesize > STORE_MAXIMUM_PAGESIZE)) 744*cdf0e10cSrcweir return store_E_InvalidParameter; 745*cdf0e10cSrcweir 746*cdf0e10cSrcweir pagesize = ((pagesize + STORE_MINIMUM_PAGESIZE - 1) & ~(STORE_MINIMUM_PAGESIZE - 1)); 747*cdf0e10cSrcweir rxLockBytes->init (pagesize); 748*cdf0e10cSrcweir 749*cdf0e10cSrcweir super = allocator->construct<SuperPage>(); 750*cdf0e10cSrcweir super->guard(); 751*cdf0e10cSrcweir 752*cdf0e10cSrcweir rxLockBytes->writeAt (0, super, super->size()); 753*cdf0e10cSrcweir 754*cdf0e10cSrcweir } 755*cdf0e10cSrcweir if (eAccessMode != store_AccessCreate) 756*cdf0e10cSrcweir { 757*cdf0e10cSrcweir rxLockBytes->readAt (0, &super, super::theSize); 758*cdf0e10cSrcweir 759*cdf0e10cSrcweir super.verify(); 760*cdf0e10cSrcweir } 761*cdf0e10cSrcweir if (eErrCode != store_E_NotExists) 762*cdf0e10cSrcweir 763*cdf0e10cSrcweir 764*cdf0e10cSrcweir } 765*cdf0e10cSrcweir #endif /* NYI */ 766*cdf0e10cSrcweir /*======================================================================*/ 767*cdf0e10cSrcweir 768*cdf0e10cSrcweir #if 0 /* NYI */ 769*cdf0e10cSrcweir class PageCache 770*cdf0e10cSrcweir { 771*cdf0e10cSrcweir std::set<const sal_uInt32, PageObject> m_pages; 772*cdf0e10cSrcweir public: 773*cdf0e10cSrcweir storeError readPageAt (PageObject & rPage, sal_uInt32 nOffset); 774*cdf0e10cSrcweir storeError writePageAt (PageObject const & rPage, sal_uInt32 nOffset); 775*cdf0e10cSrcweir }; 776*cdf0e10cSrcweir #endif /* NYI */ 777*cdf0e10cSrcweir 778*cdf0e10cSrcweir /*======================================================================*/ 779*cdf0e10cSrcweir 780*cdf0e10cSrcweir class IPageAllocator; 781*cdf0e10cSrcweir class IPageAccess 782*cdf0e10cSrcweir { 783*cdf0e10cSrcweir public: 784*cdf0e10cSrcweir virtual storeError initialize (storeAccessMode eAccessMode, sal_uInt16 nPageSize) = 0; 785*cdf0e10cSrcweir virtual IPageAllocator & getAllocator () = 0; 786*cdf0e10cSrcweir 787*cdf0e10cSrcweir public: 788*cdf0e10cSrcweir storeError readPageAt (PageHolder & rPage, sal_uInt32 nOffset) 789*cdf0e10cSrcweir { 790*cdf0e10cSrcweir return readPageAt_Impl (rPage, nOffset); 791*cdf0e10cSrcweir } 792*cdf0e10cSrcweir storeError writePageAt (PageHolder const & rPage, sal_uInt32 nOffset) 793*cdf0e10cSrcweir { 794*cdf0e10cSrcweir // [SECURITY:ValInput] 795*cdf0e10cSrcweir PageData const * pagedata = rPage.get(); 796*cdf0e10cSrcweir OSL_PRECOND(!(pagedata == 0), "invalid Page"); 797*cdf0e10cSrcweir if (pagedata == 0) 798*cdf0e10cSrcweir return store_E_InvalidParameter; 799*cdf0e10cSrcweir 800*cdf0e10cSrcweir sal_uInt32 const offset = pagedata->offset(); 801*cdf0e10cSrcweir OSL_PRECOND(!(nOffset != offset), "inconsistent Offset"); 802*cdf0e10cSrcweir if (nOffset != offset) 803*cdf0e10cSrcweir return store_E_InvalidParameter; 804*cdf0e10cSrcweir 805*cdf0e10cSrcweir OSL_PRECOND(!(nOffset == STORE_PAGE_NULL), "store::IPageAccess::writePageAt(): invalid Offset"); 806*cdf0e10cSrcweir if (nOffset == STORE_PAGE_NULL) 807*cdf0e10cSrcweir return store_E_CantSeek; 808*cdf0e10cSrcweir 809*cdf0e10cSrcweir return writePageAt_Impl (rPage, nOffset); 810*cdf0e10cSrcweir } 811*cdf0e10cSrcweir 812*cdf0e10cSrcweir storeError peekAt (sal_uInt32 nOffset, void * pBuffer, sal_uInt32 nBytes) 813*cdf0e10cSrcweir { 814*cdf0e10cSrcweir // [SECURITY:ValInput] 815*cdf0e10cSrcweir sal_uInt8 * dst_lo = static_cast<sal_uInt8*>(pBuffer); 816*cdf0e10cSrcweir if (!(dst_lo != 0)) 817*cdf0e10cSrcweir return store_E_InvalidParameter; 818*cdf0e10cSrcweir 819*cdf0e10cSrcweir sal_uInt8 * dst_hi = dst_lo + nBytes; 820*cdf0e10cSrcweir if (!(dst_lo < dst_hi)) 821*cdf0e10cSrcweir return (dst_lo > dst_hi) ? store_E_InvalidParameter : store_E_None; 822*cdf0e10cSrcweir 823*cdf0e10cSrcweir sal_uInt64 const dst_size = nOffset + nBytes; 824*cdf0e10cSrcweir if (dst_size > SAL_MAX_UINT32) 825*cdf0e10cSrcweir return store_E_CantSeek; 826*cdf0e10cSrcweir 827*cdf0e10cSrcweir return peekAt_Impl (nOffset, dst_lo, (dst_hi - dst_lo)); 828*cdf0e10cSrcweir } 829*cdf0e10cSrcweir 830*cdf0e10cSrcweir storeError pokeAt (sal_uInt32 nOffset, void const * pBuffer, sal_uInt32 nBytes) 831*cdf0e10cSrcweir { 832*cdf0e10cSrcweir // [SECURITY:ValInput] 833*cdf0e10cSrcweir sal_uInt8 const * src_lo = static_cast<sal_uInt8 const*>(pBuffer); 834*cdf0e10cSrcweir if (!(src_lo != 0)) 835*cdf0e10cSrcweir return store_E_InvalidParameter; 836*cdf0e10cSrcweir 837*cdf0e10cSrcweir sal_uInt8 const * src_hi = src_lo + nBytes; 838*cdf0e10cSrcweir if (!(src_lo < src_hi)) 839*cdf0e10cSrcweir return (src_lo > src_hi) ? store_E_InvalidParameter : store_E_None; 840*cdf0e10cSrcweir 841*cdf0e10cSrcweir sal_uInt64 const dst_size = nOffset + nBytes; 842*cdf0e10cSrcweir if (dst_size > SAL_MAX_UINT32) 843*cdf0e10cSrcweir return store_E_CantSeek; 844*cdf0e10cSrcweir 845*cdf0e10cSrcweir return pokeAt_Impl (nOffset, src_lo, (src_hi - src_lo)); 846*cdf0e10cSrcweir } 847*cdf0e10cSrcweir 848*cdf0e10cSrcweir storeError getSize (sal_uInt32 & rnSize) 849*cdf0e10cSrcweir { 850*cdf0e10cSrcweir rnSize = 0; 851*cdf0e10cSrcweir return getSize_Impl (rnSize); 852*cdf0e10cSrcweir } 853*cdf0e10cSrcweir 854*cdf0e10cSrcweir storeError setSize (sal_uInt32 nSize) 855*cdf0e10cSrcweir { 856*cdf0e10cSrcweir return setSize_Impl (nSize); 857*cdf0e10cSrcweir } 858*cdf0e10cSrcweir 859*cdf0e10cSrcweir private: 860*cdf0e10cSrcweir virtual storeError readPageAt_Impl (PageHolder & rPage, sal_uInt32 nOffset) = 0; 861*cdf0e10cSrcweir virtual storeError writePageAt_Impl (PageHolder const & rPage, sal_uInt32 nOffset) = 0; 862*cdf0e10cSrcweir 863*cdf0e10cSrcweir virtual storeError peekAt_Impl (sal_uInt32 nOffset, void * pBuffer, sal_uInt32 nBytes) = 0; 864*cdf0e10cSrcweir virtual storeError pokeAt_Impl (sal_uInt32 nOffset, void const * pBuffer, sal_uInt32 nBytes) = 0; 865*cdf0e10cSrcweir 866*cdf0e10cSrcweir virtual storeError getSize_Impl (sal_uInt32 & rnSize) = 0; 867*cdf0e10cSrcweir virtual storeError setSize_Impl (sal_uInt32 nSize) = 0; 868*cdf0e10cSrcweir }; 869*cdf0e10cSrcweir 870*cdf0e10cSrcweir /*======================================================================*/ 871*cdf0e10cSrcweir 872*cdf0e10cSrcweir template< class T > struct ResourceHolder 873*cdf0e10cSrcweir { 874*cdf0e10cSrcweir typedef typename T::destructor_type destructor_type; 875*cdf0e10cSrcweir 876*cdf0e10cSrcweir T m_value; 877*cdf0e10cSrcweir 878*cdf0e10cSrcweir explicit ResourceHolder (T const & value = T()) : m_value (value) {} 879*cdf0e10cSrcweir ~ResourceHolder() { reset(); } 880*cdf0e10cSrcweir 881*cdf0e10cSrcweir T & get() { return m_value; } 882*cdf0e10cSrcweir T const & get() const { return m_value; } 883*cdf0e10cSrcweir 884*cdf0e10cSrcweir void set (T const & value) { m_value = value; } 885*cdf0e10cSrcweir void reset (T const & value = T()) 886*cdf0e10cSrcweir { 887*cdf0e10cSrcweir T tmp (m_value); 888*cdf0e10cSrcweir if (tmp != value) 889*cdf0e10cSrcweir destructor_type()(tmp); 890*cdf0e10cSrcweir set (value); 891*cdf0e10cSrcweir } 892*cdf0e10cSrcweir T release() 893*cdf0e10cSrcweir { 894*cdf0e10cSrcweir T tmp (m_value); 895*cdf0e10cSrcweir set (T()); 896*cdf0e10cSrcweir return tmp; 897*cdf0e10cSrcweir } 898*cdf0e10cSrcweir 899*cdf0e10cSrcweir ResourceHolder (ResourceHolder & rhs) 900*cdf0e10cSrcweir { 901*cdf0e10cSrcweir set (rhs.release()); 902*cdf0e10cSrcweir } 903*cdf0e10cSrcweir ResourceHolder & operator= (ResourceHolder & rhs) 904*cdf0e10cSrcweir { 905*cdf0e10cSrcweir reset (rhs.release()); 906*cdf0e10cSrcweir return *this; 907*cdf0e10cSrcweir } 908*cdf0e10cSrcweir }; 909*cdf0e10cSrcweir 910*cdf0e10cSrcweir struct FileHandle 911*cdf0e10cSrcweir { 912*cdf0e10cSrcweir oslFileHandle m_handle; 913*cdf0e10cSrcweir 914*cdf0e10cSrcweir FileHandle() : m_handle(0) {} 915*cdf0e10cSrcweir 916*cdf0e10cSrcweir operator oslFileHandle() { return m_handle; } 917*cdf0e10cSrcweir 918*cdf0e10cSrcweir bool operator != (FileHandle const & rhs) 919*cdf0e10cSrcweir { 920*cdf0e10cSrcweir return (m_handle != rhs.m_handle); 921*cdf0e10cSrcweir } 922*cdf0e10cSrcweir 923*cdf0e10cSrcweir oslFileError initialize (rtl_uString * pFilename, sal_uInt32 nFlags) 924*cdf0e10cSrcweir { 925*cdf0e10cSrcweir // Verify arguments. 926*cdf0e10cSrcweir if (!pFilename || !nFlags) 927*cdf0e10cSrcweir return osl_File_E_INVAL; 928*cdf0e10cSrcweir 929*cdf0e10cSrcweir // Convert into FileUrl. 930*cdf0e10cSrcweir rtl::OUString aFileUrl; 931*cdf0e10cSrcweir if (osl_getFileURLFromSystemPath (pFilename, &(aFileUrl.pData)) != osl_File_E_None) 932*cdf0e10cSrcweir { 933*cdf0e10cSrcweir // Not system path. Maybe a file url, already. 934*cdf0e10cSrcweir rtl_uString_assign (&(aFileUrl.pData), pFilename); 935*cdf0e10cSrcweir } 936*cdf0e10cSrcweir 937*cdf0e10cSrcweir // Acquire handle. 938*cdf0e10cSrcweir return osl_openFile (aFileUrl.pData, &m_handle, nFlags); 939*cdf0e10cSrcweir } 940*cdf0e10cSrcweir 941*cdf0e10cSrcweir struct CloseFile 942*cdf0e10cSrcweir { 943*cdf0e10cSrcweir void operator()(FileHandle & rFile) const 944*cdf0e10cSrcweir { 945*cdf0e10cSrcweir if (rFile.m_handle != 0) 946*cdf0e10cSrcweir { 947*cdf0e10cSrcweir // Release handle. 948*cdf0e10cSrcweir (void) osl_closeFile (rFile.m_handle); 949*cdf0e10cSrcweir rFile.m_handle = 0; 950*cdf0e10cSrcweir } 951*cdf0e10cSrcweir } 952*cdf0e10cSrcweir }; 953*cdf0e10cSrcweir typedef CloseFile destructor_type; 954*cdf0e10cSrcweir }; 955*cdf0e10cSrcweir 956*cdf0e10cSrcweir struct FileMapping 957*cdf0e10cSrcweir { 958*cdf0e10cSrcweir void * m_pAddr; 959*cdf0e10cSrcweir sal_uInt64 m_uSize; 960*cdf0e10cSrcweir 961*cdf0e10cSrcweir FileMapping() : m_pAddr(0), m_uSize(0) {} 962*cdf0e10cSrcweir 963*cdf0e10cSrcweir bool operator != (FileMapping const & rhs) const 964*cdf0e10cSrcweir { 965*cdf0e10cSrcweir return ((m_pAddr != rhs.m_pAddr) || (m_uSize != rhs.m_uSize)); 966*cdf0e10cSrcweir } 967*cdf0e10cSrcweir 968*cdf0e10cSrcweir oslFileError initialize (oslFileHandle hFile) 969*cdf0e10cSrcweir { 970*cdf0e10cSrcweir // Determine mapping size. 971*cdf0e10cSrcweir oslFileError result = osl_getFileSize (hFile, &m_uSize); 972*cdf0e10cSrcweir if (result != osl_File_E_None) 973*cdf0e10cSrcweir return result; 974*cdf0e10cSrcweir if (m_uSize > SAL_MAX_UINT32) 975*cdf0e10cSrcweir return osl_File_E_OVERFLOW; 976*cdf0e10cSrcweir 977*cdf0e10cSrcweir // Acquire mapping. 978*cdf0e10cSrcweir return osl_mapFile (hFile, &m_pAddr, m_uSize, 0, 0); 979*cdf0e10cSrcweir } 980*cdf0e10cSrcweir 981*cdf0e10cSrcweir struct UnmapFile 982*cdf0e10cSrcweir { 983*cdf0e10cSrcweir void operator ()(FileMapping & rMapping) const 984*cdf0e10cSrcweir { 985*cdf0e10cSrcweir if ((rMapping.m_pAddr != 0) && (rMapping.m_uSize != 0)) 986*cdf0e10cSrcweir { 987*cdf0e10cSrcweir // Release mapping. 988*cdf0e10cSrcweir (void) osl_unmapFile (rMapping.m_pAddr, rMapping.m_uSize); 989*cdf0e10cSrcweir rMapping.m_pAddr = 0, rMapping.m_uSize = 0; 990*cdf0e10cSrcweir } 991*cdf0e10cSrcweir } 992*cdf0e10cSrcweir }; 993*cdf0e10cSrcweir typedef UnmapFile destructor_type; 994*cdf0e10cSrcweir }; 995*cdf0e10cSrcweir 996*cdf0e10cSrcweir /*======================================================================*/ 997*cdf0e10cSrcweir 998*cdf0e10cSrcweir class FilePageAccess : public IPageAccess 999*cdf0e10cSrcweir { 1000*cdf0e10cSrcweir oslFileHandle m_hFile; 1001*cdf0e10cSrcweir 1002*cdf0e10cSrcweir public: 1003*cdf0e10cSrcweir static storeError ERROR_FROM_NATIVE (oslFileError eErrno); 1004*cdf0e10cSrcweir static sal_uInt32 MODE_TO_NATIVE (storeAccessMode eMode); 1005*cdf0e10cSrcweir 1006*cdf0e10cSrcweir public: 1007*cdf0e10cSrcweir explicit FilePageAccess (oslFileHandle hFile = 0) : m_hFile (hFile) {} 1008*cdf0e10cSrcweir virtual storeError initialize (storeAccessMode eAccessMode, sal_uInt16 nPageSize); 1009*cdf0e10cSrcweir 1010*cdf0e10cSrcweir private: 1011*cdf0e10cSrcweir virtual storeError readPageAt_Impl (PageHolder & rPage, sal_uInt32 nOffset); 1012*cdf0e10cSrcweir virtual storeError writePageAt_Impl (PageHolder const & rPage, sal_uInt32 nOffset); 1013*cdf0e10cSrcweir 1014*cdf0e10cSrcweir /* see @ OFileLockBytes */ 1015*cdf0e10cSrcweir virtual storeError peekAt_Impl (sal_uInt32 nOffset, void * pBuffer, sal_uInt32 nBytes); 1016*cdf0e10cSrcweir virtual storeError pokeAt_Impl (sal_uInt32 nOffset, void const * pBuffer, sal_uInt32 nBytes); 1017*cdf0e10cSrcweir 1018*cdf0e10cSrcweir virtual storeError getSize_Impl (sal_uInt32 & rnSize); 1019*cdf0e10cSrcweir virtual storeError setSize_Impl (sal_uInt32 nSize); 1020*cdf0e10cSrcweir 1021*cdf0e10cSrcweir protected: 1022*cdf0e10cSrcweir virtual ~FilePageAccess(); 1023*cdf0e10cSrcweir 1024*cdf0e10cSrcweir private: 1025*cdf0e10cSrcweir /** Not implemented. 1026*cdf0e10cSrcweir */ 1027*cdf0e10cSrcweir FilePageAccess (FilePageAccess const &); 1028*cdf0e10cSrcweir FilePageAccess & operator= (FilePageAccess const &); 1029*cdf0e10cSrcweir }; 1030*cdf0e10cSrcweir 1031*cdf0e10cSrcweir storeError FilePageAccess::initialize (storeAccessMode eAccessMode, sal_uInt16 nPageSize) 1032*cdf0e10cSrcweir { 1033*cdf0e10cSrcweir (void) eAccessMode; // UNUSED 1034*cdf0e10cSrcweir (void) nPageSize; // UNUSED 1035*cdf0e10cSrcweir return store_E_Unknown; // NYI 1036*cdf0e10cSrcweir } 1037*cdf0e10cSrcweir FilePageAccess::~FilePageAccess() 1038*cdf0e10cSrcweir { 1039*cdf0e10cSrcweir if (m_hFile != 0) 1040*cdf0e10cSrcweir (void) osl_closeFile (m_hFile); 1041*cdf0e10cSrcweir } 1042*cdf0e10cSrcweir storeError FilePageAccess::readPageAt_Impl (PageHolder & rPage, sal_uInt32 nOffset) 1043*cdf0e10cSrcweir { 1044*cdf0e10cSrcweir PageHolder page (0/*allocate()*/); /* @@@ construct w/ deallocator argument @@@ */ 1045*cdf0e10cSrcweir if (!page.get()) 1046*cdf0e10cSrcweir return store_E_OutOfMemory; 1047*cdf0e10cSrcweir 1048*cdf0e10cSrcweir swap<PageHolder>(page, rPage); 1049*cdf0e10cSrcweir return peekAt (nOffset, rPage.get(), 0/*size*/); 1050*cdf0e10cSrcweir } 1051*cdf0e10cSrcweir storeError FilePageAccess::writePageAt_Impl (PageHolder const & rPage, sal_uInt32 nOffset) 1052*cdf0e10cSrcweir { 1053*cdf0e10cSrcweir return pokeAt (nOffset, rPage.get(), 0/*size*/); 1054*cdf0e10cSrcweir } 1055*cdf0e10cSrcweir storeError FilePageAccess::peekAt_Impl (sal_uInt32 nOffset, void * pBuffer, sal_uInt32 nBytes) 1056*cdf0e10cSrcweir { 1057*cdf0e10cSrcweir sal_uInt64 nDone = 0; 1058*cdf0e10cSrcweir oslFileError result = osl_readFileAt (m_hFile, nOffset, pBuffer, nBytes, &nDone); 1059*cdf0e10cSrcweir if (result != osl_File_E_None) 1060*cdf0e10cSrcweir return ERROR_FROM_NATIVE(result); 1061*cdf0e10cSrcweir if (nDone != nBytes) 1062*cdf0e10cSrcweir return (nDone != 0) ? store_E_CantRead : store_E_NotExists; 1063*cdf0e10cSrcweir return store_E_None; 1064*cdf0e10cSrcweir } 1065*cdf0e10cSrcweir storeError FilePageAccess::pokeAt_Impl (sal_uInt32 nOffset, void const * pBuffer, sal_uInt32 nBytes) 1066*cdf0e10cSrcweir { 1067*cdf0e10cSrcweir sal_uInt64 nDone = 0; 1068*cdf0e10cSrcweir oslFileError result = osl_writeFileAt (m_hFile, nOffset, pBuffer, nBytes, &nDone); 1069*cdf0e10cSrcweir if (result != osl_File_E_None) 1070*cdf0e10cSrcweir return ERROR_FROM_NATIVE(result); 1071*cdf0e10cSrcweir if (nDone != nBytes) 1072*cdf0e10cSrcweir return store_E_CantWrite; 1073*cdf0e10cSrcweir return store_E_None; 1074*cdf0e10cSrcweir } 1075*cdf0e10cSrcweir storeError FilePageAccess::getSize_Impl (sal_uInt32 & rnSize) 1076*cdf0e10cSrcweir { 1077*cdf0e10cSrcweir sal_uInt64 uSize = 0; 1078*cdf0e10cSrcweir oslFileError result = osl_getFileSize (m_hFile, &uSize); 1079*cdf0e10cSrcweir if (result != osl_File_E_None) 1080*cdf0e10cSrcweir return ERROR_FROM_NATIVE(result); 1081*cdf0e10cSrcweir if (uSize > SAL_MAX_UINT32) 1082*cdf0e10cSrcweir return store_E_CantSeek; 1083*cdf0e10cSrcweir 1084*cdf0e10cSrcweir rnSize = sal::static_int_cast<sal_uInt32>(uSize); 1085*cdf0e10cSrcweir return store_E_None; 1086*cdf0e10cSrcweir } 1087*cdf0e10cSrcweir storeError FilePageAccess::setSize_Impl (sal_uInt32 nSize) 1088*cdf0e10cSrcweir { 1089*cdf0e10cSrcweir oslFileError result = osl_setFileSize (m_hFile, nSize); 1090*cdf0e10cSrcweir if (result != osl_File_E_None) 1091*cdf0e10cSrcweir return ERROR_FROM_NATIVE(result); 1092*cdf0e10cSrcweir return store_E_None; 1093*cdf0e10cSrcweir } 1094*cdf0e10cSrcweir storeError FilePageAccess::ERROR_FROM_NATIVE (oslFileError eErrno) 1095*cdf0e10cSrcweir { 1096*cdf0e10cSrcweir switch (eErrno) 1097*cdf0e10cSrcweir { 1098*cdf0e10cSrcweir case osl_File_E_None: 1099*cdf0e10cSrcweir return store_E_None; 1100*cdf0e10cSrcweir 1101*cdf0e10cSrcweir case osl_File_E_NOENT: 1102*cdf0e10cSrcweir return store_E_NotExists; 1103*cdf0e10cSrcweir 1104*cdf0e10cSrcweir case osl_File_E_ACCES: 1105*cdf0e10cSrcweir case osl_File_E_PERM: 1106*cdf0e10cSrcweir return store_E_AccessViolation; 1107*cdf0e10cSrcweir 1108*cdf0e10cSrcweir case osl_File_E_AGAIN: 1109*cdf0e10cSrcweir case osl_File_E_DEADLK: 1110*cdf0e10cSrcweir return store_E_LockingViolation; 1111*cdf0e10cSrcweir 1112*cdf0e10cSrcweir case osl_File_E_BADF: 1113*cdf0e10cSrcweir return store_E_InvalidHandle; 1114*cdf0e10cSrcweir 1115*cdf0e10cSrcweir case osl_File_E_INVAL: 1116*cdf0e10cSrcweir return store_E_InvalidParameter; 1117*cdf0e10cSrcweir 1118*cdf0e10cSrcweir case osl_File_E_NOSPC: 1119*cdf0e10cSrcweir return store_E_OutOfSpace; 1120*cdf0e10cSrcweir 1121*cdf0e10cSrcweir case osl_File_E_OVERFLOW: 1122*cdf0e10cSrcweir return store_E_CantSeek; 1123*cdf0e10cSrcweir 1124*cdf0e10cSrcweir default: 1125*cdf0e10cSrcweir return store_E_Unknown; 1126*cdf0e10cSrcweir } 1127*cdf0e10cSrcweir } 1128*cdf0e10cSrcweir sal_uInt32 FilePageAccess::MODE_TO_NATIVE(storeAccessMode eAccessMode) 1129*cdf0e10cSrcweir { 1130*cdf0e10cSrcweir sal_uInt32 nMode = 0; 1131*cdf0e10cSrcweir switch (eAccessMode) 1132*cdf0e10cSrcweir { 1133*cdf0e10cSrcweir case store_AccessCreate: 1134*cdf0e10cSrcweir case store_AccessReadCreate: 1135*cdf0e10cSrcweir nMode |= osl_File_OpenFlag_Create; 1136*cdf0e10cSrcweir // fall through 1137*cdf0e10cSrcweir case store_AccessReadWrite: 1138*cdf0e10cSrcweir nMode |= osl_File_OpenFlag_Write; 1139*cdf0e10cSrcweir // fall through 1140*cdf0e10cSrcweir case store_AccessReadOnly: 1141*cdf0e10cSrcweir nMode |= osl_File_OpenFlag_Read; 1142*cdf0e10cSrcweir break; 1143*cdf0e10cSrcweir default: 1144*cdf0e10cSrcweir OSL_PRECOND(0, "store::FilePageAccess: unknown storeAccessMode"); 1145*cdf0e10cSrcweir } 1146*cdf0e10cSrcweir return nMode; 1147*cdf0e10cSrcweir } 1148*cdf0e10cSrcweir 1149*cdf0e10cSrcweir /*===*/ 1150*cdf0e10cSrcweir 1151*cdf0e10cSrcweir class MemoryPageAccess : public IPageAccess 1152*cdf0e10cSrcweir { 1153*cdf0e10cSrcweir /** Representation. 1154*cdf0e10cSrcweir */ 1155*cdf0e10cSrcweir sal_uInt8 * m_pData; 1156*cdf0e10cSrcweir sal_uInt32 m_nSize; 1157*cdf0e10cSrcweir 1158*cdf0e10cSrcweir /** Callback function to release Representation. 1159*cdf0e10cSrcweir */ 1160*cdf0e10cSrcweir typedef void (*destructor_type)(sal_uInt8 * pData, sal_uInt32 nSize); 1161*cdf0e10cSrcweir destructor_type m_destructor; 1162*cdf0e10cSrcweir 1163*cdf0e10cSrcweir /** Default destructor callback. 1164*cdf0e10cSrcweir */ 1165*cdf0e10cSrcweir static void freeMemory (sal_uInt8 * pData, sal_uInt32 nSize); 1166*cdf0e10cSrcweir 1167*cdf0e10cSrcweir public: 1168*cdf0e10cSrcweir MemoryPageAccess() 1169*cdf0e10cSrcweir : m_pData (0), m_nSize (0), m_destructor (MemoryPageAccess::freeMemory) 1170*cdf0e10cSrcweir {} 1171*cdf0e10cSrcweir MemoryPageAccess (sal_uInt8 * pData, sal_uInt32 nSize, destructor_type destructor = MemoryPageAccess::freeMemory) 1172*cdf0e10cSrcweir : m_pData (pData), m_nSize (nSize), m_destructor (destructor) 1173*cdf0e10cSrcweir {} 1174*cdf0e10cSrcweir 1175*cdf0e10cSrcweir virtual storeError initialize (storeAccessMode eAccessMode, sal_uInt16 nPageSize); 1176*cdf0e10cSrcweir 1177*cdf0e10cSrcweir private: 1178*cdf0e10cSrcweir /** Page (size aligned) access. 1179*cdf0e10cSrcweir */ 1180*cdf0e10cSrcweir virtual storeError readPageAt_Impl (PageHolder & rPage, sal_uInt32 nOffset); 1181*cdf0e10cSrcweir virtual storeError writePageAt_Impl (PageHolder const & rPage, sal_uInt32 nOffset); 1182*cdf0e10cSrcweir 1183*cdf0e10cSrcweir /** Low level access. 1184*cdf0e10cSrcweir */ 1185*cdf0e10cSrcweir virtual storeError peekAt_Impl (sal_uInt32 nOffset, void * pBuffer, sal_uInt32 nBytes); 1186*cdf0e10cSrcweir virtual storeError pokeAt_Impl (sal_uInt32 nOffset, void const * pBuffer, sal_uInt32 nBytes); 1187*cdf0e10cSrcweir 1188*cdf0e10cSrcweir virtual storeError getSize_Impl (sal_uInt32 & rnSize); 1189*cdf0e10cSrcweir virtual storeError setSize_Impl (sal_uInt32 nSize); 1190*cdf0e10cSrcweir 1191*cdf0e10cSrcweir protected: 1192*cdf0e10cSrcweir virtual ~MemoryPageAccess(); 1193*cdf0e10cSrcweir 1194*cdf0e10cSrcweir private: 1195*cdf0e10cSrcweir /** Not implemented. 1196*cdf0e10cSrcweir */ 1197*cdf0e10cSrcweir MemoryPageAccess (MemoryPageAccess const &); 1198*cdf0e10cSrcweir MemoryPageAccess & operator= (MemoryPageAccess const &); 1199*cdf0e10cSrcweir }; 1200*cdf0e10cSrcweir 1201*cdf0e10cSrcweir storeError MemoryPageAccess::initialize (storeAccessMode eAccessMode, sal_uInt16 nPageSize) 1202*cdf0e10cSrcweir { 1203*cdf0e10cSrcweir (void) eAccessMode; // UNUSED 1204*cdf0e10cSrcweir (void) nPageSize; // UNUSED 1205*cdf0e10cSrcweir return store_E_Unknown; // NYI 1206*cdf0e10cSrcweir } 1207*cdf0e10cSrcweir MemoryPageAccess::~MemoryPageAccess() 1208*cdf0e10cSrcweir { 1209*cdf0e10cSrcweir if (m_destructor != 0) 1210*cdf0e10cSrcweir { 1211*cdf0e10cSrcweir // release resource. 1212*cdf0e10cSrcweir (*m_destructor)(m_pData, m_nSize); 1213*cdf0e10cSrcweir } 1214*cdf0e10cSrcweir } 1215*cdf0e10cSrcweir storeError MemoryPageAccess::readPageAt_Impl (PageHolder & rPage, sal_uInt32 nOffset) 1216*cdf0e10cSrcweir { 1217*cdf0e10cSrcweir /* OSL_PRECOND(nOffset % size == 0, "Unaligned page read."); */ 1218*cdf0e10cSrcweir PageHolder page (reinterpret_cast< PageData* >(m_pData + nOffset)); 1219*cdf0e10cSrcweir swap<PageHolder>(page, rPage); 1220*cdf0e10cSrcweir return store_E_None; 1221*cdf0e10cSrcweir } 1222*cdf0e10cSrcweir storeError MemoryPageAccess::writePageAt_Impl (PageHolder const & rPage, sal_uInt32 nOffset) 1223*cdf0e10cSrcweir { 1224*cdf0e10cSrcweir PageData const * pagedata = rPage.get(); 1225*cdf0e10cSrcweir if (!(pagedata != 0)) 1226*cdf0e10cSrcweir return store_E_InvalidParameter; 1227*cdf0e10cSrcweir 1228*cdf0e10cSrcweir #if 0 /* NYI */ 1229*cdf0e10cSrcweir sal_uInt16 const bytes = pagedata->size(); // Descr.m_nSize; 1230*cdf0e10cSrcweir OSL_ASSERT(bytes >= PageData::thePageSize); 1231*cdf0e10cSrcweir 1232*cdf0e10cSrcweir offset = rPage.location(); // Descr.m_nAddr; 1233*cdf0e10cSrcweir OSL_ASSERT(nOffset == offset); 1234*cdf0e10cSrcweir 1235*cdf0e10cSrcweir OSL_PRECOND(offset % bytes == 0, "Unaligned page write."); 1236*cdf0e10cSrcweir #endif /* NYI */ 1237*cdf0e10cSrcweir return pokeAt (nOffset, pagedata, pagedata->size()); 1238*cdf0e10cSrcweir } 1239*cdf0e10cSrcweir storeError MemoryPageAccess::peekAt_Impl (sal_uInt32 nOffset, void * pBuffer, sal_uInt32 nBytes) 1240*cdf0e10cSrcweir { 1241*cdf0e10cSrcweir // [SECURITY:ValInput] 1242*cdf0e10cSrcweir sal_uInt8 * dst_lo = static_cast<sal_uInt8*>(pBuffer); 1243*cdf0e10cSrcweir if (!(dst_lo != 0)) 1244*cdf0e10cSrcweir return store_E_InvalidParameter; 1245*cdf0e10cSrcweir 1246*cdf0e10cSrcweir sal_uInt8 * dst_hi = dst_lo + nBytes; 1247*cdf0e10cSrcweir if (!(dst_lo <= dst_hi)) 1248*cdf0e10cSrcweir return store_E_InvalidParameter; 1249*cdf0e10cSrcweir 1250*cdf0e10cSrcweir // ... 1251*cdf0e10cSrcweir sal_uInt8 const * src_lo = m_pData + nOffset; 1252*cdf0e10cSrcweir if (!(src_lo <= m_pData + m_nSize)) 1253*cdf0e10cSrcweir return store_E_CantSeek; 1254*cdf0e10cSrcweir 1255*cdf0e10cSrcweir sal_uInt8 const * src_hi = src_lo + nBytes; 1256*cdf0e10cSrcweir if (!(src_hi <= m_pData + m_nSize)) 1257*cdf0e10cSrcweir return store_E_CantRead; 1258*cdf0e10cSrcweir 1259*cdf0e10cSrcweir // copy. 1260*cdf0e10cSrcweir memcpy (pBuffer, src_lo, (src_hi - src_lo)); 1261*cdf0e10cSrcweir return store_E_None; 1262*cdf0e10cSrcweir } 1263*cdf0e10cSrcweir storeError MemoryPageAccess::pokeAt_Impl (sal_uInt32 nOffset, void const * pBuffer, sal_uInt32 nBytes) 1264*cdf0e10cSrcweir { 1265*cdf0e10cSrcweir // [SECURITY:ValInput] 1266*cdf0e10cSrcweir sal_uInt8 const * src_lo = static_cast<sal_uInt8 const*>(pBuffer); 1267*cdf0e10cSrcweir if (!(src_lo != 0)) 1268*cdf0e10cSrcweir return store_E_InvalidParameter; 1269*cdf0e10cSrcweir 1270*cdf0e10cSrcweir sal_uInt8 const * src_hi = src_lo + nBytes; 1271*cdf0e10cSrcweir if (!(src_lo <= src_hi)) 1272*cdf0e10cSrcweir return store_E_InvalidParameter; 1273*cdf0e10cSrcweir 1274*cdf0e10cSrcweir sal_uInt64 const uSize = nOffset + nBytes; 1275*cdf0e10cSrcweir if (uSize > SAL_MAX_UINT32) 1276*cdf0e10cSrcweir return store_E_CantSeek; 1277*cdf0e10cSrcweir 1278*cdf0e10cSrcweir // ... 1279*cdf0e10cSrcweir if (uSize > m_nSize) 1280*cdf0e10cSrcweir { 1281*cdf0e10cSrcweir // increase size. 1282*cdf0e10cSrcweir storeError eErrCode = setSize (sal::static_int_cast<sal_uInt32>(uSize)); 1283*cdf0e10cSrcweir if (eErrCode != store_E_None) 1284*cdf0e10cSrcweir return eErrCode; 1285*cdf0e10cSrcweir } 1286*cdf0e10cSrcweir 1287*cdf0e10cSrcweir sal_uInt8 * dst_lo = m_pData + nOffset; 1288*cdf0e10cSrcweir if (!(dst_lo <= m_pData + m_nSize)) 1289*cdf0e10cSrcweir return store_E_CantSeek; 1290*cdf0e10cSrcweir 1291*cdf0e10cSrcweir sal_uInt8 * dst_hi = dst_lo + nBytes; 1292*cdf0e10cSrcweir if (!(dst_hi <= m_pData + m_nSize)) 1293*cdf0e10cSrcweir return store_E_CantWrite; 1294*cdf0e10cSrcweir 1295*cdf0e10cSrcweir // copy. 1296*cdf0e10cSrcweir memcpy (dst_lo, src_lo, (src_hi - src_lo)); 1297*cdf0e10cSrcweir return store_E_None; 1298*cdf0e10cSrcweir } 1299*cdf0e10cSrcweir storeError MemoryPageAccess::getSize_Impl (sal_uInt32 & rnSize) 1300*cdf0e10cSrcweir { 1301*cdf0e10cSrcweir rnSize = m_nSize; 1302*cdf0e10cSrcweir return store_E_None; 1303*cdf0e10cSrcweir } 1304*cdf0e10cSrcweir storeError MemoryPageAccess::setSize_Impl (sal_uInt32 nSize) 1305*cdf0e10cSrcweir { 1306*cdf0e10cSrcweir if (nSize != m_nSize) 1307*cdf0e10cSrcweir { 1308*cdf0e10cSrcweir sal_uInt8 * pData = static_cast<sal_uInt8*>(rtl_reallocateMemory (m_pData, nSize)); 1309*cdf0e10cSrcweir if (pData != 0) 1310*cdf0e10cSrcweir { 1311*cdf0e10cSrcweir if (nSize > m_nSize) 1312*cdf0e10cSrcweir memset (pData + m_nSize, 0, sal::static_int_cast< size_t >(nSize - m_nSize)); 1313*cdf0e10cSrcweir } 1314*cdf0e10cSrcweir else 1315*cdf0e10cSrcweir { 1316*cdf0e10cSrcweir if (nSize != 0) 1317*cdf0e10cSrcweir return store_E_OutOfMemory; 1318*cdf0e10cSrcweir } 1319*cdf0e10cSrcweir m_pData = pData, m_nSize = nSize; 1320*cdf0e10cSrcweir } 1321*cdf0e10cSrcweir return store_E_None; 1322*cdf0e10cSrcweir } 1323*cdf0e10cSrcweir void MemoryPageAccess::freeMemory (sal_uInt8 * pData, sal_uInt32 /*nSize*/) 1324*cdf0e10cSrcweir { 1325*cdf0e10cSrcweir rtl_freeMemory (pData); 1326*cdf0e10cSrcweir } 1327*cdf0e10cSrcweir 1328*cdf0e10cSrcweir /*===*/ 1329*cdf0e10cSrcweir 1330*cdf0e10cSrcweir class MappedPageAccess : public MemoryPageAccess 1331*cdf0e10cSrcweir { 1332*cdf0e10cSrcweir /** @see MemoryPageAccess::destructor_type callback function. 1333*cdf0e10cSrcweir */ 1334*cdf0e10cSrcweir static void unmapFile (sal_uInt8 * pData, sal_uInt32 nSize); 1335*cdf0e10cSrcweir 1336*cdf0e10cSrcweir public: 1337*cdf0e10cSrcweir MappedPageAccess (sal_uInt8 * pData, sal_uInt32 nSize); 1338*cdf0e10cSrcweir 1339*cdf0e10cSrcweir virtual storeError initialize (storeAccessMode eAccessMode, sal_uInt16 nPageSize); 1340*cdf0e10cSrcweir 1341*cdf0e10cSrcweir virtual storeError writePageAt (PageHolder const & rPage, sal_uInt32 nOffset); 1342*cdf0e10cSrcweir 1343*cdf0e10cSrcweir private: 1344*cdf0e10cSrcweir virtual storeError pokeAt_Impl (sal_uInt32 nOffset, void const * pBuffer, sal_uInt32 nBytes); 1345*cdf0e10cSrcweir virtual storeError setSize_Impl (sal_uInt32 nSize); 1346*cdf0e10cSrcweir 1347*cdf0e10cSrcweir protected: 1348*cdf0e10cSrcweir virtual ~MappedPageAccess() {} 1349*cdf0e10cSrcweir }; 1350*cdf0e10cSrcweir 1351*cdf0e10cSrcweir MappedPageAccess::MappedPageAccess (sal_uInt8 * pData, sal_uInt32 nSize) 1352*cdf0e10cSrcweir : MemoryPageAccess (pData, nSize, MappedPageAccess::unmapFile) 1353*cdf0e10cSrcweir { 1354*cdf0e10cSrcweir } 1355*cdf0e10cSrcweir storeError MappedPageAccess::initialize (storeAccessMode eAccessMode, sal_uInt16 nPageSize) 1356*cdf0e10cSrcweir { 1357*cdf0e10cSrcweir OSL_PRECOND(eAccessMode == store_AccessReadOnly, "store::MappedPageAccess: invalid AccessMode"); 1358*cdf0e10cSrcweir return MemoryPageAccess::initialize (eAccessMode, nPageSize); 1359*cdf0e10cSrcweir } 1360*cdf0e10cSrcweir storeError MappedPageAccess::writePageAt (PageHolder const & /*rPage*/, sal_uInt32 /*nOffset*/) 1361*cdf0e10cSrcweir { 1362*cdf0e10cSrcweir return store_E_AccessViolation; 1363*cdf0e10cSrcweir } 1364*cdf0e10cSrcweir storeError MappedPageAccess::pokeAt_Impl (sal_uInt32 /*nOffset*/, void const * /*pBuffer*/, sal_uInt32 /*nBytes*/) 1365*cdf0e10cSrcweir { 1366*cdf0e10cSrcweir return store_E_AccessViolation; 1367*cdf0e10cSrcweir } 1368*cdf0e10cSrcweir storeError MappedPageAccess::setSize_Impl (sal_uInt32 /*nSize*/) 1369*cdf0e10cSrcweir { 1370*cdf0e10cSrcweir return store_E_AccessViolation; 1371*cdf0e10cSrcweir } 1372*cdf0e10cSrcweir void MappedPageAccess::unmapFile (sal_uInt8 * pData, sal_uInt32 nSize) 1373*cdf0e10cSrcweir { 1374*cdf0e10cSrcweir (void) osl_unmapFile (pData, nSize); 1375*cdf0e10cSrcweir } 1376*cdf0e10cSrcweir 1377*cdf0e10cSrcweir #if 0 /* NYI */ 1378*cdf0e10cSrcweir storeError MemoryPageAccess_createInstance ( 1379*cdf0e10cSrcweir rtl::Reference< IPageAccess > & rxPageAccess, 1380*cdf0e10cSrcweir storeAccessMode eAccessMode, 1381*cdf0e10cSrcweir sal_uInt16 nPageSize 1382*cdf0e10cSrcweir ) 1383*cdf0e10cSrcweir { 1384*cdf0e10cSrcweir rxPageAccess = new MemoryPageAccess(); 1385*cdf0e10cSrcweir if (!rxPageAccess.is()) 1386*cdf0e10cSrcweir return store_E_OutOfMemory; 1387*cdf0e10cSrcweir 1388*cdf0e10cSrcweir return rxPageAccess->initialize (eAccessMode, nPageSize); 1389*cdf0e10cSrcweir } 1390*cdf0e10cSrcweir 1391*cdf0e10cSrcweir storeError FilePageAccess_createInstance ( 1392*cdf0e10cSrcweir rtl::Reference< IPageAccess > & rxPageAccess, 1393*cdf0e10cSrcweir rtl_uString * pFilename, 1394*cdf0e10cSrcweir storeAccessMode eAccessMode, 1395*cdf0e10cSrcweir sal_uInt16 nPageSize 1396*cdf0e10cSrcweir ) 1397*cdf0e10cSrcweir { 1398*cdf0e10cSrcweir // Acquire file handle. 1399*cdf0e10cSrcweir ResourceHolder<FileHandle> xFile; 1400*cdf0e10cSrcweir result = xFile.get().initialize (pFilename, MODE_TO_NATIVE(eAccessMode)); 1401*cdf0e10cSrcweir if (result != osl_File_E_None) 1402*cdf0e10cSrcweir return ERROR_FROM_NATIVE(result); 1403*cdf0e10cSrcweir 1404*cdf0e10cSrcweir if (eAccessMode == store_AccessReadOnly) 1405*cdf0e10cSrcweir { 1406*cdf0e10cSrcweir ResourceHolder<FileMapping> xMapping; 1407*cdf0e10cSrcweir result = xMapping.get().initialize (xFile.get()); 1408*cdf0e10cSrcweir if (result == osl_File_E_None) 1409*cdf0e10cSrcweir { 1410*cdf0e10cSrcweir const sal_uInt32 nSize = sal::static_int_cast<sal_uInt32>(xMapping.get().m_uSize); 1411*cdf0e10cSrcweir rxPageAccess = new MappedPageAccess (xMapping.get().m_pAddr, nSize); 1412*cdf0e10cSrcweir if (!rxPageAccess.is()) 1413*cdf0e10cSrcweir return store_E_OutOfMemory; 1414*cdf0e10cSrcweir (void) xMapping.release(); 1415*cdf0e10cSrcweir } 1416*cdf0e10cSrcweir } 1417*cdf0e10cSrcweir if (!rxPageAccess.is()) 1418*cdf0e10cSrcweir { 1419*cdf0e10cSrcweir rxPageAccess = new FilePageAccess (xFile.get()); 1420*cdf0e10cSrcweir if (!rxPageAccess.is()) 1421*cdf0e10cSrcweir return store_E_OutOfMemory; 1422*cdf0e10cSrcweir (void) xFile.release(); 1423*cdf0e10cSrcweir } 1424*cdf0e10cSrcweir return rxPageAccess->initialize (eAccessMode, nPageSize); 1425*cdf0e10cSrcweir } 1426*cdf0e10cSrcweir #endif /* NYI */ 1427*cdf0e10cSrcweir 1428*cdf0e10cSrcweir /*======================================================================== 1429*cdf0e10cSrcweir * 1430*cdf0e10cSrcweir * test... 1431*cdf0e10cSrcweir * 1432*cdf0e10cSrcweir *======================================================================*/ 1433*cdf0e10cSrcweir #if 0 /* NYI */ 1434*cdf0e10cSrcweir 1435*cdf0e10cSrcweir struct IDataBlock 1436*cdf0e10cSrcweir { 1437*cdf0e10cSrcweir virtual sal_uInt16 singleCount() const = 0; 1438*cdf0e10cSrcweir virtual sal_uInt32 singleLink (sal_uInt16 nIndex) const = 0; 1439*cdf0e10cSrcweir virtual void singleLink (sal_uInt16 nIndex, sal_uInt32 nAddr) = 0; 1440*cdf0e10cSrcweir 1441*cdf0e10cSrcweir virtual storeError get() = 0; 1442*cdf0e10cSrcweir virtual storeError put() = 0; 1443*cdf0e10cSrcweir virtual storeError truncate() = 0; 1444*cdf0e10cSrcweir }; 1445*cdf0e10cSrcweir 1446*cdf0e10cSrcweir struct InodePageData : public PageData 1447*cdf0e10cSrcweir { 1448*cdf0e10cSrcweir virtual INameBlock & getNameBlock() = 0; 1449*cdf0e10cSrcweir virtual IDataBlock & getDataBlock() = 0; 1450*cdf0e10cSrcweir }; 1451*cdf0e10cSrcweir 1452*cdf0e10cSrcweir template< class page_data_type > 1453*cdf0e10cSrcweir page_data_type * query (PageData *, page_data_type *); 1454*cdf0e10cSrcweir 1455*cdf0e10cSrcweir template<> InodePageDataV2* 1456*cdf0e10cSrcweir query (PageData & rData, InodePageDataV2 *) 1457*cdf0e10cSrcweir { 1458*cdf0e10cSrcweir if (rData.isKindOf(InodePageDataV2::m_nTypeId)) 1459*cdf0e10cSrcweir return static_cast<InodePageDataV2*>(&rData); 1460*cdf0e10cSrcweir return 0; 1461*cdf0e10cSrcweir } 1462*cdf0e10cSrcweir 1463*cdf0e10cSrcweir class InodePageObject : public PageObject 1464*cdf0e10cSrcweir { 1465*cdf0e10cSrcweir public: 1466*cdf0e10cSrcweir static InodePageObject createInstance (PageData & rData) 1467*cdf0e10cSrcweir { 1468*cdf0e10cSrcweir if (query(&rData, static_cast<InodePageDataV2*>(0))) 1469*cdf0e10cSrcweir return InodePageObjectV2 (static_cast<InodePageDataV2&>(rData)); 1470*cdf0e10cSrcweir else if (query(&rData, static_cast<InodePageDataV1*>(0))) 1471*cdf0e10cSrcweir return InodePageObjectV1 (static_cast<InodePageDataV1&>(rData)); 1472*cdf0e10cSrcweir } 1473*cdf0e10cSrcweir }; 1474*cdf0e10cSrcweir 1475*cdf0e10cSrcweir #endif /* NYI */ 1476*cdf0e10cSrcweir 1477*cdf0e10cSrcweir /*======================================================================== 1478*cdf0e10cSrcweir * 1479*cdf0e10cSrcweir * main. 1480*cdf0e10cSrcweir * 1481*cdf0e10cSrcweir *======================================================================*/ 1482*cdf0e10cSrcweir 1483*cdf0e10cSrcweir #include <stdio.h> 1484*cdf0e10cSrcweir 1485*cdf0e10cSrcweir #if 0 /* EXP */ 1486*cdf0e10cSrcweir class Interface 1487*cdf0e10cSrcweir { 1488*cdf0e10cSrcweir public: 1489*cdf0e10cSrcweir virtual void deallocate(void *) /* = 0 */; 1490*cdf0e10cSrcweir }; 1491*cdf0e10cSrcweir 1492*cdf0e10cSrcweir class Implementation : public Interface 1493*cdf0e10cSrcweir { 1494*cdf0e10cSrcweir SharedCount m_count; 1495*cdf0e10cSrcweir 1496*cdf0e10cSrcweir public: 1497*cdf0e10cSrcweir Implementation() : Interface() { printf("Ctor(%p)\n", this); } 1498*cdf0e10cSrcweir ~Implementation() { printf("Dtor(%p)\n", this); } 1499*cdf0e10cSrcweir 1500*cdf0e10cSrcweir Implementation (Implementation const & rhs) : Interface(), m_count (rhs.m_count) { printf("CopyCtor(%p)\n", this); } 1501*cdf0e10cSrcweir 1502*cdf0e10cSrcweir virtual void deallocate(void *) {} 1503*cdf0e10cSrcweir }; 1504*cdf0e10cSrcweir 1505*cdf0e10cSrcweir Interface Interface_createInstance() 1506*cdf0e10cSrcweir { 1507*cdf0e10cSrcweir Implementation aInst; 1508*cdf0e10cSrcweir return aInst; 1509*cdf0e10cSrcweir } 1510*cdf0e10cSrcweir #endif /* EXP */ 1511*cdf0e10cSrcweir 1512*cdf0e10cSrcweir int SAL_CALL main (int argc, char ** argv) 1513*cdf0e10cSrcweir { 1514*cdf0e10cSrcweir OSL_PRECOND(argc >= 1, "t_page: error: insufficient number of arguments."); 1515*cdf0e10cSrcweir if (argc < 1) 1516*cdf0e10cSrcweir return 0; 1517*cdf0e10cSrcweir 1518*cdf0e10cSrcweir { 1519*cdf0e10cSrcweir void *a = (void*)1, *b = (void*)2; 1520*cdf0e10cSrcweir swap<void*>(a, b); 1521*cdf0e10cSrcweir } 1522*cdf0e10cSrcweir { 1523*cdf0e10cSrcweir PageObject a; 1524*cdf0e10cSrcweir PageObject b (a); 1525*cdf0e10cSrcweir PageObject c; 1526*cdf0e10cSrcweir 1527*cdf0e10cSrcweir c = b; 1528*cdf0e10cSrcweir a = a; 1529*cdf0e10cSrcweir 1530*cdf0e10cSrcweir } 1531*cdf0e10cSrcweir { 1532*cdf0e10cSrcweir TestBIOS aBIOS; 1533*cdf0e10cSrcweir TestClient aClient; 1534*cdf0e10cSrcweir aClient.dwim (aBIOS); 1535*cdf0e10cSrcweir } 1536*cdf0e10cSrcweir #if 0 /* EXP */ 1537*cdf0e10cSrcweir { 1538*cdf0e10cSrcweir Interface aIfc1 (Interface_createInstance()); 1539*cdf0e10cSrcweir Interface aIfc2 (aIfc1); 1540*cdf0e10cSrcweir } 1541*cdf0e10cSrcweir #endif /* EXP */ 1542*cdf0e10cSrcweir 1543*cdf0e10cSrcweir if (argc > 1) 1544*cdf0e10cSrcweir { 1545*cdf0e10cSrcweir rtl_uString * pFilename = 0; 1546*cdf0e10cSrcweir rtl_uString_newFromAscii (&pFilename, argv[1]); 1547*cdf0e10cSrcweir storeAccessMode eAccessMode = store_AccessReadOnly; 1548*cdf0e10cSrcweir 1549*cdf0e10cSrcweir // Acquire file handle. 1550*cdf0e10cSrcweir ResourceHolder<FileHandle> h1; 1551*cdf0e10cSrcweir oslFileError result = h1.get().initialize (pFilename, FilePageAccess::MODE_TO_NATIVE(eAccessMode)); 1552*cdf0e10cSrcweir if (result == osl_File_E_None) 1553*cdf0e10cSrcweir { 1554*cdf0e10cSrcweir ResourceHolder<FileHandle> h2 (h1); 1555*cdf0e10cSrcweir h1 = h2; 1556*cdf0e10cSrcweir 1557*cdf0e10cSrcweir if (eAccessMode == store_AccessReadOnly) 1558*cdf0e10cSrcweir { 1559*cdf0e10cSrcweir ResourceHolder<FileMapping> m1; 1560*cdf0e10cSrcweir result = m1.get().initialize (h1.get()); 1561*cdf0e10cSrcweir 1562*cdf0e10cSrcweir const sal_uInt32 nSize = sal::static_int_cast<sal_uInt32>(m1.get().m_uSize); 1563*cdf0e10cSrcweir (void) nSize; // UNUSED 1564*cdf0e10cSrcweir 1565*cdf0e10cSrcweir ResourceHolder<FileMapping> m2 (m1); 1566*cdf0e10cSrcweir m1 = m2; 1567*cdf0e10cSrcweir 1568*cdf0e10cSrcweir result = osl_File_E_None; 1569*cdf0e10cSrcweir } 1570*cdf0e10cSrcweir } 1571*cdf0e10cSrcweir } 1572*cdf0e10cSrcweir 1573*cdf0e10cSrcweir return 0; 1574*cdf0e10cSrcweir } 1575