merge the formfield patch from ooo-build
[ooovba.git] / sal / rtl / source / alloc_global.c
blob5645bd0568cf2dd9a2857bfa3124bccd6715a4ac
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: alloc_global.c,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "rtl/alloc.h"
33 #ifndef INCLUDED_STRING_H
34 #include <string.h>
35 #define INCLUDED_STRING_H
36 #endif
38 #if !defined(FORCE_SYSALLOC)
40 /* ================================================================= *
42 * custom allocator includes.
44 * ================================================================= */
46 #ifndef INCLUDED_STDIO_H
47 #include <stdio.h>
48 #define INCLUDED_STDIO_H
49 #endif
50 #include "alloc_impl.h"
51 #include "internal/once.h"
52 #include "sal/macros.h"
53 #include "osl/diagnose.h"
55 /* ================================================================= *
57 * custom allocator internals.
59 * ================================================================= */
61 static const sal_Size g_alloc_sizes[] =
63 /* powers of 2**(1/4) */
64 4 * 4, 6 * 4,
65 4 * 8, 5 * 8, 6 * 8, 7 * 8,
66 4 * 16, 5 * 16, 6 * 16, 7 * 16,
67 4 * 32, 5 * 32, 6 * 32, 7 * 32,
68 4 * 64, 5 * 64, 6 * 64, 7 * 64,
69 4 * 128, 5 * 128, 6 * 128, 7 * 128,
70 4 * 256, 5 * 256, 6 * 256, 7 * 256,
71 4 * 512, 5 * 512, 6 * 512, 7 * 512,
72 4 * 1024, 5 * 1024, 6 * 1024, 7 * 1024,
73 4 * 2048, 5 * 2048, 6 * 2048, 7 * 2048,
74 4 * 4096
77 #define RTL_MEMORY_CACHED_LIMIT 4 * 4096
78 #define RTL_MEMORY_CACHED_SIZES (sizeof(g_alloc_sizes) / sizeof(g_alloc_sizes[0]))
80 static rtl_cache_type * g_alloc_caches[RTL_MEMORY_CACHED_SIZES] =
85 #define RTL_MEMALIGN 8
86 #define RTL_MEMALIGN_SHIFT 3
88 static rtl_cache_type * g_alloc_table[RTL_MEMORY_CACHED_LIMIT >> RTL_MEMALIGN_SHIFT] =
93 static rtl_arena_type * gp_alloc_arena = 0;
95 /* ================================================================= *
97 * custom allocator initialization / finalization.
99 * ================================================================= */
101 static void
102 rtl_memory_once_init (void)
105 /* global memory arena */
106 OSL_ASSERT(gp_alloc_arena == 0);
108 gp_alloc_arena = rtl_arena_create (
109 "rtl_alloc_arena",
110 2048, /* quantum */
111 0, /* w/o quantum caching */
112 0, /* default source */
113 rtl_arena_alloc,
114 rtl_arena_free,
115 0 /* flags */
117 OSL_ASSERT(gp_alloc_arena != 0);
120 sal_Size size;
121 int i, n = RTL_MEMORY_CACHED_SIZES;
123 for (i = 0; i < n; i++)
125 char name[RTL_CACHE_NAME_LENGTH + 1];
126 (void) snprintf (name, sizeof(name), "rtl_alloc_%lu", g_alloc_sizes[i]);
127 g_alloc_caches[i] = rtl_cache_create (name, g_alloc_sizes[i], 0, NULL, NULL, NULL, NULL, NULL, 0);
130 size = RTL_MEMALIGN;
131 for (i = 0; i < n; i++)
133 while (size <= g_alloc_sizes[i])
135 g_alloc_table[(size - 1) >> RTL_MEMALIGN_SHIFT] = g_alloc_caches[i];
136 size += RTL_MEMALIGN;
142 static int
143 rtl_memory_init (void)
145 static sal_once_type g_once = SAL_ONCE_INIT;
146 SAL_ONCE(&g_once, rtl_memory_once_init);
147 return (gp_alloc_arena != 0);
150 /* ================================================================= */
153 Issue http://udk.openoffice.org/issues/show_bug.cgi?id=92388
155 Mac OS X does not seem to support "__cxa__atexit", thus leading
156 to the situation that "__attribute__((destructor))__" functions
157 (in particular "rtl_memory_fini") become called _before_ global
158 C++ object d'tors.
160 Delegated the call to "rtl_memory_fini" into a dummy C++ object,
161 see memory_fini.cxx .
163 #if defined(__GNUC__) && !defined(MACOSX)
164 static void rtl_memory_fini (void) __attribute__((destructor));
165 #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
166 #pragma fini(rtl_memory_fini)
167 static void rtl_memory_fini (void);
168 #endif /* __GNUC__ || __SUNPRO_C */
170 void
171 rtl_memory_fini (void)
173 int i, n;
175 /* clear g_alloc_table */
176 memset (g_alloc_table, 0, sizeof(g_alloc_table));
178 /* cleanup g_alloc_caches */
179 for (i = 0, n = RTL_MEMORY_CACHED_SIZES; i < n; i++)
181 if (g_alloc_caches[i] != 0)
183 rtl_cache_destroy (g_alloc_caches[i]);
184 g_alloc_caches[i] = 0;
188 /* cleanup gp_alloc_arena */
189 if (gp_alloc_arena != 0)
191 rtl_arena_destroy (gp_alloc_arena);
192 gp_alloc_arena = 0;
196 /* ================================================================= *
198 * custom allocator implemenation.
200 * ================================================================= */
202 void *
203 SAL_CALL rtl_allocateMemory (sal_Size n) SAL_THROW_EXTERN_C()
205 void * p = 0;
206 if (n > 0)
208 char * addr;
209 sal_Size size = RTL_MEMORY_ALIGN(n + RTL_MEMALIGN, RTL_MEMALIGN);
211 OSL_ASSERT(RTL_MEMALIGN >= sizeof(sal_Size));
212 if (n >= SAL_MAX_SIZE - (RTL_MEMALIGN + RTL_MEMALIGN - 1))
214 /* requested size too large for roundup alignment */
215 return 0;
218 try_alloc:
219 if (size <= RTL_MEMORY_CACHED_LIMIT)
220 addr = (char*)rtl_cache_alloc(g_alloc_table[(size - 1) >> RTL_MEMALIGN_SHIFT]);
221 else
222 addr = (char*)rtl_arena_alloc (gp_alloc_arena, &size);
224 if (addr != 0)
226 ((sal_Size*)(addr))[0] = size;
227 p = addr + RTL_MEMALIGN;
229 else if (gp_alloc_arena == 0)
231 if (rtl_memory_init())
233 /* try again */
234 goto try_alloc;
238 return (p);
241 /* ================================================================= */
243 void SAL_CALL rtl_freeMemory (void * p) SAL_THROW_EXTERN_C()
245 if (p != 0)
247 char * addr = (char*)(p) - RTL_MEMALIGN;
248 sal_Size size = ((sal_Size*)(addr))[0];
250 if (size <= RTL_MEMORY_CACHED_LIMIT)
251 rtl_cache_free(g_alloc_table[(size - 1) >> RTL_MEMALIGN_SHIFT], addr);
252 else
253 rtl_arena_free (gp_alloc_arena, addr, size);
257 /* ================================================================= */
259 void * SAL_CALL rtl_reallocateMemory (void * p, sal_Size n) SAL_THROW_EXTERN_C()
261 if (n > 0)
263 if (p != 0)
265 void * p_old = p;
266 sal_Size n_old = ((sal_Size*)( (char*)(p) - RTL_MEMALIGN ))[0] - RTL_MEMALIGN;
268 p = rtl_allocateMemory (n);
269 if (p != 0)
271 memcpy (p, p_old, SAL_MIN(n, n_old));
272 rtl_freeMemory (p_old);
275 else
277 p = rtl_allocateMemory (n);
280 else if (p != 0)
282 rtl_freeMemory (p), p = 0;
284 return (p);
287 #else /* FORCE_SYSALLOC */
289 /* ================================================================= *
291 * system allocator includes.
293 * ================================================================= */
295 #ifndef INCLUDED_STDLIB_H
296 #include <stdlib.h>
297 #define INCLUDED_STDLIB_H
298 #endif
300 /* ================================================================= *
302 * system allocator implemenation.
304 * ================================================================= */
306 void * SAL_CALL rtl_allocateMemory (sal_Size n)
308 return malloc (n);
311 /* ================================================================= */
313 void SAL_CALL rtl_freeMemory (void * p)
315 free (p);
318 /* ================================================================= */
320 void * SAL_CALL rtl_reallocateMemory (void * p, sal_Size n)
322 return realloc (p, n);
325 /* ================================================================= */
327 void
328 rtl_memory_fini (void)
330 /* nothing to do */
333 #endif /* FORCE_SYSALLOC */
335 /* ================================================================= *
337 * rtl_(allocate|free)ZeroMemory() implemenation.
339 * ================================================================= */
341 void * SAL_CALL rtl_allocateZeroMemory (sal_Size n) SAL_THROW_EXTERN_C()
343 void * p = rtl_allocateMemory (n);
344 if (p != 0)
345 memset (p, 0, n);
346 return (p);
349 /* ================================================================= */
351 void SAL_CALL rtl_freeZeroMemory (void * p, sal_Size n) SAL_THROW_EXTERN_C()
353 if (p != 0)
355 memset (p, 0, n);
356 rtl_freeMemory (p);
360 /* ================================================================= */