Line data Source code
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_RTL_ALLOC_IMPL_HXX
21 : #define INCLUDED_RTL_ALLOC_IMPL_HXX
22 :
23 : #include "sal/types.h"
24 :
25 : /** Alignment macros
26 : */
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 */
32 :
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 */
38 :
39 : #if 0 /* @@@ */
40 : #define RTL_MEMORY_ALIGNMENT_1 8
41 : #define RTL_MEMORY_ALIGNMENT_2 (sizeof(void*) * 2)
42 : #endif /* @@@ */
43 :
44 : #define RTL_MEMORY_ALIGN(value, align) (((value) + ((align) - 1)) & ~((align) - 1))
45 :
46 : #define RTL_MEMORY_ISP2(value) (((value) & ((value) - 1)) == 0)
47 : #define RTL_MEMORY_P2ALIGN(value, align) ((value) & -(sal_IntPtr)(align))
48 :
49 : #define RTL_MEMORY_P2ROUNDUP(value, align) \
50 : (-(-(sal_IntPtr)(value) & -(sal_IntPtr)(align)))
51 : #define RTL_MEMORY_P2END(value, align) \
52 : (-(~(sal_IntPtr)(value) & -(sal_IntPtr)(align)))
53 :
54 :
55 : /** Function inlining macros
56 : * (compiler dependent)
57 : */
58 : #ifndef RTL_MEMORY_INLINE
59 : #if defined(__GNUC__)
60 : #define RTL_MEMORY_INLINE __inline__
61 : #elif defined(_MSC_VER)
62 : #define RTL_MEMORY_INLINE __inline
63 : #else
64 : #define RTL_MEMORY_INLINE
65 : #endif /* __GNUC__ || _MSC_VER */
66 : #endif /* RTL_MEMORY_INLINE */
67 :
68 :
69 : /** printf() format specifier(s)
70 : * (from C90 <sys/int_fmtio.h>)
71 : */
72 : #ifndef PRIu64
73 : #if defined(_MSC_VER)
74 : #define PRIu64 "I64u"
75 : #else /* !_MSC_VER */
76 : #define PRIu64 "llu"
77 : #endif /* !_MSC_VER */
78 : #endif /* PRIu64 */
79 :
80 :
81 : /** highbit(): log2() + 1
82 : * (complexity O(1))
83 : */
84 : static RTL_MEMORY_INLINE int
85 954592 : highbit(sal_Size n)
86 : {
87 954592 : int k = 1;
88 :
89 954592 : if (n == 0)
90 0 : return (0);
91 : #if SAL_TYPES_SIZEOFLONG == 8
92 : if (n & 0xffffffff00000000ul)
93 : k |= 32, n >>= 32;
94 : #endif
95 954592 : if (n & 0xffff0000)
96 5300 : k |= 16, n >>= 16;
97 954592 : if (n & 0xff00)
98 853339 : k |= 8, n >>= 8;
99 954592 : if (n & 0xf0)
100 598197 : k |= 4, n >>= 4;
101 954592 : if (n & 0x0c)
102 745528 : k |= 2, n >>= 2;
103 954592 : if (n & 0x02)
104 454484 : k++;
105 :
106 954592 : return (k);
107 : }
108 :
109 : #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
110 : #pragma inline(highbit)
111 : #endif /* __SUNPRO_C */
112 :
113 :
114 : /** lowbit(): find first bit set
115 : * (complexity O(1))
116 : */
117 : static RTL_MEMORY_INLINE int
118 400961 : lowbit(sal_Size n)
119 : {
120 400961 : int k = 1;
121 :
122 400961 : if (n == 0)
123 161399 : return (0);
124 : #if SAL_TYPES_SIZEOFLONG == 8
125 : if (!(n & 0xffffffff))
126 : k |= 32, n >>= 32;
127 : #endif
128 239562 : if (!(n & 0xffff))
129 80 : k |= 16, n >>= 16;
130 239562 : if (!(n & 0xff))
131 239482 : k |= 8, n >>= 8;
132 239562 : if (!(n & 0xf))
133 185887 : k |= 4, n >>= 4;
134 239562 : if (!(n & 0x3))
135 202246 : k |= 2, n >>= 2;
136 239562 : if (!(n & 0x1))
137 176849 : k++;
138 239562 : return (k);
139 : }
140 :
141 : #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
142 : #pragma inline(lowbit)
143 : #endif /* __SUNPRO_C */
144 :
145 :
146 : /** Queue manipulation macros
147 : * (doubly linked circular list)
148 : * (complexity O(1))
149 : */
150 : #define QUEUE_STARTED_NAMED(entry, name) \
151 : (((entry)->m_##name##next == (entry)) && ((entry)->m_##name##prev == (entry)))
152 :
153 : #define QUEUE_START_NAMED(entry, name) \
154 : { \
155 : (entry)->m_##name##next = (entry); \
156 : (entry)->m_##name##prev = (entry); \
157 : }
158 :
159 : #define QUEUE_REMOVE_NAMED(entry, name) \
160 : { \
161 : (entry)->m_##name##prev->m_##name##next = (entry)->m_##name##next; \
162 : (entry)->m_##name##next->m_##name##prev = (entry)->m_##name##prev; \
163 : QUEUE_START_NAMED(entry, name); \
164 : }
165 :
166 : #define QUEUE_INSERT_HEAD_NAMED(head, entry, name) \
167 : { \
168 : (entry)->m_##name##prev = (head); \
169 : (entry)->m_##name##next = (head)->m_##name##next; \
170 : (head)->m_##name##next = (entry); \
171 : (entry)->m_##name##next->m_##name##prev = (entry); \
172 : }
173 :
174 : #define QUEUE_INSERT_TAIL_NAMED(head, entry, name) \
175 : { \
176 : (entry)->m_##name##next = (head); \
177 : (entry)->m_##name##prev = (head)->m_##name##prev; \
178 : (head)->m_##name##prev = (entry); \
179 : (entry)->m_##name##prev->m_##name##next = (entry); \
180 : }
181 :
182 :
183 : /** rtl_memory_lock_type
184 : * (platform dependent)
185 : */
186 : #if defined(SAL_UNX)
187 :
188 : #include <unistd.h>
189 : #include <pthread.h>
190 :
191 : typedef pthread_mutex_t rtl_memory_lock_type;
192 :
193 : #define RTL_MEMORY_LOCK_INIT(lock) pthread_mutex_init((lock), NULL)
194 : #define RTL_MEMORY_LOCK_DESTROY(lock) pthread_mutex_destroy((lock))
195 :
196 : #define RTL_MEMORY_LOCK_ACQUIRE(lock) pthread_mutex_lock((lock))
197 : #define RTL_MEMORY_LOCK_RELEASE(lock) pthread_mutex_unlock((lock))
198 :
199 : #elif defined(SAL_W32)
200 :
201 : #define WIN32_LEAN_AND_MEAN
202 : #ifdef _MSC_VER
203 : #pragma warning(push,1) /* disable warnings within system headers */
204 : #endif
205 : #include <windows.h>
206 : #ifdef _MSC_VER
207 : #pragma warning(pop)
208 : #endif
209 :
210 : typedef CRITICAL_SECTION rtl_memory_lock_type;
211 :
212 : #define RTL_MEMORY_LOCK_INIT(lock) InitializeCriticalSection((lock))
213 : #define RTL_MEMORY_LOCK_DESTROY(lock) DeleteCriticalSection((lock))
214 :
215 : #define RTL_MEMORY_LOCK_ACQUIRE(lock) EnterCriticalSection((lock))
216 : #define RTL_MEMORY_LOCK_RELEASE(lock) LeaveCriticalSection((lock))
217 :
218 : #else
219 : #error Unknown platform
220 : #endif /* SAL_UNX | SAL_W32 */
221 :
222 :
223 : /** Cache creation flags.
224 : * @internal
225 : */
226 : #define RTL_CACHE_FLAG_NOMAGAZINE (1 << 13) /* w/o magazine layer */
227 : #define RTL_CACHE_FLAG_QUANTUMCACHE (2 << 13) /* used as arena quantum cache */
228 :
229 : typedef enum { AMode_CUSTOM, AMode_SYSTEM, AMode_UNSET } AllocMode;
230 :
231 : extern AllocMode alloc_mode;
232 :
233 : #endif /* INCLUDED_RTL_ALLOC_IMPL_HXX */
234 :
235 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|