1 /* Sparse Arrays for Objective C dispatch tables
2 Copyright (C) 1993, 1995 Free Software Foundation, Inc.
4 Author: Kresten Krab Thorup
6 This file is part of GNU CC.
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)
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.
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. */
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. */
29 #ifndef __sarray_INCLUDE_GNU
30 #define __sarray_INCLUDE_GNU
32 #define OBJC_SPARSE2 /* 2-level sparse array */
33 /* #define OBJC_SPARSE3 */ /* 3-level sparse array */
36 extern const char* __objc_sparse2_id
;
40 extern const char* __objc_sparse3_id
;
45 extern int nbuckets
; /* for stats */
52 /* An unsigned integer of same size as a pointer */
53 #define SIZET_BITS (sizeof(size_t)*8)
55 #if defined(__sparc__) || defined(OBJC_SPARSE2)
56 #define PRECOMPUTE_SELECTORS
61 /* Buckets are 8 words each */
63 #define BUCKET_SIZE (1<<BUCKET_BITS)
64 #define BUCKET_MASK (BUCKET_SIZE-1)
66 /* Indices are 16 words each */
68 #define INDEX_SIZE (1<<INDEX_BITS)
69 #define INDEX_MASK (INDEX_SIZE-1)
71 #define INDEX_CAPACITY (BUCKET_SIZE*INDEX_SIZE)
73 #else /* OBJC_SPARSE2 */
75 /* Buckets are 32 words each */
77 #define BUCKET_SIZE (1<<BUCKET_BITS)
78 #define BUCKET_MASK (BUCKET_SIZE-1)
80 #endif /* OBJC_SPARSE2 */
84 #ifdef PRECOMPUTE_SELECTORS
88 unsigned int unused
: SIZET_BITS
/4;
89 unsigned int eoffset
: SIZET_BITS
/4;
90 unsigned int boffset
: SIZET_BITS
/4;
91 unsigned int ioffset
: SIZET_BITS
/4;
92 #else /* OBJC_SPARSE2 */
94 unsigned int boffset
: (SIZET_BITS
- 2) - BUCKET_BITS
;
95 unsigned int eoffset
: BUCKET_BITS
;
96 unsigned int unused
: 2;
98 unsigned int boffset
: SIZET_BITS
/2;
99 unsigned int eoffset
: SIZET_BITS
/2;
101 #endif /* OBJC_SPARSE2 */
109 #endif /* not PRECOMPUTE_SELECTORS */
111 void * __objc_xrealloc (void *optr
, size_t size
);
112 void * __objc_xmalloc (size_t size
);
115 void* elems
[BUCKET_SIZE
]; /* elements stored in array */
116 short version
; /* used for copy-on-write */
122 struct sbucket
* buckets
[INDEX_SIZE
];
126 #endif /* OBJC_SPARSE3 */
130 struct sindex
** indices
;
131 struct sindex
* empty_index
;
132 #else /* OBJC_SPARSE2 */
133 struct sbucket
** buckets
;
134 #endif /* OBJC_SPARSE2 */
135 struct sbucket
* empty_bucket
;
138 struct sarray
* is_copy_of
;
142 struct sarray
* sarray_new(int, void* default_element
);
143 void sarray_free(struct sarray
*);
144 struct sarray
* sarray_lazy_copy(struct sarray
*);
145 struct sarray
* sarray_hard_copy(struct sarray
*); /* ... like the name? */
146 void sarray_realloc(struct sarray
*, int new_size
);
147 void sarray_at_put(struct sarray
*, sidx index
, void* elem
);
148 void sarray_at_put_safe(struct sarray
*, sidx index
, void* elem
);
151 #ifdef PRECOMPUTE_SELECTORS
152 /* Transform soffset values to ints and vica verca */
153 static inline unsigned int
154 soffset_decode(sidx index
)
160 + (x
.off
.boffset
*BUCKET_SIZE
)
161 + (x
.off
.ioffset
*INDEX_CAPACITY
);
162 #else /* OBJC_SPARSE2 */
163 return x
.off
.eoffset
+ (x
.off
.boffset
*BUCKET_SIZE
);
164 #endif /* OBJC_SPARSE2 */
168 soffset_encode(size_t offset
)
171 x
.off
.eoffset
= offset
%BUCKET_SIZE
;
173 x
.off
.boffset
= (offset
/BUCKET_SIZE
)%INDEX_SIZE
;
174 x
.off
.ioffset
= offset
/INDEX_CAPACITY
;
175 #else /* OBJC_SPARSE2 */
176 x
.off
.boffset
= offset
/BUCKET_SIZE
;
181 #else /* not PRECOMPUTE_SELECTORS */
184 soffset_decode(sidx index
)
190 soffset_encode(size_t offset
)
194 #endif /* not PRECOMPUTE_SELECTORS */
196 /* Get element from the Sparse array `array' at offset `index' */
198 static inline void* sarray_get(struct sarray
* array
, sidx index
)
200 #ifdef PRECOMPUTE_SELECTORS
206 indices
[x
.off
.ioffset
]->
207 buckets
[x
.off
.boffset
]->
208 elems
[x
.off
.eoffset
];
209 #else /* OBJC_SPARSE2 */
210 return array
->buckets
[x
.off
.boffset
]->elems
[x
.off
.eoffset
];
211 #endif /* OBJC_SPARSE2 */
212 #else /* not PRECOMPUTE_SELECTORS */
215 indices
[index
/INDEX_CAPACITY
]->
216 buckets
[(index
/BUCKET_SIZE
)%INDEX_SIZE
]->
217 elems
[index
%BUCKET_SIZE
];
218 #else /* OBJC_SPARSE2 */
219 return array
->buckets
[index
/BUCKET_SIZE
]->elems
[index
%BUCKET_SIZE
];
220 #endif /* not OBJC_SPARSE3 */
221 #endif /* not PRECOMPUTE_SELECTORS */
224 static inline void* sarray_get_safe(struct sarray
* array
, sidx index
)
226 if(soffset_decode(index
) < array
->capacity
)
227 return sarray_get(array
, index
);
229 return (array
->empty_bucket
->elems
[0]);
232 #endif /* __sarray_INCLUDE_GNU */