1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "storbase.hxx"
23 #include "sal/types.h"
24 #include "rtl/alloc.h"
25 #include "rtl/ref.hxx"
26 #include "osl/diagnose.h"
28 #include "store/types.h"
33 using namespace store
;
35 /*========================================================================
37 * SharedCount::Allocator.
39 *======================================================================*/
40 SharedCount::Allocator
&
41 SharedCount::Allocator::get()
43 static Allocator g_aSharedCountAllocator
;
44 return g_aSharedCountAllocator
;
47 SharedCount::Allocator::Allocator()
49 m_cache
= rtl_cache_create (
50 "store_shared_count_cache",
62 SharedCount::Allocator::~Allocator()
64 rtl_cache_destroy (m_cache
), m_cache
= 0;
67 /*========================================================================
69 * PageData::Allocator_Impl (default allocator).
71 *======================================================================*/
75 class PageData::Allocator_Impl
:
76 public store::OStoreObject
,
77 public store::PageData::Allocator
80 /** Construction (two phase).
84 storeError
initialize (sal_uInt16 nPageSize
);
86 /** Delegate multiple inherited rtl::IReference.
88 virtual oslInterlockedCount SAL_CALL
acquire()
90 return OStoreObject::acquire();
92 virtual oslInterlockedCount SAL_CALL
release()
94 return OStoreObject::release();
100 virtual ~Allocator_Impl();
105 rtl_cache_type
* m_page_cache
;
106 sal_uInt16 m_page_size
;
108 /** PageData::Allocator implementation.
110 virtual void allocate_Impl (void ** ppPage
, sal_uInt16
* pnSize
);
111 virtual void deallocate_Impl (void * pPage
);
115 Allocator_Impl (Allocator_Impl
const &);
116 Allocator_Impl
& operator= (Allocator_Impl
const &);
121 PageData::Allocator_Impl::Allocator_Impl()
122 : m_page_cache(0), m_page_size(0)
126 PageData::Allocator_Impl::initialize (sal_uInt16 nPageSize
)
128 char name
[RTL_CACHE_NAME_LENGTH
+ 1];
129 sal_Size size
= sal::static_int_cast
< sal_Size
>(nPageSize
);
130 (void) snprintf (name
, sizeof(name
), "store_page_alloc_%lu", size
);
132 m_page_cache
= rtl_cache_create (name
, size
, 0, 0, 0, 0, 0, 0, 0);
134 return store_E_OutOfMemory
;
136 m_page_size
= nPageSize
;
140 PageData::Allocator_Impl::~Allocator_Impl()
142 rtl_cache_destroy(m_page_cache
), m_page_cache
= 0;
145 void PageData::Allocator_Impl::allocate_Impl (void ** ppPage
, sal_uInt16
* pnSize
)
147 OSL_PRECOND((ppPage
!= 0) && (pnSize
!= 0), "contract violation");
148 if ((ppPage
!= 0) && (pnSize
!= 0))
149 *ppPage
= rtl_cache_alloc(m_page_cache
), *pnSize
= m_page_size
;
152 void PageData::Allocator_Impl::deallocate_Impl (void * pPage
)
154 OSL_PRECOND(pPage
!= 0, "contract violation");
155 rtl_cache_free(m_page_cache
, pPage
);
158 /*========================================================================
160 * PageData::Allocator factory.
162 *======================================================================*/
165 PageData::Allocator::createInstance (rtl::Reference
< PageData::Allocator
> & rxAllocator
, sal_uInt16 nPageSize
)
167 rtl::Reference
< PageData::Allocator_Impl
> xAllocator (new PageData::Allocator_Impl());
168 if (!xAllocator
.is())
169 return store_E_OutOfMemory
;
171 rxAllocator
= &*xAllocator
;
172 return xAllocator
->initialize (nPageSize
);
175 /*========================================================================
179 *======================================================================*/
183 OStorePageObject::~OStorePageObject (void)
187 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */