Kaydet (Commit) 98323f93 authored tarafından Stephan Bergmann's avatar Stephan Bergmann

cid#1371195, cide#1371212: Replace PageHolder with shared_ptr

...removing the need for SharedCount, too

Change-Id: I20c724c940c571aef1c12453da30c3e9fbb46466
üst 904959b8
......@@ -30,6 +30,7 @@
#include "object.hxx"
#include "storbase.hxx"
#include <memory>
#include <string.h>
using namespace store;
......@@ -46,7 +47,7 @@ storeError ILockBytes::initialize (rtl::Reference< PageData::Allocator > & rxAll
return initialize_Impl (rxAllocator, nPageSize);
}
storeError ILockBytes::readPageAt (PageHolder & rPage, sal_uInt32 nOffset)
storeError ILockBytes::readPageAt (std::shared_ptr<PageData> & rPage, sal_uInt32 nOffset)
{
OSL_PRECOND(!(nOffset == STORE_PAGE_NULL), "store::ILockBytes::readPageAt(): invalid Offset");
if (nOffset == STORE_PAGE_NULL)
......@@ -55,7 +56,7 @@ storeError ILockBytes::readPageAt (PageHolder & rPage, sal_uInt32 nOffset)
return readPageAt_Impl (rPage, nOffset);
}
storeError ILockBytes::writePageAt (PageHolder const & rPage, sal_uInt32 nOffset)
storeError ILockBytes::writePageAt (std::shared_ptr<PageData> const & rPage, sal_uInt32 nOffset)
{
// [SECURITY:ValInput]
PageData const * pagedata = rPage.get();
......@@ -291,8 +292,8 @@ class FileLockBytes :
*/
virtual storeError initialize_Impl (rtl::Reference< PageData::Allocator > & rxAllocator, sal_uInt16 nPageSize) override;
virtual storeError readPageAt_Impl (PageHolder & rPage, sal_uInt32 nOffset) override;
virtual storeError writePageAt_Impl (PageHolder const & rPage, sal_uInt32 nOffset) override;
virtual storeError readPageAt_Impl (std::shared_ptr<PageData> & rPage, sal_uInt32 nOffset) override;
virtual storeError writePageAt_Impl (std::shared_ptr<PageData> const & rPage, sal_uInt32 nOffset) override;
virtual storeError readAt_Impl (sal_uInt32 nOffset, void * pBuffer, sal_uInt32 nBytes) override;
virtual storeError writeAt_Impl (sal_uInt32 nOffset, void const * pBuffer, sal_uInt32 nBytes) override;
......@@ -360,11 +361,11 @@ storeError FileLockBytes::initialize_Impl (rtl::Reference< PageData::Allocator >
return store_E_None;
}
storeError FileLockBytes::readPageAt_Impl (PageHolder & rPage, sal_uInt32 nOffset)
storeError FileLockBytes::readPageAt_Impl (std::shared_ptr<PageData> & rPage, sal_uInt32 nOffset)
{
if (m_xAllocator.is())
{
PageHolder page (m_xAllocator->construct<PageData>(), m_xAllocator);
std::shared_ptr<PageData> page (m_xAllocator->construct<PageData>(), PageData::Deallocate(m_xAllocator));
page.swap (rPage);
}
......@@ -377,7 +378,7 @@ storeError FileLockBytes::readPageAt_Impl (PageHolder & rPage, sal_uInt32 nOffse
return readAt_Impl (nOffset, pagedata, pagedata->size());
}
storeError FileLockBytes::writePageAt_Impl (PageHolder const & rPage, sal_uInt32 nOffset)
storeError FileLockBytes::writePageAt_Impl (std::shared_ptr<PageData> const & rPage, sal_uInt32 nOffset)
{
PageData const * pagedata = rPage.get();
OSL_PRECOND(pagedata != nullptr, "contract violation");
......@@ -519,8 +520,8 @@ class MappedLockBytes :
*/
virtual storeError initialize_Impl (rtl::Reference< PageData::Allocator > & rxAllocator, sal_uInt16 nPageSize) override;
virtual storeError readPageAt_Impl (PageHolder & rPage, sal_uInt32 nOffset) override;
virtual storeError writePageAt_Impl (PageHolder const & rPage, sal_uInt32 nOffset) override;
virtual storeError readPageAt_Impl (std::shared_ptr<PageData> & rPage, sal_uInt32 nOffset) override;
virtual storeError writePageAt_Impl (std::shared_ptr<PageData> const & rPage, sal_uInt32 nOffset) override;
virtual storeError readAt_Impl (sal_uInt32 nOffset, void * pBuffer, sal_uInt32 nBytes) override;
virtual storeError writeAt_Impl (sal_uInt32 nOffset, const void * pBuffer, sal_uInt32 nBytes) override;
......@@ -579,7 +580,7 @@ storeError MappedLockBytes::initialize_Impl (rtl::Reference< PageData::Allocator
return store_E_None;
}
storeError MappedLockBytes::readPageAt_Impl (PageHolder & rPage, sal_uInt32 nOffset)
storeError MappedLockBytes::readPageAt_Impl (std::shared_ptr<PageData> & rPage, sal_uInt32 nOffset)
{
sal_uInt8 * src_lo = m_pData + nOffset;
if ((m_pData > src_lo) || (src_lo >= m_pData + m_nSize))
......@@ -589,13 +590,13 @@ storeError MappedLockBytes::readPageAt_Impl (PageHolder & rPage, sal_uInt32 nOff
if ((m_pData > src_hi) || (src_hi > m_pData + m_nSize))
return store_E_CantRead;
PageHolder page (reinterpret_cast< PageData* >(src_lo), static_cast< PageData::Allocator* >(this));
std::shared_ptr<PageData> page (reinterpret_cast< PageData* >(src_lo), PageData::Deallocate(static_cast< PageData::Allocator* >(this)));
page.swap (rPage);
return store_E_None;
}
storeError MappedLockBytes::writePageAt_Impl (PageHolder const & /*rPage*/, sal_uInt32 /*nOffset*/)
storeError MappedLockBytes::writePageAt_Impl (std::shared_ptr<PageData> const & /*rPage*/, sal_uInt32 /*nOffset*/)
{
return store_E_AccessViolation;
}
......@@ -657,8 +658,8 @@ class MemoryLockBytes :
*/
virtual storeError initialize_Impl (rtl::Reference< PageData::Allocator > & rxAllocator, sal_uInt16 nPageSize) override;
virtual storeError readPageAt_Impl (PageHolder & rPage, sal_uInt32 nOffset) override;
virtual storeError writePageAt_Impl (PageHolder const & rPage, sal_uInt32 nOffset) override;
virtual storeError readPageAt_Impl (std::shared_ptr<PageData> & rPage, sal_uInt32 nOffset) override;
virtual storeError writePageAt_Impl (std::shared_ptr<PageData> const & rPage, sal_uInt32 nOffset) override;
virtual storeError readAt_Impl (sal_uInt32 nOffset, void * pBuffer, sal_uInt32 nBytes) override;
virtual storeError writeAt_Impl (sal_uInt32 nOffset, const void * pBuffer, sal_uInt32 nBytes) override;
......@@ -704,11 +705,11 @@ storeError MemoryLockBytes::initialize_Impl (rtl::Reference< PageData::Allocator
return result;
}
storeError MemoryLockBytes::readPageAt_Impl (PageHolder & rPage, sal_uInt32 nOffset)
storeError MemoryLockBytes::readPageAt_Impl (std::shared_ptr<PageData> & rPage, sal_uInt32 nOffset)
{
if (m_xAllocator.is())
{
PageHolder page (m_xAllocator->construct<PageData>(), m_xAllocator);
std::shared_ptr<PageData> page (m_xAllocator->construct<PageData>(), PageData::Deallocate(m_xAllocator));
page.swap (rPage);
}
......@@ -721,7 +722,7 @@ storeError MemoryLockBytes::readPageAt_Impl (PageHolder & rPage, sal_uInt32 nOff
return readAt_Impl (nOffset, pagedata, pagedata->size());
}
storeError MemoryLockBytes::writePageAt_Impl (PageHolder const & rPage, sal_uInt32 nOffset)
storeError MemoryLockBytes::writePageAt_Impl (std::shared_ptr<PageData> const & rPage, sal_uInt32 nOffset)
{
PageData const * pagedata = rPage.get();
OSL_PRECOND(!(pagedata == nullptr), "contract violation");
......
......@@ -20,6 +20,10 @@
#ifndef INCLUDED_STORE_SOURCE_LOCKBYTE_HXX
#define INCLUDED_STORE_SOURCE_LOCKBYTE_HXX
#include <sal/config.h>
#include <memory>
#include "sal/types.h"
#include "rtl/ref.hxx"
......@@ -53,7 +57,7 @@ public:
@param nOffset [in]
*/
storeError readPageAt (
PageHolder & rPage,
std::shared_ptr<PageData> & rPage,
sal_uInt32 nOffset);
/**
......@@ -61,7 +65,7 @@ public:
@param nOffset [in]
*/
storeError writePageAt (
PageHolder const & rPage,
std::shared_ptr<PageData> const & rPage,
sal_uInt32 nOffset);
/**
......@@ -114,11 +118,11 @@ private:
sal_uInt16 nPageSize) = 0;
virtual storeError readPageAt_Impl (
PageHolder & rPage,
std::shared_ptr<PageData> & rPage,
sal_uInt32 nOffset) = 0;
virtual storeError writePageAt_Impl (
PageHolder const & rPage,
std::shared_ptr<PageData> const & rPage,
sal_uInt32 nOffset) = 0;
virtual storeError readAt_Impl (
......
......@@ -34,6 +34,7 @@
#include "store/types.h"
#include <memory>
#include <stddef.h>
#include <string.h>
#include <utility>
......@@ -75,52 +76,6 @@ inline sal_uInt32 htonl (sal_uInt32 h) { return h; }
inline sal_uInt32 ntohl (sal_uInt32 n) { return n; }
#endif /* OSL_BIGENDIAN */
/*========================================================================
*
* SharedCount.
*
*======================================================================*/
class SharedCount
{
long * m_pCount;
public:
SharedCount()
: m_pCount(new long)
{
(*m_pCount) = 1;
}
~SharedCount()
{
long new_count = --(*m_pCount);
if (new_count == 0)
delete m_pCount;
}
void swap (SharedCount & rhs) // nothrow
{
std::swap(m_pCount, rhs.m_pCount);
}
SharedCount (SharedCount const & rhs) // nothrow
: m_pCount (rhs.m_pCount)
{
++(*m_pCount);
}
SharedCount & operator= (SharedCount const & rhs) // nothrow
{
SharedCount tmp(rhs);
swap(tmp);
return *this;
}
bool operator== (long count) const
{
return *m_pCount == count;
}
};
/*========================================================================
*
* OStorePageGuard.
......@@ -416,6 +371,17 @@ struct PageData
virtual void deallocate_Impl (void * pPage) = 0;
};
class Deallocate {
public:
explicit Deallocate(rtl::Reference<Allocator> const & allocator):
allocator_(allocator) {};
void operator ()(void * page) const { allocator_->deallocate(page); }
private:
rtl::Reference<Allocator> allocator_;
};
static void* operator new (size_t, void * p) { return p; }
static void operator delete (void * , void *) {}
......@@ -477,73 +443,6 @@ struct PageData
};
/*========================================================================
*
* PageHolder.
*
*======================================================================*/
class PageHolder
{
SharedCount m_refcount;
PageData * m_pagedata;
typedef rtl::Reference< PageData::Allocator > allocator_type;
allocator_type m_allocator;
public:
explicit PageHolder (PageData * pagedata = nullptr, allocator_type const & allocator = allocator_type())
: m_refcount (),
m_pagedata (pagedata),
m_allocator(allocator)
{
OSL_ENSURE((m_pagedata == nullptr) || m_allocator.is(), "store::PageHolder::ctor(): pagedata w/o allocator.");
}
~PageHolder()
{
if ((m_refcount == 1) && (m_pagedata != nullptr))
{
// free pagedata.
OSL_ENSURE(m_allocator.is(), "store::PageHolder::dtor(): pagedata w/o allocator.");
m_allocator->deallocate (m_pagedata);
}
}
void swap (PageHolder & rhs) // nothrow
{
m_refcount.swap(rhs.m_refcount);
std::swap(m_pagedata, rhs.m_pagedata);
std::swap(m_allocator, rhs.m_allocator);
}
PageHolder (PageHolder const & rhs) // nothrow
: m_refcount (rhs.m_refcount),
m_pagedata (rhs.m_pagedata),
m_allocator(rhs.m_allocator)
{}
PageHolder & operator= (PageHolder const & rhs) // nothrow
{
PageHolder tmp (rhs);
swap(tmp);
return *this;
}
PageData * get() { return m_pagedata; }
PageData const * get() const { return m_pagedata; }
PageData * operator->()
{
OSL_PRECOND(m_pagedata != nullptr, "store::PageHolder::operator->(): Null pointer");
return m_pagedata;
}
PageData const * operator->() const
{
OSL_PRECOND(m_pagedata != nullptr, "store::PageHolder::operator->(): Null pointer");
return m_pagedata;
}
};
/*========================================================================
*
* PageHolderObject.
......@@ -554,7 +453,7 @@ class PageHolderObject
{
/** Representation.
*/
PageHolder m_xPage;
std::shared_ptr<PageData> m_xPage;
/** Checked cast.
*/
......@@ -581,13 +480,13 @@ public:
{
if ((m_xPage.get() == 0) && rxAllocator.is())
{
PageHolder tmp (rxAllocator->construct<T>(), rxAllocator);
std::shared_ptr<PageData> tmp (rxAllocator->construct<T>(), PageData::Deallocate(rxAllocator));
m_xPage.swap (tmp);
}
return (m_xPage.get() != 0);
}
explicit PageHolderObject (PageHolder const & rxPage = PageHolder())
explicit PageHolderObject (std::shared_ptr<PageData> const & rxPage = std::shared_ptr<PageData>())
: m_xPage (rxPage)
{}
......@@ -612,8 +511,8 @@ public:
return (m_xPage.get() != 0);
}
PageHolder & get() { return m_xPage; }
PageHolder const & get() const { return m_xPage; }
std::shared_ptr<PageData> & get() { return m_xPage; }
std::shared_ptr<PageData> const & get() const { return m_xPage; }
T * operator->()
{
......@@ -641,7 +540,7 @@ public:
return (*pImpl);
}
static storeError guard (PageHolder & rxPage, sal_uInt32 nAddr)
static storeError guard (std::shared_ptr<PageData> & rxPage, sal_uInt32 nAddr)
{
PageData * pHead = rxPage.get();
if (!pHead)
......@@ -654,7 +553,7 @@ public:
return store_E_None;
}
static storeError verify (PageHolder const & rxPage, sal_uInt32 nAddr)
static storeError verify (std::shared_ptr<PageData> const & rxPage, sal_uInt32 nAddr)
{
PageData const * pHead = rxPage.get();
if (!pHead)
......@@ -704,12 +603,12 @@ public:
protected:
/** Representation.
*/
PageHolder m_xPage;
std::shared_ptr<PageData> m_xPage;
bool m_bDirty;
/** Construction.
*/
explicit OStorePageObject (PageHolder const & rxPage = PageHolder())
explicit OStorePageObject (std::shared_ptr<PageData> const & rxPage = std::shared_ptr<PageData>())
: m_xPage (rxPage), m_bDirty (false)
{}
......@@ -730,7 +629,7 @@ public:
if (!rxAllocator.is())
return store_E_InvalidAccess;
PageHolder tmp (rxAllocator->construct<U>(), rxAllocator);
std::shared_ptr<PageData> tmp (rxAllocator->construct<U>(), PageData::Deallocate(rxAllocator));
if (!tmp.get())
return store_E_OutOfMemory;
......@@ -738,7 +637,7 @@ public:
return store_E_None;
}
PageHolder & get() { return m_xPage; }
std::shared_ptr<PageData> & get() { return m_xPage; }
virtual storeError guard (sal_uInt32 nAddr) = 0;
virtual storeError verify (sal_uInt32 nAddr) const = 0;
......
......@@ -31,6 +31,7 @@
#include "object.hxx"
#include "storbase.hxx"
#include <memory>
#include <stddef.h>
using namespace store;
......@@ -41,7 +42,7 @@ namespace store {
struct Entry
{
// Representation
PageHolder m_xPage;
std::shared_ptr<PageData> m_xPage;
sal_uInt32 m_nOffset;
Entry * m_pNext;
......@@ -50,7 +51,7 @@ struct Entry
static void operator delete (void *, void *) {}
// Construction
explicit Entry (PageHolder const & rxPage = PageHolder(), sal_uInt32 nOffset = STORE_PAGE_NULL)
explicit Entry (std::shared_ptr<PageData> const & rxPage = std::shared_ptr<PageData>(), sal_uInt32 nOffset = STORE_PAGE_NULL)
: m_xPage(rxPage), m_nOffset(nOffset), m_pNext(nullptr)
{}
......@@ -70,7 +71,7 @@ class EntryCache
public:
static EntryCache & get();
Entry * create (PageHolder const & rxPage, sal_uInt32 nOffset);
Entry * create (std::shared_ptr<PageData> const & rxPage, sal_uInt32 nOffset);
void destroy (Entry * entry);
......@@ -109,7 +110,7 @@ EntryCache::~EntryCache()
m_entry_cache = nullptr;
}
Entry * EntryCache::create (PageHolder const & rxPage, sal_uInt32 nOffset)
Entry * EntryCache::create (std::shared_ptr<PageData> const & rxPage, sal_uInt32 nOffset)
{
void * pAddr = rtl_cache_alloc (m_entry_cache);
if (pAddr != nullptr)
......@@ -285,7 +286,7 @@ Entry * PageCache::lookup_Impl (Entry * entry, sal_uInt32 nOffset)
return entry;
}
storeError PageCache::lookupPageAt (PageHolder & rxPage, sal_uInt32 nOffset)
storeError PageCache::lookupPageAt (std::shared_ptr<PageData> & rxPage, sal_uInt32 nOffset)
{
OSL_PRECOND(!(nOffset == STORE_PAGE_NULL), "store::PageCache::lookupPageAt(): invalid Offset");
if (nOffset == STORE_PAGE_NULL)
......@@ -308,7 +309,7 @@ storeError PageCache::lookupPageAt (PageHolder & rxPage, sal_uInt32 nOffset)
return store_E_NotExists;
}
storeError PageCache::insertPageAt (PageHolder const & rxPage, sal_uInt32 nOffset)
storeError PageCache::insertPageAt (std::shared_ptr<PageData> const & rxPage, sal_uInt32 nOffset)
{
// [SECURITY:ValInput]
PageData const * pagedata = rxPage.get();
......@@ -340,7 +341,7 @@ storeError PageCache::insertPageAt (PageHolder const & rxPage, sal_uInt32 nOffse
return store_E_OutOfMemory;
}
storeError PageCache::updatePageAt (PageHolder const & rxPage, sal_uInt32 nOffset)
storeError PageCache::updatePageAt (std::shared_ptr<PageData> const & rxPage, sal_uInt32 nOffset)
{
// [SECURITY:ValInput]
PageData const * pagedata = rxPage.get();
......
......@@ -20,6 +20,10 @@
#ifndef INCLUDED_STORE_SOURCE_STORCACH_HXX
#define INCLUDED_STORE_SOURCE_STORCACH_HXX
#include <sal/config.h>
#include <memory>
#include "sal/types.h"
#include "rtl/ref.hxx"
......@@ -77,19 +81,19 @@ public:
/** load.
*/
storeError lookupPageAt (
PageHolder & rxPage,
std::shared_ptr<PageData> & rxPage,
sal_uInt32 nOffset);
/** insert.
*/
storeError insertPageAt (
PageHolder const & rxPage,
std::shared_ptr<PageData> const & rxPage,
sal_uInt32 nOffset);
/** update, or insert.
*/
storeError updatePageAt (
PageHolder const & rxPage,
std::shared_ptr<PageData> const & rxPage,
sal_uInt32 nOffset);
/** remove (invalidate).
......
......@@ -22,6 +22,8 @@
#include "sal/config.h"
#include <memory>
#include "sal/types.h"
#include "sal/macros.h"
#include "rtl/string.h"
......@@ -106,7 +108,7 @@ class OStoreDataPageObject : public store::OStorePageObject
public:
/** Construction.
*/
explicit OStoreDataPageObject (PageHolder const & rxPage = PageHolder())
explicit OStoreDataPageObject (std::shared_ptr<PageData> const & rxPage = std::shared_ptr<PageData>())
: OStorePageObject (rxPage)
{}
......@@ -216,7 +218,7 @@ class OStoreIndirectionPageObject : public store::OStorePageObject
public:
/** Construction.
*/
explicit OStoreIndirectionPageObject (PageHolder const & rxPage = PageHolder())
explicit OStoreIndirectionPageObject (std::shared_ptr<PageData> const & rxPage = std::shared_ptr<PageData>())
: OStorePageObject (rxPage)
{}
......@@ -641,7 +643,7 @@ class OStoreDirectoryPageObject : public store::OStorePageObject
public:
/** Construction.
*/
explicit OStoreDirectoryPageObject (PageHolder const & rxPage = PageHolder())
explicit OStoreDirectoryPageObject (std::shared_ptr<PageData> const & rxPage = std::shared_ptr<PageData>())
: OStorePageObject (rxPage)
{}
......
......@@ -17,6 +17,10 @@
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <sal/config.h>
#include <memory>
#include "stortree.hxx"
#include "sal/types.h"
......@@ -358,7 +362,7 @@ storeError OStoreBTreeRootObject::change (
// Change root.
rxPageL.swap (xPage);
{
PageHolder tmp (xPage.get());
std::shared_ptr<PageData> tmp (xPage.get());
tmp.swap (m_xPage);
}
......@@ -381,7 +385,7 @@ storeError OStoreBTreeRootObject::find_lookup (
// Init node w/ root page.
testInvariant("OStoreBTreeRootObject::find_lookup(): enter");
{
PageHolder tmp (m_xPage);
std::shared_ptr<PageData> tmp (m_xPage);
tmp.swap (rNode.get());
}
......@@ -466,7 +470,7 @@ storeError OStoreBTreeRootObject::find_insert (
// Init node w/ root page.
{
PageHolder tmp (m_xPage);
std::shared_ptr<PageData> tmp (m_xPage);
tmp.swap (rNode.get());
}
......@@ -515,7 +519,7 @@ storeError OStoreBTreeRootObject::find_insert (
}
// Let next page be current.
PageHolder tmp (aNext.get());
std::shared_ptr<PageData> tmp (aNext.get());
tmp.swap (rNode.get());
}
......
......@@ -22,6 +22,8 @@
#include "sal/config.h"
#include <memory>
#include "sal/types.h"
#include "store/types.h"
......@@ -232,7 +234,7 @@ class OStoreBTreeNodeObject : public store::OStorePageObject
public:
/** Construction.
*/
explicit OStoreBTreeNodeObject (PageHolder const & rxPage = PageHolder())
explicit OStoreBTreeNodeObject (std::shared_ptr<PageData> const & rxPage = std::shared_ptr<PageData>())
: OStorePageObject (rxPage)
{}
......@@ -273,7 +275,7 @@ class OStoreBTreeRootObject : public store::OStoreBTreeNodeObject
public:
/** Construction.
*/
explicit OStoreBTreeRootObject (PageHolder const & rxPage = PageHolder())
explicit OStoreBTreeRootObject (std::shared_ptr<PageData> const & rxPage = std::shared_ptr<PageData>())
: OStoreBTreeNodeObject (rxPage)
{}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment