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.
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 2, or (at your option)
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 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. */
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. */
28 #ifndef __sarray_INCLUDE_GNU
29 #define __sarray_INCLUDE_GNU
35 #endif /* __cplusplus */
37 #define OBJC_SPARSE2 /* 2-level sparse array */
38 /* #define OBJC_SPARSE3 */ /* 3-level sparse array */
41 extern const char* __objc_sparse2_id;
45 extern const char* __objc_sparse3_id;
50 extern int nbuckets; /* for stats */
57 /* An unsigned integer of same size as a pointer */
58 #define SIZET_BITS (sizeof(size_t)*8)
60 #if defined(__sparc__) || defined(OBJC_SPARSE2)
61 #define PRECOMPUTE_SELECTORS
66 /* Buckets are 8 words each */
68 #define BUCKET_SIZE (1<<BUCKET_BITS)
69 #define BUCKET_MASK (BUCKET_SIZE-1)
71 /* Indices are 16 words each */
73 #define INDEX_SIZE (1<<INDEX_BITS)
74 #define INDEX_MASK (INDEX_SIZE-1)
76 #define INDEX_CAPACITY (BUCKET_SIZE*INDEX_SIZE)
78 #else /* OBJC_SPARSE2 */
80 /* Buckets are 32 words each */
82 #define BUCKET_SIZE (1<<BUCKET_BITS)
83 #define BUCKET_MASK (BUCKET_SIZE-1)
85 #endif /* OBJC_SPARSE2 */
89 #ifdef PRECOMPUTE_SELECTORS
93 unsigned int unused : SIZET_BITS/4;
94 unsigned int eoffset : SIZET_BITS/4;
95 unsigned int boffset : SIZET_BITS/4;
96 unsigned int ioffset : SIZET_BITS/4;
97 #else /* OBJC_SPARSE2 */
99 unsigned long boffset : (SIZET_BITS - 2) - BUCKET_BITS;
100 unsigned int eoffset : BUCKET_BITS;
101 unsigned int unused : 2;
103 unsigned int boffset : SIZET_BITS/2;
104 unsigned int eoffset : SIZET_BITS/2;
106 #endif /* OBJC_SPARSE2 */
114 #endif /* not PRECOMPUTE_SELECTORS */
122 void* elems[BUCKET_SIZE]; /* elements stored in array */
123 union sversion version; /* used for copy-on-write */
129 struct sbucket* buckets[INDEX_SIZE];
130 union sversion version; /* used for copy-on-write */
133 #endif /* OBJC_SPARSE3 */
137 struct sindex** indices;
138 struct sindex* empty_index;
139 #else /* OBJC_SPARSE2 */
140 struct sbucket** buckets;
141 #endif /* OBJC_SPARSE2 */
142 struct sbucket* empty_bucket;
143 union sversion version; /* used for copy-on-write */
145 struct sarray* is_copy_of;
149 struct sarray* sarray_new(int, void* default_element);
150 void sarray_free(struct sarray*);
151 struct sarray* sarray_lazy_copy(struct sarray*);
152 void sarray_realloc(struct sarray*, int new_size);
153 void sarray_at_put(struct sarray*, sidx indx, void* elem);
154 void sarray_at_put_safe(struct sarray*, sidx indx, void* elem);
156 struct sarray* sarray_hard_copy(struct sarray*); /* ... like the name? */
157 void sarray_remove_garbage(void);
160 #ifdef PRECOMPUTE_SELECTORS
161 /* Transform soffset values to ints and vica verca */
162 static inline unsigned int
163 soffset_decode(sidx indx)
169 + (x.off.boffset*BUCKET_SIZE)
170 + (x.off.ioffset*INDEX_CAPACITY);
171 #else /* OBJC_SPARSE2 */
172 return x.off.eoffset + (x.off.boffset*BUCKET_SIZE);
173 #endif /* OBJC_SPARSE2 */
177 soffset_encode(size_t offset)
180 x.off.eoffset = offset%BUCKET_SIZE;
182 x.off.boffset = (offset/BUCKET_SIZE)%INDEX_SIZE;
183 x.off.ioffset = offset/INDEX_CAPACITY;
184 #else /* OBJC_SPARSE2 */
185 x.off.boffset = offset/BUCKET_SIZE;
190 #else /* not PRECOMPUTE_SELECTORS */
193 soffset_decode(sidx indx)
199 soffset_encode(size_t offset)
203 #endif /* not PRECOMPUTE_SELECTORS */
205 /* Get element from the Sparse array `array' at offset `indx' */
207 static inline void* sarray_get(struct sarray* array, sidx indx)
209 #ifdef PRECOMPUTE_SELECTORS
215 indices[x.off.ioffset]->
216 buckets[x.off.boffset]->
217 elems[x.off.eoffset];
218 #else /* OBJC_SPARSE2 */
219 return array->buckets[x.off.boffset]->elems[x.off.eoffset];
220 #endif /* OBJC_SPARSE2 */
221 #else /* not PRECOMPUTE_SELECTORS */
224 indices[indx/INDEX_CAPACITY]->
225 buckets[(indx/BUCKET_SIZE)%INDEX_SIZE]->
226 elems[indx%BUCKET_SIZE];
227 #else /* OBJC_SPARSE2 */
228 return array->buckets[indx/BUCKET_SIZE]->elems[indx%BUCKET_SIZE];
229 #endif /* not OBJC_SPARSE3 */
230 #endif /* not PRECOMPUTE_SELECTORS */
233 static inline void* sarray_get_safe(struct sarray* array, sidx indx)
235 if(soffset_decode(indx) < array->capacity)
236 return sarray_get(array, indx);
238 return (array->empty_bucket->elems[0]);
243 #endif /* __cplusplus */
245 #endif /* __sarray_INCLUDE_GNU */