annotate libobjc/objc-private/sarray.h @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Sparse Arrays for Objective C dispatch tables
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2 Copyright (C) 1993-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3 Contributed by Kresten Krab Thorup.
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 This file is part of GCC.
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 GCC is free software; you can redistribute it and/or modify
kono
parents:
diff changeset
8 it under the terms of the GNU General Public License as published by
kono
parents:
diff changeset
9 the Free Software Foundation; either version 3, or (at your option)
kono
parents:
diff changeset
10 any later version.
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 GCC is distributed in the hope that it will be useful,
kono
parents:
diff changeset
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
kono
parents:
diff changeset
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
kono
parents:
diff changeset
15 GNU General Public License for more details.
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 Under Section 7 of GPL version 3, you are granted additional
kono
parents:
diff changeset
18 permissions described in the GCC Runtime Library Exception, version
kono
parents:
diff changeset
19 3.1, as published by the Free Software Foundation.
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21 You should have received a copy of the GNU General Public License and
kono
parents:
diff changeset
22 a copy of the GCC Runtime Library Exception along with this program;
kono
parents:
diff changeset
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
kono
parents:
diff changeset
24 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
25
kono
parents:
diff changeset
26 #ifndef __sarray_INCLUDE_GNU
kono
parents:
diff changeset
27 #define __sarray_INCLUDE_GNU
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 #define OBJC_SPARSE2 /* 2-level sparse array. */
kono
parents:
diff changeset
30 /* #define OBJC_SPARSE3 */ /* 3-level sparse array. */
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 #ifdef OBJC_SPARSE2
kono
parents:
diff changeset
33 extern const char* __objc_sparse2_id;
kono
parents:
diff changeset
34 #endif
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
37 extern const char* __objc_sparse3_id;
kono
parents:
diff changeset
38 #endif
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 #include <stddef.h>
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 extern int nbuckets; /* for stats */
kono
parents:
diff changeset
43 extern int nindices;
kono
parents:
diff changeset
44 extern int narrays;
kono
parents:
diff changeset
45 extern int idxsize;
kono
parents:
diff changeset
46
kono
parents:
diff changeset
47 /* An unsigned integer of same size as a pointer. */
kono
parents:
diff changeset
48 #define SIZET_BITS (sizeof (size_t) * 8)
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 #if defined (__sparc__) || defined (OBJC_SPARSE2)
kono
parents:
diff changeset
51 #define PRECOMPUTE_SELECTORS
kono
parents:
diff changeset
52 #endif
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56 /* Buckets are 8 words each. */
kono
parents:
diff changeset
57 #define BUCKET_BITS 3
kono
parents:
diff changeset
58 #define BUCKET_SIZE (1 << BUCKET_BITS)
kono
parents:
diff changeset
59 #define BUCKET_MASK (BUCKET_SIZE - 1)
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 /* Indices are 16 words each. */
kono
parents:
diff changeset
62 #define INDEX_BITS 4
kono
parents:
diff changeset
63 #define INDEX_SIZE (1 << INDEX_BITS)
kono
parents:
diff changeset
64 #define INDEX_MASK (INDEX_SIZE - 1)
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 #define INDEX_CAPACITY (BUCKET_SIZE * INDEX_SIZE)
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 #else /* OBJC_SPARSE2 */
kono
parents:
diff changeset
69
kono
parents:
diff changeset
70 /* Buckets are 32 words each. */
kono
parents:
diff changeset
71 #define BUCKET_BITS 5
kono
parents:
diff changeset
72 #define BUCKET_SIZE (1 << BUCKET_BITS)
kono
parents:
diff changeset
73 #define BUCKET_MASK (BUCKET_SIZE - 1)
kono
parents:
diff changeset
74
kono
parents:
diff changeset
75 #endif /* OBJC_SPARSE2 */
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 typedef size_t sidx;
kono
parents:
diff changeset
78
kono
parents:
diff changeset
79 #ifdef PRECOMPUTE_SELECTORS
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81 struct soffset
kono
parents:
diff changeset
82 {
kono
parents:
diff changeset
83 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
84 unsigned int unused : SIZET_BITS / 4;
kono
parents:
diff changeset
85 unsigned int eoffset : SIZET_BITS / 4;
kono
parents:
diff changeset
86 unsigned int boffset : SIZET_BITS / 4;
kono
parents:
diff changeset
87 unsigned int ioffset : SIZET_BITS / 4;
kono
parents:
diff changeset
88 #else /* OBJC_SPARSE2 */
kono
parents:
diff changeset
89 #ifdef __sparc__
kono
parents:
diff changeset
90 unsigned long boffset : (SIZET_BITS - 2) - BUCKET_BITS;
kono
parents:
diff changeset
91 unsigned int eoffset : BUCKET_BITS;
kono
parents:
diff changeset
92 unsigned int unused : 2;
kono
parents:
diff changeset
93 #else
kono
parents:
diff changeset
94 unsigned int boffset : SIZET_BITS / 2;
kono
parents:
diff changeset
95 unsigned int eoffset : SIZET_BITS / 2;
kono
parents:
diff changeset
96 #endif
kono
parents:
diff changeset
97 #endif /* OBJC_SPARSE2 */
kono
parents:
diff changeset
98 };
kono
parents:
diff changeset
99
kono
parents:
diff changeset
100 union sofftype
kono
parents:
diff changeset
101 {
kono
parents:
diff changeset
102 struct soffset off;
kono
parents:
diff changeset
103 sidx idx;
kono
parents:
diff changeset
104 };
kono
parents:
diff changeset
105
kono
parents:
diff changeset
106 #endif /* not PRECOMPUTE_SELECTORS */
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108 union sversion
kono
parents:
diff changeset
109 {
kono
parents:
diff changeset
110 int version;
kono
parents:
diff changeset
111 void *next_free;
kono
parents:
diff changeset
112 };
kono
parents:
diff changeset
113
kono
parents:
diff changeset
114 struct sbucket
kono
parents:
diff changeset
115 {
kono
parents:
diff changeset
116 /* Elements stored in array. */
kono
parents:
diff changeset
117 void* elems[BUCKET_SIZE];
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 /* Used for copy-on-write. */
kono
parents:
diff changeset
120 union sversion version;
kono
parents:
diff changeset
121 };
kono
parents:
diff changeset
122
kono
parents:
diff changeset
123 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125 struct sindex
kono
parents:
diff changeset
126 {
kono
parents:
diff changeset
127 struct sbucket* buckets[INDEX_SIZE];
kono
parents:
diff changeset
128
kono
parents:
diff changeset
129 /* Used for copy-on-write. */
kono
parents:
diff changeset
130 union sversion version;
kono
parents:
diff changeset
131 };
kono
parents:
diff changeset
132
kono
parents:
diff changeset
133 #endif /* OBJC_SPARSE3 */
kono
parents:
diff changeset
134
kono
parents:
diff changeset
135 struct sarray
kono
parents:
diff changeset
136 {
kono
parents:
diff changeset
137 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
138 struct sindex** indices;
kono
parents:
diff changeset
139 struct sindex* empty_index;
kono
parents:
diff changeset
140 #else /* OBJC_SPARSE2 */
kono
parents:
diff changeset
141 struct sbucket** buckets;
kono
parents:
diff changeset
142 #endif /* OBJC_SPARSE2 */
kono
parents:
diff changeset
143 struct sbucket* empty_bucket;
kono
parents:
diff changeset
144
kono
parents:
diff changeset
145 /* Used for copy-on-write. */
kono
parents:
diff changeset
146 union sversion version;
kono
parents:
diff changeset
147
kono
parents:
diff changeset
148 short ref_count;
kono
parents:
diff changeset
149 struct sarray* is_copy_of;
kono
parents:
diff changeset
150 size_t capacity;
kono
parents:
diff changeset
151 };
kono
parents:
diff changeset
152
kono
parents:
diff changeset
153 struct sarray* sarray_new (int, void* default_element);
kono
parents:
diff changeset
154 void sarray_free (struct sarray*);
kono
parents:
diff changeset
155 struct sarray* sarray_lazy_copy (struct sarray*);
kono
parents:
diff changeset
156 void sarray_realloc (struct sarray*, int new_size);
kono
parents:
diff changeset
157 void sarray_at_put (struct sarray*, sidx indx, void* elem);
kono
parents:
diff changeset
158 void sarray_at_put_safe (struct sarray*, sidx indx, void* elem);
kono
parents:
diff changeset
159
kono
parents:
diff changeset
160 struct sarray* sarray_hard_copy (struct sarray*); /* ... like the name ? */
kono
parents:
diff changeset
161 void sarray_remove_garbage (void);
kono
parents:
diff changeset
162
kono
parents:
diff changeset
163
kono
parents:
diff changeset
164 #ifdef PRECOMPUTE_SELECTORS
kono
parents:
diff changeset
165 /* Transform soffset values to ints and vice versa. */
kono
parents:
diff changeset
166 static inline unsigned int
kono
parents:
diff changeset
167 soffset_decode (sidx indx)
kono
parents:
diff changeset
168 {
kono
parents:
diff changeset
169 union sofftype x;
kono
parents:
diff changeset
170 x.idx = indx;
kono
parents:
diff changeset
171 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
172 return x.off.eoffset
kono
parents:
diff changeset
173 + (x.off.boffset * BUCKET_SIZE)
kono
parents:
diff changeset
174 + (x.off.ioffset * INDEX_CAPACITY);
kono
parents:
diff changeset
175 #else /* OBJC_SPARSE2 */
kono
parents:
diff changeset
176 return x.off.eoffset + (x.off.boffset * BUCKET_SIZE);
kono
parents:
diff changeset
177 #endif /* OBJC_SPARSE2 */
kono
parents:
diff changeset
178 }
kono
parents:
diff changeset
179
kono
parents:
diff changeset
180 static inline sidx
kono
parents:
diff changeset
181 soffset_encode (size_t offset)
kono
parents:
diff changeset
182 {
kono
parents:
diff changeset
183 union sofftype x;
kono
parents:
diff changeset
184 x.off.eoffset = offset % BUCKET_SIZE;
kono
parents:
diff changeset
185 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
186 x.off.boffset = (offset / BUCKET_SIZE) % INDEX_SIZE;
kono
parents:
diff changeset
187 x.off.ioffset = offset / INDEX_CAPACITY;
kono
parents:
diff changeset
188 #else /* OBJC_SPARSE2 */
kono
parents:
diff changeset
189 x.off.boffset = offset / BUCKET_SIZE;
kono
parents:
diff changeset
190 #endif
kono
parents:
diff changeset
191 return (sidx)x.idx;
kono
parents:
diff changeset
192 }
kono
parents:
diff changeset
193
kono
parents:
diff changeset
194 #else /* not PRECOMPUTE_SELECTORS */
kono
parents:
diff changeset
195
kono
parents:
diff changeset
196 static inline size_t
kono
parents:
diff changeset
197 soffset_decode (sidx indx)
kono
parents:
diff changeset
198 {
kono
parents:
diff changeset
199 return indx;
kono
parents:
diff changeset
200 }
kono
parents:
diff changeset
201
kono
parents:
diff changeset
202 static inline sidx
kono
parents:
diff changeset
203 soffset_encode (size_t offset)
kono
parents:
diff changeset
204 {
kono
parents:
diff changeset
205 return offset;
kono
parents:
diff changeset
206 }
kono
parents:
diff changeset
207 #endif /* not PRECOMPUTE_SELECTORS */
kono
parents:
diff changeset
208
kono
parents:
diff changeset
209 /* Get element from the Sparse array `array' at offset `indx'. */
kono
parents:
diff changeset
210 static inline void* sarray_get (struct sarray* array, sidx indx)
kono
parents:
diff changeset
211 {
kono
parents:
diff changeset
212 #ifdef PRECOMPUTE_SELECTORS
kono
parents:
diff changeset
213 union sofftype x;
kono
parents:
diff changeset
214 x.idx = indx;
kono
parents:
diff changeset
215 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
216 return array->
kono
parents:
diff changeset
217 indices[x.off.ioffset]->
kono
parents:
diff changeset
218 buckets[x.off.boffset]->
kono
parents:
diff changeset
219 elems[x.off.eoffset];
kono
parents:
diff changeset
220 #else /* OBJC_SPARSE2 */
kono
parents:
diff changeset
221 return array->buckets[x.off.boffset]->elems[x.off.eoffset];
kono
parents:
diff changeset
222 #endif /* OBJC_SPARSE2 */
kono
parents:
diff changeset
223 #else /* not PRECOMPUTE_SELECTORS */
kono
parents:
diff changeset
224 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
225 return array->
kono
parents:
diff changeset
226 indices[indx / INDEX_CAPACITY]->
kono
parents:
diff changeset
227 buckets[(indx / BUCKET_SIZE) % INDEX_SIZE]->
kono
parents:
diff changeset
228 elems[indx % BUCKET_SIZE];
kono
parents:
diff changeset
229 #else /* OBJC_SPARSE2 */
kono
parents:
diff changeset
230 return array->buckets[indx / BUCKET_SIZE]->elems[indx % BUCKET_SIZE];
kono
parents:
diff changeset
231 #endif /* not OBJC_SPARSE3 */
kono
parents:
diff changeset
232 #endif /* not PRECOMPUTE_SELECTORS */
kono
parents:
diff changeset
233 }
kono
parents:
diff changeset
234
kono
parents:
diff changeset
235 static inline void* sarray_get_safe (struct sarray* array, sidx indx)
kono
parents:
diff changeset
236 {
kono
parents:
diff changeset
237 if (soffset_decode (indx) < array->capacity)
kono
parents:
diff changeset
238 return sarray_get (array, indx);
kono
parents:
diff changeset
239 else
kono
parents:
diff changeset
240 return (array->empty_bucket->elems[0]);
kono
parents:
diff changeset
241 }
kono
parents:
diff changeset
242
kono
parents:
diff changeset
243 #endif /* __sarray_INCLUDE_GNU */