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