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 .
20 #include "alloc_cache.hxx"
21 #include "alloc_impl.hxx"
22 #include "alloc_arena.hxx"
23 #include <rtllifecycle.h>
31 provided for cache_type allocations, and hash_table resizing.
35 static rtl_arena_type
* gp_cache_arena
= nullptr;
40 rtl_cache_type
* rtl_cache_activate(
41 rtl_cache_type
* cache
,
45 int (SAL_CALL
* constructor
)(void * obj
, void * userarg
),
46 void (SAL_CALL
* destructor
) (void * obj
, void * userarg
),
52 snprintf (cache
->m_name
, sizeof(cache
->m_name
), "%s", name
);
56 /* determine default alignment */
57 if (objsize
>= RTL_MEMORY_ALIGNMENT_8
)
58 objalign
= RTL_MEMORY_ALIGNMENT_8
;
60 objalign
= RTL_MEMORY_ALIGNMENT_4
;
64 /* ensure minimum alignment */
65 if(objalign
< RTL_MEMORY_ALIGNMENT_4
)
67 objalign
= RTL_MEMORY_ALIGNMENT_4
;
70 assert(RTL_MEMORY_ISP2(objalign
));
72 cache
->m_type_size
= RTL_MEMORY_P2ROUNDUP(objsize
, objalign
);
74 cache
->m_constructor
= constructor
;
75 cache
->m_destructor
= destructor
;
76 cache
->m_userarg
= userarg
;
83 rtl_cache_type
* SAL_CALL
rtl_cache_create(
87 int (SAL_CALL
* constructor
)(void * obj
, void * userarg
),
88 void (SAL_CALL
* destructor
) (void * obj
, void * userarg
),
89 void (SAL_CALL
* /*reclaim*/) (void * userarg
),
93 ) SAL_THROW_EXTERN_C()
95 rtl_cache_type
* result
= nullptr;
96 sal_Size size
= sizeof(rtl_cache_type
);
99 result
= static_cast<rtl_cache_type
*>(rtl_arena_alloc (gp_cache_arena
, &size
));
102 rtl_cache_type
* cache
= result
;
103 memset (cache
, 0, sizeof(rtl_cache_type
));
105 result
= rtl_cache_activate (
117 /* activation failed */
118 rtl_arena_free (gp_cache_arena
, cache
, size
);
121 else if (!gp_cache_arena
)
123 ensureCacheSingleton();
133 void SAL_CALL
rtl_cache_destroy(rtl_cache_type
* cache
) SAL_THROW_EXTERN_C()
137 rtl_arena_free (gp_cache_arena
, cache
, sizeof(rtl_cache_type
));
141 void * SAL_CALL
rtl_cache_alloc(rtl_cache_type
* cache
) SAL_THROW_EXTERN_C()
143 void * obj
= nullptr;
148 obj
= std::malloc(cache
->m_type_size
);
149 if (obj
&& cache
->m_constructor
)
151 if (!(cache
->m_constructor
)(obj
, cache
->m_userarg
))
153 /* construction failure */
161 void SAL_CALL
rtl_cache_free(
162 rtl_cache_type
* cache
,
164 ) SAL_THROW_EXTERN_C()
168 if (cache
->m_destructor
)
170 /* destruct object */
171 (cache
->m_destructor
)(obj
, cache
->m_userarg
);
179 void SAL_CALL
rtl_secureZeroMemory(void *Ptr
, sal_Size Bytes
) SAL_THROW_EXTERN_C()
181 //currently glibc doesn't implement memset_s
182 volatile char *p
= static_cast<volatile char*>(Ptr
);
187 #elif defined(_WIN32)
189 void SAL_CALL
rtl_secureZeroMemory(void *Ptr
, sal_Size Bytes
) SAL_THROW_EXTERN_C()
191 RtlSecureZeroMemory(Ptr
, Bytes
);
194 #endif /* SAL_UNX || _WIN32 */
196 void rtl_cache_init()
198 /* cache: internal arena */
199 assert(!gp_cache_arena
);
201 gp_cache_arena
= rtl_arena_create (
202 "rtl_cache_internal_arena",
204 0, /* no quantum caching */
205 nullptr, /* default source */
210 assert(gp_cache_arena
);
212 /* check 'gp_default_arena' initialization */
213 assert(gp_default_arena
);
216 void rtl_cache_fini()
220 rtl_arena_destroy (gp_cache_arena
);
221 gp_cache_arena
= nullptr;
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */