Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sal / rtl / strimp.cxx
blob51a1a94bd7053d8c3f5f0b7b7526638d83cd991c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
21 #include <assert.h>
22 #include <rtl/alloc.h>
23 #include <rtl/ustring.h>
24 #include <rtllifecycle.h>
26 #include "strimp.hxx"
27 #include "alloc_impl.hxx"
28 #include "alloc_arena.hxx"
30 sal_Int16 rtl_ImplGetDigit( sal_Unicode ch, sal_Int16 nRadix )
32 sal_Int16 n = -1;
33 if ( (ch >= '0') && (ch <= '9') )
34 n = ch-'0';
35 else if ( (ch >= 'a') && (ch <= 'z') )
36 n = ch-'a'+10;
37 else if ( (ch >= 'A') && (ch <= 'Z') )
38 n = ch-'A'+10;
39 return (n < nRadix) ? n : -1;
42 bool rtl_ImplIsWhitespace( sal_Unicode c )
44 /* Space or Control character? */
45 if ( (c <= 32) && c )
46 return true;
48 /* Only in the General Punctuation area Space or Control characters are included? */
49 if ( (c < 0x2000) || (c > 0x206F) )
50 return false;
52 if ( ((c >= 0x2000) && (c <= 0x200B)) || /* All Spaces */
53 (c == 0x2028) || /* LINE SEPARATOR */
54 (c == 0x2029) ) /* PARAGRAPH SEPARATOR */
55 return true;
57 return false;
61 * TODO: add a slower, more awful, but more space efficient
62 * custom allocator for the pre-init phase. Existing slab
63 * allocator's minimum alloc size is 24bytes, and by default
64 * is 32 bytes.
66 static rtl_arena_type *pre_arena = nullptr;
68 rtl_allocateStringFn rtl_allocateString = malloc;
69 rtl_freeStringFn rtl_freeString = free;
71 extern "C" {
72 static void *pre_allocateStringFn(size_t n)
74 sal_Size size = RTL_MEMORY_ALIGN(n + 4, 4);
75 char *addr = static_cast<char*>(rtl_arena_alloc(pre_arena, &size));
76 assert(size>= 12);
77 reinterpret_cast<sal_uInt32*>(addr)[0] = size - 12;
78 return addr + 4;
81 static void pre_freeStringFn(void *data)
83 char *addr = static_cast<char*>(data) - 4;
84 sal_uInt32 size = reinterpret_cast<sal_uInt32*>(addr)[0] + 12;
86 rtl_arena_free(pre_arena, addr, size);
88 } // extern "C"
90 static void mark_static(void *addr, sal_Size /* size */)
92 char *inner = static_cast<char*>(addr) + 4;
93 rtl_uString *str = reinterpret_cast<rtl_uString *>(inner);
94 str->refCount |= SAL_STRING_STATIC_FLAG;
97 void SAL_CALL rtl_alloc_preInit (sal_Bool start) SAL_THROW_EXTERN_C()
99 if (start)
101 rtl_allocateString = pre_allocateStringFn;
102 rtl_freeString = pre_freeStringFn;
103 pre_arena = rtl_arena_create("pre-init strings", 4, 0,
104 nullptr, rtl_arena_alloc,
105 rtl_arena_free, 0);
107 // To be consistent (and to ensure the rtl_cache threads are started).
108 ensureCacheSingleton();
110 else
112 rtl_arena_foreach(pre_arena, mark_static);
113 rtl_allocateString = malloc;
114 rtl_freeString = free;
116 // TODO: also re-initialize main allocator as well.
120 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */