sarray.h 5.81 KB
Newer Older
Nicola Pero committed
1
/* Sparse Arrays for Objective C dispatch tables
2
   Copyright (C) 1993, 1995, 1996, 2004, 2009, 2010 Free Software Foundation, Inc.
Nicola Pero committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
   Contributed by Kresten Krab Thorup.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.

You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
<http://www.gnu.org/licenses/>.  */

#ifndef __sarray_INCLUDE_GNU
#define __sarray_INCLUDE_GNU

29 30
#define OBJC_SPARSE2		/* 2-level sparse array.  */
/* #define OBJC_SPARSE3 */      /* 3-level sparse array.  */
Nicola Pero committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

#ifdef OBJC_SPARSE2
extern const char* __objc_sparse2_id;
#endif

#ifdef OBJC_SPARSE3
extern const char* __objc_sparse3_id;
#endif

#include <stddef.h>

extern int nbuckets;		/* for stats */
extern int nindices;
extern int narrays;
extern int idxsize;

47 48
/* An unsigned integer of same size as a pointer.  */
#define SIZET_BITS (sizeof (size_t) * 8)
Nicola Pero committed
49

50
#if defined (__sparc__) || defined (OBJC_SPARSE2)
Nicola Pero committed
51 52 53 54 55
#define PRECOMPUTE_SELECTORS
#endif

#ifdef OBJC_SPARSE3

56
/* Buckets are 8 words each.  */
Nicola Pero committed
57
#define BUCKET_BITS 3
58 59
#define BUCKET_SIZE (1 << BUCKET_BITS)
#define BUCKET_MASK (BUCKET_SIZE - 1)
Nicola Pero committed
60

61
/* Indices are 16 words each.  */
Nicola Pero committed
62
#define INDEX_BITS 4
63 64
#define INDEX_SIZE (1 << INDEX_BITS)
#define INDEX_MASK (INDEX_SIZE - 1)
Nicola Pero committed
65

66
#define INDEX_CAPACITY (BUCKET_SIZE * INDEX_SIZE)
Nicola Pero committed
67 68 69

#else /* OBJC_SPARSE2 */

70
/* Buckets are 32 words each.  */
Nicola Pero committed
71
#define BUCKET_BITS 5
72 73
#define BUCKET_SIZE (1 << BUCKET_BITS)
#define BUCKET_MASK (BUCKET_SIZE - 1)
Nicola Pero committed
74 75 76 77 78 79 80

#endif /* OBJC_SPARSE2 */

typedef size_t sidx;

#ifdef PRECOMPUTE_SELECTORS

81 82
struct soffset
{
Nicola Pero committed
83
#ifdef OBJC_SPARSE3
84 85 86 87
  unsigned int unused : SIZET_BITS / 4;
  unsigned int eoffset : SIZET_BITS / 4;
  unsigned int boffset : SIZET_BITS / 4;
  unsigned int ioffset : SIZET_BITS / 4;
Nicola Pero committed
88 89 90 91 92 93
#else /* OBJC_SPARSE2 */
#ifdef __sparc__
  unsigned long boffset : (SIZET_BITS - 2) - BUCKET_BITS;
  unsigned int eoffset : BUCKET_BITS;
  unsigned int unused  : 2;
#else
94 95
  unsigned int boffset : SIZET_BITS / 2;
  unsigned int eoffset : SIZET_BITS / 2;
Nicola Pero committed
96 97 98 99
#endif
#endif /* OBJC_SPARSE2 */
};

100 101
union sofftype
{
Nicola Pero committed
102 103 104 105 106 107
  struct soffset off;
  sidx idx;
};

#endif /* not PRECOMPUTE_SELECTORS */

108 109
union sversion
{
Nicola Pero committed
110 111 112 113
  int	version;
  void *next_free;
};

114 115 116 117 118 119 120
struct sbucket
{
  /* Elements stored in array.  */
  void* elems[BUCKET_SIZE];

  /* Used for copy-on-write.  */
  union sversion version;
Nicola Pero committed
121 122 123 124
};

#ifdef OBJC_SPARSE3

125 126
struct sindex
{
Nicola Pero committed
127
  struct sbucket* buckets[INDEX_SIZE];
128 129 130

  /* Used for copy-on-write. */
  union sversion version;		
Nicola Pero committed
131 132 133 134
};

#endif /* OBJC_SPARSE3 */

135 136
struct sarray
{
Nicola Pero committed
137 138 139 140 141 142 143
#ifdef OBJC_SPARSE3
  struct sindex** indices;
  struct sindex* empty_index;
#else /* OBJC_SPARSE2 */
  struct sbucket** buckets;
#endif  /* OBJC_SPARSE2 */
  struct sbucket* empty_bucket;
144 145 146 147

  /* Used for copy-on-write. */
  union sversion version;

Nicola Pero committed
148 149 150 151 152
  short ref_count;
  struct sarray* is_copy_of;
  size_t capacity;
};

153 154 155 156 157 158
struct sarray* sarray_new (int, void* default_element);
void sarray_free (struct sarray*);
struct sarray* sarray_lazy_copy (struct sarray*);
void sarray_realloc (struct sarray*, int new_size);
void sarray_at_put (struct sarray*, sidx indx, void* elem);
void sarray_at_put_safe (struct sarray*, sidx indx, void* elem);
Nicola Pero committed
159

160 161
struct sarray* sarray_hard_copy (struct sarray*); /* ... like the name ?  */
void sarray_remove_garbage (void);
Nicola Pero committed
162 163 164


#ifdef PRECOMPUTE_SELECTORS
165
/* Transform soffset values to ints and vice versa.  */
Nicola Pero committed
166
static inline unsigned int
167
soffset_decode (sidx indx)
Nicola Pero committed
168 169 170 171 172
{
  union sofftype x;
  x.idx = indx;
#ifdef OBJC_SPARSE3
  return x.off.eoffset
173 174
    + (x.off.boffset * BUCKET_SIZE)
    + (x.off.ioffset * INDEX_CAPACITY);
Nicola Pero committed
175
#else /* OBJC_SPARSE2 */
176
  return x.off.eoffset + (x.off.boffset * BUCKET_SIZE);
Nicola Pero committed
177 178 179 180
#endif /* OBJC_SPARSE2 */
}

static inline sidx
181
soffset_encode (size_t offset)
Nicola Pero committed
182 183
{
  union sofftype x;
184
  x.off.eoffset = offset % BUCKET_SIZE;
Nicola Pero committed
185
#ifdef OBJC_SPARSE3
186 187
  x.off.boffset = (offset / BUCKET_SIZE) % INDEX_SIZE;
  x.off.ioffset = offset / INDEX_CAPACITY;
Nicola Pero committed
188
#else /* OBJC_SPARSE2 */
189
  x.off.boffset = offset / BUCKET_SIZE;
Nicola Pero committed
190 191 192 193 194 195 196
#endif
  return (sidx)x.idx;
}

#else /* not PRECOMPUTE_SELECTORS */

static inline size_t
197
soffset_decode (sidx indx)
Nicola Pero committed
198 199 200 201 202
{
  return indx;
}

static inline sidx
203
soffset_encode (size_t offset)
Nicola Pero committed
204 205 206 207 208
{
  return offset;
}
#endif /* not PRECOMPUTE_SELECTORS */

209 210
/* Get element from the Sparse array `array' at offset `indx'.  */
static inline void* sarray_get (struct sarray* array, sidx indx)
Nicola Pero committed
211 212 213 214 215
{
#ifdef PRECOMPUTE_SELECTORS
  union sofftype x;
  x.idx = indx;
#ifdef OBJC_SPARSE3
216 217 218 219
  return array->
    indices[x.off.ioffset]->
    buckets[x.off.boffset]->
    elems[x.off.eoffset];
Nicola Pero committed
220 221 222 223 224 225
#else /* OBJC_SPARSE2 */
  return array->buckets[x.off.boffset]->elems[x.off.eoffset];
#endif /* OBJC_SPARSE2 */
#else /* not PRECOMPUTE_SELECTORS */
#ifdef OBJC_SPARSE3
  return array->
226 227 228
    indices[indx / INDEX_CAPACITY]->
    buckets[(indx / BUCKET_SIZE) % INDEX_SIZE]->
    elems[indx % BUCKET_SIZE];
Nicola Pero committed
229
#else /* OBJC_SPARSE2 */
230
  return array->buckets[indx / BUCKET_SIZE]->elems[indx % BUCKET_SIZE];
Nicola Pero committed
231 232 233 234
#endif /* not OBJC_SPARSE3 */
#endif /* not PRECOMPUTE_SELECTORS */
}

235
static inline void* sarray_get_safe (struct sarray* array, sidx indx)
Nicola Pero committed
236
{
237 238
  if (soffset_decode (indx) < array->capacity)
    return sarray_get (array, indx);
Nicola Pero committed
239 240 241 242 243
  else
    return (array->empty_bucket->elems[0]);
}

#endif /* __sarray_INCLUDE_GNU */