xref: /trunk/main/store/source/storbase.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_store.hxx"
30 
31 #include "storbase.hxx"
32 
33 #include "sal/types.h"
34 #include "rtl/alloc.h"
35 #include "rtl/ref.hxx"
36 #include "osl/diagnose.h"
37 
38 #include "store/types.h"
39 #include "object.hxx"
40 
41 #ifndef INCLUDED_STDIO_H
42 #include <stdio.h>
43 #define INCLUDED_STDIO_H
44 #endif
45 
46 using namespace store;
47 
48 /*========================================================================
49  *
50  * SharedCount::Allocator.
51  *
52  *======================================================================*/
53 SharedCount::Allocator &
54 SharedCount::Allocator::get()
55 {
56     static Allocator g_aSharedCountAllocator;
57     return g_aSharedCountAllocator;
58 }
59 
60 SharedCount::Allocator::Allocator()
61 {
62     m_cache = rtl_cache_create (
63         "store_shared_count_cache",
64         sizeof(long),
65         0, // objalign
66         0, // constructor
67         0, // destructor
68         0, // reclaim
69         0, // userarg
70         0, // default source
71         0  // flags
72         );
73 }
74 
75 SharedCount::Allocator::~Allocator()
76 {
77     rtl_cache_destroy (m_cache), m_cache = 0;
78 }
79 
80 /*========================================================================
81  *
82  * PageData::Allocator_Impl (default allocator).
83  *
84  *======================================================================*/
85 namespace store
86 {
87 
88 class PageData::Allocator_Impl :
89     public store::OStoreObject,
90     public store::PageData::Allocator
91 {
92 public:
93     /** Construction (two phase).
94      */
95     Allocator_Impl();
96 
97     storeError initialize (sal_uInt16 nPageSize);
98 
99     /** Delegate multiple inherited rtl::IReference.
100      */
101     virtual oslInterlockedCount SAL_CALL acquire()
102     {
103         return OStoreObject::acquire();
104     }
105     virtual oslInterlockedCount SAL_CALL release()
106     {
107         return OStoreObject::release();
108     }
109 
110 protected:
111     /** Destruction.
112      */
113     virtual ~Allocator_Impl();
114 
115 private:
116     /** Representation.
117      */
118     rtl_cache_type * m_page_cache;
119     sal_uInt16       m_page_size;
120 
121     /** PageData::Allocator implementation.
122      */
123     virtual void allocate_Impl (void ** ppPage, sal_uInt16 * pnSize);
124     virtual void deallocate_Impl (void * pPage);
125 
126     /** Not implemented.
127      */
128     Allocator_Impl (Allocator_Impl const &);
129     Allocator_Impl & operator= (Allocator_Impl const &);
130 };
131 
132 } // namespace store
133 
134 PageData::Allocator_Impl::Allocator_Impl()
135     : m_page_cache(0), m_page_size(0)
136 {}
137 
138 storeError
139 PageData::Allocator_Impl::initialize (sal_uInt16 nPageSize)
140 {
141     char name[RTL_CACHE_NAME_LENGTH + 1];
142     sal_Size size = sal::static_int_cast< sal_Size >(nPageSize);
143     (void) snprintf (name, sizeof(name), "store_page_alloc_%lu", size);
144 
145     m_page_cache = rtl_cache_create (name, size, 0, 0, 0, 0, 0, 0, 0);
146     if (!m_page_cache)
147         return store_E_OutOfMemory;
148 
149     m_page_size = nPageSize;
150     return store_E_None;
151 }
152 
153 PageData::Allocator_Impl::~Allocator_Impl()
154 {
155     rtl_cache_destroy(m_page_cache), m_page_cache = 0;
156 }
157 
158 void PageData::Allocator_Impl::allocate_Impl (void ** ppPage, sal_uInt16 * pnSize)
159 {
160     OSL_PRECOND((ppPage != 0) && (pnSize != 0), "contract violation");
161     if ((ppPage != 0) && (pnSize != 0))
162         *ppPage = rtl_cache_alloc(m_page_cache), *pnSize = m_page_size;
163 }
164 
165 void PageData::Allocator_Impl::deallocate_Impl (void * pPage)
166 {
167     OSL_PRECOND(pPage != 0, "contract violation");
168     rtl_cache_free(m_page_cache, pPage);
169 }
170 
171 /*========================================================================
172  *
173  * PageData::Allocator factory.
174  *
175  *======================================================================*/
176 
177 storeError
178 PageData::Allocator::createInstance (rtl::Reference< PageData::Allocator > & rxAllocator, sal_uInt16 nPageSize)
179 {
180     rtl::Reference< PageData::Allocator_Impl > xAllocator (new PageData::Allocator_Impl());
181     if (!xAllocator.is())
182         return store_E_OutOfMemory;
183 
184     rxAllocator = &*xAllocator;
185     return xAllocator->initialize (nPageSize);
186 }
187 
188 /*========================================================================
189  *
190  * OStorePageObject.
191  *
192  *======================================================================*/
193 /*
194  * ~OStorePageObject.
195  */
196 OStorePageObject::~OStorePageObject (void)
197 {
198 }
199