Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef INCLUDED_SAL_RTL_ALLOC_IMPL_HXX
21 : #define INCLUDED_SAL_RTL_ALLOC_IMPL_HXX
22 :
23 : #include "sal/types.h"
24 :
25 : /** Alignment macros
26 : */
27 : #if SAL_TYPES_ALIGNMENT4 > 1
28 : #define RTL_MEMORY_ALIGNMENT_4 SAL_TYPES_ALIGNMENT4
29 : #else
30 : #define RTL_MEMORY_ALIGNMENT_4 sizeof(int)
31 : #endif /* SAL_TYPES_ALIGNMENT4 */
32 :
33 : #if SAL_TYPES_ALIGNMENT8 > 1
34 : #define RTL_MEMORY_ALIGNMENT_8 SAL_TYPES_ALIGNMENT8
35 : #else
36 : #define RTL_MEMORY_ALIGNMENT_8 sizeof(void*)
37 : #endif /* SAL_TYPES_ALIGNMENT8 */
38 :
39 : #if 0 /* @@@ */
40 : #define RTL_MEMORY_ALIGNMENT_1 8
41 : #define RTL_MEMORY_ALIGNMENT_2 (sizeof(void*) * 2)
42 : #endif /* @@@ */
43 :
44 : #define RTL_MEMORY_ALIGN(value, align) (((value) + ((align) - 1)) & ~((align) - 1))
45 :
46 : #define RTL_MEMORY_ISP2(value) (((value) & ((value) - 1)) == 0)
47 : #define RTL_MEMORY_P2ALIGN(value, align) ((value) & -(sal_IntPtr)(align))
48 :
49 : #define RTL_MEMORY_P2ROUNDUP(value, align) \
50 : (-(-(sal_IntPtr)(value) & -(sal_IntPtr)(align)))
51 : #define RTL_MEMORY_P2END(value, align) \
52 : (-(~(sal_IntPtr)(value) & -(sal_IntPtr)(align)))
53 :
54 : /** printf() format specifier(s)
55 : * (from C90 <sys/int_fmtio.h>)
56 : */
57 : #ifndef PRIu64
58 : #if defined(_MSC_VER)
59 : #define PRIu64 "I64u"
60 : #else /* !_MSC_VER */
61 : #define PRIu64 "llu"
62 : #endif /* !_MSC_VER */
63 : #endif /* PRIu64 */
64 :
65 : /** highbit(): log2() + 1
66 : * (complexity O(1))
67 : */
68 : static inline int
69 3693483 : highbit(sal_Size n)
70 : {
71 3693483 : int k = 1;
72 :
73 3693483 : if (n == 0)
74 0 : return (0);
75 : #if SAL_TYPES_SIZEOFLONG == 8
76 3693483 : if (n & 0xffffffff00000000ul)
77 0 : k |= 32, n >>= 32;
78 : #endif
79 3693483 : if (n & 0xffff0000)
80 252587 : k |= 16, n >>= 16;
81 3693483 : if (n & 0xff00)
82 3254634 : k |= 8, n >>= 8;
83 3693483 : if (n & 0xf0)
84 2621972 : k |= 4, n >>= 4;
85 3693483 : if (n & 0x0c)
86 3017520 : k |= 2, n >>= 2;
87 3693483 : if (n & 0x02)
88 1820532 : k++;
89 :
90 3693483 : return (k);
91 : }
92 :
93 : /** lowbit(): find first bit set
94 : * (complexity O(1))
95 : */
96 : static inline int
97 1690890 : lowbit(sal_Size n)
98 : {
99 1690890 : int k = 1;
100 :
101 1690890 : if (n == 0)
102 876880 : return (0);
103 : #if SAL_TYPES_SIZEOFLONG == 8
104 814010 : if (!(n & 0xffffffff))
105 0 : k |= 32, n >>= 32;
106 : #endif
107 814010 : if (!(n & 0xffff))
108 116450 : k |= 16, n >>= 16;
109 814010 : if (!(n & 0xff))
110 697560 : k |= 8, n >>= 8;
111 814010 : if (!(n & 0xf))
112 619005 : k |= 4, n >>= 4;
113 814010 : if (!(n & 0x3))
114 682666 : k |= 2, n >>= 2;
115 814010 : if (!(n & 0x1))
116 608468 : k++;
117 814010 : return (k);
118 : }
119 :
120 : /** Queue manipulation macros
121 : * (doubly linked circular list)
122 : * (complexity O(1))
123 : */
124 : #define QUEUE_STARTED_NAMED(entry, name) \
125 : (((entry)->m_##name##next == (entry)) && ((entry)->m_##name##prev == (entry)))
126 :
127 : #define QUEUE_START_NAMED(entry, name) \
128 : { \
129 : (entry)->m_##name##next = (entry); \
130 : (entry)->m_##name##prev = (entry); \
131 : }
132 :
133 : #define QUEUE_REMOVE_NAMED(entry, name) \
134 : { \
135 : (entry)->m_##name##prev->m_##name##next = (entry)->m_##name##next; \
136 : (entry)->m_##name##next->m_##name##prev = (entry)->m_##name##prev; \
137 : QUEUE_START_NAMED(entry, name); \
138 : }
139 :
140 : #define QUEUE_INSERT_HEAD_NAMED(head, entry, name) \
141 : { \
142 : (entry)->m_##name##prev = (head); \
143 : (entry)->m_##name##next = (head)->m_##name##next; \
144 : (head)->m_##name##next = (entry); \
145 : (entry)->m_##name##next->m_##name##prev = (entry); \
146 : }
147 :
148 : #define QUEUE_INSERT_TAIL_NAMED(head, entry, name) \
149 : { \
150 : (entry)->m_##name##next = (head); \
151 : (entry)->m_##name##prev = (head)->m_##name##prev; \
152 : (head)->m_##name##prev = (entry); \
153 : (entry)->m_##name##prev->m_##name##next = (entry); \
154 : }
155 :
156 : /** rtl_memory_lock_type
157 : * (platform dependent)
158 : */
159 : #if defined(SAL_UNX)
160 :
161 : #include <unistd.h>
162 : #include <pthread.h>
163 :
164 : typedef pthread_mutex_t rtl_memory_lock_type;
165 :
166 : #define RTL_MEMORY_LOCK_INIT(lock) pthread_mutex_init((lock), NULL)
167 : #define RTL_MEMORY_LOCK_DESTROY(lock) pthread_mutex_destroy((lock))
168 :
169 : #define RTL_MEMORY_LOCK_ACQUIRE(lock) pthread_mutex_lock((lock))
170 : #define RTL_MEMORY_LOCK_RELEASE(lock) pthread_mutex_unlock((lock))
171 :
172 : #elif defined(SAL_W32)
173 :
174 : #define WIN32_LEAN_AND_MEAN
175 : #ifdef _MSC_VER
176 : #pragma warning(push,1) /* disable warnings within system headers */
177 : #endif
178 : #include <windows.h>
179 : #ifdef _MSC_VER
180 : #pragma warning(pop)
181 : #endif
182 :
183 : typedef CRITICAL_SECTION rtl_memory_lock_type;
184 :
185 : #define RTL_MEMORY_LOCK_INIT(lock) InitializeCriticalSection((lock))
186 : #define RTL_MEMORY_LOCK_DESTROY(lock) DeleteCriticalSection((lock))
187 :
188 : #define RTL_MEMORY_LOCK_ACQUIRE(lock) EnterCriticalSection((lock))
189 : #define RTL_MEMORY_LOCK_RELEASE(lock) LeaveCriticalSection((lock))
190 :
191 : #else
192 : #error Unknown platform
193 : #endif /* SAL_UNX | SAL_W32 */
194 :
195 : /** Cache creation flags.
196 : * @internal
197 : */
198 : #define RTL_CACHE_FLAG_NOMAGAZINE (1 << 13) /* w/o magazine layer */
199 : #define RTL_CACHE_FLAG_QUANTUMCACHE (2 << 13) /* used as arena quantum cache */
200 :
201 : typedef enum { AMode_CUSTOM, AMode_SYSTEM, AMode_UNSET } AllocMode;
202 :
203 : extern AllocMode alloc_mode;
204 :
205 : #endif // INCLUDED_SAL_RTL_ALLOC_IMPL_HXX
206 :
207 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|