1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: alloc_impl.h,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef INCLUDED_RTL_ALLOC_IMPL_H
32 #define INCLUDED_RTL_ALLOC_IMPL_H
34 #include "sal/types.h"
43 #if SAL_TYPES_ALIGNMENT4 > 1
44 #define RTL_MEMORY_ALIGNMENT_4 SAL_TYPES_ALIGNMENT4
46 #define RTL_MEMORY_ALIGNMENT_4 sizeof(int)
47 #endif /* SAL_TYPES_ALIGNMENT4 */
49 #if SAL_TYPES_ALIGNMENT8 > 1
50 #define RTL_MEMORY_ALIGNMENT_8 SAL_TYPES_ALIGNMENT8
52 #define RTL_MEMORY_ALIGNMENT_8 sizeof(void*)
53 #endif /* SAL_TYPES_ALIGNMENT8 */
56 #define RTL_MEMORY_ALIGNMENT_1 8
57 #define RTL_MEMORY_ALIGNMENT_2 (sizeof(void*) * 2)
60 #define RTL_MEMORY_ALIGN(value, align) (((value) + ((align) - 1)) & ~((align) - 1))
62 #define RTL_MEMORY_ISP2(value) (((value) & ((value) - 1)) == 0)
63 #define RTL_MEMORY_P2ALIGN(value, align) ((value) & -(sal_IntPtr)(align))
65 #define RTL_MEMORY_P2ROUNDUP(value, align) \
66 (-(-(sal_IntPtr)(value) & -(sal_IntPtr)(align)))
67 #define RTL_MEMORY_P2END(value, align) \
68 (-(~(sal_IntPtr)(value) & -(sal_IntPtr)(align)))
71 /** Function inlining macros
72 * (compiler dependent)
74 #ifndef RTL_MEMORY_INLINE
76 #define RTL_MEMORY_INLINE __inline__
77 #elif defined(_MSC_VER)
78 #define RTL_MEMORY_INLINE __inline
80 #define RTL_MEMORY_INLINE
81 #endif /* __GNUC__ || _MSC_VER */
82 #endif /* RTL_MEMORY_INLINE */
85 /** printf() format specifier(s)
86 * (from C90 <sys/int_fmtio.h>)
93 #endif /* !_MSC_VER */
97 /** highbit(): log2() + 1
100 static RTL_MEMORY_INLINE
int
107 #if SAL_TYPES_SIZEOFLONG == 8
108 if (n
& 0xffffffff00000000ul
)
125 #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
126 #pragma inline(highbit)
127 #endif /* __SUNPRO_C */
130 /** lowbit(): find first bit set
133 static RTL_MEMORY_INLINE
int
140 #if SAL_TYPES_SIZEOFLONG == 8
141 if (!(n
& 0xffffffff))
157 #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
158 #pragma inline(lowbit)
159 #endif /* __SUNPRO_C */
162 /** Queue manipulation macros
163 * (doubly linked circular list)
166 #define QUEUE_STARTED_NAMED(entry, name) \
167 (((entry)->m_##name##next == (entry)) && ((entry)->m_##name##prev == (entry)))
169 #define QUEUE_START_NAMED(entry, name) \
171 (entry)->m_##name##next = (entry); \
172 (entry)->m_##name##prev = (entry); \
175 #define QUEUE_REMOVE_NAMED(entry, name) \
177 (entry)->m_##name##prev->m_##name##next = (entry)->m_##name##next; \
178 (entry)->m_##name##next->m_##name##prev = (entry)->m_##name##prev; \
179 QUEUE_START_NAMED(entry, name); \
182 #define QUEUE_INSERT_HEAD_NAMED(head, entry, name) \
184 (entry)->m_##name##prev = (head); \
185 (entry)->m_##name##next = (head)->m_##name##next; \
186 (head)->m_##name##next = (entry); \
187 (entry)->m_##name##next->m_##name##prev = (entry); \
190 #define QUEUE_INSERT_TAIL_NAMED(head, entry, name) \
192 (entry)->m_##name##next = (head); \
193 (entry)->m_##name##prev = (head)->m_##name##prev; \
194 (head)->m_##name##prev = (entry); \
195 (entry)->m_##name##prev->m_##name##next = (entry); \
199 /** rtl_memory_lock_type
200 * (platform dependent)
202 #if defined(SAL_UNX) || defined(SAL_OS2)
207 typedef pthread_mutex_t rtl_memory_lock_type
;
209 #define RTL_MEMORY_LOCK_INIT(lock) pthread_mutex_init((lock), NULL)
210 #define RTL_MEMORY_LOCK_DESTROY(lock) pthread_mutex_destroy((lock))
212 #define RTL_MEMORY_LOCK_ACQUIRE(lock) pthread_mutex_lock((lock))
213 #define RTL_MEMORY_LOCK_RELEASE(lock) pthread_mutex_unlock((lock))
215 #elif defined(SAL_W32)
217 #define WIN32_LEAN_AND_MEAN
219 #pragma warning(push,1) /* disable warnings within system headers */
226 typedef CRITICAL_SECTION rtl_memory_lock_type
;
228 #define RTL_MEMORY_LOCK_INIT(lock) InitializeCriticalSection((lock))
229 #define RTL_MEMORY_LOCK_DESTROY(lock) DeleteCriticalSection((lock))
231 #define RTL_MEMORY_LOCK_ACQUIRE(lock) EnterCriticalSection((lock))
232 #define RTL_MEMORY_LOCK_RELEASE(lock) LeaveCriticalSection((lock))
235 #error Unknown platform
236 #endif /* SAL_UNX | SAL_W32 */
239 /** Cache creation flags.
242 #define RTL_CACHE_FLAG_NOMAGAZINE (1 << 13) /* w/o magazine layer */
243 #define RTL_CACHE_FLAG_QUANTUMCACHE (2 << 13) /* used as arena quantum cache */
250 #endif /* INCLUDED_RTL_ALLOC_IMPL_H */