xref: /aoo41x/main/store/source/storbase.cxx (revision 73d9b18a)
1*73d9b18aSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*73d9b18aSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*73d9b18aSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*73d9b18aSAndrew Rist  * distributed with this work for additional information
6*73d9b18aSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*73d9b18aSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*73d9b18aSAndrew Rist  * "License"); you may not use this file except in compliance
9*73d9b18aSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*73d9b18aSAndrew Rist  *
11*73d9b18aSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*73d9b18aSAndrew Rist  *
13*73d9b18aSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*73d9b18aSAndrew Rist  * software distributed under the License is distributed on an
15*73d9b18aSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*73d9b18aSAndrew Rist  * KIND, either express or implied.  See the License for the
17*73d9b18aSAndrew Rist  * specific language governing permissions and limitations
18*73d9b18aSAndrew Rist  * under the License.
19*73d9b18aSAndrew Rist  *
20*73d9b18aSAndrew Rist  *************************************************************/
21*73d9b18aSAndrew Rist 
22*73d9b18aSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_store.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "storbase.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include "sal/types.h"
30cdf0e10cSrcweir #include "rtl/alloc.h"
31cdf0e10cSrcweir #include "rtl/ref.hxx"
32cdf0e10cSrcweir #include "osl/diagnose.h"
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include "store/types.h"
35cdf0e10cSrcweir #include "object.hxx"
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #ifndef INCLUDED_STDIO_H
38cdf0e10cSrcweir #include <stdio.h>
39cdf0e10cSrcweir #define INCLUDED_STDIO_H
40cdf0e10cSrcweir #endif
41cdf0e10cSrcweir 
42cdf0e10cSrcweir using namespace store;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir /*========================================================================
45cdf0e10cSrcweir  *
46cdf0e10cSrcweir  * SharedCount::Allocator.
47cdf0e10cSrcweir  *
48cdf0e10cSrcweir  *======================================================================*/
49cdf0e10cSrcweir SharedCount::Allocator &
get()50cdf0e10cSrcweir SharedCount::Allocator::get()
51cdf0e10cSrcweir {
52cdf0e10cSrcweir     static Allocator g_aSharedCountAllocator;
53cdf0e10cSrcweir     return g_aSharedCountAllocator;
54cdf0e10cSrcweir }
55cdf0e10cSrcweir 
Allocator()56cdf0e10cSrcweir SharedCount::Allocator::Allocator()
57cdf0e10cSrcweir {
58cdf0e10cSrcweir     m_cache = rtl_cache_create (
59cdf0e10cSrcweir         "store_shared_count_cache",
60cdf0e10cSrcweir         sizeof(long),
61cdf0e10cSrcweir         0, // objalign
62cdf0e10cSrcweir         0, // constructor
63cdf0e10cSrcweir         0, // destructor
64cdf0e10cSrcweir         0, // reclaim
65cdf0e10cSrcweir         0, // userarg
66cdf0e10cSrcweir         0, // default source
67cdf0e10cSrcweir         0  // flags
68cdf0e10cSrcweir         );
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
~Allocator()71cdf0e10cSrcweir SharedCount::Allocator::~Allocator()
72cdf0e10cSrcweir {
73cdf0e10cSrcweir     rtl_cache_destroy (m_cache), m_cache = 0;
74cdf0e10cSrcweir }
75cdf0e10cSrcweir 
76cdf0e10cSrcweir /*========================================================================
77cdf0e10cSrcweir  *
78cdf0e10cSrcweir  * PageData::Allocator_Impl (default allocator).
79cdf0e10cSrcweir  *
80cdf0e10cSrcweir  *======================================================================*/
81cdf0e10cSrcweir namespace store
82cdf0e10cSrcweir {
83cdf0e10cSrcweir 
84cdf0e10cSrcweir class PageData::Allocator_Impl :
85cdf0e10cSrcweir     public store::OStoreObject,
86cdf0e10cSrcweir     public store::PageData::Allocator
87cdf0e10cSrcweir {
88cdf0e10cSrcweir public:
89cdf0e10cSrcweir     /** Construction (two phase).
90cdf0e10cSrcweir      */
91cdf0e10cSrcweir     Allocator_Impl();
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     storeError initialize (sal_uInt16 nPageSize);
94cdf0e10cSrcweir 
95cdf0e10cSrcweir     /** Delegate multiple inherited rtl::IReference.
96cdf0e10cSrcweir      */
acquire()97cdf0e10cSrcweir     virtual oslInterlockedCount SAL_CALL acquire()
98cdf0e10cSrcweir     {
99cdf0e10cSrcweir         return OStoreObject::acquire();
100cdf0e10cSrcweir     }
release()101cdf0e10cSrcweir     virtual oslInterlockedCount SAL_CALL release()
102cdf0e10cSrcweir     {
103cdf0e10cSrcweir         return OStoreObject::release();
104cdf0e10cSrcweir     }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir protected:
107cdf0e10cSrcweir     /** Destruction.
108cdf0e10cSrcweir      */
109cdf0e10cSrcweir     virtual ~Allocator_Impl();
110cdf0e10cSrcweir 
111cdf0e10cSrcweir private:
112cdf0e10cSrcweir     /** Representation.
113cdf0e10cSrcweir      */
114cdf0e10cSrcweir     rtl_cache_type * m_page_cache;
115cdf0e10cSrcweir     sal_uInt16       m_page_size;
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     /** PageData::Allocator implementation.
118cdf0e10cSrcweir      */
119cdf0e10cSrcweir     virtual void allocate_Impl (void ** ppPage, sal_uInt16 * pnSize);
120cdf0e10cSrcweir     virtual void deallocate_Impl (void * pPage);
121cdf0e10cSrcweir 
122cdf0e10cSrcweir     /** Not implemented.
123cdf0e10cSrcweir      */
124cdf0e10cSrcweir     Allocator_Impl (Allocator_Impl const &);
125cdf0e10cSrcweir     Allocator_Impl & operator= (Allocator_Impl const &);
126cdf0e10cSrcweir };
127cdf0e10cSrcweir 
128cdf0e10cSrcweir } // namespace store
129cdf0e10cSrcweir 
Allocator_Impl()130cdf0e10cSrcweir PageData::Allocator_Impl::Allocator_Impl()
131cdf0e10cSrcweir     : m_page_cache(0), m_page_size(0)
132cdf0e10cSrcweir {}
133cdf0e10cSrcweir 
134cdf0e10cSrcweir storeError
initialize(sal_uInt16 nPageSize)135cdf0e10cSrcweir PageData::Allocator_Impl::initialize (sal_uInt16 nPageSize)
136cdf0e10cSrcweir {
137cdf0e10cSrcweir     char name[RTL_CACHE_NAME_LENGTH + 1];
138cdf0e10cSrcweir     sal_Size size = sal::static_int_cast< sal_Size >(nPageSize);
139cdf0e10cSrcweir     (void) snprintf (name, sizeof(name), "store_page_alloc_%lu", size);
140cdf0e10cSrcweir 
141cdf0e10cSrcweir     m_page_cache = rtl_cache_create (name, size, 0, 0, 0, 0, 0, 0, 0);
142cdf0e10cSrcweir     if (!m_page_cache)
143cdf0e10cSrcweir         return store_E_OutOfMemory;
144cdf0e10cSrcweir 
145cdf0e10cSrcweir     m_page_size = nPageSize;
146cdf0e10cSrcweir     return store_E_None;
147cdf0e10cSrcweir }
148cdf0e10cSrcweir 
~Allocator_Impl()149cdf0e10cSrcweir PageData::Allocator_Impl::~Allocator_Impl()
150cdf0e10cSrcweir {
151cdf0e10cSrcweir     rtl_cache_destroy(m_page_cache), m_page_cache = 0;
152cdf0e10cSrcweir }
153cdf0e10cSrcweir 
allocate_Impl(void ** ppPage,sal_uInt16 * pnSize)154cdf0e10cSrcweir void PageData::Allocator_Impl::allocate_Impl (void ** ppPage, sal_uInt16 * pnSize)
155cdf0e10cSrcweir {
156cdf0e10cSrcweir     OSL_PRECOND((ppPage != 0) && (pnSize != 0), "contract violation");
157cdf0e10cSrcweir     if ((ppPage != 0) && (pnSize != 0))
158cdf0e10cSrcweir         *ppPage = rtl_cache_alloc(m_page_cache), *pnSize = m_page_size;
159cdf0e10cSrcweir }
160cdf0e10cSrcweir 
deallocate_Impl(void * pPage)161cdf0e10cSrcweir void PageData::Allocator_Impl::deallocate_Impl (void * pPage)
162cdf0e10cSrcweir {
163cdf0e10cSrcweir     OSL_PRECOND(pPage != 0, "contract violation");
164cdf0e10cSrcweir     rtl_cache_free(m_page_cache, pPage);
165cdf0e10cSrcweir }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir /*========================================================================
168cdf0e10cSrcweir  *
169cdf0e10cSrcweir  * PageData::Allocator factory.
170cdf0e10cSrcweir  *
171cdf0e10cSrcweir  *======================================================================*/
172cdf0e10cSrcweir 
173cdf0e10cSrcweir storeError
createInstance(rtl::Reference<PageData::Allocator> & rxAllocator,sal_uInt16 nPageSize)174cdf0e10cSrcweir PageData::Allocator::createInstance (rtl::Reference< PageData::Allocator > & rxAllocator, sal_uInt16 nPageSize)
175cdf0e10cSrcweir {
176cdf0e10cSrcweir     rtl::Reference< PageData::Allocator_Impl > xAllocator (new PageData::Allocator_Impl());
177cdf0e10cSrcweir     if (!xAllocator.is())
178cdf0e10cSrcweir         return store_E_OutOfMemory;
179cdf0e10cSrcweir 
180cdf0e10cSrcweir     rxAllocator = &*xAllocator;
181cdf0e10cSrcweir     return xAllocator->initialize (nPageSize);
182cdf0e10cSrcweir }
183cdf0e10cSrcweir 
184cdf0e10cSrcweir /*========================================================================
185cdf0e10cSrcweir  *
186cdf0e10cSrcweir  * OStorePageObject.
187cdf0e10cSrcweir  *
188cdf0e10cSrcweir  *======================================================================*/
189cdf0e10cSrcweir /*
190cdf0e10cSrcweir  * ~OStorePageObject.
191cdf0e10cSrcweir  */
~OStorePageObject(void)192cdf0e10cSrcweir OStorePageObject::~OStorePageObject (void)
193cdf0e10cSrcweir {
194cdf0e10cSrcweir }
195