xref: /aoo41x/main/sal/rtl/source/alloc_impl.h (revision 514f4c20)
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 0  /* @@@ */
49 #define RTL_MEMORY_ALIGNMENT_1 8
50 #define RTL_MEMORY_ALIGNMENT_2 (sizeof(void*) * 2)
51 #endif /* @@@ */
52 
53 #define RTL_MEMORY_ALIGN(value, align) (((value) + ((align) - 1)) & ~((align) - 1))
54 
55 #define RTL_MEMORY_ISP2(value) (((value) & ((value) - 1)) == 0)
56 #define RTL_MEMORY_P2ALIGN(value, align) ((value) & -(sal_IntPtr)(align))
57 
58 #define RTL_MEMORY_P2ROUNDUP(value, align) \
59 	(-(-(sal_IntPtr)(value) & -(sal_IntPtr)(align)))
60 #define RTL_MEMORY_P2END(value, align) \
61 	(-(~(sal_IntPtr)(value) & -(sal_IntPtr)(align)))
62 
63 
64 /** Function inlining macros
65  *  (compiler dependent)
66  */
67 #ifndef RTL_MEMORY_INLINE
68 #if defined(__GNUC__)
69 #define RTL_MEMORY_INLINE __inline__
70 #elif defined(_MSC_VER)
71 #define RTL_MEMORY_INLINE __inline
72 #else
73 #define RTL_MEMORY_INLINE
74 #endif /* __GNUC__ || _MSC_VER */
75 #endif /* RTL_MEMORY_INLINE */
76 
77 
78 /** printf() format specifier(s)
79  *  (from C90 <sys/int_fmtio.h>)
80  */
81 #ifndef PRIu64
82 #if defined(_MSC_VER)
83 #define PRIu64 "I64u"
84 #else  /* !_MSC_VER */
85 #define PRIu64 "llu"
86 #endif /* !_MSC_VER */
87 #endif /* PRIu64 */
88 
89 
90 /** highbit(): log2() + 1
91  *  (complexity O(1))
92  */
93 static RTL_MEMORY_INLINE int
highbit(sal_Size n)94 highbit(sal_Size n)
95 {
96   register int k = 1;
97 
98   if (n == 0)
99 	return (0);
100 #if SAL_TYPES_SIZEOFLONG == 8
101   if (n & 0xffffffff00000000ul)
102 	k |= 32, n >>= 32;
103 #endif
104   if (n & 0xffff0000)
105     k |= 16, n >>= 16;
106   if (n & 0xff00)
107     k |= 8, n >>= 8;
108   if (n & 0xf0)
109 	k |= 4, n >>= 4;
110   if (n & 0x0c)
111     k |= 2, n >>= 2;
112   if (n & 0x02)
113     k++;
114 
115   return (k);
116 }
117 
118 #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
119 #pragma inline(highbit)
120 #endif /* __SUNPRO_C */
121 
122 
123 /** lowbit(): find first bit set
124  *  (complexity O(1))
125  */
126 static RTL_MEMORY_INLINE int
lowbit(sal_Size n)127 lowbit(sal_Size n)
128 {
129   register int k = 1;
130 
131   if (n == 0)
132 	return (0);
133 #if SAL_TYPES_SIZEOFLONG == 8
134   if (!(n & 0xffffffff))
135 	k |= 32, n >>= 32;
136 #endif
137   if (!(n & 0xffff))
138 	k |= 16, n >>= 16;
139   if (!(n & 0xff))
140 	k |= 8, n >>= 8;
141   if (!(n & 0xf))
142 	k |= 4, n >>= 4;
143   if (!(n & 0x3))
144 	k |= 2, n >>= 2;
145   if (!(n & 0x1))
146 	k++;
147   return (k);
148 }
149 
150 #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
151 #pragma inline(lowbit)
152 #endif /* __SUNPRO_C */
153 
154 
155 /** Queue manipulation macros
156  *  (doubly linked circular list)
157  *  (complexity O(1))
158  */
159 #define QUEUE_STARTED_NAMED(entry, name) \
160   (((entry)->m_##name##next == (entry)) && ((entry)->m_##name##prev == (entry)))
161 
162 #define QUEUE_START_NAMED(entry, name) \
163 { \
164   (entry)->m_##name##next = (entry); \
165   (entry)->m_##name##prev = (entry); \
166 }
167 
168 #define QUEUE_REMOVE_NAMED(entry, name) \
169 { \
170   (entry)->m_##name##prev->m_##name##next = (entry)->m_##name##next; \
171   (entry)->m_##name##next->m_##name##prev = (entry)->m_##name##prev; \
172   QUEUE_START_NAMED(entry, name); \
173 }
174 
175 #define QUEUE_INSERT_HEAD_NAMED(head, entry, name) \
176 { \
177   (entry)->m_##name##prev = (head); \
178   (entry)->m_##name##next = (head)->m_##name##next; \
179   (head)->m_##name##next = (entry); \
180   (entry)->m_##name##next->m_##name##prev = (entry); \
181 }
182 
183 #define QUEUE_INSERT_TAIL_NAMED(head, entry, name) \
184 { \
185   (entry)->m_##name##next = (head); \
186   (entry)->m_##name##prev = (head)->m_##name##prev; \
187   (head)->m_##name##prev = (entry); \
188   (entry)->m_##name##prev->m_##name##next = (entry); \
189 }
190 
191 
192 /** rtl_memory_lock_type
193  *  (platform dependent)
194  */
195 #if defined(SAL_UNX) || defined(SAL_OS2)
196 
197 #include <unistd.h>
198 #include <pthread.h>
199 
200 typedef pthread_mutex_t rtl_memory_lock_type;
201 
202 #define RTL_MEMORY_LOCK_INIT(lock)    pthread_mutex_init((lock), NULL)
203 #define RTL_MEMORY_LOCK_DESTROY(lock) pthread_mutex_destroy((lock))
204 
205 #define RTL_MEMORY_LOCK_ACQUIRE(lock) pthread_mutex_lock((lock))
206 #define RTL_MEMORY_LOCK_RELEASE(lock) pthread_mutex_unlock((lock))
207 
208 #elif defined(SAL_W32)
209 
210 #define WIN32_LEAN_AND_MEAN
211 #ifdef _MSC_VER
212 #pragma warning(push,1) /* disable warnings within system headers */
213 #endif
214 #include <windows.h>
215 #ifdef _MSC_VER
216 #pragma warning(pop)
217 #endif
218 
219 typedef CRITICAL_SECTION rtl_memory_lock_type;
220 
221 #define RTL_MEMORY_LOCK_INIT(lock)    InitializeCriticalSection((lock))
222 #define RTL_MEMORY_LOCK_DESTROY(lock) DeleteCriticalSection((lock))
223 
224 #define RTL_MEMORY_LOCK_ACQUIRE(lock) EnterCriticalSection((lock))
225 #define RTL_MEMORY_LOCK_RELEASE(lock) LeaveCriticalSection((lock))
226 
227 #else
228 #error Unknown platform
229 #endif /* SAL_UNX | SAL_W32 */
230 
231 
232 /** Cache creation flags.
233  *  @internal
234  */
235 #define RTL_CACHE_FLAG_NOMAGAZINE   (1 << 13) /* w/o magazine layer */
236 #define RTL_CACHE_FLAG_QUANTUMCACHE (2 << 13) /* used as arena quantum cache */
237 
238 
239 /** Valgrind support macros.
240  */
241 #if !defined(HAVE_MEMCHECK_H) || (OSL_DEBUG_LEVEL == 0)
242 #if !defined(NVALGRIND)
243 #define NVALGRIND 1
244 #endif /* ! NVALGRIND */
245 #endif /* ! HAVE_MEMCHECK_H || (OSL_DEBUG_LEVEL == 0) */
246 
247 #if defined(NVALGRIND)
248 #define VALGRIND_MAKE_MEM_UNDEFINED(addr, size)
249 #define VALGRIND_MAKE_MEM_DEFINED(addr, size)
250 #define VALGRIND_MALLOCLIKE_BLOCK(addr, sizeB, rzB, is_zeroed)
251 #define VALGRIND_FREELIKE_BLOCK(addr, rzB)
252 #define VALGRIND_CREATE_MEMPOOL(pool, rzB, is_zeroed)
253 #define VALGRIND_DESTROY_MEMPOOL(pool)
254 #define VALGRIND_MEMPOOL_ALLOC(pool, addr, size)
255 #define VALGRIND_MEMPOOL_FREE(pool, addr)
256 #elif defined(HAVE_MEMCHECK_H)
257 #include <memcheck.h>
258 #if !defined(FORCE_SYSALLOC)
259 #define FORCE_SYSALLOC 1
260 #endif /* !FORCE_SYSALLOC */
261 #endif /* NVALGRIND || HAVE_MEMCHECK_H */
262 
263 #ifdef __cplusplus
264 }
265 #endif
266 
267 #endif /* INCLUDED_RTL_ALLOC_IMPL_H */
268