Object.h: Move includes out of extern "C" blocks.
[gcc.git] / libobjc / objc / sarray.h
1 /* Sparse Arrays for Objective C dispatch tables
2 Copyright (C) 1993, 1995, 1996, 2004 Free Software Foundation, Inc.
3 Contributed by Kresten Krab Thorup.
4
5 This file is part of GCC.
6
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 2, or (at your option)
10 any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* As a special exception, if you link this library with files
23 compiled with GCC to produce an executable, this does not cause
24 the resulting executable to be covered by the GNU General Public License.
25 This exception does not however invalidate any other reasons why
26 the executable file might be covered by the GNU General Public License. */
27
28 #ifndef __sarray_INCLUDE_GNU
29 #define __sarray_INCLUDE_GNU
30
31 #include <objc/thr.h>
32
33 #define OBJC_SPARSE2 /* 2-level sparse array */
34 /* #define OBJC_SPARSE3 */ /* 3-level sparse array */
35
36 #ifdef OBJC_SPARSE2
37 extern const char* __objc_sparse2_id;
38 #endif
39
40 #ifdef OBJC_SPARSE3
41 extern const char* __objc_sparse3_id;
42 #endif
43
44 #include <stddef.h>
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif /* __cplusplus */
49
50 extern int nbuckets; /* for stats */
51 extern int nindices;
52 extern int narrays;
53 extern int idxsize;
54
55 #ifdef __cplusplus
56 }
57 #endif /* __cplusplus */
58
59
60 #include <assert.h>
61
62 /* An unsigned integer of same size as a pointer */
63 #define SIZET_BITS (sizeof(size_t)*8)
64
65 #if defined(__sparc__) || defined(OBJC_SPARSE2)
66 #define PRECOMPUTE_SELECTORS
67 #endif
68
69 #ifdef OBJC_SPARSE3
70
71 /* Buckets are 8 words each */
72 #define BUCKET_BITS 3
73 #define BUCKET_SIZE (1<<BUCKET_BITS)
74 #define BUCKET_MASK (BUCKET_SIZE-1)
75
76 /* Indices are 16 words each */
77 #define INDEX_BITS 4
78 #define INDEX_SIZE (1<<INDEX_BITS)
79 #define INDEX_MASK (INDEX_SIZE-1)
80
81 #define INDEX_CAPACITY (BUCKET_SIZE*INDEX_SIZE)
82
83 #else /* OBJC_SPARSE2 */
84
85 /* Buckets are 32 words each */
86 #define BUCKET_BITS 5
87 #define BUCKET_SIZE (1<<BUCKET_BITS)
88 #define BUCKET_MASK (BUCKET_SIZE-1)
89
90 #endif /* OBJC_SPARSE2 */
91
92 typedef size_t sidx;
93
94 #ifdef PRECOMPUTE_SELECTORS
95
96 struct soffset {
97 #ifdef OBJC_SPARSE3
98 unsigned int unused : SIZET_BITS/4;
99 unsigned int eoffset : SIZET_BITS/4;
100 unsigned int boffset : SIZET_BITS/4;
101 unsigned int ioffset : SIZET_BITS/4;
102 #else /* OBJC_SPARSE2 */
103 #ifdef __sparc__
104 unsigned long boffset : (SIZET_BITS - 2) - BUCKET_BITS;
105 unsigned int eoffset : BUCKET_BITS;
106 unsigned int unused : 2;
107 #else
108 unsigned int boffset : SIZET_BITS/2;
109 unsigned int eoffset : SIZET_BITS/2;
110 #endif
111 #endif /* OBJC_SPARSE2 */
112 };
113
114 union sofftype {
115 struct soffset off;
116 sidx idx;
117 };
118
119 #endif /* not PRECOMPUTE_SELECTORS */
120
121 union sversion {
122 int version;
123 void *next_free;
124 };
125
126 struct sbucket {
127 void* elems[BUCKET_SIZE]; /* elements stored in array */
128 union sversion version; /* used for copy-on-write */
129 };
130
131 #ifdef OBJC_SPARSE3
132
133 struct sindex {
134 struct sbucket* buckets[INDEX_SIZE];
135 union sversion version; /* used for copy-on-write */
136 };
137
138 #endif /* OBJC_SPARSE3 */
139
140 struct sarray {
141 #ifdef OBJC_SPARSE3
142 struct sindex** indices;
143 struct sindex* empty_index;
144 #else /* OBJC_SPARSE2 */
145 struct sbucket** buckets;
146 #endif /* OBJC_SPARSE2 */
147 struct sbucket* empty_bucket;
148 union sversion version; /* used for copy-on-write */
149 short ref_count;
150 struct sarray* is_copy_of;
151 size_t capacity;
152 };
153
154 #ifdef __cplusplus
155 extern "C" {
156 #endif /* __cplusplus */
157
158 struct sarray* sarray_new(int, void* default_element);
159 void sarray_free(struct sarray*);
160 struct sarray* sarray_lazy_copy(struct sarray*);
161 void sarray_realloc(struct sarray*, int new_size);
162 void sarray_at_put(struct sarray*, sidx indx, void* elem);
163 void sarray_at_put_safe(struct sarray*, sidx indx, void* elem);
164
165 struct sarray* sarray_hard_copy(struct sarray*); /* ... like the name? */
166 void sarray_remove_garbage(void);
167 \f
168
169 #ifdef PRECOMPUTE_SELECTORS
170 /* Transform soffset values to ints and vica verca */
171 static inline unsigned int
172 soffset_decode(sidx indx)
173 {
174 union sofftype x;
175 x.idx = indx;
176 #ifdef OBJC_SPARSE3
177 return x.off.eoffset
178 + (x.off.boffset*BUCKET_SIZE)
179 + (x.off.ioffset*INDEX_CAPACITY);
180 #else /* OBJC_SPARSE2 */
181 return x.off.eoffset + (x.off.boffset*BUCKET_SIZE);
182 #endif /* OBJC_SPARSE2 */
183 }
184
185 static inline sidx
186 soffset_encode(size_t offset)
187 {
188 union sofftype x;
189 x.off.eoffset = offset%BUCKET_SIZE;
190 #ifdef OBJC_SPARSE3
191 x.off.boffset = (offset/BUCKET_SIZE)%INDEX_SIZE;
192 x.off.ioffset = offset/INDEX_CAPACITY;
193 #else /* OBJC_SPARSE2 */
194 x.off.boffset = offset/BUCKET_SIZE;
195 #endif
196 return (sidx)x.idx;
197 }
198
199 #else /* not PRECOMPUTE_SELECTORS */
200
201 static inline size_t
202 soffset_decode(sidx indx)
203 {
204 return indx;
205 }
206
207 static inline sidx
208 soffset_encode(size_t offset)
209 {
210 return offset;
211 }
212 #endif /* not PRECOMPUTE_SELECTORS */
213
214 /* Get element from the Sparse array `array' at offset `indx' */
215
216 static inline void* sarray_get(struct sarray* array, sidx indx)
217 {
218 #ifdef PRECOMPUTE_SELECTORS
219 union sofftype x;
220 x.idx = indx;
221 #ifdef OBJC_SPARSE3
222 return
223 array->
224 indices[x.off.ioffset]->
225 buckets[x.off.boffset]->
226 elems[x.off.eoffset];
227 #else /* OBJC_SPARSE2 */
228 return array->buckets[x.off.boffset]->elems[x.off.eoffset];
229 #endif /* OBJC_SPARSE2 */
230 #else /* not PRECOMPUTE_SELECTORS */
231 #ifdef OBJC_SPARSE3
232 return array->
233 indices[indx/INDEX_CAPACITY]->
234 buckets[(indx/BUCKET_SIZE)%INDEX_SIZE]->
235 elems[indx%BUCKET_SIZE];
236 #else /* OBJC_SPARSE2 */
237 return array->buckets[indx/BUCKET_SIZE]->elems[indx%BUCKET_SIZE];
238 #endif /* not OBJC_SPARSE3 */
239 #endif /* not PRECOMPUTE_SELECTORS */
240 }
241
242 static inline void* sarray_get_safe(struct sarray* array, sidx indx)
243 {
244 if(soffset_decode(indx) < array->capacity)
245 return sarray_get(array, indx);
246 else
247 return (array->empty_bucket->elems[0]);
248 }
249
250 #ifdef __cplusplus
251 }
252 #endif /* __cplusplus */
253
254 #endif /* __sarray_INCLUDE_GNU */