xref: /aoo42x/main/sal/rtl/source/alloc_impl.h (revision 787e1130)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef INCLUDED_RTL_ALLOC_IMPL_H
25 #define INCLUDED_RTL_ALLOC_IMPL_H
26 
27 #include "sal/types.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 
34 /** Alignment macros
35  */
36 #if SAL_TYPES_ALIGNMENT4 > 1
37 #define RTL_MEMORY_ALIGNMENT_4 SAL_TYPES_ALIGNMENT4
38 #else
39 #define RTL_MEMORY_ALIGNMENT_4 sizeof(int)
40 #endif /* SAL_TYPES_ALIGNMENT4 */
41 
42 #if SAL_TYPES_ALIGNMENT8 > 1
43 #define RTL_MEMORY_ALIGNMENT_8 SAL_TYPES_ALIGNMENT8
44 #else
45 #define RTL_MEMORY_ALIGNMENT_8 sizeof(void*)
46 #endif /* SAL_TYPES_ALIGNMENT8 */
47 
48 #if defined(SAL_TYPES_ALIGNMENT16) && SAL_TYPES_ALIGNMENT16 > 1
49 #define RTL_MEMORY_ALIGNMENT_16 SAL_TYPES_ALIGNMENT16
50 #else
51 #define RTL_MEMORY_ALIGNMENT_16 16
52 #endif /* SAL_TYPES_ALIGNMENT16 */
53 
54 #if 0  /* @@@ */
55 #define RTL_MEMORY_ALIGNMENT_1 8
56 #define RTL_MEMORY_ALIGNMENT_2 (sizeof(void*) * 2)
57 #endif /* @@@ */
58 
59 #define RTL_MEMORY_ALIGN(value, align) (((value) + ((align) - 1)) & ~((align) - 1))
60 
61 #define RTL_MEMORY_ISP2(value) (((value) & ((value) - 1)) == 0)
62 #define RTL_MEMORY_P2ALIGN(value, align) ((value) & -(sal_IntPtr)(align))
63 
64 #define RTL_MEMORY_P2ROUNDUP(value, align) \
65 	(-(-(sal_IntPtr)(value) & -(sal_IntPtr)(align)))
66 #define RTL_MEMORY_P2END(value, align) \
67 	(-(~(sal_IntPtr)(value) & -(sal_IntPtr)(align)))
68 
69 
70 /** Function inlining macros
71  *  (compiler dependent)
72  */
73 #ifndef RTL_MEMORY_INLINE
74 #if defined(__GNUC__)
75 #define RTL_MEMORY_INLINE __inline__
76 #elif defined(_MSC_VER)
77 #define RTL_MEMORY_INLINE __inline
78 #else
79 #define RTL_MEMORY_INLINE
80 #endif /* __GNUC__ || _MSC_VER */
81 #endif /* RTL_MEMORY_INLINE */
82 
83 
84 /** printf() format specifier(s)
85  *  (from C90 <sys/int_fmtio.h>)
86  */
87 #ifndef PRIu64
88 #if defined(_MSC_VER)
89 #define PRIu64 "I64u"
90 #else  /* !_MSC_VER */
91 #define PRIu64 "llu"
92 #endif /* !_MSC_VER */
93 #endif /* PRIu64 */
94 
95 
96 /** highbit(): log2() + 1
97  *  (complexity O(1))
98  */
99 static RTL_MEMORY_INLINE int
highbit(sal_Size n)100 highbit(sal_Size n)
101 {
102   register int k = 1;
103 
104   if (n == 0)
105 	return (0);
106 #if SAL_TYPES_SIZEOFLONG == 8
107   if (n & 0xffffffff00000000ul)
108 	k |= 32, n >>= 32;
109 #endif
110   if (n & 0xffff0000)
111     k |= 16, n >>= 16;
112   if (n & 0xff00)
113     k |= 8, n >>= 8;
114   if (n & 0xf0)
115 	k |= 4, n >>= 4;
116   if (n & 0x0c)
117     k |= 2, n >>= 2;
118   if (n & 0x02)
119     k++;
120 
121   return (k);
122 }
123 
124 #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
125 #pragma inline(highbit)
126 #endif /* __SUNPRO_C */
127 
128 
129 /** lowbit(): find first bit set
130  *  (complexity O(1))
131  */
132 static RTL_MEMORY_INLINE int
lowbit(sal_Size n)133 lowbit(sal_Size n)
134 {
135   register int k = 1;
136 
137   if (n == 0)
138 	return (0);
139 #if SAL_TYPES_SIZEOFLONG == 8
140   if (!(n & 0xffffffff))
141 	k |= 32, n >>= 32;
142 #endif
143   if (!(n & 0xffff))
144 	k |= 16, n >>= 16;
145   if (!(n & 0xff))
146 	k |= 8, n >>= 8;
147   if (!(n & 0xf))
148 	k |= 4, n >>= 4;
149   if (!(n & 0x3))
150 	k |= 2, n >>= 2;
151   if (!(n & 0x1))
152 	k++;
153   return (k);
154 }
155 
156 #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
157 #pragma inline(lowbit)
158 #endif /* __SUNPRO_C */
159 
160 
161 /** Queue manipulation macros
162  *  (doubly linked circular list)
163  *  (complexity O(1))
164  */
165 #define QUEUE_STARTED_NAMED(entry, name) \
166   (((entry)->m_##name##next == (entry)) && ((entry)->m_##name##prev == (entry)))
167 
168 #define QUEUE_START_NAMED(entry, name) \
169 { \
170   (entry)->m_##name##next = (entry); \
171   (entry)->m_##name##prev = (entry); \
172 }
173 
174 #define QUEUE_REMOVE_NAMED(entry, name) \
175 { \
176   (entry)->m_##name##prev->m_##name##next = (entry)->m_##name##next; \
177   (entry)->m_##name##next->m_##name##prev = (entry)->m_##name##prev; \
178   QUEUE_START_NAMED(entry, name); \
179 }
180 
181 #define QUEUE_INSERT_HEAD_NAMED(head, entry, name) \
182 { \
183   (entry)->m_##name##prev = (head); \
184   (entry)->m_##name##next = (head)->m_##name##next; \
185   (head)->m_##name##next = (entry); \
186   (entry)->m_##name##next->m_##name##prev = (entry); \
187 }
188 
189 #define QUEUE_INSERT_TAIL_NAMED(head, entry, name) \
190 { \
191   (entry)->m_##name##next = (head); \
192   (entry)->m_##name##prev = (head)->m_##name##prev; \
193   (head)->m_##name##prev = (entry); \
194   (entry)->m_##name##prev->m_##name##next = (entry); \
195 }
196 
197 
198 /** rtl_memory_lock_type
199  *  (platform dependent)
200  */
201 #if defined(SAL_UNX) || defined(SAL_OS2)
202 
203 #include <unistd.h>
204 #include <pthread.h>
205 
206 typedef pthread_mutex_t rtl_memory_lock_type;
207 
208 #define RTL_MEMORY_LOCK_INIT(lock)    pthread_mutex_init((lock), NULL)
209 #define RTL_MEMORY_LOCK_DESTROY(lock) pthread_mutex_destroy((lock))
210 
211 #define RTL_MEMORY_LOCK_ACQUIRE(lock) pthread_mutex_lock((lock))
212 #define RTL_MEMORY_LOCK_RELEASE(lock) pthread_mutex_unlock((lock))
213 
214 #elif defined(SAL_W32)
215 
216 #define WIN32_LEAN_AND_MEAN
217 #ifdef _MSC_VER
218 #pragma warning(push,1) /* disable warnings within system headers */
219 #endif
220 #include <windows.h>
221 #ifdef _MSC_VER
222 #pragma warning(pop)
223 #endif
224 
225 typedef CRITICAL_SECTION rtl_memory_lock_type;
226 
227 #define RTL_MEMORY_LOCK_INIT(lock)    InitializeCriticalSection((lock))
228 #define RTL_MEMORY_LOCK_DESTROY(lock) DeleteCriticalSection((lock))
229 
230 #define RTL_MEMORY_LOCK_ACQUIRE(lock) EnterCriticalSection((lock))
231 #define RTL_MEMORY_LOCK_RELEASE(lock) LeaveCriticalSection((lock))
232 
233 #else
234 #error Unknown platform
235 #endif /* SAL_UNX | SAL_W32 */
236 
237 
238 /** Cache creation flags.
239  *  @internal
240  */
241 #define RTL_CACHE_FLAG_NOMAGAZINE   (1 << 13) /* w/o magazine layer */
242 #define RTL_CACHE_FLAG_QUANTUMCACHE (2 << 13) /* used as arena quantum cache */
243 
244 
245 /** Valgrind support macros.
246  */
247 #if !defined(HAVE_MEMCHECK_H) || (OSL_DEBUG_LEVEL == 0)
248 #if !defined(NVALGRIND)
249 #define NVALGRIND 1
250 #endif /* ! NVALGRIND */
251 #endif /* ! HAVE_MEMCHECK_H || (OSL_DEBUG_LEVEL == 0) */
252 
253 #if defined(NVALGRIND)
254 #define VALGRIND_MAKE_MEM_UNDEFINED(addr, size)
255 #define VALGRIND_MAKE_MEM_DEFINED(addr, size)
256 #define VALGRIND_MALLOCLIKE_BLOCK(addr, sizeB, rzB, is_zeroed)
257 #define VALGRIND_FREELIKE_BLOCK(addr, rzB)
258 #define VALGRIND_CREATE_MEMPOOL(pool, rzB, is_zeroed)
259 #define VALGRIND_DESTROY_MEMPOOL(pool)
260 #define VALGRIND_MEMPOOL_ALLOC(pool, addr, size)
261 #define VALGRIND_MEMPOOL_FREE(pool, addr)
262 #elif defined(HAVE_MEMCHECK_H)
263 #include <memcheck.h>
264 #if !defined(FORCE_SYSALLOC)
265 #define FORCE_SYSALLOC 1
266 #endif /* !FORCE_SYSALLOC */
267 #endif /* NVALGRIND || HAVE_MEMCHECK_H */
268 
269 #ifdef __cplusplus
270 }
271 #endif
272 
273 #endif /* INCLUDED_RTL_ALLOC_IMPL_H */
274