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 1277 : static void determine_alloc_mode()
35 : {
36 : assert(alloc_mode == AMode_UNSET);
37 1277 : alloc_mode = (getenv("G_SLICE") == NULL ? AMode_CUSTOM : AMode_SYSTEM);
38 1277 : }
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 60792470 : SAL_CALL rtl_allocateMemory_CUSTOM (sal_Size n) SAL_THROW_EXTERN_C()
96 : {
97 60792470 : void * p = 0;
98 60792470 : if (n > 0)
99 : {
100 : char * addr;
101 60769892 : sal_Size size = RTL_MEMORY_ALIGN(n + RTL_MEMALIGN, RTL_MEMALIGN);
102 :
103 : assert(RTL_MEMALIGN >= sizeof(sal_Size));
104 60769892 : 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 60771167 : if (size <= RTL_MEMORY_CACHED_LIMIT)
112 60677315 : addr = (char*)rtl_cache_alloc(g_alloc_table[(size - 1) >> RTL_MEMALIGN_SHIFT]);
113 : else
114 93852 : addr = (char*)rtl_arena_alloc (gp_alloc_arena, &size);
115 :
116 60771167 : if (addr != 0)
117 : {
118 60769892 : ((sal_Size*)(addr))[0] = size;
119 60769892 : p = addr + RTL_MEMALIGN;
120 : }
121 1275 : else if (gp_alloc_arena == 0)
122 : {
123 1275 : ensureMemorySingleton();
124 1275 : if (gp_alloc_arena)
125 : {
126 : /* try again */
127 1275 : goto try_alloc;
128 : }
129 : }
130 : }
131 60792470 : return (p);
132 : }
133 :
134 : /* ================================================================= */
135 :
136 58742346 : void SAL_CALL rtl_freeMemory_CUSTOM (void * p) SAL_THROW_EXTERN_C()
137 : {
138 58742346 : if (p != 0)
139 : {
140 58719891 : char * addr = (char*)(p) - RTL_MEMALIGN;
141 58719891 : sal_Size size = ((sal_Size*)(addr))[0];
142 :
143 58719891 : if (size <= RTL_MEMORY_CACHED_LIMIT)
144 58626652 : rtl_cache_free(g_alloc_table[(size - 1) >> RTL_MEMALIGN_SHIFT], addr);
145 : else
146 93239 : rtl_arena_free (gp_alloc_arena, addr, size);
147 : }
148 58742346 : }
149 :
150 : /* ================================================================= */
151 :
152 2625259 : void * SAL_CALL rtl_reallocateMemory_CUSTOM (void * p, sal_Size n) SAL_THROW_EXTERN_C()
153 : {
154 2625259 : if (n > 0)
155 : {
156 2625259 : if (p != 0)
157 : {
158 2625259 : void * p_old = p;
159 2625259 : sal_Size n_old = ((sal_Size*)( (char*)(p) - RTL_MEMALIGN ))[0] - RTL_MEMALIGN;
160 :
161 2625259 : p = rtl_allocateMemory (n);
162 2625259 : if (p != 0)
163 : {
164 2625259 : memcpy (p, p_old, (n < n_old) ? n : n_old);
165 2625259 : 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 2625259 : return (p);
178 : }
179 :
180 : #endif
181 :
182 : /* ================================================================= *
183 : *
184 : * custom allocator initialization / finalization.
185 : *
186 : * ================================================================= */
187 :
188 1275 : 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 1275 : );
204 : assert(gp_alloc_arena != 0);
205 : }
206 : {
207 : sal_Size size;
208 1275 : int i, n = RTL_MEMORY_CACHED_SIZES;
209 :
210 51000 : for (i = 0; i < n; i++)
211 : {
212 : char name[RTL_CACHE_NAME_LENGTH + 1];
213 49725 : (void) snprintf (name, sizeof(name), "rtl_alloc_%lu", g_alloc_sizes[i]);
214 49725 : g_alloc_caches[i] = rtl_cache_create (name, g_alloc_sizes[i], 0, NULL, NULL, NULL, NULL, NULL, 0);
215 : }
216 :
217 1275 : size = RTL_MEMALIGN;
218 51000 : for (i = 0; i < n; i++)
219 : {
220 2710650 : while (size <= g_alloc_sizes[i])
221 : {
222 2611200 : g_alloc_table[(size - 1) >> RTL_MEMALIGN_SHIFT] = g_alloc_caches[i];
223 2611200 : size += RTL_MEMALIGN;
224 : }
225 : }
226 : }
227 : #endif
228 : // SAL_INFO("sal.rtl", "rtl_memory_init completed");
229 1275 : }
230 :
231 : /* ================================================================= */
232 :
233 1275 : void rtl_memory_fini()
234 : {
235 : #if !defined(FORCE_SYSALLOC)
236 : int i, n;
237 :
238 : /* clear g_alloc_table */
239 1275 : memset (g_alloc_table, 0, sizeof(g_alloc_table));
240 :
241 : /* cleanup g_alloc_caches */
242 51000 : for (i = 0, n = RTL_MEMORY_CACHED_SIZES; i < n; i++)
243 : {
244 49725 : if (g_alloc_caches[i] != 0)
245 : {
246 49725 : rtl_cache_destroy (g_alloc_caches[i]);
247 49725 : g_alloc_caches[i] = 0;
248 : }
249 : }
250 :
251 : /* cleanup gp_alloc_arena */
252 1275 : if (gp_alloc_arena != 0)
253 : {
254 1275 : rtl_arena_destroy (gp_alloc_arena);
255 1275 : gp_alloc_arena = 0;
256 : }
257 : #endif
258 : // SAL_INFO("sal.rtl", "rtl_memory_fini completed");
259 1275 : }
260 :
261 : /* ================================================================= *
262 : *
263 : * system allocator implemenation.
264 : *
265 : * ================================================================= */
266 :
267 0 : void * SAL_CALL rtl_allocateMemory_SYSTEM (sal_Size n)
268 : {
269 0 : return malloc (n);
270 : }
271 :
272 : /* ================================================================= */
273 :
274 0 : void SAL_CALL rtl_freeMemory_SYSTEM (void * p)
275 : {
276 0 : free (p);
277 0 : }
278 :
279 : /* ================================================================= */
280 :
281 0 : void * SAL_CALL rtl_reallocateMemory_SYSTEM (void * p, sal_Size n)
282 : {
283 0 : return realloc (p, n);
284 : }
285 :
286 : /* ================================================================= */
287 :
288 60792470 : 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 1275 : while (1)
295 : {
296 60793745 : if (alloc_mode == AMode_CUSTOM)
297 : {
298 60792470 : return rtl_allocateMemory_CUSTOM(n);
299 : }
300 1275 : if (alloc_mode == AMode_SYSTEM)
301 : {
302 0 : return rtl_allocateMemory_SYSTEM(n);
303 : }
304 1275 : determine_alloc_mode();
305 : }
306 : #else
307 : return rtl_allocateMemory_SYSTEM(n);
308 : #endif
309 : }
310 :
311 2625259 : 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 2625259 : if (alloc_mode == AMode_CUSTOM)
320 : {
321 2625259 : return rtl_reallocateMemory_CUSTOM(p,n);
322 : }
323 0 : if (alloc_mode == AMode_SYSTEM)
324 : {
325 0 : 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 58742348 : void SAL_CALL rtl_freeMemory (void * p) SAL_THROW_EXTERN_C()
335 : {
336 : #if !defined(FORCE_SYSALLOC)
337 2 : while (1)
338 : {
339 58742348 : if (alloc_mode == AMode_CUSTOM)
340 : {
341 58742346 : rtl_freeMemory_CUSTOM(p);
342 58742346 : return;
343 : }
344 2 : if (alloc_mode == AMode_SYSTEM)
345 : {
346 0 : rtl_freeMemory_SYSTEM(p);
347 0 : return;
348 : }
349 2 : 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 35459 : void * SAL_CALL rtl_allocateZeroMemory (sal_Size n) SAL_THROW_EXTERN_C()
363 : {
364 35459 : void * p = rtl_allocateMemory (n);
365 35459 : if (p != 0)
366 12882 : memset (p, 0, n);
367 35459 : return (p);
368 : }
369 :
370 : /* ================================================================= */
371 :
372 243 : void SAL_CALL rtl_freeZeroMemory (void * p, sal_Size n) SAL_THROW_EXTERN_C()
373 : {
374 243 : if (p != 0)
375 : {
376 243 : memset (p, 0, n);
377 243 : rtl_freeMemory (p);
378 : }
379 243 : }
380 :
381 : /* ================================================================= */
382 :
383 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|