/src/libreoffice/sal/rtl/alloc_impl.hxx
Line | Count | Source |
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 | | #ifndef INCLUDED_SAL_RTL_ALLOC_IMPL_HXX |
21 | | #define INCLUDED_SAL_RTL_ALLOC_IMPL_HXX |
22 | | |
23 | | #include <sal/types.h> |
24 | | |
25 | | /** Alignment macros |
26 | | */ |
27 | | #if SAL_TYPES_ALIGNMENT4 > 1 |
28 | 0 | #define RTL_MEMORY_ALIGNMENT_4 SAL_TYPES_ALIGNMENT4 |
29 | | #else |
30 | | #define RTL_MEMORY_ALIGNMENT_4 sizeof(int) |
31 | | #endif /* SAL_TYPES_ALIGNMENT4 */ |
32 | | |
33 | | #if SAL_TYPES_ALIGNMENT8 > 1 |
34 | 0 | #define RTL_MEMORY_ALIGNMENT_8 SAL_TYPES_ALIGNMENT8 |
35 | | #else |
36 | | #define RTL_MEMORY_ALIGNMENT_8 sizeof(void*) |
37 | | #endif /* SAL_TYPES_ALIGNMENT8 */ |
38 | | |
39 | 0 | #define RTL_MEMORY_ALIGN(value, align) (((value) + ((align) - 1)) & ~((align) - 1)) |
40 | | |
41 | 0 | #define RTL_MEMORY_ISP2(value) (((value) & ((value) - 1)) == 0) |
42 | 0 | #define RTL_MEMORY_P2ALIGN(value, align) ((value) & -static_cast<sal_IntPtr>(align)) |
43 | | |
44 | | #define RTL_MEMORY_P2ROUNDUP(value, align) \ |
45 | 0 | (-(-static_cast<sal_IntPtr>(value) & -static_cast<sal_IntPtr>(align))) |
46 | | #define RTL_MEMORY_P2END(value, align) \ |
47 | | (-(~static_cast<sal_IntPtr>(value) & -static_cast<sal_IntPtr>(align))) |
48 | | |
49 | | /** highbit(): log2() + 1 |
50 | | (complexity O(1)) |
51 | | */ |
52 | | static inline unsigned int highbit(sal_Size n) |
53 | 0 | { |
54 | 0 | unsigned int k = 1; |
55 | |
|
56 | 0 | if (n == 0) |
57 | 0 | return 0; |
58 | | if constexpr (sizeof(n) >= 8) |
59 | 0 | { |
60 | 0 | if (n & 0xffffffff00000000) |
61 | 0 | { |
62 | 0 | k |= 32; |
63 | 0 | n >>= 32; |
64 | 0 | } |
65 | 0 | } |
66 | 0 | if (n & 0xffff0000) |
67 | 0 | { |
68 | 0 | k |= 16; |
69 | 0 | n >>= 16; |
70 | 0 | } |
71 | 0 | if (n & 0xff00) |
72 | 0 | { |
73 | 0 | k |= 8; |
74 | 0 | n >>= 8; |
75 | 0 | } |
76 | 0 | if (n & 0xf0) |
77 | 0 | { |
78 | 0 | k |= 4; |
79 | 0 | n >>= 4; |
80 | 0 | } |
81 | 0 | if (n & 0x0c) |
82 | 0 | { |
83 | 0 | k |= 2; |
84 | 0 | n >>= 2; |
85 | 0 | } |
86 | 0 | if (n & 0x02) |
87 | 0 | k++; |
88 | |
|
89 | 0 | return k; |
90 | 0 | } Unexecuted instantiation: alloc_arena.cxx:highbit(unsigned long) Unexecuted instantiation: alloc_cache.cxx:highbit(unsigned long) Unexecuted instantiation: strimp.cxx:highbit(unsigned long) |
91 | | |
92 | | /** find first bit set |
93 | | (complexity O(1)) |
94 | | */ |
95 | | static inline unsigned int lowbit(sal_Size n) |
96 | 0 | { |
97 | 0 | unsigned int k = 1; |
98 | |
|
99 | 0 | if (n == 0) |
100 | 0 | return 0; |
101 | | |
102 | | if constexpr (sizeof(n) >= 8) |
103 | 0 | { |
104 | 0 | if (!(n & 0xffffffff)) |
105 | 0 | { |
106 | 0 | k |= 32; |
107 | 0 | n >>= 32; |
108 | 0 | } |
109 | 0 | } |
110 | |
|
111 | 0 | if (!(n & 0xffff)) |
112 | 0 | { |
113 | 0 | k |= 16; |
114 | 0 | n >>= 16; |
115 | 0 | } |
116 | |
|
117 | 0 | if (!(n & 0xff)) |
118 | 0 | { |
119 | 0 | k |= 8; |
120 | 0 | n >>= 8; |
121 | 0 | } |
122 | |
|
123 | 0 | if (!(n & 0xf)) |
124 | 0 | { |
125 | 0 | k |= 4; |
126 | 0 | n >>= 4; |
127 | 0 | } |
128 | |
|
129 | 0 | if (!(n & 0x3)) |
130 | 0 | { |
131 | 0 | k |= 2; |
132 | 0 | n >>= 2; |
133 | 0 | } |
134 | |
|
135 | 0 | if (!(n & 0x1)) |
136 | 0 | k++; |
137 | |
|
138 | 0 | return k; |
139 | 0 | } Unexecuted instantiation: alloc_arena.cxx:lowbit(unsigned long) Unexecuted instantiation: alloc_cache.cxx:lowbit(unsigned long) Unexecuted instantiation: strimp.cxx:lowbit(unsigned long) |
140 | | |
141 | | /** Queue manipulation macros |
142 | | (doubly linked circular list) |
143 | | (complexity O(1)) |
144 | | */ |
145 | | #define QUEUE_STARTED_NAMED(entry, name) \ |
146 | | (((entry)->m_##name##next == (entry)) && ((entry)->m_##name##prev == (entry))) |
147 | | |
148 | 0 | #define QUEUE_START_NAMED(entry, name) \ |
149 | 0 | { \ |
150 | 0 | (entry)->m_##name##next = (entry); \ |
151 | 0 | (entry)->m_##name##prev = (entry); \ |
152 | 0 | } |
153 | | |
154 | 0 | #define QUEUE_REMOVE_NAMED(entry, name) \ |
155 | 0 | { \ |
156 | 0 | (entry)->m_##name##prev->m_##name##next = (entry)->m_##name##next; \ |
157 | 0 | (entry)->m_##name##next->m_##name##prev = (entry)->m_##name##prev; \ |
158 | 0 | QUEUE_START_NAMED(entry, name); \ |
159 | 0 | } |
160 | | |
161 | 0 | #define QUEUE_INSERT_HEAD_NAMED(head, entry, name) \ |
162 | 0 | { \ |
163 | 0 | (entry)->m_##name##prev = (head); \ |
164 | 0 | (entry)->m_##name##next = (head)->m_##name##next; \ |
165 | 0 | (head)->m_##name##next = (entry); \ |
166 | 0 | (entry)->m_##name##next->m_##name##prev = (entry); \ |
167 | 0 | } |
168 | | |
169 | 0 | #define QUEUE_INSERT_TAIL_NAMED(head, entry, name) \ |
170 | 0 | { \ |
171 | 0 | (entry)->m_##name##next = (head); \ |
172 | 0 | (entry)->m_##name##prev = (head)->m_##name##prev; \ |
173 | 0 | (head)->m_##name##prev = (entry); \ |
174 | 0 | (entry)->m_##name##prev->m_##name##next = (entry); \ |
175 | 0 | } |
176 | | |
177 | | #if defined(SAL_UNX) |
178 | | |
179 | | #include <pthread.h> |
180 | | |
181 | | typedef pthread_mutex_t rtl_memory_lock_type; |
182 | | |
183 | 0 | #define RTL_MEMORY_LOCK_INIT(lock) pthread_mutex_init((lock), nullptr) |
184 | 0 | #define RTL_MEMORY_LOCK_DESTROY(lock) pthread_mutex_destroy((lock)) |
185 | | |
186 | 0 | #define RTL_MEMORY_LOCK_ACQUIRE(lock) pthread_mutex_lock((lock)) |
187 | 0 | #define RTL_MEMORY_LOCK_RELEASE(lock) pthread_mutex_unlock((lock)) |
188 | | |
189 | | #elif defined(_WIN32) |
190 | | |
191 | | #define WIN32_LEAN_AND_MEAN |
192 | | #include <windows.h> |
193 | | |
194 | | typedef CRITICAL_SECTION rtl_memory_lock_type; |
195 | | |
196 | | #define RTL_MEMORY_LOCK_INIT(lock) InitializeCriticalSection((lock)) |
197 | | #define RTL_MEMORY_LOCK_DESTROY(lock) DeleteCriticalSection((lock)) |
198 | | |
199 | | #define RTL_MEMORY_LOCK_ACQUIRE(lock) EnterCriticalSection((lock)) |
200 | | #define RTL_MEMORY_LOCK_RELEASE(lock) LeaveCriticalSection((lock)) |
201 | | |
202 | | #else |
203 | | #error Unknown platform |
204 | | #endif /* SAL_UNX | _WIN32 */ |
205 | | |
206 | | #endif // INCLUDED_SAL_RTL_ALLOC_IMPL_HXX |
207 | | |
208 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |