1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "storbase.hxx"
32 #include "sal/types.h"
33 #include "rtl/alloc.h"
34 #include "rtl/ref.hxx"
35 #include "osl/diagnose.h"
37 #include "store/types.h"
40 #ifndef INCLUDED_STDIO_H
42 #define INCLUDED_STDIO_H
45 using namespace store
;
47 /*========================================================================
49 * SharedCount::Allocator.
51 *======================================================================*/
52 SharedCount::Allocator
&
53 SharedCount::Allocator::get()
55 static Allocator g_aSharedCountAllocator
;
56 return g_aSharedCountAllocator
;
59 SharedCount::Allocator::Allocator()
61 m_cache
= rtl_cache_create (
62 "store_shared_count_cache",
74 SharedCount::Allocator::~Allocator()
76 rtl_cache_destroy (m_cache
), m_cache
= 0;
79 /*========================================================================
81 * PageData::Allocator_Impl (default allocator).
83 *======================================================================*/
87 class PageData::Allocator_Impl
:
88 public store::OStoreObject
,
89 public store::PageData::Allocator
92 /** Construction (two phase).
96 storeError
initialize (sal_uInt16 nPageSize
);
98 /** Delegate multiple inherited rtl::IReference.
100 virtual oslInterlockedCount SAL_CALL
acquire()
102 return OStoreObject::acquire();
104 virtual oslInterlockedCount SAL_CALL
release()
106 return OStoreObject::release();
112 virtual ~Allocator_Impl();
117 rtl_cache_type
* m_page_cache
;
118 sal_uInt16 m_page_size
;
120 /** PageData::Allocator implementation.
122 virtual void allocate_Impl (void ** ppPage
, sal_uInt16
* pnSize
);
123 virtual void deallocate_Impl (void * pPage
);
127 Allocator_Impl (Allocator_Impl
const &);
128 Allocator_Impl
& operator= (Allocator_Impl
const &);
133 PageData::Allocator_Impl::Allocator_Impl()
134 : m_page_cache(0), m_page_size(0)
138 PageData::Allocator_Impl::initialize (sal_uInt16 nPageSize
)
140 char name
[RTL_CACHE_NAME_LENGTH
+ 1];
141 sal_Size size
= sal::static_int_cast
< sal_Size
>(nPageSize
);
142 (void) snprintf (name
, sizeof(name
), "store_page_alloc_%lu", size
);
144 m_page_cache
= rtl_cache_create (name
, size
, 0, 0, 0, 0, 0, 0, 0);
146 return store_E_OutOfMemory
;
148 m_page_size
= nPageSize
;
152 PageData::Allocator_Impl::~Allocator_Impl()
154 rtl_cache_destroy(m_page_cache
), m_page_cache
= 0;
157 void PageData::Allocator_Impl::allocate_Impl (void ** ppPage
, sal_uInt16
* pnSize
)
159 OSL_PRECOND((ppPage
!= 0) && (pnSize
!= 0), "contract violation");
160 if ((ppPage
!= 0) && (pnSize
!= 0))
161 *ppPage
= rtl_cache_alloc(m_page_cache
), *pnSize
= m_page_size
;
164 void PageData::Allocator_Impl::deallocate_Impl (void * pPage
)
166 OSL_PRECOND(pPage
!= 0, "contract violation");
167 rtl_cache_free(m_page_cache
, pPage
);
170 /*========================================================================
172 * PageData::Allocator factory.
174 *======================================================================*/
177 PageData::Allocator::createInstance (rtl::Reference
< PageData::Allocator
> & rxAllocator
, sal_uInt16 nPageSize
)
179 rtl::Reference
< PageData::Allocator_Impl
> xAllocator (new PageData::Allocator_Impl());
180 if (!xAllocator
.is())
181 return store_E_OutOfMemory
;
183 rxAllocator
= &*xAllocator
;
184 return xAllocator
->initialize (nPageSize
);
187 /*========================================================================
191 *======================================================================*/
195 OStorePageObject::~OStorePageObject (void)
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */