Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sal / rtl / alloc_impl.hxx
blobbdacba04a29d34e56cc7f97d3faa6ce732faf9d8
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 .
20 #ifndef INCLUDED_SAL_RTL_ALLOC_IMPL_HXX
21 #define INCLUDED_SAL_RTL_ALLOC_IMPL_HXX
23 #include <sal/types.h>
25 /** Alignment macros
27 #if SAL_TYPES_ALIGNMENT4 > 1
28 #define RTL_MEMORY_ALIGNMENT_4 SAL_TYPES_ALIGNMENT4
29 #else
30 #define RTL_MEMORY_ALIGNMENT_4 sizeof(int)
31 #endif /* SAL_TYPES_ALIGNMENT4 */
33 #if SAL_TYPES_ALIGNMENT8 > 1
34 #define RTL_MEMORY_ALIGNMENT_8 SAL_TYPES_ALIGNMENT8
35 #else
36 #define RTL_MEMORY_ALIGNMENT_8 sizeof(void*)
37 #endif /* SAL_TYPES_ALIGNMENT8 */
39 #if 0 /* @@@ */
40 #define RTL_MEMORY_ALIGNMENT_1 8
41 #define RTL_MEMORY_ALIGNMENT_2 (sizeof(void*) * 2)
42 #endif /* @@@ */
44 #define RTL_MEMORY_ALIGN(value, align) (((value) + ((align) - 1)) & ~((align) - 1))
46 #define RTL_MEMORY_ISP2(value) (((value) & ((value) - 1)) == 0)
47 #define RTL_MEMORY_P2ALIGN(value, align) ((value) & -static_cast<sal_IntPtr>(align))
49 #define RTL_MEMORY_P2ROUNDUP(value, align) \
50 (-(-static_cast<sal_IntPtr>(value) & -static_cast<sal_IntPtr>(align)))
51 #define RTL_MEMORY_P2END(value, align) \
52 (-(~static_cast<sal_IntPtr>(value) & -static_cast<sal_IntPtr>(align)))
54 /** highbit(): log2() + 1
55 (complexity O(1))
57 static inline unsigned int highbit(sal_Size n)
59 unsigned int k = 1;
61 if (n == 0)
62 return 0;
63 #if SAL_TYPES_SIZEOFLONG == 8
64 if (n & 0xffffffff00000000ul)
66 k |= 32;
67 n >>= 32;
69 #endif
70 if (n & 0xffff0000)
72 k |= 16;
73 n >>= 16;
75 if (n & 0xff00)
77 k |= 8;
78 n >>= 8;
80 if (n & 0xf0)
82 k |= 4;
83 n >>= 4;
85 if (n & 0x0c)
87 k |= 2;
88 n >>= 2;
90 if (n & 0x02)
91 k++;
93 return k;
96 /** find first bit set
97 (complexity O(1))
99 static inline unsigned int lowbit(sal_Size n)
101 unsigned int k = 1;
103 if (n == 0)
104 return 0;
106 #if SAL_TYPES_SIZEOFLONG == 8
107 if (!(n & 0xffffffff))
109 k |= 32;
110 n >>= 32;
112 #endif
114 if (!(n & 0xffff))
116 k |= 16;
117 n >>= 16;
120 if (!(n & 0xff))
122 k |= 8;
123 n >>= 8;
126 if (!(n & 0xf))
128 k |= 4;
129 n >>= 4;
132 if (!(n & 0x3))
134 k |= 2;
135 n >>= 2;
138 if (!(n & 0x1))
139 k++;
141 return k;
144 /** Queue manipulation macros
145 (doubly linked circular list)
146 (complexity O(1))
148 #define QUEUE_STARTED_NAMED(entry, name) \
149 (((entry)->m_##name##next == (entry)) && ((entry)->m_##name##prev == (entry)))
151 #define QUEUE_START_NAMED(entry, name) \
153 (entry)->m_##name##next = (entry); \
154 (entry)->m_##name##prev = (entry); \
157 #define QUEUE_REMOVE_NAMED(entry, name) \
159 (entry)->m_##name##prev->m_##name##next = (entry)->m_##name##next; \
160 (entry)->m_##name##next->m_##name##prev = (entry)->m_##name##prev; \
161 QUEUE_START_NAMED(entry, name); \
164 #define QUEUE_INSERT_HEAD_NAMED(head, entry, name) \
166 (entry)->m_##name##prev = (head); \
167 (entry)->m_##name##next = (head)->m_##name##next; \
168 (head)->m_##name##next = (entry); \
169 (entry)->m_##name##next->m_##name##prev = (entry); \
172 #define QUEUE_INSERT_TAIL_NAMED(head, entry, name) \
174 (entry)->m_##name##next = (head); \
175 (entry)->m_##name##prev = (head)->m_##name##prev; \
176 (head)->m_##name##prev = (entry); \
177 (entry)->m_##name##prev->m_##name##next = (entry); \
180 #if defined(SAL_UNX)
182 #include <unistd.h>
183 #include <pthread.h>
185 typedef pthread_mutex_t rtl_memory_lock_type;
187 #define RTL_MEMORY_LOCK_INIT(lock) pthread_mutex_init((lock), nullptr)
188 #define RTL_MEMORY_LOCK_DESTROY(lock) pthread_mutex_destroy((lock))
190 #define RTL_MEMORY_LOCK_ACQUIRE(lock) pthread_mutex_lock((lock))
191 #define RTL_MEMORY_LOCK_RELEASE(lock) pthread_mutex_unlock((lock))
193 #elif defined(_WIN32)
195 #define WIN32_LEAN_AND_MEAN
196 #include <windows.h>
198 typedef CRITICAL_SECTION rtl_memory_lock_type;
200 #define RTL_MEMORY_LOCK_INIT(lock) InitializeCriticalSection((lock))
201 #define RTL_MEMORY_LOCK_DESTROY(lock) DeleteCriticalSection((lock))
203 #define RTL_MEMORY_LOCK_ACQUIRE(lock) EnterCriticalSection((lock))
204 #define RTL_MEMORY_LOCK_RELEASE(lock) LeaveCriticalSection((lock))
206 #else
207 #error Unknown platform
208 #endif /* SAL_UNX | _WIN32 */
210 /** Cache creation flags.
211 @internal
213 #define RTL_CACHE_FLAG_NOMAGAZINE (1 << 13) /* w/o magazine layer */
215 #endif // INCLUDED_SAL_RTL_ALLOC_IMPL_HXX
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */