OSDN Git Service

(union sversion): New.
[pf3gnuchains/gcc-fork.git] / gcc / objc / sarray.h
1 /* Sparse Arrays for Objective C dispatch tables
2    Copyright (C) 1993, 1995, 1996 Free Software Foundation, Inc.
3
4 Author: Kresten Krab Thorup
5
6 This file is part of GNU CC.
7
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23 /* As a special exception, if you link this library with files
24    compiled with GCC to produce an executable, this does not cause
25    the resulting executable to be covered by the GNU General Public License.
26    This exception does not however invalidate any other reasons why
27    the executable file might be covered by the GNU General Public License.  */
28
29 #ifndef __sarray_INCLUDE_GNU
30 #define __sarray_INCLUDE_GNU
31
32 #define OBJC_SPARSE2            /* 2-level sparse array */
33 /* #define OBJC_SPARSE3 */      /* 3-level sparse array */
34
35 #ifdef OBJC_SPARSE2
36 extern const char* __objc_sparse2_id;
37 #endif
38
39 #ifdef OBJC_SPARSE3
40 extern const char* __objc_sparse3_id;
41 #endif
42
43 #include <stddef.h>
44
45 #include "objc/thread.h"
46
47 extern int nbuckets;            /* for stats */
48 extern int nindices;
49 extern int narrays;
50 extern int idxsize;
51
52 #include <assert.h>
53
54 /* An unsigned integer of same size as a pointer */
55 #define SIZET_BITS (sizeof(size_t)*8)
56
57 #if defined(__sparc__) || defined(OBJC_SPARSE2)
58 #define PRECOMPUTE_SELECTORS
59 #endif
60
61 #ifdef OBJC_SPARSE3
62
63 /* Buckets are 8 words each */
64 #define BUCKET_BITS 3
65 #define BUCKET_SIZE (1<<BUCKET_BITS)
66 #define BUCKET_MASK (BUCKET_SIZE-1)
67
68 /* Indices are 16 words each */
69 #define INDEX_BITS 4
70 #define INDEX_SIZE (1<<INDEX_BITS)
71 #define INDEX_MASK (INDEX_SIZE-1)
72
73 #define INDEX_CAPACITY (BUCKET_SIZE*INDEX_SIZE)
74
75 #else /* OBJC_SPARSE2 */
76
77 /* Buckets are 32 words each */
78 #define BUCKET_BITS 5
79 #define BUCKET_SIZE (1<<BUCKET_BITS)
80 #define BUCKET_MASK (BUCKET_SIZE-1)
81
82 #endif /* OBJC_SPARSE2 */
83
84 typedef size_t sidx;
85
86 #ifdef PRECOMPUTE_SELECTORS
87
88 struct soffset {
89 #ifdef OBJC_SPARSE3
90   unsigned int unused : SIZET_BITS/4;
91   unsigned int eoffset : SIZET_BITS/4;
92   unsigned int boffset : SIZET_BITS/4;
93   unsigned int ioffset : SIZET_BITS/4;
94 #else /* OBJC_SPARSE2 */
95 #ifdef __sparc__
96   unsigned int boffset : (SIZET_BITS - 2) - BUCKET_BITS;
97   unsigned int eoffset : BUCKET_BITS;
98   unsigned int unused  : 2;
99 #else
100   unsigned int boffset : SIZET_BITS/2;
101   unsigned int eoffset : SIZET_BITS/2;
102 #endif
103 #endif /* OBJC_SPARSE2 */
104 };
105
106 union sofftype {
107   struct soffset off;
108   sidx idx;
109 };
110
111 #endif /* not PRECOMPUTE_SELECTORS */
112
113 void * __objc_xrealloc (void *optr, size_t size);
114 void * __objc_xmalloc (size_t size);
115
116 union sversion {
117   int   version;
118   void *next_free;
119 };
120
121 struct sbucket {
122   void* elems[BUCKET_SIZE];     /* elements stored in array */
123   union sversion        version;                /* used for copy-on-write */
124 };
125
126 #ifdef OBJC_SPARSE3
127
128 struct sindex {
129   struct sbucket* buckets[INDEX_SIZE];
130   union sversion        version;                /* used for copy-on-write */
131 };
132
133 #endif /* OBJC_SPARSE3 */
134
135 struct sarray {
136 #ifdef 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 */
144   short ref_count;
145   struct sarray* is_copy_of;
146   size_t capacity;
147 };
148
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 index, void* elem);
154 void sarray_at_put_safe(struct sarray*, sidx index, void* elem);
155
156 struct sarray* sarray_hard_copy(struct sarray*); /* ... like the name? */
157 void sarray_remove_garbage(void);
158 \f
159
160 #ifdef PRECOMPUTE_SELECTORS
161 /* Transform soffset values to ints and vica verca */
162 static inline unsigned int
163 soffset_decode(sidx index)
164 {
165   union sofftype x;
166   x.idx = index;
167 #ifdef OBJC_SPARSE3
168   return x.off.eoffset
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 */
174 }
175
176 static inline sidx
177 soffset_encode(size_t offset)
178 {
179   union sofftype x;
180   x.off.eoffset = offset%BUCKET_SIZE;
181 #ifdef OBJC_SPARSE3
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;
186 #endif
187   return (sidx)x.idx;
188 }
189
190 #else /* not PRECOMPUTE_SELECTORS */
191
192 static inline size_t
193 soffset_decode(sidx index)
194 {
195   return index;
196 }
197
198 static inline sidx
199 soffset_encode(size_t offset)
200 {
201   return offset;
202 }
203 #endif /* not PRECOMPUTE_SELECTORS */
204
205 /* Get element from the Sparse array `array' at offset `index' */
206
207 static inline void* sarray_get(struct sarray* array, sidx index)
208 {
209 #ifdef PRECOMPUTE_SELECTORS
210   union sofftype x;
211   x.idx = index;
212 #ifdef OBJC_SPARSE3
213   return 
214     array->
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 */
222 #ifdef OBJC_SPARSE3
223   return array->
224     indices[index/INDEX_CAPACITY]->
225       buckets[(index/BUCKET_SIZE)%INDEX_SIZE]->
226         elems[index%BUCKET_SIZE];
227 #else /* OBJC_SPARSE2 */
228   return array->buckets[index/BUCKET_SIZE]->elems[index%BUCKET_SIZE];
229 #endif /* not OBJC_SPARSE3 */
230 #endif /* not PRECOMPUTE_SELECTORS */
231 }
232
233 static inline void* sarray_get_safe(struct sarray* array, sidx index)
234 {
235   if(soffset_decode(index) < array->capacity)
236     return sarray_get(array, index);
237   else
238     return (array->empty_bucket->elems[0]);
239 }
240
241 #endif /* __sarray_INCLUDE_GNU */