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>
22 #include <rtl/alloc.h>
23 #include <rtl/ustring.h>
24 #include <rtllifecycle.h>
27 #include "alloc_impl.hxx"
28 #include "alloc_arena.hxx"
31 * TODO: add a slower, more awful, but more space efficient
32 * custom allocator for the pre-init phase. Existing slab
33 * allocator's minimum alloc size is 24bytes, and by default
36 static rtl_arena_type
*pre_arena
= nullptr;
38 rtl_allocateStringFn rtl_allocateString
= malloc
;
39 rtl_freeStringFn rtl_freeString
= free
;
42 static void *pre_allocateStringFn(size_t n
)
44 sal_Size size
= RTL_MEMORY_ALIGN(n
+ 4, 4);
45 char *addr
= static_cast<char*>(rtl_arena_alloc(pre_arena
, &size
));
47 reinterpret_cast<sal_uInt32
*>(addr
)[0] = size
- 12;
51 static void pre_freeStringFn(void *data
)
53 char *addr
= static_cast<char*>(data
) - 4;
54 sal_uInt32 size
= reinterpret_cast<sal_uInt32
*>(addr
)[0] + 12;
56 rtl_arena_free(pre_arena
, addr
, size
);
60 static void mark_static(void *addr
, sal_Size
/* size */)
62 char *inner
= static_cast<char*>(addr
) + 4;
63 rtl_uString
*str
= reinterpret_cast<rtl_uString
*>(inner
);
64 str
->refCount
|= SAL_STRING_STATIC_FLAG
;
67 void SAL_CALL
rtl_alloc_preInit (sal_Bool start
) SAL_THROW_EXTERN_C()
71 rtl_allocateString
= pre_allocateStringFn
;
72 rtl_freeString
= pre_freeStringFn
;
73 pre_arena
= rtl_arena_create("pre-init strings", 4, 0,
74 nullptr, rtl_arena_alloc
,
77 // To be consistent (and to ensure the rtl_cache threads are started).
78 ensureCacheSingleton();
82 rtl_arena_foreach(pre_arena
, mark_static
);
83 rtl_allocateString
= malloc
;
84 rtl_freeString
= free
;
86 // TODO: also re-initialize main allocator as well.
90 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */