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 .
19 #include <sal/config.h>
23 #include <rtl/alloc.h>
24 #include <rtl/ustring.h>
25 #include <rtllifecycle.h>
28 #include "alloc_impl.hxx"
29 #include "alloc_arena.hxx"
32 * TODO: add a slower, more awful, but more space efficient
33 * custom allocator for the pre-init phase. Existing slab
34 * allocator's minimum alloc size is 24bytes, and by default
37 static rtl_arena_type
*pre_arena
= nullptr;
39 rtl_allocateStringFn rtl_allocateString
= malloc
;
40 rtl_freeStringFn rtl_freeString
= free
;
43 static void *pre_allocateStringFn(size_t n
)
45 sal_Size size
= RTL_MEMORY_ALIGN(n
+ 4, 4);
46 char *addr
= static_cast<char*>(rtl_arena_alloc(pre_arena
, &size
));
48 reinterpret_cast<sal_uInt32
*>(addr
)[0] = size
- 12;
52 static void pre_freeStringFn(void *data
)
54 char *addr
= static_cast<char*>(data
) - 4;
55 sal_uInt32 size
= reinterpret_cast<sal_uInt32
*>(addr
)[0] + 12;
57 rtl_arena_free(pre_arena
, addr
, size
);
61 static void mark_static(void *addr
, sal_Size
/* size */)
63 char *inner
= static_cast<char*>(addr
) + 4;
64 rtl_uString
*str
= reinterpret_cast<rtl_uString
*>(inner
);
65 str
->refCount
|= SAL_STRING_STATIC_FLAG
;
68 void SAL_CALL
rtl_alloc_preInit (sal_Bool start
) SAL_THROW_EXTERN_C()
72 rtl_allocateString
= pre_allocateStringFn
;
73 rtl_freeString
= pre_freeStringFn
;
74 pre_arena
= rtl_arena_create("pre-init strings", 4, 0,
75 nullptr, rtl_arena_alloc
,
78 // To be consistent (and to ensure the rtl_cache threads are started).
79 ensureCacheSingleton();
83 rtl_arena_foreach(pre_arena
, mark_static
);
84 rtl_allocateString
= malloc
;
85 rtl_freeString
= free
;
87 // TODO: also re-initialize main allocator as well.
91 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */