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