Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "storbase.hxx"
21 :
22 : #include "boost/noncopyable.hpp"
23 : #include "sal/types.h"
24 : #include "rtl/alloc.h"
25 : #include "rtl/ref.hxx"
26 : #include "osl/diagnose.h"
27 :
28 : #include "store/types.h"
29 : #include "object.hxx"
30 :
31 : #include <stdio.h>
32 :
33 : using namespace store;
34 :
35 : /*========================================================================
36 : *
37 : * SharedCount::Allocator.
38 : *
39 : *======================================================================*/
40 : SharedCount::Allocator &
41 0 : SharedCount::Allocator::get()
42 : {
43 0 : static Allocator g_aSharedCountAllocator;
44 0 : return g_aSharedCountAllocator;
45 : }
46 :
47 0 : SharedCount::Allocator::Allocator()
48 : {
49 : m_cache = rtl_cache_create (
50 : "store_shared_count_cache",
51 : sizeof(long),
52 : 0, // objalign
53 : 0, // constructor
54 : 0, // destructor
55 : 0, // reclaim
56 : 0, // userarg
57 : 0, // default source
58 : 0 // flags
59 0 : );
60 0 : }
61 :
62 0 : SharedCount::Allocator::~Allocator()
63 : {
64 0 : rtl_cache_destroy (m_cache), m_cache = 0;
65 0 : }
66 :
67 : /*========================================================================
68 : *
69 : * PageData::Allocator_Impl (default allocator).
70 : *
71 : *======================================================================*/
72 : namespace store
73 : {
74 :
75 : class PageData::Allocator_Impl :
76 : public store::OStoreObject,
77 : public store::PageData::Allocator,
78 : private boost::noncopyable
79 : {
80 : public:
81 : /** Construction (two phase).
82 : */
83 : Allocator_Impl();
84 :
85 : storeError initialize (sal_uInt16 nPageSize);
86 :
87 : /** Delegate multiple inherited rtl::IReference.
88 : */
89 0 : virtual oslInterlockedCount SAL_CALL acquire() SAL_OVERRIDE
90 : {
91 0 : return OStoreObject::acquire();
92 : }
93 0 : virtual oslInterlockedCount SAL_CALL release() SAL_OVERRIDE
94 : {
95 0 : return OStoreObject::release();
96 : }
97 :
98 : protected:
99 : /** Destruction.
100 : */
101 : virtual ~Allocator_Impl();
102 :
103 : private:
104 : /** Representation.
105 : */
106 : rtl_cache_type * m_page_cache;
107 : sal_uInt16 m_page_size;
108 :
109 : /** PageData::Allocator implementation.
110 : */
111 : virtual void allocate_Impl (void ** ppPage, sal_uInt16 * pnSize) SAL_OVERRIDE;
112 : virtual void deallocate_Impl (void * pPage) SAL_OVERRIDE;
113 : };
114 :
115 : } // namespace store
116 :
117 0 : PageData::Allocator_Impl::Allocator_Impl()
118 0 : : m_page_cache(0), m_page_size(0)
119 0 : {}
120 :
121 : storeError
122 0 : PageData::Allocator_Impl::initialize (sal_uInt16 nPageSize)
123 : {
124 : char name[RTL_CACHE_NAME_LENGTH + 1];
125 0 : sal_Size size = sal::static_int_cast< sal_Size >(nPageSize);
126 0 : (void) snprintf (name, sizeof(name), "store_page_alloc_%" SAL_PRIuUINTPTR, size);
127 :
128 0 : m_page_cache = rtl_cache_create (name, size, 0, 0, 0, 0, 0, 0, 0);
129 0 : if (!m_page_cache)
130 0 : return store_E_OutOfMemory;
131 :
132 0 : m_page_size = nPageSize;
133 0 : return store_E_None;
134 : }
135 :
136 0 : PageData::Allocator_Impl::~Allocator_Impl()
137 : {
138 0 : rtl_cache_destroy(m_page_cache), m_page_cache = 0;
139 0 : }
140 :
141 0 : void PageData::Allocator_Impl::allocate_Impl (void ** ppPage, sal_uInt16 * pnSize)
142 : {
143 : OSL_PRECOND((ppPage != 0) && (pnSize != 0), "contract violation");
144 0 : if ((ppPage != 0) && (pnSize != 0))
145 0 : *ppPage = rtl_cache_alloc(m_page_cache), *pnSize = m_page_size;
146 0 : }
147 :
148 0 : void PageData::Allocator_Impl::deallocate_Impl (void * pPage)
149 : {
150 : OSL_PRECOND(pPage != 0, "contract violation");
151 0 : rtl_cache_free(m_page_cache, pPage);
152 0 : }
153 :
154 : /*========================================================================
155 : *
156 : * PageData::Allocator factory.
157 : *
158 : *======================================================================*/
159 :
160 : storeError
161 0 : PageData::Allocator::createInstance (rtl::Reference< PageData::Allocator > & rxAllocator, sal_uInt16 nPageSize)
162 : {
163 0 : rtl::Reference< PageData::Allocator_Impl > xAllocator (new PageData::Allocator_Impl());
164 0 : if (!xAllocator.is())
165 0 : return store_E_OutOfMemory;
166 :
167 0 : rxAllocator = &*xAllocator;
168 0 : return xAllocator->initialize (nPageSize);
169 : }
170 :
171 : /*========================================================================
172 : *
173 : * OStorePageObject.
174 : *
175 : *======================================================================*/
176 : /*
177 : * ~OStorePageObject.
178 : */
179 0 : OStorePageObject::~OStorePageObject (void)
180 : {
181 0 : }
182 :
183 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|