xref: /aoo42x/main/store/source/storbase.cxx (revision cdf0e10c)
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 "storbase.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include "sal/types.h"
34*cdf0e10cSrcweir #include "rtl/alloc.h"
35*cdf0e10cSrcweir #include "rtl/ref.hxx"
36*cdf0e10cSrcweir #include "osl/diagnose.h"
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #include "store/types.h"
39*cdf0e10cSrcweir #include "object.hxx"
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #ifndef INCLUDED_STDIO_H
42*cdf0e10cSrcweir #include <stdio.h>
43*cdf0e10cSrcweir #define INCLUDED_STDIO_H
44*cdf0e10cSrcweir #endif
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir using namespace store;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir /*========================================================================
49*cdf0e10cSrcweir  *
50*cdf0e10cSrcweir  * SharedCount::Allocator.
51*cdf0e10cSrcweir  *
52*cdf0e10cSrcweir  *======================================================================*/
53*cdf0e10cSrcweir SharedCount::Allocator &
54*cdf0e10cSrcweir SharedCount::Allocator::get()
55*cdf0e10cSrcweir {
56*cdf0e10cSrcweir     static Allocator g_aSharedCountAllocator;
57*cdf0e10cSrcweir     return g_aSharedCountAllocator;
58*cdf0e10cSrcweir }
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir SharedCount::Allocator::Allocator()
61*cdf0e10cSrcweir {
62*cdf0e10cSrcweir     m_cache = rtl_cache_create (
63*cdf0e10cSrcweir         "store_shared_count_cache",
64*cdf0e10cSrcweir         sizeof(long),
65*cdf0e10cSrcweir         0, // objalign
66*cdf0e10cSrcweir         0, // constructor
67*cdf0e10cSrcweir         0, // destructor
68*cdf0e10cSrcweir         0, // reclaim
69*cdf0e10cSrcweir         0, // userarg
70*cdf0e10cSrcweir         0, // default source
71*cdf0e10cSrcweir         0  // flags
72*cdf0e10cSrcweir         );
73*cdf0e10cSrcweir }
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir SharedCount::Allocator::~Allocator()
76*cdf0e10cSrcweir {
77*cdf0e10cSrcweir     rtl_cache_destroy (m_cache), m_cache = 0;
78*cdf0e10cSrcweir }
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir /*========================================================================
81*cdf0e10cSrcweir  *
82*cdf0e10cSrcweir  * PageData::Allocator_Impl (default allocator).
83*cdf0e10cSrcweir  *
84*cdf0e10cSrcweir  *======================================================================*/
85*cdf0e10cSrcweir namespace store
86*cdf0e10cSrcweir {
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir class PageData::Allocator_Impl :
89*cdf0e10cSrcweir     public store::OStoreObject,
90*cdf0e10cSrcweir     public store::PageData::Allocator
91*cdf0e10cSrcweir {
92*cdf0e10cSrcweir public:
93*cdf0e10cSrcweir     /** Construction (two phase).
94*cdf0e10cSrcweir      */
95*cdf0e10cSrcweir     Allocator_Impl();
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir     storeError initialize (sal_uInt16 nPageSize);
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir     /** Delegate multiple inherited rtl::IReference.
100*cdf0e10cSrcweir      */
101*cdf0e10cSrcweir     virtual oslInterlockedCount SAL_CALL acquire()
102*cdf0e10cSrcweir     {
103*cdf0e10cSrcweir         return OStoreObject::acquire();
104*cdf0e10cSrcweir     }
105*cdf0e10cSrcweir     virtual oslInterlockedCount SAL_CALL release()
106*cdf0e10cSrcweir     {
107*cdf0e10cSrcweir         return OStoreObject::release();
108*cdf0e10cSrcweir     }
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir protected:
111*cdf0e10cSrcweir     /** Destruction.
112*cdf0e10cSrcweir      */
113*cdf0e10cSrcweir     virtual ~Allocator_Impl();
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir private:
116*cdf0e10cSrcweir     /** Representation.
117*cdf0e10cSrcweir      */
118*cdf0e10cSrcweir     rtl_cache_type * m_page_cache;
119*cdf0e10cSrcweir     sal_uInt16       m_page_size;
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir     /** PageData::Allocator implementation.
122*cdf0e10cSrcweir      */
123*cdf0e10cSrcweir     virtual void allocate_Impl (void ** ppPage, sal_uInt16 * pnSize);
124*cdf0e10cSrcweir     virtual void deallocate_Impl (void * pPage);
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir     /** Not implemented.
127*cdf0e10cSrcweir      */
128*cdf0e10cSrcweir     Allocator_Impl (Allocator_Impl const &);
129*cdf0e10cSrcweir     Allocator_Impl & operator= (Allocator_Impl const &);
130*cdf0e10cSrcweir };
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir } // namespace store
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir PageData::Allocator_Impl::Allocator_Impl()
135*cdf0e10cSrcweir     : m_page_cache(0), m_page_size(0)
136*cdf0e10cSrcweir {}
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir storeError
139*cdf0e10cSrcweir PageData::Allocator_Impl::initialize (sal_uInt16 nPageSize)
140*cdf0e10cSrcweir {
141*cdf0e10cSrcweir     char name[RTL_CACHE_NAME_LENGTH + 1];
142*cdf0e10cSrcweir     sal_Size size = sal::static_int_cast< sal_Size >(nPageSize);
143*cdf0e10cSrcweir     (void) snprintf (name, sizeof(name), "store_page_alloc_%lu", size);
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir     m_page_cache = rtl_cache_create (name, size, 0, 0, 0, 0, 0, 0, 0);
146*cdf0e10cSrcweir     if (!m_page_cache)
147*cdf0e10cSrcweir         return store_E_OutOfMemory;
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir     m_page_size = nPageSize;
150*cdf0e10cSrcweir     return store_E_None;
151*cdf0e10cSrcweir }
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir PageData::Allocator_Impl::~Allocator_Impl()
154*cdf0e10cSrcweir {
155*cdf0e10cSrcweir     rtl_cache_destroy(m_page_cache), m_page_cache = 0;
156*cdf0e10cSrcweir }
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir void PageData::Allocator_Impl::allocate_Impl (void ** ppPage, sal_uInt16 * pnSize)
159*cdf0e10cSrcweir {
160*cdf0e10cSrcweir     OSL_PRECOND((ppPage != 0) && (pnSize != 0), "contract violation");
161*cdf0e10cSrcweir     if ((ppPage != 0) && (pnSize != 0))
162*cdf0e10cSrcweir         *ppPage = rtl_cache_alloc(m_page_cache), *pnSize = m_page_size;
163*cdf0e10cSrcweir }
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir void PageData::Allocator_Impl::deallocate_Impl (void * pPage)
166*cdf0e10cSrcweir {
167*cdf0e10cSrcweir     OSL_PRECOND(pPage != 0, "contract violation");
168*cdf0e10cSrcweir     rtl_cache_free(m_page_cache, pPage);
169*cdf0e10cSrcweir }
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir /*========================================================================
172*cdf0e10cSrcweir  *
173*cdf0e10cSrcweir  * PageData::Allocator factory.
174*cdf0e10cSrcweir  *
175*cdf0e10cSrcweir  *======================================================================*/
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir storeError
178*cdf0e10cSrcweir PageData::Allocator::createInstance (rtl::Reference< PageData::Allocator > & rxAllocator, sal_uInt16 nPageSize)
179*cdf0e10cSrcweir {
180*cdf0e10cSrcweir     rtl::Reference< PageData::Allocator_Impl > xAllocator (new PageData::Allocator_Impl());
181*cdf0e10cSrcweir     if (!xAllocator.is())
182*cdf0e10cSrcweir         return store_E_OutOfMemory;
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir     rxAllocator = &*xAllocator;
185*cdf0e10cSrcweir     return xAllocator->initialize (nPageSize);
186*cdf0e10cSrcweir }
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir /*========================================================================
189*cdf0e10cSrcweir  *
190*cdf0e10cSrcweir  * OStorePageObject.
191*cdf0e10cSrcweir  *
192*cdf0e10cSrcweir  *======================================================================*/
193*cdf0e10cSrcweir /*
194*cdf0e10cSrcweir  * ~OStorePageObject.
195*cdf0e10cSrcweir  */
196*cdf0e10cSrcweir OStorePageObject::~OStorePageObject (void)
197*cdf0e10cSrcweir {
198*cdf0e10cSrcweir }
199