| 1 |
14 |
jlechner |
/* 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, 51 Franklin Street, Fifth Floor,
|
| 20 |
|
|
Boston, MA 02110-1301, 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 "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 |
|
|
#include <assert.h>
|
| 46 |
|
|
|
| 47 |
|
|
#ifdef __cplusplus
|
| 48 |
|
|
extern "C" {
|
| 49 |
|
|
#endif /* __cplusplus */
|
| 50 |
|
|
|
| 51 |
|
|
extern int nbuckets; /* for stats */
|
| 52 |
|
|
extern int nindices;
|
| 53 |
|
|
extern int narrays;
|
| 54 |
|
|
extern int idxsize;
|
| 55 |
|
|
|
| 56 |
|
|
/* An unsigned integer of same size as a pointer */
|
| 57 |
|
|
#define SIZET_BITS (sizeof(size_t)*8)
|
| 58 |
|
|
|
| 59 |
|
|
#if defined(__sparc__) || defined(OBJC_SPARSE2)
|
| 60 |
|
|
#define PRECOMPUTE_SELECTORS
|
| 61 |
|
|
#endif
|
| 62 |
|
|
|
| 63 |
|
|
#ifdef OBJC_SPARSE3
|
| 64 |
|
|
|
| 65 |
|
|
/* Buckets are 8 words each */
|
| 66 |
|
|
#define BUCKET_BITS 3
|
| 67 |
|
|
#define BUCKET_SIZE (1<<BUCKET_BITS)
|
| 68 |
|
|
#define BUCKET_MASK (BUCKET_SIZE-1)
|
| 69 |
|
|
|
| 70 |
|
|
/* Indices are 16 words each */
|
| 71 |
|
|
#define INDEX_BITS 4
|
| 72 |
|
|
#define INDEX_SIZE (1<<INDEX_BITS)
|
| 73 |
|
|
#define INDEX_MASK (INDEX_SIZE-1)
|
| 74 |
|
|
|
| 75 |
|
|
#define INDEX_CAPACITY (BUCKET_SIZE*INDEX_SIZE)
|
| 76 |
|
|
|
| 77 |
|
|
#else /* OBJC_SPARSE2 */
|
| 78 |
|
|
|
| 79 |
|
|
/* Buckets are 32 words each */
|
| 80 |
|
|
#define BUCKET_BITS 5
|
| 81 |
|
|
#define BUCKET_SIZE (1<<BUCKET_BITS)
|
| 82 |
|
|
#define BUCKET_MASK (BUCKET_SIZE-1)
|
| 83 |
|
|
|
| 84 |
|
|
#endif /* OBJC_SPARSE2 */
|
| 85 |
|
|
|
| 86 |
|
|
typedef size_t sidx;
|
| 87 |
|
|
|
| 88 |
|
|
#ifdef PRECOMPUTE_SELECTORS
|
| 89 |
|
|
|
| 90 |
|
|
struct soffset {
|
| 91 |
|
|
#ifdef OBJC_SPARSE3
|
| 92 |
|
|
unsigned int unused : SIZET_BITS/4;
|
| 93 |
|
|
unsigned int eoffset : SIZET_BITS/4;
|
| 94 |
|
|
unsigned int boffset : SIZET_BITS/4;
|
| 95 |
|
|
unsigned int ioffset : SIZET_BITS/4;
|
| 96 |
|
|
#else /* OBJC_SPARSE2 */
|
| 97 |
|
|
#ifdef __sparc__
|
| 98 |
|
|
unsigned long boffset : (SIZET_BITS - 2) - BUCKET_BITS;
|
| 99 |
|
|
unsigned int eoffset : BUCKET_BITS;
|
| 100 |
|
|
unsigned int unused : 2;
|
| 101 |
|
|
#else
|
| 102 |
|
|
unsigned int boffset : SIZET_BITS/2;
|
| 103 |
|
|
unsigned int eoffset : SIZET_BITS/2;
|
| 104 |
|
|
#endif
|
| 105 |
|
|
#endif /* OBJC_SPARSE2 */
|
| 106 |
|
|
};
|
| 107 |
|
|
|
| 108 |
|
|
union sofftype {
|
| 109 |
|
|
struct soffset off;
|
| 110 |
|
|
sidx idx;
|
| 111 |
|
|
};
|
| 112 |
|
|
|
| 113 |
|
|
#endif /* not PRECOMPUTE_SELECTORS */
|
| 114 |
|
|
|
| 115 |
|
|
union sversion {
|
| 116 |
|
|
int version;
|
| 117 |
|
|
void *next_free;
|
| 118 |
|
|
};
|
| 119 |
|
|
|
| 120 |
|
|
struct sbucket {
|
| 121 |
|
|
void* elems[BUCKET_SIZE]; /* elements stored in array */
|
| 122 |
|
|
union sversion version; /* used for copy-on-write */
|
| 123 |
|
|
};
|
| 124 |
|
|
|
| 125 |
|
|
#ifdef OBJC_SPARSE3
|
| 126 |
|
|
|
| 127 |
|
|
struct sindex {
|
| 128 |
|
|
struct sbucket* buckets[INDEX_SIZE];
|
| 129 |
|
|
union sversion version; /* used for copy-on-write */
|
| 130 |
|
|
};
|
| 131 |
|
|
|
| 132 |
|
|
#endif /* OBJC_SPARSE3 */
|
| 133 |
|
|
|
| 134 |
|
|
struct sarray {
|
| 135 |
|
|
#ifdef OBJC_SPARSE3
|
| 136 |
|
|
struct sindex** indices;
|
| 137 |
|
|
struct sindex* empty_index;
|
| 138 |
|
|
#else /* OBJC_SPARSE2 */
|
| 139 |
|
|
struct sbucket** buckets;
|
| 140 |
|
|
#endif /* OBJC_SPARSE2 */
|
| 141 |
|
|
struct sbucket* empty_bucket;
|
| 142 |
|
|
union sversion version; /* used for copy-on-write */
|
| 143 |
|
|
short ref_count;
|
| 144 |
|
|
struct sarray* is_copy_of;
|
| 145 |
|
|
size_t capacity;
|
| 146 |
|
|
};
|
| 147 |
|
|
|
| 148 |
|
|
struct sarray* sarray_new(int, void* default_element);
|
| 149 |
|
|
void sarray_free(struct sarray*);
|
| 150 |
|
|
struct sarray* sarray_lazy_copy(struct sarray*);
|
| 151 |
|
|
void sarray_realloc(struct sarray*, int new_size);
|
| 152 |
|
|
void sarray_at_put(struct sarray*, sidx indx, void* elem);
|
| 153 |
|
|
void sarray_at_put_safe(struct sarray*, sidx indx, void* elem);
|
| 154 |
|
|
|
| 155 |
|
|
struct sarray* sarray_hard_copy(struct sarray*); /* ... like the name? */
|
| 156 |
|
|
void sarray_remove_garbage(void);
|
| 157 |
|
|
|
| 158 |
|
|
|
| 159 |
|
|
#ifdef PRECOMPUTE_SELECTORS
|
| 160 |
|
|
/* Transform soffset values to ints and vica verca */
|
| 161 |
|
|
static inline unsigned int
|
| 162 |
|
|
soffset_decode(sidx indx)
|
| 163 |
|
|
{
|
| 164 |
|
|
union sofftype x;
|
| 165 |
|
|
x.idx = indx;
|
| 166 |
|
|
#ifdef OBJC_SPARSE3
|
| 167 |
|
|
return x.off.eoffset
|
| 168 |
|
|
+ (x.off.boffset*BUCKET_SIZE)
|
| 169 |
|
|
+ (x.off.ioffset*INDEX_CAPACITY);
|
| 170 |
|
|
#else /* OBJC_SPARSE2 */
|
| 171 |
|
|
return x.off.eoffset + (x.off.boffset*BUCKET_SIZE);
|
| 172 |
|
|
#endif /* OBJC_SPARSE2 */
|
| 173 |
|
|
}
|
| 174 |
|
|
|
| 175 |
|
|
static inline sidx
|
| 176 |
|
|
soffset_encode(size_t offset)
|
| 177 |
|
|
{
|
| 178 |
|
|
union sofftype x;
|
| 179 |
|
|
x.off.eoffset = offset%BUCKET_SIZE;
|
| 180 |
|
|
#ifdef OBJC_SPARSE3
|
| 181 |
|
|
x.off.boffset = (offset/BUCKET_SIZE)%INDEX_SIZE;
|
| 182 |
|
|
x.off.ioffset = offset/INDEX_CAPACITY;
|
| 183 |
|
|
#else /* OBJC_SPARSE2 */
|
| 184 |
|
|
x.off.boffset = offset/BUCKET_SIZE;
|
| 185 |
|
|
#endif
|
| 186 |
|
|
return (sidx)x.idx;
|
| 187 |
|
|
}
|
| 188 |
|
|
|
| 189 |
|
|
#else /* not PRECOMPUTE_SELECTORS */
|
| 190 |
|
|
|
| 191 |
|
|
static inline size_t
|
| 192 |
|
|
soffset_decode(sidx indx)
|
| 193 |
|
|
{
|
| 194 |
|
|
return indx;
|
| 195 |
|
|
}
|
| 196 |
|
|
|
| 197 |
|
|
static inline sidx
|
| 198 |
|
|
soffset_encode(size_t offset)
|
| 199 |
|
|
{
|
| 200 |
|
|
return offset;
|
| 201 |
|
|
}
|
| 202 |
|
|
#endif /* not PRECOMPUTE_SELECTORS */
|
| 203 |
|
|
|
| 204 |
|
|
/* Get element from the Sparse array `array' at offset `indx' */
|
| 205 |
|
|
|
| 206 |
|
|
static inline void* sarray_get(struct sarray* array, sidx indx)
|
| 207 |
|
|
{
|
| 208 |
|
|
#ifdef PRECOMPUTE_SELECTORS
|
| 209 |
|
|
union sofftype x;
|
| 210 |
|
|
x.idx = indx;
|
| 211 |
|
|
#ifdef OBJC_SPARSE3
|
| 212 |
|
|
return
|
| 213 |
|
|
array->
|
| 214 |
|
|
indices[x.off.ioffset]->
|
| 215 |
|
|
buckets[x.off.boffset]->
|
| 216 |
|
|
elems[x.off.eoffset];
|
| 217 |
|
|
#else /* OBJC_SPARSE2 */
|
| 218 |
|
|
return array->buckets[x.off.boffset]->elems[x.off.eoffset];
|
| 219 |
|
|
#endif /* OBJC_SPARSE2 */
|
| 220 |
|
|
#else /* not PRECOMPUTE_SELECTORS */
|
| 221 |
|
|
#ifdef OBJC_SPARSE3
|
| 222 |
|
|
return array->
|
| 223 |
|
|
indices[indx/INDEX_CAPACITY]->
|
| 224 |
|
|
buckets[(indx/BUCKET_SIZE)%INDEX_SIZE]->
|
| 225 |
|
|
elems[indx%BUCKET_SIZE];
|
| 226 |
|
|
#else /* OBJC_SPARSE2 */
|
| 227 |
|
|
return array->buckets[indx/BUCKET_SIZE]->elems[indx%BUCKET_SIZE];
|
| 228 |
|
|
#endif /* not OBJC_SPARSE3 */
|
| 229 |
|
|
#endif /* not PRECOMPUTE_SELECTORS */
|
| 230 |
|
|
}
|
| 231 |
|
|
|
| 232 |
|
|
static inline void* sarray_get_safe(struct sarray* array, sidx indx)
|
| 233 |
|
|
{
|
| 234 |
|
|
if(soffset_decode(indx) < array->capacity)
|
| 235 |
|
|
return sarray_get(array, indx);
|
| 236 |
|
|
else
|
| 237 |
|
|
return (array->empty_bucket->elems[0]);
|
| 238 |
|
|
}
|
| 239 |
|
|
|
| 240 |
|
|
#ifdef __cplusplus
|
| 241 |
|
|
}
|
| 242 |
|
|
#endif /* __cplusplus */
|
| 243 |
|
|
|
| 244 |
|
|
#endif /* __sarray_INCLUDE_GNU */
|