2010-09-18 11:34:58 +02:00
|
|
|
|
/* Sparse Arrays for Objective C dispatch tables
|
2020-01-01 12:51:42 +01:00
|
|
|
|
Copyright (C) 1993-2020 Free Software Foundation, Inc.
|
2010-09-18 11:34:58 +02:00
|
|
|
|
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
|
|
|
|
|
|
2010-12-22 01:17:21 +01:00
|
|
|
|
#define OBJC_SPARSE2 /* 2-level sparse array. */
|
|
|
|
|
/* #define OBJC_SPARSE3 */ /* 3-level sparse array. */
|
2010-09-18 11:34:58 +02:00
|
|
|
|
|
|
|
|
|
#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;
|
|
|
|
|
|
2010-12-22 01:17:21 +01:00
|
|
|
|
/* An unsigned integer of same size as a pointer. */
|
|
|
|
|
#define SIZET_BITS (sizeof (size_t) * 8)
|
2010-09-18 11:34:58 +02:00
|
|
|
|
|
2010-12-22 01:17:21 +01:00
|
|
|
|
#if defined (__sparc__) || defined (OBJC_SPARSE2)
|
2010-09-18 11:34:58 +02:00
|
|
|
|
#define PRECOMPUTE_SELECTORS
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef OBJC_SPARSE3
|
|
|
|
|
|
2010-12-22 01:17:21 +01:00
|
|
|
|
/* Buckets are 8 words each. */
|
2010-09-18 11:34:58 +02:00
|
|
|
|
#define BUCKET_BITS 3
|
2010-12-22 01:17:21 +01:00
|
|
|
|
#define BUCKET_SIZE (1 << BUCKET_BITS)
|
|
|
|
|
#define BUCKET_MASK (BUCKET_SIZE - 1)
|
2010-09-18 11:34:58 +02:00
|
|
|
|
|
2010-12-22 01:17:21 +01:00
|
|
|
|
/* Indices are 16 words each. */
|
2010-09-18 11:34:58 +02:00
|
|
|
|
#define INDEX_BITS 4
|
2010-12-22 01:17:21 +01:00
|
|
|
|
#define INDEX_SIZE (1 << INDEX_BITS)
|
|
|
|
|
#define INDEX_MASK (INDEX_SIZE - 1)
|
2010-09-18 11:34:58 +02:00
|
|
|
|
|
2010-12-22 01:17:21 +01:00
|
|
|
|
#define INDEX_CAPACITY (BUCKET_SIZE * INDEX_SIZE)
|
2010-09-18 11:34:58 +02:00
|
|
|
|
|
|
|
|
|
#else /* OBJC_SPARSE2 */
|
|
|
|
|
|
2010-12-22 01:17:21 +01:00
|
|
|
|
/* Buckets are 32 words each. */
|
2010-09-18 11:34:58 +02:00
|
|
|
|
#define BUCKET_BITS 5
|
2010-12-22 01:17:21 +01:00
|
|
|
|
#define BUCKET_SIZE (1 << BUCKET_BITS)
|
|
|
|
|
#define BUCKET_MASK (BUCKET_SIZE - 1)
|
2010-09-18 11:34:58 +02:00
|
|
|
|
|
|
|
|
|
#endif /* OBJC_SPARSE2 */
|
|
|
|
|
|
|
|
|
|
typedef size_t sidx;
|
|
|
|
|
|
|
|
|
|
#ifdef PRECOMPUTE_SELECTORS
|
|
|
|
|
|
2010-12-22 01:17:21 +01:00
|
|
|
|
struct soffset
|
|
|
|
|
{
|
2010-09-18 11:34:58 +02:00
|
|
|
|
#ifdef OBJC_SPARSE3
|
2010-12-22 01:17:21 +01:00
|
|
|
|
unsigned int unused : SIZET_BITS / 4;
|
|
|
|
|
unsigned int eoffset : SIZET_BITS / 4;
|
|
|
|
|
unsigned int boffset : SIZET_BITS / 4;
|
|
|
|
|
unsigned int ioffset : SIZET_BITS / 4;
|
2010-09-18 11:34:58 +02:00
|
|
|
|
#else /* OBJC_SPARSE2 */
|
|
|
|
|
#ifdef __sparc__
|
|
|
|
|
unsigned long boffset : (SIZET_BITS - 2) - BUCKET_BITS;
|
|
|
|
|
unsigned int eoffset : BUCKET_BITS;
|
|
|
|
|
unsigned int unused : 2;
|
|
|
|
|
#else
|
2010-12-22 01:17:21 +01:00
|
|
|
|
unsigned int boffset : SIZET_BITS / 2;
|
|
|
|
|
unsigned int eoffset : SIZET_BITS / 2;
|
2010-09-18 11:34:58 +02:00
|
|
|
|
#endif
|
|
|
|
|
#endif /* OBJC_SPARSE2 */
|
|
|
|
|
};
|
|
|
|
|
|
2010-12-22 01:17:21 +01:00
|
|
|
|
union sofftype
|
|
|
|
|
{
|
2010-09-18 11:34:58 +02:00
|
|
|
|
struct soffset off;
|
|
|
|
|
sidx idx;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif /* not PRECOMPUTE_SELECTORS */
|
|
|
|
|
|
2010-12-22 01:17:21 +01:00
|
|
|
|
union sversion
|
|
|
|
|
{
|
2010-09-18 11:34:58 +02:00
|
|
|
|
int version;
|
|
|
|
|
void *next_free;
|
|
|
|
|
};
|
|
|
|
|
|
2010-12-22 01:17:21 +01:00
|
|
|
|
struct sbucket
|
|
|
|
|
{
|
|
|
|
|
/* Elements stored in array. */
|
|
|
|
|
void* elems[BUCKET_SIZE];
|
|
|
|
|
|
|
|
|
|
/* Used for copy-on-write. */
|
|
|
|
|
union sversion version;
|
2010-09-18 11:34:58 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#ifdef OBJC_SPARSE3
|
|
|
|
|
|
2010-12-22 01:17:21 +01:00
|
|
|
|
struct sindex
|
|
|
|
|
{
|
2010-09-18 11:34:58 +02:00
|
|
|
|
struct sbucket* buckets[INDEX_SIZE];
|
2010-12-22 01:17:21 +01:00
|
|
|
|
|
|
|
|
|
/* Used for copy-on-write. */
|
|
|
|
|
union sversion version;
|
2010-09-18 11:34:58 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif /* OBJC_SPARSE3 */
|
|
|
|
|
|
2010-12-22 01:17:21 +01:00
|
|
|
|
struct sarray
|
|
|
|
|
{
|
2010-09-18 11:34:58 +02:00
|
|
|
|
#ifdef OBJC_SPARSE3
|
|
|
|
|
struct sindex** indices;
|
|
|
|
|
struct sindex* empty_index;
|
|
|
|
|
#else /* OBJC_SPARSE2 */
|
|
|
|
|
struct sbucket** buckets;
|
|
|
|
|
#endif /* OBJC_SPARSE2 */
|
|
|
|
|
struct sbucket* empty_bucket;
|
2010-12-22 01:17:21 +01:00
|
|
|
|
|
|
|
|
|
/* Used for copy-on-write. */
|
|
|
|
|
union sversion version;
|
|
|
|
|
|
2010-09-18 11:34:58 +02:00
|
|
|
|
short ref_count;
|
|
|
|
|
struct sarray* is_copy_of;
|
|
|
|
|
size_t capacity;
|
|
|
|
|
};
|
|
|
|
|
|
2010-12-22 01:17:21 +01:00
|
|
|
|
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);
|
2010-09-18 11:34:58 +02:00
|
|
|
|
|
2010-12-22 01:17:21 +01:00
|
|
|
|
struct sarray* sarray_hard_copy (struct sarray*); /* ... like the name ? */
|
|
|
|
|
void sarray_remove_garbage (void);
|
2010-09-18 11:34:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef PRECOMPUTE_SELECTORS
|
2010-12-22 01:17:21 +01:00
|
|
|
|
/* Transform soffset values to ints and vice versa. */
|
2010-09-18 11:34:58 +02:00
|
|
|
|
static inline unsigned int
|
2010-12-22 01:17:21 +01:00
|
|
|
|
soffset_decode (sidx indx)
|
2010-09-18 11:34:58 +02:00
|
|
|
|
{
|
|
|
|
|
union sofftype x;
|
|
|
|
|
x.idx = indx;
|
|
|
|
|
#ifdef OBJC_SPARSE3
|
|
|
|
|
return x.off.eoffset
|
2010-12-22 01:17:21 +01:00
|
|
|
|
+ (x.off.boffset * BUCKET_SIZE)
|
|
|
|
|
+ (x.off.ioffset * INDEX_CAPACITY);
|
2010-09-18 11:34:58 +02:00
|
|
|
|
#else /* OBJC_SPARSE2 */
|
2010-12-22 01:17:21 +01:00
|
|
|
|
return x.off.eoffset + (x.off.boffset * BUCKET_SIZE);
|
2010-09-18 11:34:58 +02:00
|
|
|
|
#endif /* OBJC_SPARSE2 */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline sidx
|
2010-12-22 01:17:21 +01:00
|
|
|
|
soffset_encode (size_t offset)
|
2010-09-18 11:34:58 +02:00
|
|
|
|
{
|
|
|
|
|
union sofftype x;
|
2010-12-22 01:17:21 +01:00
|
|
|
|
x.off.eoffset = offset % BUCKET_SIZE;
|
2010-09-18 11:34:58 +02:00
|
|
|
|
#ifdef OBJC_SPARSE3
|
2010-12-22 01:17:21 +01:00
|
|
|
|
x.off.boffset = (offset / BUCKET_SIZE) % INDEX_SIZE;
|
|
|
|
|
x.off.ioffset = offset / INDEX_CAPACITY;
|
2010-09-18 11:34:58 +02:00
|
|
|
|
#else /* OBJC_SPARSE2 */
|
2010-12-22 01:17:21 +01:00
|
|
|
|
x.off.boffset = offset / BUCKET_SIZE;
|
2010-09-18 11:34:58 +02:00
|
|
|
|
#endif
|
|
|
|
|
return (sidx)x.idx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else /* not PRECOMPUTE_SELECTORS */
|
|
|
|
|
|
|
|
|
|
static inline size_t
|
2010-12-22 01:17:21 +01:00
|
|
|
|
soffset_decode (sidx indx)
|
2010-09-18 11:34:58 +02:00
|
|
|
|
{
|
|
|
|
|
return indx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline sidx
|
2010-12-22 01:17:21 +01:00
|
|
|
|
soffset_encode (size_t offset)
|
2010-09-18 11:34:58 +02:00
|
|
|
|
{
|
|
|
|
|
return offset;
|
|
|
|
|
}
|
|
|
|
|
#endif /* not PRECOMPUTE_SELECTORS */
|
|
|
|
|
|
2010-12-22 01:17:21 +01:00
|
|
|
|
/* Get element from the Sparse array `array' at offset `indx'. */
|
|
|
|
|
static inline void* sarray_get (struct sarray* array, sidx indx)
|
2010-09-18 11:34:58 +02:00
|
|
|
|
{
|
|
|
|
|
#ifdef PRECOMPUTE_SELECTORS
|
|
|
|
|
union sofftype x;
|
|
|
|
|
x.idx = indx;
|
|
|
|
|
#ifdef OBJC_SPARSE3
|
2010-12-22 01:17:21 +01:00
|
|
|
|
return array->
|
|
|
|
|
indices[x.off.ioffset]->
|
|
|
|
|
buckets[x.off.boffset]->
|
|
|
|
|
elems[x.off.eoffset];
|
2010-09-18 11:34:58 +02:00
|
|
|
|
#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->
|
2010-12-22 01:17:21 +01:00
|
|
|
|
indices[indx / INDEX_CAPACITY]->
|
|
|
|
|
buckets[(indx / BUCKET_SIZE) % INDEX_SIZE]->
|
|
|
|
|
elems[indx % BUCKET_SIZE];
|
2010-09-18 11:34:58 +02:00
|
|
|
|
#else /* OBJC_SPARSE2 */
|
2010-12-22 01:17:21 +01:00
|
|
|
|
return array->buckets[indx / BUCKET_SIZE]->elems[indx % BUCKET_SIZE];
|
2010-09-18 11:34:58 +02:00
|
|
|
|
#endif /* not OBJC_SPARSE3 */
|
|
|
|
|
#endif /* not PRECOMPUTE_SELECTORS */
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-22 01:17:21 +01:00
|
|
|
|
static inline void* sarray_get_safe (struct sarray* array, sidx indx)
|
2010-09-18 11:34:58 +02:00
|
|
|
|
{
|
2010-12-22 01:17:21 +01:00
|
|
|
|
if (soffset_decode (indx) < array->capacity)
|
|
|
|
|
return sarray_get (array, indx);
|
2010-09-18 11:34:58 +02:00
|
|
|
|
else
|
|
|
|
|
return (array->empty_bucket->elems[0]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif /* __sarray_INCLUDE_GNU */
|