Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "alloc_impl.hxx"
21 : #include "rtl/alloc.h"
22 : #include <sal/log.hxx>
23 : #include <sal/macros.h>
24 :
25 : #include <cassert>
26 : #include <string.h>
27 : #include <stdio.h>
28 :
29 : #include "internal/rtllifecycle.h"
30 :
31 : AllocMode alloc_mode = AMode_UNSET;
32 :
33 : #if !defined(FORCE_SYSALLOC)
34 556 : static void determine_alloc_mode()
35 : {
36 : assert(alloc_mode == AMode_UNSET);
37 556 : alloc_mode = (getenv("G_SLICE") == NULL ? AMode_CUSTOM : AMode_SYSTEM);
38 556 : }
39 :
40 : /* ================================================================= *
41 : *
42 : * custom allocator includes.
43 : *
44 : * ================================================================= */
45 :
46 : #include "sal/macros.h"
47 :
48 : /* ================================================================= *
49 : *
50 : * custom allocator internals.
51 : *
52 : * ================================================================= */
53 :
54 : static const sal_Size g_alloc_sizes[] =
55 : {
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
68 : };
69 :
70 : #define RTL_MEMORY_CACHED_LIMIT 4 * 4096
71 : #define RTL_MEMORY_CACHED_SIZES (SAL_N_ELEMENTS(g_alloc_sizes))
72 :
73 : static rtl_cache_type * g_alloc_caches[RTL_MEMORY_CACHED_SIZES] =
74 : {
75 : 0,
76 : };
77 :
78 : #define RTL_MEMALIGN 8
79 : #define RTL_MEMALIGN_SHIFT 3
80 :
81 : static rtl_cache_type * g_alloc_table[RTL_MEMORY_CACHED_LIMIT >> RTL_MEMALIGN_SHIFT] =
82 : {
83 : 0,
84 : };
85 :
86 : static rtl_arena_type * gp_alloc_arena = 0;
87 :
88 : /* ================================================================= *
89 : *
90 : * custom allocator implemenation.
91 : *
92 : * ================================================================= */
93 :
94 : void *
95 44970086 : SAL_CALL rtl_allocateMemory_CUSTOM (sal_Size n) SAL_THROW_EXTERN_C()
96 : {
97 44970086 : void * p = 0;
98 44970086 : if (n > 0)
99 : {
100 : char * addr;
101 44969986 : sal_Size size = RTL_MEMORY_ALIGN(n + RTL_MEMALIGN, RTL_MEMALIGN);
102 :
103 : assert(RTL_MEMALIGN >= sizeof(sal_Size));
104 44969986 : if (n >= SAL_MAX_SIZE - (RTL_MEMALIGN + RTL_MEMALIGN - 1))
105 : {
106 : /* requested size too large for roundup alignment */
107 0 : return 0;
108 : }
109 :
110 : try_alloc:
111 44970358 : if (size <= RTL_MEMORY_CACHED_LIMIT)
112 44953850 : addr = (char*)rtl_cache_alloc(g_alloc_table[(size - 1) >> RTL_MEMALIGN_SHIFT]);
113 : else
114 16508 : addr = (char*)rtl_arena_alloc (gp_alloc_arena, &size);
115 :
116 44970358 : if (addr != 0)
117 : {
118 44969986 : ((sal_Size*)(addr))[0] = size;
119 44969986 : p = addr + RTL_MEMALIGN;
120 : }
121 372 : else if (gp_alloc_arena == 0)
122 : {
123 372 : ensureMemorySingleton();
124 372 : if (gp_alloc_arena)
125 : {
126 : /* try again */
127 372 : goto try_alloc;
128 : }
129 : }
130 : }
131 44970086 : return (p);
132 : }
133 :
134 : /* ================================================================= */
135 :
136 44054760 : void SAL_CALL rtl_freeMemory_CUSTOM (void * p) SAL_THROW_EXTERN_C()
137 : {
138 44054760 : if (p != 0)
139 : {
140 44053858 : char * addr = (char*)(p) - RTL_MEMALIGN;
141 44053858 : sal_Size size = ((sal_Size*)(addr))[0];
142 :
143 44053858 : if (size <= RTL_MEMORY_CACHED_LIMIT)
144 44039262 : rtl_cache_free(g_alloc_table[(size - 1) >> RTL_MEMALIGN_SHIFT], addr);
145 : else
146 14596 : rtl_arena_free (gp_alloc_arena, addr, size);
147 : }
148 44054760 : }
149 :
150 : /* ================================================================= */
151 :
152 330052 : void * SAL_CALL rtl_reallocateMemory_CUSTOM (void * p, sal_Size n) SAL_THROW_EXTERN_C()
153 : {
154 330052 : if (n > 0)
155 : {
156 330052 : if (p != 0)
157 : {
158 330052 : void * p_old = p;
159 330052 : sal_Size n_old = ((sal_Size*)( (char*)(p) - RTL_MEMALIGN ))[0] - RTL_MEMALIGN;
160 :
161 330052 : p = rtl_allocateMemory (n);
162 330052 : if (p != 0)
163 : {
164 330052 : memcpy (p, p_old, (n < n_old) ? n : n_old);
165 330052 : rtl_freeMemory (p_old);
166 : }
167 : }
168 : else
169 : {
170 0 : p = rtl_allocateMemory (n);
171 : }
172 : }
173 0 : else if (p != 0)
174 : {
175 0 : rtl_freeMemory (p), p = 0;
176 : }
177 330052 : return (p);
178 : }
179 :
180 : #endif
181 :
182 : /* ================================================================= *
183 : *
184 : * custom allocator initialization / finalization.
185 : *
186 : * ================================================================= */
187 :
188 372 : void rtl_memory_init()
189 : {
190 : #if !defined(FORCE_SYSALLOC)
191 : {
192 : /* global memory arena */
193 : assert(gp_alloc_arena == 0);
194 :
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 */
203 372 : );
204 : assert(gp_alloc_arena != 0);
205 : }
206 : {
207 : sal_Size size;
208 372 : int i, n = RTL_MEMORY_CACHED_SIZES;
209 :
210 14880 : for (i = 0; i < n; i++)
211 : {
212 : char name[RTL_CACHE_NAME_LENGTH + 1];
213 14508 : (void) snprintf (name, sizeof(name), "rtl_alloc_%lu", g_alloc_sizes[i]);
214 14508 : g_alloc_caches[i] = rtl_cache_create (name, g_alloc_sizes[i], 0, NULL, NULL, NULL, NULL, NULL, 0);
215 : }
216 :
217 372 : size = RTL_MEMALIGN;
218 14880 : for (i = 0; i < n; i++)
219 : {
220 790872 : while (size <= g_alloc_sizes[i])
221 : {
222 761856 : g_alloc_table[(size - 1) >> RTL_MEMALIGN_SHIFT] = g_alloc_caches[i];
223 761856 : size += RTL_MEMALIGN;
224 : }
225 : }
226 : }
227 : #endif
228 : // SAL_INFO("sal.rtl", "rtl_memory_init completed");
229 372 : }
230 :
231 : /* ================================================================= */
232 :
233 372 : void rtl_memory_fini()
234 : {
235 : #if !defined(FORCE_SYSALLOC)
236 : int i, n;
237 :
238 : /* clear g_alloc_table */
239 372 : memset (g_alloc_table, 0, sizeof(g_alloc_table));
240 :
241 : /* cleanup g_alloc_caches */
242 14880 : for (i = 0, n = RTL_MEMORY_CACHED_SIZES; i < n; i++)
243 : {
244 14508 : if (g_alloc_caches[i] != 0)
245 : {
246 14508 : rtl_cache_destroy (g_alloc_caches[i]);
247 14508 : g_alloc_caches[i] = 0;
248 : }
249 : }
250 :
251 : /* cleanup gp_alloc_arena */
252 372 : if (gp_alloc_arena != 0)
253 : {
254 372 : rtl_arena_destroy (gp_alloc_arena);
255 372 : gp_alloc_arena = 0;
256 : }
257 : #endif
258 : // SAL_INFO("sal.rtl", "rtl_memory_fini completed");
259 372 : }
260 :
261 : /* ================================================================= *
262 : *
263 : * system allocator implemenation.
264 : *
265 : * ================================================================= */
266 :
267 3066143 : void * SAL_CALL rtl_allocateMemory_SYSTEM (sal_Size n)
268 : {
269 3066143 : return malloc (n);
270 : }
271 :
272 : /* ================================================================= */
273 :
274 3033338 : void SAL_CALL rtl_freeMemory_SYSTEM (void * p)
275 : {
276 3033338 : free (p);
277 3033338 : }
278 :
279 : /* ================================================================= */
280 :
281 20280 : void * SAL_CALL rtl_reallocateMemory_SYSTEM (void * p, sal_Size n)
282 : {
283 20280 : return realloc (p, n);
284 : }
285 :
286 : /* ================================================================= */
287 :
288 48036226 : void* SAL_CALL rtl_allocateMemory (sal_Size n) SAL_THROW_EXTERN_C()
289 : {
290 : SAL_WARN_IF(
291 : n >= SAL_MAX_INT32, "sal.rtl",
292 : "suspicious massive alloc " << n);
293 : #if !defined(FORCE_SYSALLOC)
294 552 : while (1)
295 : {
296 48036778 : if (alloc_mode == AMode_CUSTOM)
297 : {
298 44970086 : return rtl_allocateMemory_CUSTOM(n);
299 : }
300 3066692 : if (alloc_mode == AMode_SYSTEM)
301 : {
302 3066140 : return rtl_allocateMemory_SYSTEM(n);
303 : }
304 552 : determine_alloc_mode();
305 : }
306 : #else
307 : return rtl_allocateMemory_SYSTEM(n);
308 : #endif
309 : }
310 :
311 350332 : void* SAL_CALL rtl_reallocateMemory (void * p, sal_Size n) SAL_THROW_EXTERN_C()
312 : {
313 : SAL_WARN_IF(
314 : n >= SAL_MAX_INT32, "sal.rtl",
315 : "suspicious massive alloc " << n);
316 : #if !defined(FORCE_SYSALLOC)
317 0 : while (1)
318 : {
319 350332 : if (alloc_mode == AMode_CUSTOM)
320 : {
321 330052 : return rtl_reallocateMemory_CUSTOM(p,n);
322 : }
323 20280 : if (alloc_mode == AMode_SYSTEM)
324 : {
325 20280 : return rtl_reallocateMemory_SYSTEM(p,n);
326 : }
327 0 : determine_alloc_mode();
328 : }
329 : #else
330 : return rtl_reallocateMemory_SYSTEM(p,n);
331 : #endif
332 : }
333 :
334 47088101 : void SAL_CALL rtl_freeMemory (void * p) SAL_THROW_EXTERN_C()
335 : {
336 : #if !defined(FORCE_SYSALLOC)
337 4 : while (1)
338 : {
339 47088101 : if (alloc_mode == AMode_CUSTOM)
340 : {
341 44054760 : rtl_freeMemory_CUSTOM(p);
342 44054760 : return;
343 : }
344 3033341 : if (alloc_mode == AMode_SYSTEM)
345 : {
346 3033337 : rtl_freeMemory_SYSTEM(p);
347 3033339 : return;
348 : }
349 4 : determine_alloc_mode();
350 : }
351 : #else
352 : rtl_freeMemory_SYSTEM(p);
353 : #endif
354 : }
355 :
356 : /* ================================================================= *
357 : *
358 : * rtl_(allocate|free)ZeroMemory() implemenation.
359 : *
360 : * ================================================================= */
361 :
362 1710 : void * SAL_CALL rtl_allocateZeroMemory (sal_Size n) SAL_THROW_EXTERN_C()
363 : {
364 1710 : void * p = rtl_allocateMemory (n);
365 1710 : if (p != 0)
366 1710 : memset (p, 0, n);
367 1710 : return (p);
368 : }
369 :
370 : /* ================================================================= */
371 :
372 614 : void SAL_CALL rtl_freeZeroMemory (void * p, sal_Size n) SAL_THROW_EXTERN_C()
373 : {
374 614 : if (p != 0)
375 : {
376 614 : memset (p, 0, n);
377 614 : rtl_freeMemory (p);
378 : }
379 614 : }
380 :
381 : /* ================================================================= */
382 :
383 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|