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