1647f063dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3647f063dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4647f063dSAndrew Rist * or more contributor license agreements. See the NOTICE file
5647f063dSAndrew Rist * distributed with this work for additional information
6647f063dSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7647f063dSAndrew Rist * to you under the Apache License, Version 2.0 (the
8647f063dSAndrew Rist * "License"); you may not use this file except in compliance
9647f063dSAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11647f063dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13647f063dSAndrew Rist * Unless required by applicable law or agreed to in writing,
14647f063dSAndrew Rist * software distributed under the License is distributed on an
15647f063dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16647f063dSAndrew Rist * KIND, either express or implied. See the License for the
17647f063dSAndrew Rist * specific language governing permissions and limitations
18647f063dSAndrew Rist * under the License.
19cdf0e10cSrcweir *
20647f063dSAndrew Rist *************************************************************/
21647f063dSAndrew Rist
22647f063dSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #include "rtl/alloc.h"
25cdf0e10cSrcweir #include "alloc_impl.h"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #ifndef INCLUDED_STRING_H
28cdf0e10cSrcweir #include <string.h>
29cdf0e10cSrcweir #define INCLUDED_STRING_H
30cdf0e10cSrcweir #endif
31cdf0e10cSrcweir
32cdf0e10cSrcweir #if !defined(FORCE_SYSALLOC)
33cdf0e10cSrcweir
34cdf0e10cSrcweir /* ================================================================= *
35cdf0e10cSrcweir *
36cdf0e10cSrcweir * custom allocator includes.
37cdf0e10cSrcweir *
38cdf0e10cSrcweir * ================================================================= */
39cdf0e10cSrcweir
40cdf0e10cSrcweir #ifndef INCLUDED_STDIO_H
41cdf0e10cSrcweir #include <stdio.h>
42cdf0e10cSrcweir #define INCLUDED_STDIO_H
43cdf0e10cSrcweir #endif
44cdf0e10cSrcweir #include "internal/once.h"
45cdf0e10cSrcweir #include "sal/macros.h"
46cdf0e10cSrcweir #include "osl/diagnose.h"
47cdf0e10cSrcweir
48cdf0e10cSrcweir /* ================================================================= *
49cdf0e10cSrcweir *
50cdf0e10cSrcweir * custom allocator internals.
51cdf0e10cSrcweir *
52cdf0e10cSrcweir * ================================================================= */
53cdf0e10cSrcweir
54cdf0e10cSrcweir static const sal_Size g_alloc_sizes[] =
55cdf0e10cSrcweir {
56cdf0e10cSrcweir /* powers of 2**(1/4) */
57cdf0e10cSrcweir 4 * 4, 6 * 4,
58cdf0e10cSrcweir 4 * 8, 5 * 8, 6 * 8, 7 * 8,
59cdf0e10cSrcweir 4 * 16, 5 * 16, 6 * 16, 7 * 16,
60cdf0e10cSrcweir 4 * 32, 5 * 32, 6 * 32, 7 * 32,
61cdf0e10cSrcweir 4 * 64, 5 * 64, 6 * 64, 7 * 64,
62cdf0e10cSrcweir 4 * 128, 5 * 128, 6 * 128, 7 * 128,
63cdf0e10cSrcweir 4 * 256, 5 * 256, 6 * 256, 7 * 256,
64cdf0e10cSrcweir 4 * 512, 5 * 512, 6 * 512, 7 * 512,
65cdf0e10cSrcweir 4 * 1024, 5 * 1024, 6 * 1024, 7 * 1024,
66cdf0e10cSrcweir 4 * 2048, 5 * 2048, 6 * 2048, 7 * 2048,
67cdf0e10cSrcweir 4 * 4096
68cdf0e10cSrcweir };
69cdf0e10cSrcweir
70cdf0e10cSrcweir #define RTL_MEMORY_CACHED_LIMIT 4 * 4096
71cdf0e10cSrcweir #define RTL_MEMORY_CACHED_SIZES (sizeof(g_alloc_sizes) / sizeof(g_alloc_sizes[0]))
72cdf0e10cSrcweir
73cdf0e10cSrcweir static rtl_cache_type * g_alloc_caches[RTL_MEMORY_CACHED_SIZES] =
74cdf0e10cSrcweir {
75cdf0e10cSrcweir 0,
76cdf0e10cSrcweir };
77cdf0e10cSrcweir
78787e1130SDon Lewis #ifdef NEED_ALIGN16
79787e1130SDon Lewis #define RTL_MEMALIGN 16
80787e1130SDon Lewis #define RTL_MEMALIGN_SHIFT 4
81787e1130SDon Lewis #else
82cdf0e10cSrcweir #define RTL_MEMALIGN 8
83cdf0e10cSrcweir #define RTL_MEMALIGN_SHIFT 3
84787e1130SDon Lewis #endif
85cdf0e10cSrcweir
86cdf0e10cSrcweir static rtl_cache_type * g_alloc_table[RTL_MEMORY_CACHED_LIMIT >> RTL_MEMALIGN_SHIFT] =
87cdf0e10cSrcweir {
88cdf0e10cSrcweir 0,
89cdf0e10cSrcweir };
90cdf0e10cSrcweir
91509a48ffSpfg static rtl_arena_type * gp_alloc_arena = NULL;
92cdf0e10cSrcweir
93cdf0e10cSrcweir /* ================================================================= *
94cdf0e10cSrcweir *
95cdf0e10cSrcweir * custom allocator initialization / finalization.
96cdf0e10cSrcweir *
97cdf0e10cSrcweir * ================================================================= */
98cdf0e10cSrcweir
99cdf0e10cSrcweir static void
rtl_memory_once_init(void)100cdf0e10cSrcweir rtl_memory_once_init (void)
101cdf0e10cSrcweir {
102cdf0e10cSrcweir {
103cdf0e10cSrcweir /* global memory arena */
104509a48ffSpfg OSL_ASSERT(gp_alloc_arena == NULL);
105cdf0e10cSrcweir
106cdf0e10cSrcweir gp_alloc_arena = rtl_arena_create (
107cdf0e10cSrcweir "rtl_alloc_arena",
108cdf0e10cSrcweir 2048, /* quantum */
109cdf0e10cSrcweir 0, /* w/o quantum caching */
110cdf0e10cSrcweir 0, /* default source */
111cdf0e10cSrcweir rtl_arena_alloc,
112cdf0e10cSrcweir rtl_arena_free,
113cdf0e10cSrcweir 0 /* flags */
114cdf0e10cSrcweir );
115509a48ffSpfg OSL_ASSERT(gp_alloc_arena != NULL);
116cdf0e10cSrcweir }
117cdf0e10cSrcweir {
118cdf0e10cSrcweir sal_Size size;
119cdf0e10cSrcweir int i, n = RTL_MEMORY_CACHED_SIZES;
120cdf0e10cSrcweir
121cdf0e10cSrcweir for (i = 0; i < n; i++)
122cdf0e10cSrcweir {
123cdf0e10cSrcweir char name[RTL_CACHE_NAME_LENGTH + 1];
124cdf0e10cSrcweir (void) snprintf (name, sizeof(name), "rtl_alloc_%lu", g_alloc_sizes[i]);
125cdf0e10cSrcweir g_alloc_caches[i] = rtl_cache_create (name, g_alloc_sizes[i], 0, NULL, NULL, NULL, NULL, NULL, 0);
126cdf0e10cSrcweir }
127cdf0e10cSrcweir
128cdf0e10cSrcweir size = RTL_MEMALIGN;
129cdf0e10cSrcweir for (i = 0; i < n; i++)
130cdf0e10cSrcweir {
131cdf0e10cSrcweir while (size <= g_alloc_sizes[i])
132cdf0e10cSrcweir {
133cdf0e10cSrcweir g_alloc_table[(size - 1) >> RTL_MEMALIGN_SHIFT] = g_alloc_caches[i];
134cdf0e10cSrcweir size += RTL_MEMALIGN;
135cdf0e10cSrcweir }
136cdf0e10cSrcweir }
137cdf0e10cSrcweir }
138cdf0e10cSrcweir }
139cdf0e10cSrcweir
140cdf0e10cSrcweir static int
rtl_memory_init(void)141cdf0e10cSrcweir rtl_memory_init (void)
142cdf0e10cSrcweir {
143cdf0e10cSrcweir static sal_once_type g_once = SAL_ONCE_INIT;
144cdf0e10cSrcweir SAL_ONCE(&g_once, rtl_memory_once_init);
145509a48ffSpfg return (gp_alloc_arena != NULL);
146cdf0e10cSrcweir }
147cdf0e10cSrcweir
148cdf0e10cSrcweir /* ================================================================= */
149cdf0e10cSrcweir
150cdf0e10cSrcweir /*
151cdf0e10cSrcweir Issue http://udk.openoffice.org/issues/show_bug.cgi?id=92388
152cdf0e10cSrcweir
153cdf0e10cSrcweir Mac OS X does not seem to support "__cxa__atexit", thus leading
154cdf0e10cSrcweir to the situation that "__attribute__((destructor))__" functions
155cdf0e10cSrcweir (in particular "rtl_{memory|cache|arena}_fini") become called
156cdf0e10cSrcweir _before_ global C++ object d'tors.
157cdf0e10cSrcweir
158cdf0e10cSrcweir Delegated the call to "rtl_memory_fini()" into a dummy C++ object,
159cdf0e10cSrcweir see alloc_fini.cxx .
160cdf0e10cSrcweir */
161cdf0e10cSrcweir #if defined(__GNUC__) && !defined(MACOSX)
162cdf0e10cSrcweir static void rtl_memory_fini (void) __attribute__((destructor));
163cdf0e10cSrcweir #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
164cdf0e10cSrcweir #pragma fini(rtl_memory_fini)
165cdf0e10cSrcweir static void rtl_memory_fini (void);
166cdf0e10cSrcweir #endif /* __GNUC__ || __SUNPRO_C */
167cdf0e10cSrcweir
168cdf0e10cSrcweir void
rtl_memory_fini(void)169cdf0e10cSrcweir rtl_memory_fini (void)
170cdf0e10cSrcweir {
171cdf0e10cSrcweir int i, n;
172cdf0e10cSrcweir
173cdf0e10cSrcweir /* clear g_alloc_table */
174cdf0e10cSrcweir memset (g_alloc_table, 0, sizeof(g_alloc_table));
175cdf0e10cSrcweir
176cdf0e10cSrcweir /* cleanup g_alloc_caches */
177cdf0e10cSrcweir for (i = 0, n = RTL_MEMORY_CACHED_SIZES; i < n; i++)
178cdf0e10cSrcweir {
179509a48ffSpfg if (g_alloc_caches[i] != NULL)
180cdf0e10cSrcweir {
181cdf0e10cSrcweir rtl_cache_destroy (g_alloc_caches[i]);
182509a48ffSpfg g_alloc_caches[i] = NULL;
183cdf0e10cSrcweir }
184cdf0e10cSrcweir }
185cdf0e10cSrcweir
186cdf0e10cSrcweir /* cleanup gp_alloc_arena */
187509a48ffSpfg if (gp_alloc_arena != NULL)
188cdf0e10cSrcweir {
189cdf0e10cSrcweir rtl_arena_destroy (gp_alloc_arena);
190509a48ffSpfg gp_alloc_arena = NULL;
191cdf0e10cSrcweir }
192cdf0e10cSrcweir }
193cdf0e10cSrcweir
194cdf0e10cSrcweir /* ================================================================= *
195cdf0e10cSrcweir *
196*4772a777SJohn Bampton * custom allocator implementation.
197cdf0e10cSrcweir *
198cdf0e10cSrcweir * ================================================================= */
199cdf0e10cSrcweir
200cdf0e10cSrcweir void *
rtl_allocateMemory(sal_Size n)201cdf0e10cSrcweir SAL_CALL rtl_allocateMemory (sal_Size n) SAL_THROW_EXTERN_C()
202cdf0e10cSrcweir {
203cdf0e10cSrcweir void * p = 0;
204cdf0e10cSrcweir if (n > 0)
205cdf0e10cSrcweir {
206cdf0e10cSrcweir char * addr;
207cdf0e10cSrcweir sal_Size size = RTL_MEMORY_ALIGN(n + RTL_MEMALIGN, RTL_MEMALIGN);
208cdf0e10cSrcweir
209cdf0e10cSrcweir OSL_ASSERT(RTL_MEMALIGN >= sizeof(sal_Size));
210cdf0e10cSrcweir if (n >= SAL_MAX_SIZE - (RTL_MEMALIGN + RTL_MEMALIGN - 1))
211cdf0e10cSrcweir {
212cdf0e10cSrcweir /* requested size too large for roundup alignment */
213cdf0e10cSrcweir return 0;
214cdf0e10cSrcweir }
215cdf0e10cSrcweir
216cdf0e10cSrcweir try_alloc:
217cdf0e10cSrcweir if (size <= RTL_MEMORY_CACHED_LIMIT)
218cdf0e10cSrcweir addr = (char*)rtl_cache_alloc(g_alloc_table[(size - 1) >> RTL_MEMALIGN_SHIFT]);
219cdf0e10cSrcweir else
220cdf0e10cSrcweir addr = (char*)rtl_arena_alloc (gp_alloc_arena, &size);
221cdf0e10cSrcweir
222cdf0e10cSrcweir if (addr != 0)
223cdf0e10cSrcweir {
224cdf0e10cSrcweir ((sal_Size*)(addr))[0] = size;
225cdf0e10cSrcweir p = addr + RTL_MEMALIGN;
226cdf0e10cSrcweir }
227cdf0e10cSrcweir else if (gp_alloc_arena == 0)
228cdf0e10cSrcweir {
229cdf0e10cSrcweir if (rtl_memory_init())
230cdf0e10cSrcweir {
231cdf0e10cSrcweir /* try again */
232cdf0e10cSrcweir goto try_alloc;
233cdf0e10cSrcweir }
234cdf0e10cSrcweir }
235cdf0e10cSrcweir }
236cdf0e10cSrcweir return (p);
237cdf0e10cSrcweir }
238cdf0e10cSrcweir
239cdf0e10cSrcweir /* ================================================================= */
240cdf0e10cSrcweir
rtl_freeMemory(void * p)241cdf0e10cSrcweir void SAL_CALL rtl_freeMemory (void * p) SAL_THROW_EXTERN_C()
242cdf0e10cSrcweir {
243cdf0e10cSrcweir if (p != 0)
244cdf0e10cSrcweir {
245cdf0e10cSrcweir char * addr = (char*)(p) - RTL_MEMALIGN;
246cdf0e10cSrcweir sal_Size size = ((sal_Size*)(addr))[0];
247cdf0e10cSrcweir
248cdf0e10cSrcweir if (size <= RTL_MEMORY_CACHED_LIMIT)
249cdf0e10cSrcweir rtl_cache_free(g_alloc_table[(size - 1) >> RTL_MEMALIGN_SHIFT], addr);
250cdf0e10cSrcweir else
251cdf0e10cSrcweir rtl_arena_free (gp_alloc_arena, addr, size);
252cdf0e10cSrcweir }
253cdf0e10cSrcweir }
254cdf0e10cSrcweir
255cdf0e10cSrcweir /* ================================================================= */
256cdf0e10cSrcweir
rtl_reallocateMemory(void * p,sal_Size n)257cdf0e10cSrcweir void * SAL_CALL rtl_reallocateMemory (void * p, sal_Size n) SAL_THROW_EXTERN_C()
258cdf0e10cSrcweir {
259cdf0e10cSrcweir if (n > 0)
260cdf0e10cSrcweir {
261cdf0e10cSrcweir if (p != 0)
262cdf0e10cSrcweir {
263cdf0e10cSrcweir void * p_old = p;
264cdf0e10cSrcweir sal_Size n_old = ((sal_Size*)( (char*)(p) - RTL_MEMALIGN ))[0] - RTL_MEMALIGN;
265cdf0e10cSrcweir
266cdf0e10cSrcweir p = rtl_allocateMemory (n);
267cdf0e10cSrcweir if (p != 0)
268cdf0e10cSrcweir {
269cdf0e10cSrcweir memcpy (p, p_old, SAL_MIN(n, n_old));
270cdf0e10cSrcweir rtl_freeMemory (p_old);
271cdf0e10cSrcweir }
272cdf0e10cSrcweir }
273cdf0e10cSrcweir else
274cdf0e10cSrcweir {
275cdf0e10cSrcweir p = rtl_allocateMemory (n);
276cdf0e10cSrcweir }
277cdf0e10cSrcweir }
278cdf0e10cSrcweir else if (p != 0)
279cdf0e10cSrcweir {
280cdf0e10cSrcweir rtl_freeMemory (p), p = 0;
281cdf0e10cSrcweir }
282cdf0e10cSrcweir return (p);
283cdf0e10cSrcweir }
284cdf0e10cSrcweir
285cdf0e10cSrcweir #else /* FORCE_SYSALLOC */
286cdf0e10cSrcweir
287cdf0e10cSrcweir /* ================================================================= *
288cdf0e10cSrcweir *
289cdf0e10cSrcweir * system allocator includes.
290cdf0e10cSrcweir *
291cdf0e10cSrcweir * ================================================================= */
292cdf0e10cSrcweir
293cdf0e10cSrcweir #ifndef INCLUDED_STDLIB_H
294cdf0e10cSrcweir #include <stdlib.h>
295cdf0e10cSrcweir #define INCLUDED_STDLIB_H
296cdf0e10cSrcweir #endif
297cdf0e10cSrcweir
298cdf0e10cSrcweir /* ================================================================= *
299cdf0e10cSrcweir *
300*4772a777SJohn Bampton * system allocator implementation.
301cdf0e10cSrcweir *
302cdf0e10cSrcweir * ================================================================= */
303cdf0e10cSrcweir
rtl_allocateMemory(sal_Size n)304cdf0e10cSrcweir void * SAL_CALL rtl_allocateMemory (sal_Size n)
305cdf0e10cSrcweir {
306cdf0e10cSrcweir return malloc (n);
307cdf0e10cSrcweir }
308cdf0e10cSrcweir
309cdf0e10cSrcweir /* ================================================================= */
310cdf0e10cSrcweir
rtl_freeMemory(void * p)311cdf0e10cSrcweir void SAL_CALL rtl_freeMemory (void * p)
312cdf0e10cSrcweir {
313cdf0e10cSrcweir free (p);
314cdf0e10cSrcweir }
315cdf0e10cSrcweir
316cdf0e10cSrcweir /* ================================================================= */
317cdf0e10cSrcweir
rtl_reallocateMemory(void * p,sal_Size n)318cdf0e10cSrcweir void * SAL_CALL rtl_reallocateMemory (void * p, sal_Size n)
319cdf0e10cSrcweir {
320cdf0e10cSrcweir return realloc (p, n);
321cdf0e10cSrcweir }
322cdf0e10cSrcweir
323cdf0e10cSrcweir /* ================================================================= */
324cdf0e10cSrcweir
325cdf0e10cSrcweir void
rtl_memory_fini(void)326cdf0e10cSrcweir rtl_memory_fini (void)
327cdf0e10cSrcweir {
328cdf0e10cSrcweir /* nothing to do */
329cdf0e10cSrcweir }
330cdf0e10cSrcweir
331cdf0e10cSrcweir #endif /* FORCE_SYSALLOC */
332cdf0e10cSrcweir
333cdf0e10cSrcweir /* ================================================================= *
334cdf0e10cSrcweir *
335*4772a777SJohn Bampton * rtl_(allocate|free)ZeroMemory() implementation.
336cdf0e10cSrcweir *
337cdf0e10cSrcweir * ================================================================= */
338cdf0e10cSrcweir
rtl_allocateZeroMemory(sal_Size n)339cdf0e10cSrcweir void * SAL_CALL rtl_allocateZeroMemory (sal_Size n) SAL_THROW_EXTERN_C()
340cdf0e10cSrcweir {
341cdf0e10cSrcweir void * p = rtl_allocateMemory (n);
342cdf0e10cSrcweir if (p != 0)
343cdf0e10cSrcweir memset (p, 0, n);
344cdf0e10cSrcweir return (p);
345cdf0e10cSrcweir }
346cdf0e10cSrcweir
347cdf0e10cSrcweir /* ================================================================= */
348cdf0e10cSrcweir
rtl_freeZeroMemory(void * p,sal_Size n)349cdf0e10cSrcweir void SAL_CALL rtl_freeZeroMemory (void * p, sal_Size n) SAL_THROW_EXTERN_C()
350cdf0e10cSrcweir {
351cdf0e10cSrcweir if (p != 0)
352cdf0e10cSrcweir {
353cdf0e10cSrcweir memset (p, 0, n);
354cdf0e10cSrcweir rtl_freeMemory (p);
355cdf0e10cSrcweir }
356cdf0e10cSrcweir }
357cdf0e10cSrcweir
358cdf0e10cSrcweir /* ================================================================= */
359