bump product version to 4.1.6.2
[LibreOffice.git] / sal / rtl / alloc_global.cxx
blobab571c6636315412edc2bc79dd39ca4f8a7ae4fa
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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_impl.hxx"
21 #include "rtl/alloc.h"
22 #include <sal/log.hxx>
23 #include <sal/macros.h>
25 #include <cassert>
26 #include <string.h>
27 #include <stdio.h>
29 #include "internal/rtllifecycle.h"
31 AllocMode alloc_mode = AMode_UNSET;
33 #if !defined(FORCE_SYSALLOC)
34 static void determine_alloc_mode()
36 assert(alloc_mode == AMode_UNSET);
37 alloc_mode = (getenv("G_SLICE") == NULL ? AMode_CUSTOM : AMode_SYSTEM);
40 /* ================================================================= *
42 * custom allocator includes.
44 * ================================================================= */
46 #include "sal/macros.h"
48 /* ================================================================= *
50 * custom allocator internals.
52 * ================================================================= */
54 static const sal_Size g_alloc_sizes[] =
56 /* powers of 2**(1/4) */
57 4 * 4, 6 * 4,
58 4 * 8, 5 * 8, 6 * 8, 7 * 8,
59 4 * 16, 5 * 16, 6 * 16, 7 * 16,
60 4 * 32, 5 * 32, 6 * 32, 7 * 32,
61 4 * 64, 5 * 64, 6 * 64, 7 * 64,
62 4 * 128, 5 * 128, 6 * 128, 7 * 128,
63 4 * 256, 5 * 256, 6 * 256, 7 * 256,
64 4 * 512, 5 * 512, 6 * 512, 7 * 512,
65 4 * 1024, 5 * 1024, 6 * 1024, 7 * 1024,
66 4 * 2048, 5 * 2048, 6 * 2048, 7 * 2048,
67 4 * 4096
70 #define RTL_MEMORY_CACHED_LIMIT 4 * 4096
71 #define RTL_MEMORY_CACHED_SIZES (SAL_N_ELEMENTS(g_alloc_sizes))
73 static rtl_cache_type * g_alloc_caches[RTL_MEMORY_CACHED_SIZES] =
78 #define RTL_MEMALIGN 8
79 #define RTL_MEMALIGN_SHIFT 3
81 static rtl_cache_type * g_alloc_table[RTL_MEMORY_CACHED_LIMIT >> RTL_MEMALIGN_SHIFT] =
86 static rtl_arena_type * gp_alloc_arena = 0;
88 /* ================================================================= *
90 * custom allocator implemenation.
92 * ================================================================= */
94 void *
95 SAL_CALL rtl_allocateMemory_CUSTOM (sal_Size n) SAL_THROW_EXTERN_C()
97 void * p = 0;
98 if (n > 0)
100 char * addr;
101 sal_Size size = RTL_MEMORY_ALIGN(n + RTL_MEMALIGN, RTL_MEMALIGN);
103 assert(RTL_MEMALIGN >= sizeof(sal_Size));
104 if (n >= SAL_MAX_SIZE - (RTL_MEMALIGN + RTL_MEMALIGN - 1))
106 /* requested size too large for roundup alignment */
107 return 0;
110 try_alloc:
111 if (size <= RTL_MEMORY_CACHED_LIMIT)
112 addr = (char*)rtl_cache_alloc(g_alloc_table[(size - 1) >> RTL_MEMALIGN_SHIFT]);
113 else
114 addr = (char*)rtl_arena_alloc (gp_alloc_arena, &size);
116 if (addr != 0)
118 ((sal_Size*)(addr))[0] = size;
119 p = addr + RTL_MEMALIGN;
121 else if (gp_alloc_arena == 0)
123 ensureMemorySingleton();
124 if (gp_alloc_arena)
126 /* try again */
127 goto try_alloc;
131 return (p);
134 /* ================================================================= */
136 void SAL_CALL rtl_freeMemory_CUSTOM (void * p) SAL_THROW_EXTERN_C()
138 if (p != 0)
140 char * addr = (char*)(p) - RTL_MEMALIGN;
141 sal_Size size = ((sal_Size*)(addr))[0];
143 if (size <= RTL_MEMORY_CACHED_LIMIT)
144 rtl_cache_free(g_alloc_table[(size - 1) >> RTL_MEMALIGN_SHIFT], addr);
145 else
146 rtl_arena_free (gp_alloc_arena, addr, size);
150 /* ================================================================= */
152 void * SAL_CALL rtl_reallocateMemory_CUSTOM (void * p, sal_Size n) SAL_THROW_EXTERN_C()
154 if (n > 0)
156 if (p != 0)
158 void * p_old = p;
159 sal_Size n_old = ((sal_Size*)( (char*)(p) - RTL_MEMALIGN ))[0] - RTL_MEMALIGN;
161 p = rtl_allocateMemory (n);
162 if (p != 0)
164 memcpy (p, p_old, (n < n_old) ? n : n_old);
165 rtl_freeMemory (p_old);
168 else
170 p = rtl_allocateMemory (n);
173 else if (p != 0)
175 rtl_freeMemory (p), p = 0;
177 return (p);
180 #endif
182 /* ================================================================= *
184 * custom allocator initialization / finalization.
186 * ================================================================= */
188 void rtl_memory_init()
190 #if !defined(FORCE_SYSALLOC)
192 /* global memory arena */
193 assert(gp_alloc_arena == 0);
195 gp_alloc_arena = rtl_arena_create (
196 "rtl_alloc_arena",
197 2048, /* quantum */
198 0, /* w/o quantum caching */
199 0, /* default source */
200 rtl_arena_alloc,
201 rtl_arena_free,
202 0 /* flags */
204 assert(gp_alloc_arena != 0);
207 sal_Size size;
208 int i, n = RTL_MEMORY_CACHED_SIZES;
210 for (i = 0; i < n; i++)
212 char name[RTL_CACHE_NAME_LENGTH + 1];
213 (void) snprintf (name, sizeof(name), "rtl_alloc_%lu", g_alloc_sizes[i]);
214 g_alloc_caches[i] = rtl_cache_create (name, g_alloc_sizes[i], 0, NULL, NULL, NULL, NULL, NULL, 0);
217 size = RTL_MEMALIGN;
218 for (i = 0; i < n; i++)
220 while (size <= g_alloc_sizes[i])
222 g_alloc_table[(size - 1) >> RTL_MEMALIGN_SHIFT] = g_alloc_caches[i];
223 size += RTL_MEMALIGN;
227 #endif
228 // SAL_INFO("sal.rtl", "rtl_memory_init completed");
231 /* ================================================================= */
233 void rtl_memory_fini()
235 #if !defined(FORCE_SYSALLOC)
236 int i, n;
238 /* clear g_alloc_table */
239 memset (g_alloc_table, 0, sizeof(g_alloc_table));
241 /* cleanup g_alloc_caches */
242 for (i = 0, n = RTL_MEMORY_CACHED_SIZES; i < n; i++)
244 if (g_alloc_caches[i] != 0)
246 rtl_cache_destroy (g_alloc_caches[i]);
247 g_alloc_caches[i] = 0;
251 /* cleanup gp_alloc_arena */
252 if (gp_alloc_arena != 0)
254 rtl_arena_destroy (gp_alloc_arena);
255 gp_alloc_arena = 0;
257 #endif
258 // SAL_INFO("sal.rtl", "rtl_memory_fini completed");
261 /* ================================================================= *
263 * system allocator implemenation.
265 * ================================================================= */
267 void * SAL_CALL rtl_allocateMemory_SYSTEM (sal_Size n)
269 return malloc (n);
272 /* ================================================================= */
274 void SAL_CALL rtl_freeMemory_SYSTEM (void * p)
276 free (p);
279 /* ================================================================= */
281 void * SAL_CALL rtl_reallocateMemory_SYSTEM (void * p, sal_Size n)
283 return realloc (p, n);
286 /* ================================================================= */
288 void* SAL_CALL rtl_allocateMemory (sal_Size n) SAL_THROW_EXTERN_C()
290 SAL_WARN_IF(
291 n >= SAL_MAX_INT32, "sal.rtl",
292 "suspicious massive alloc " << n);
293 #if !defined(FORCE_SYSALLOC)
294 while (1)
296 if (alloc_mode == AMode_CUSTOM)
298 return rtl_allocateMemory_CUSTOM(n);
300 if (alloc_mode == AMode_SYSTEM)
302 return rtl_allocateMemory_SYSTEM(n);
304 determine_alloc_mode();
306 #else
307 return rtl_allocateMemory_SYSTEM(n);
308 #endif
311 void* SAL_CALL rtl_reallocateMemory (void * p, sal_Size n) SAL_THROW_EXTERN_C()
313 SAL_WARN_IF(
314 n >= SAL_MAX_INT32, "sal.rtl",
315 "suspicious massive alloc " << n);
316 #if !defined(FORCE_SYSALLOC)
317 while (1)
319 if (alloc_mode == AMode_CUSTOM)
321 return rtl_reallocateMemory_CUSTOM(p,n);
323 if (alloc_mode == AMode_SYSTEM)
325 return rtl_reallocateMemory_SYSTEM(p,n);
327 determine_alloc_mode();
329 #else
330 return rtl_reallocateMemory_SYSTEM(p,n);
331 #endif
334 void SAL_CALL rtl_freeMemory (void * p) SAL_THROW_EXTERN_C()
336 #if !defined(FORCE_SYSALLOC)
337 while (1)
339 if (alloc_mode == AMode_CUSTOM)
341 rtl_freeMemory_CUSTOM(p);
342 return;
344 if (alloc_mode == AMode_SYSTEM)
346 rtl_freeMemory_SYSTEM(p);
347 return;
349 determine_alloc_mode();
351 #else
352 rtl_freeMemory_SYSTEM(p);
353 #endif
356 /* ================================================================= *
358 * rtl_(allocate|free)ZeroMemory() implemenation.
360 * ================================================================= */
362 void * SAL_CALL rtl_allocateZeroMemory (sal_Size n) SAL_THROW_EXTERN_C()
364 void * p = rtl_allocateMemory (n);
365 if (p != 0)
366 memset (p, 0, n);
367 return (p);
370 /* ================================================================= */
372 void SAL_CALL rtl_freeZeroMemory (void * p, sal_Size n) SAL_THROW_EXTERN_C()
374 if (p != 0)
376 memset (p, 0, n);
377 rtl_freeMemory (p);
381 /* ================================================================= */
383 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */