Merge from mainline (163495:164578).
[official-gcc/graphite-test-results.git] / libobjc / objc-private / sarray.h
blobf81b08e71e756b3154dc831ea79c50dae873d37b
1 /* Sparse Arrays for Objective C dispatch tables
2 Copyright (C) 1993, 1995, 1996, 2004, 2009 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 #ifdef __cplusplus
43 extern "C" {
44 #endif /* __cplusplus */
46 extern int nbuckets; /* for stats */
47 extern int nindices;
48 extern int narrays;
49 extern int idxsize;
51 /* An unsigned integer of same size as a pointer */
52 #define SIZET_BITS (sizeof(size_t)*8)
54 #if defined(__sparc__) || defined(OBJC_SPARSE2)
55 #define PRECOMPUTE_SELECTORS
56 #endif
58 #ifdef OBJC_SPARSE3
60 /* Buckets are 8 words each */
61 #define BUCKET_BITS 3
62 #define BUCKET_SIZE (1<<BUCKET_BITS)
63 #define BUCKET_MASK (BUCKET_SIZE-1)
65 /* Indices are 16 words each */
66 #define INDEX_BITS 4
67 #define INDEX_SIZE (1<<INDEX_BITS)
68 #define INDEX_MASK (INDEX_SIZE-1)
70 #define INDEX_CAPACITY (BUCKET_SIZE*INDEX_SIZE)
72 #else /* OBJC_SPARSE2 */
74 /* Buckets are 32 words each */
75 #define BUCKET_BITS 5
76 #define BUCKET_SIZE (1<<BUCKET_BITS)
77 #define BUCKET_MASK (BUCKET_SIZE-1)
79 #endif /* OBJC_SPARSE2 */
81 typedef size_t sidx;
83 #ifdef PRECOMPUTE_SELECTORS
85 struct soffset {
86 #ifdef OBJC_SPARSE3
87 unsigned int unused : SIZET_BITS/4;
88 unsigned int eoffset : SIZET_BITS/4;
89 unsigned int boffset : SIZET_BITS/4;
90 unsigned int ioffset : SIZET_BITS/4;
91 #else /* OBJC_SPARSE2 */
92 #ifdef __sparc__
93 unsigned long boffset : (SIZET_BITS - 2) - BUCKET_BITS;
94 unsigned int eoffset : BUCKET_BITS;
95 unsigned int unused : 2;
96 #else
97 unsigned int boffset : SIZET_BITS/2;
98 unsigned int eoffset : SIZET_BITS/2;
99 #endif
100 #endif /* OBJC_SPARSE2 */
103 union sofftype {
104 struct soffset off;
105 sidx idx;
108 #endif /* not PRECOMPUTE_SELECTORS */
110 union sversion {
111 int version;
112 void *next_free;
115 struct sbucket {
116 void* elems[BUCKET_SIZE]; /* elements stored in array */
117 union sversion version; /* used for copy-on-write */
120 #ifdef OBJC_SPARSE3
122 struct sindex {
123 struct sbucket* buckets[INDEX_SIZE];
124 union sversion version; /* used for copy-on-write */
127 #endif /* OBJC_SPARSE3 */
129 struct sarray {
130 #ifdef OBJC_SPARSE3
131 struct sindex** indices;
132 struct sindex* empty_index;
133 #else /* OBJC_SPARSE2 */
134 struct sbucket** buckets;
135 #endif /* OBJC_SPARSE2 */
136 struct sbucket* empty_bucket;
137 union sversion version; /* used for copy-on-write */
138 short ref_count;
139 struct sarray* is_copy_of;
140 size_t capacity;
143 struct sarray* sarray_new(int, void* default_element);
144 void sarray_free(struct sarray*);
145 struct sarray* sarray_lazy_copy(struct sarray*);
146 void sarray_realloc(struct sarray*, int new_size);
147 void sarray_at_put(struct sarray*, sidx indx, void* elem);
148 void sarray_at_put_safe(struct sarray*, sidx indx, void* elem);
150 struct sarray* sarray_hard_copy(struct sarray*); /* ... like the name? */
151 void sarray_remove_garbage(void);
154 #ifdef PRECOMPUTE_SELECTORS
155 /* Transform soffset values to ints and vica verca */
156 static inline unsigned int
157 soffset_decode(sidx indx)
159 union sofftype x;
160 x.idx = indx;
161 #ifdef OBJC_SPARSE3
162 return x.off.eoffset
163 + (x.off.boffset*BUCKET_SIZE)
164 + (x.off.ioffset*INDEX_CAPACITY);
165 #else /* OBJC_SPARSE2 */
166 return x.off.eoffset + (x.off.boffset*BUCKET_SIZE);
167 #endif /* OBJC_SPARSE2 */
170 static inline sidx
171 soffset_encode(size_t offset)
173 union sofftype x;
174 x.off.eoffset = offset%BUCKET_SIZE;
175 #ifdef OBJC_SPARSE3
176 x.off.boffset = (offset/BUCKET_SIZE)%INDEX_SIZE;
177 x.off.ioffset = offset/INDEX_CAPACITY;
178 #else /* OBJC_SPARSE2 */
179 x.off.boffset = offset/BUCKET_SIZE;
180 #endif
181 return (sidx)x.idx;
184 #else /* not PRECOMPUTE_SELECTORS */
186 static inline size_t
187 soffset_decode(sidx indx)
189 return indx;
192 static inline sidx
193 soffset_encode(size_t offset)
195 return offset;
197 #endif /* not PRECOMPUTE_SELECTORS */
199 /* Get element from the Sparse array `array' at offset `indx' */
201 static inline void* sarray_get(struct sarray* array, sidx indx)
203 #ifdef PRECOMPUTE_SELECTORS
204 union sofftype x;
205 x.idx = indx;
206 #ifdef OBJC_SPARSE3
207 return
208 array->
209 indices[x.off.ioffset]->
210 buckets[x.off.boffset]->
211 elems[x.off.eoffset];
212 #else /* OBJC_SPARSE2 */
213 return array->buckets[x.off.boffset]->elems[x.off.eoffset];
214 #endif /* OBJC_SPARSE2 */
215 #else /* not PRECOMPUTE_SELECTORS */
216 #ifdef OBJC_SPARSE3
217 return array->
218 indices[indx/INDEX_CAPACITY]->
219 buckets[(indx/BUCKET_SIZE)%INDEX_SIZE]->
220 elems[indx%BUCKET_SIZE];
221 #else /* OBJC_SPARSE2 */
222 return array->buckets[indx/BUCKET_SIZE]->elems[indx%BUCKET_SIZE];
223 #endif /* not OBJC_SPARSE3 */
224 #endif /* not PRECOMPUTE_SELECTORS */
227 static inline void* sarray_get_safe(struct sarray* array, sidx indx)
229 if(soffset_decode(indx) < array->capacity)
230 return sarray_get(array, indx);
231 else
232 return (array->empty_bucket->elems[0]);
235 #ifdef __cplusplus
237 #endif /* __cplusplus */
239 #endif /* __sarray_INCLUDE_GNU */