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