1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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) */
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,
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 * ================================================================= */
95 SAL_CALL
rtl_allocateMemory_CUSTOM (sal_Size n
) SAL_THROW_EXTERN_C()
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 */
111 if (size
<= RTL_MEMORY_CACHED_LIMIT
)
112 addr
= (char*)rtl_cache_alloc(g_alloc_table
[(size
- 1) >> RTL_MEMALIGN_SHIFT
]);
114 addr
= (char*)rtl_arena_alloc (gp_alloc_arena
, &size
);
118 ((sal_Size
*)(addr
))[0] = size
;
119 p
= addr
+ RTL_MEMALIGN
;
121 else if (gp_alloc_arena
== 0)
123 ensureMemorySingleton();
134 /* ================================================================= */
136 void SAL_CALL
rtl_freeMemory_CUSTOM (void * p
) SAL_THROW_EXTERN_C()
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
);
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()
159 sal_Size n_old
= ((sal_Size
*)( (char*)(p
) - RTL_MEMALIGN
))[0] - RTL_MEMALIGN
;
161 p
= rtl_allocateMemory (n
);
164 memcpy (p
, p_old
, (n
< n_old
) ? n
: n_old
);
165 rtl_freeMemory (p_old
);
170 p
= rtl_allocateMemory (n
);
175 rtl_freeMemory (p
), p
= 0;
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 (
198 0, /* w/o quantum caching */
199 0, /* default source */
204 assert(gp_alloc_arena
!= 0);
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);
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
;
228 // SAL_INFO("sal.rtl", "rtl_memory_init completed");
231 /* ================================================================= */
233 void rtl_memory_fini()
235 #if !defined(FORCE_SYSALLOC)
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
);
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
)
272 /* ================================================================= */
274 void SAL_CALL
rtl_freeMemory_SYSTEM (void * 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()
291 n
>= SAL_MAX_INT32
, "sal.rtl",
292 "suspicious massive alloc " << n
);
293 #if !defined(FORCE_SYSALLOC)
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();
307 return rtl_allocateMemory_SYSTEM(n
);
311 void* SAL_CALL
rtl_reallocateMemory (void * p
, sal_Size n
) SAL_THROW_EXTERN_C()
314 n
>= SAL_MAX_INT32
, "sal.rtl",
315 "suspicious massive alloc " << n
);
316 #if !defined(FORCE_SYSALLOC)
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();
330 return rtl_reallocateMemory_SYSTEM(p
,n
);
334 void SAL_CALL
rtl_freeMemory (void * p
) SAL_THROW_EXTERN_C()
336 #if !defined(FORCE_SYSALLOC)
339 if (alloc_mode
== AMode_CUSTOM
)
341 rtl_freeMemory_CUSTOM(p
);
344 if (alloc_mode
== AMode_SYSTEM
)
346 rtl_freeMemory_SYSTEM(p
);
349 determine_alloc_mode();
352 rtl_freeMemory_SYSTEM(p
);
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
);
370 /* ================================================================= */
372 void SAL_CALL
rtl_freeZeroMemory (void * p
, sal_Size n
) SAL_THROW_EXTERN_C()
381 /* ================================================================= */
383 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */