hppa: Always enable PIE on 64-bit target
[official-gcc.git] / libobjc / objc-private / sarray.h
blob460407b995021fc369512e33dce7bc5eac8ba3af
1 /* Sparse Arrays for Objective C dispatch tables
2 Copyright (C) 1993-2024 Free Software Foundation, Inc.
3 Contributed by Kresten Krab Thorup.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #ifndef __sarray_INCLUDE_GNU
27 #define __sarray_INCLUDE_GNU
29 #define OBJC_SPARSE2 /* 2-level sparse array. */
30 /* #define OBJC_SPARSE3 */ /* 3-level sparse array. */
32 #ifdef OBJC_SPARSE2
33 extern const char* __objc_sparse2_id;
34 #endif
36 #ifdef OBJC_SPARSE3
37 extern const char* __objc_sparse3_id;
38 #endif
40 #include <stddef.h>
42 extern int nbuckets; /* for stats */
43 extern int nindices;
44 extern int narrays;
45 extern int idxsize;
47 /* An unsigned integer of same size as a pointer. */
48 #define SIZET_BITS (sizeof (size_t) * 8)
50 #if defined (__sparc__) || defined (OBJC_SPARSE2)
51 #define PRECOMPUTE_SELECTORS
52 #endif
54 #ifdef OBJC_SPARSE3
56 /* Buckets are 8 words each. */
57 #define BUCKET_BITS 3
58 #define BUCKET_SIZE (1 << BUCKET_BITS)
59 #define BUCKET_MASK (BUCKET_SIZE - 1)
61 /* Indices are 16 words each. */
62 #define INDEX_BITS 4
63 #define INDEX_SIZE (1 << INDEX_BITS)
64 #define INDEX_MASK (INDEX_SIZE - 1)
66 #define INDEX_CAPACITY (BUCKET_SIZE * INDEX_SIZE)
68 #else /* OBJC_SPARSE2 */
70 /* Buckets are 32 words each. */
71 #define BUCKET_BITS 5
72 #define BUCKET_SIZE (1 << BUCKET_BITS)
73 #define BUCKET_MASK (BUCKET_SIZE - 1)
75 #endif /* OBJC_SPARSE2 */
77 typedef size_t sidx;
79 #ifdef PRECOMPUTE_SELECTORS
81 struct soffset
83 #ifdef OBJC_SPARSE3
84 unsigned int unused : SIZET_BITS / 4;
85 unsigned int eoffset : SIZET_BITS / 4;
86 unsigned int boffset : SIZET_BITS / 4;
87 unsigned int ioffset : SIZET_BITS / 4;
88 #else /* OBJC_SPARSE2 */
89 #ifdef __sparc__
90 unsigned long boffset : (SIZET_BITS - 2) - BUCKET_BITS;
91 unsigned int eoffset : BUCKET_BITS;
92 unsigned int unused : 2;
93 #else
94 unsigned int boffset : SIZET_BITS / 2;
95 unsigned int eoffset : SIZET_BITS / 2;
96 #endif
97 #endif /* OBJC_SPARSE2 */
100 union sofftype
102 struct soffset off;
103 sidx idx;
106 #endif /* not PRECOMPUTE_SELECTORS */
108 union sversion
110 int version;
111 void *next_free;
114 struct sbucket
116 /* Elements stored in array. */
117 void* elems[BUCKET_SIZE];
119 /* Used for copy-on-write. */
120 union sversion version;
123 #ifdef OBJC_SPARSE3
125 struct sindex
127 struct sbucket* buckets[INDEX_SIZE];
129 /* Used for copy-on-write. */
130 union sversion version;
133 #endif /* OBJC_SPARSE3 */
135 struct sarray
137 #ifdef OBJC_SPARSE3
138 struct sindex** indices;
139 struct sindex* empty_index;
140 #else /* OBJC_SPARSE2 */
141 struct sbucket** buckets;
142 #endif /* OBJC_SPARSE2 */
143 struct sbucket* empty_bucket;
145 /* Used for copy-on-write. */
146 union sversion version;
148 short ref_count;
149 struct sarray* is_copy_of;
150 size_t capacity;
153 struct sarray* sarray_new (int, void* default_element);
154 void sarray_free (struct sarray*);
155 struct sarray* sarray_lazy_copy (struct sarray*);
156 void sarray_realloc (struct sarray*, int new_size);
157 void sarray_at_put (struct sarray*, sidx indx, void* elem);
158 void sarray_at_put_safe (struct sarray*, sidx indx, void* elem);
160 struct sarray* sarray_hard_copy (struct sarray*); /* ... like the name ? */
161 void sarray_remove_garbage (void);
164 #ifdef PRECOMPUTE_SELECTORS
165 /* Transform soffset values to ints and vice versa. */
166 static inline unsigned int
167 soffset_decode (sidx indx)
169 union sofftype x;
170 x.idx = indx;
171 #ifdef OBJC_SPARSE3
172 return x.off.eoffset
173 + (x.off.boffset * BUCKET_SIZE)
174 + (x.off.ioffset * INDEX_CAPACITY);
175 #else /* OBJC_SPARSE2 */
176 return x.off.eoffset + (x.off.boffset * BUCKET_SIZE);
177 #endif /* OBJC_SPARSE2 */
180 static inline sidx
181 soffset_encode (size_t offset)
183 union sofftype x;
184 x.off.eoffset = offset % BUCKET_SIZE;
185 #ifdef OBJC_SPARSE3
186 x.off.boffset = (offset / BUCKET_SIZE) % INDEX_SIZE;
187 x.off.ioffset = offset / INDEX_CAPACITY;
188 #else /* OBJC_SPARSE2 */
189 x.off.boffset = offset / BUCKET_SIZE;
190 #endif
191 return (sidx)x.idx;
194 #else /* not PRECOMPUTE_SELECTORS */
196 static inline size_t
197 soffset_decode (sidx indx)
199 return indx;
202 static inline sidx
203 soffset_encode (size_t offset)
205 return offset;
207 #endif /* not PRECOMPUTE_SELECTORS */
209 /* Get element from the Sparse array `array' at offset `indx'. */
210 static inline void* sarray_get (struct sarray* array, sidx indx)
212 #ifdef PRECOMPUTE_SELECTORS
213 union sofftype x;
214 x.idx = indx;
215 #ifdef OBJC_SPARSE3
216 return array->
217 indices[x.off.ioffset]->
218 buckets[x.off.boffset]->
219 elems[x.off.eoffset];
220 #else /* OBJC_SPARSE2 */
221 return array->buckets[x.off.boffset]->elems[x.off.eoffset];
222 #endif /* OBJC_SPARSE2 */
223 #else /* not PRECOMPUTE_SELECTORS */
224 #ifdef OBJC_SPARSE3
225 return array->
226 indices[indx / INDEX_CAPACITY]->
227 buckets[(indx / BUCKET_SIZE) % INDEX_SIZE]->
228 elems[indx % BUCKET_SIZE];
229 #else /* OBJC_SPARSE2 */
230 return array->buckets[indx / BUCKET_SIZE]->elems[indx % BUCKET_SIZE];
231 #endif /* not OBJC_SPARSE3 */
232 #endif /* not PRECOMPUTE_SELECTORS */
235 static inline void* sarray_get_safe (struct sarray* array, sidx indx)
237 if (soffset_decode (indx) < array->capacity)
238 return sarray_get (array, indx);
239 else
240 return (array->empty_bucket->elems[0]);
243 #endif /* __sarray_INCLUDE_GNU */