Protocol Buffers - Google's data interchange format (grpc依赖) https://developers.google.com/protocol-buffers/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

681 lines
19 KiB

/*
** upb_table Implementation
**
** Implementation is heavily inspired by Lua's ltable.c.
*/
#include <string.h>
#include "third_party/wyhash/wyhash.h"
#include "upb/table.int.h"
/* Must be last. */
#include "upb/port_def.inc"
#define UPB_MAXARRSIZE 16 /* 64k. */
/* From Chromium. */
#define ARRAY_SIZE(x) \
((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
static const double MAX_LOAD = 0.85;
/* The minimum utilization of the array part of a mixed hash/array table. This
* is a speed/memory-usage tradeoff (though it's not straightforward because of
* cache effects). The lower this is, the more memory we'll use. */
static const double MIN_DENSITY = 0.1;
static bool is_pow2(uint64_t v) { return v == 0 || (v & (v - 1)) == 0; }
static upb_value _upb_value_val(uint64_t val) {
upb_value ret;
_upb_value_setval(&ret, val);
return ret;
}
static int log2ceil(uint64_t v) {
int ret = 0;
bool pow2 = is_pow2(v);
while (v >>= 1) ret++;
ret = pow2 ? ret : ret + 1; /* Ceiling. */
return UPB_MIN(UPB_MAXARRSIZE, ret);
}
char *upb_strdup2(const char *s, size_t len, upb_arena *a) {
size_t n;
char *p;
/* Prevent overflow errors. */
if (len == SIZE_MAX) return NULL;
/* Always null-terminate, even if binary data; but don't rely on the input to
* have a null-terminating byte since it may be a raw binary buffer. */
n = len + 1;
p = upb_arena_malloc(a, n);
if (p) {
memcpy(p, s, len);
p[len] = 0;
}
return p;
}
/* A type to represent the lookup key of either a strtable or an inttable. */
typedef union {
uintptr_t num;
struct {
const char *str;
size_t len;
} str;
} lookupkey_t;
static lookupkey_t strkey2(const char *str, size_t len) {
lookupkey_t k;
k.str.str = str;
k.str.len = len;
return k;
}
static lookupkey_t intkey(uintptr_t key) {
lookupkey_t k;
k.num = key;
return k;
}
typedef uint32_t hashfunc_t(upb_tabkey key);
typedef bool eqlfunc_t(upb_tabkey k1, lookupkey_t k2);
/* Base table (shared code) ***************************************************/
static uint32_t upb_inthash(uintptr_t key) {
return (uint32_t)key;
}
static const upb_tabent *upb_getentry(const upb_table *t, uint32_t hash) {
return t->entries + (hash & t->mask);
}
static bool upb_arrhas(upb_tabval key) {
return key.val != (uint64_t)-1;
}
static bool isfull(upb_table *t) {
return t->count == t->max_count;
}
static bool init(upb_table *t, uint8_t size_lg2, upb_arena *a) {
size_t bytes;
t->count = 0;
t->size_lg2 = size_lg2;
t->mask = upb_table_size(t) ? upb_table_size(t) - 1 : 0;
t->max_count = upb_table_size(t) * MAX_LOAD;
bytes = upb_table_size(t) * sizeof(upb_tabent);
if (bytes > 0) {
t->entries = upb_arena_malloc(a, bytes);
if (!t->entries) return false;
memset(t->entries, 0, bytes);
} else {
t->entries = NULL;
}
return true;
}
static upb_tabent *emptyent(upb_table *t, upb_tabent *e) {
upb_tabent *begin = t->entries;
upb_tabent *end = begin + upb_table_size(t);
for (e = e + 1; e < end; e++) {
if (upb_tabent_isempty(e)) return e;
}
for (e = begin; e < end; e++) {
if (upb_tabent_isempty(e)) return e;
}
UPB_ASSERT(false);
return NULL;
}
static upb_tabent *getentry_mutable(upb_table *t, uint32_t hash) {
return (upb_tabent*)upb_getentry(t, hash);
}
static const upb_tabent *findentry(const upb_table *t, lookupkey_t key,
uint32_t hash, eqlfunc_t *eql) {
const upb_tabent *e;
if (t->size_lg2 == 0) return NULL;
e = upb_getentry(t, hash);
if (upb_tabent_isempty(e)) return NULL;
while (1) {
if (eql(e->key, key)) return e;
if ((e = e->next) == NULL) return NULL;
}
}
static upb_tabent *findentry_mutable(upb_table *t, lookupkey_t key,
uint32_t hash, eqlfunc_t *eql) {
return (upb_tabent*)findentry(t, key, hash, eql);
}
static bool lookup(const upb_table *t, lookupkey_t key, upb_value *v,
uint32_t hash, eqlfunc_t *eql) {
const upb_tabent *e = findentry(t, key, hash, eql);
if (e) {
if (v) {
_upb_value_setval(v, e->val.val);
}
return true;
} else {
return false;
}
}
/* The given key must not already exist in the table. */
static void insert(upb_table *t, lookupkey_t key, upb_tabkey tabkey,
upb_value val, uint32_t hash,
hashfunc_t *hashfunc, eqlfunc_t *eql) {
upb_tabent *mainpos_e;
upb_tabent *our_e;
UPB_ASSERT(findentry(t, key, hash, eql) == NULL);
t->count++;
mainpos_e = getentry_mutable(t, hash);
our_e = mainpos_e;
if (upb_tabent_isempty(mainpos_e)) {
/* Our main position is empty; use it. */
our_e->next = NULL;
} else {
/* Collision. */
upb_tabent *new_e = emptyent(t, mainpos_e);
/* Head of collider's chain. */
upb_tabent *chain = getentry_mutable(t, hashfunc(mainpos_e->key));
if (chain == mainpos_e) {
/* Existing ent is in its main position (it has the same hash as us, and
* is the head of our chain). Insert to new ent and append to this chain. */
new_e->next = mainpos_e->next;
mainpos_e->next = new_e;
our_e = new_e;
} else {
/* Existing ent is not in its main position (it is a node in some other
* chain). This implies that no existing ent in the table has our hash.
* Evict it (updating its chain) and use its ent for head of our chain. */
*new_e = *mainpos_e; /* copies next. */
while (chain->next != mainpos_e) {
chain = (upb_tabent*)chain->next;
UPB_ASSERT(chain);
}
chain->next = new_e;
our_e = mainpos_e;
our_e->next = NULL;
}
}
our_e->key = tabkey;
our_e->val.val = val.val;
UPB_ASSERT(findentry(t, key, hash, eql) == our_e);
}
static bool rm(upb_table *t, lookupkey_t key, upb_value *val,
upb_tabkey *removed, uint32_t hash, eqlfunc_t *eql) {
upb_tabent *chain = getentry_mutable(t, hash);
if (upb_tabent_isempty(chain)) return false;
if (eql(chain->key, key)) {
/* Element to remove is at the head of its chain. */
t->count--;
if (val) _upb_value_setval(val, chain->val.val);
if (removed) *removed = chain->key;
if (chain->next) {
upb_tabent *move = (upb_tabent*)chain->next;
*chain = *move;
move->key = 0; /* Make the slot empty. */
} else {
chain->key = 0; /* Make the slot empty. */
}
return true;
} else {
/* Element to remove is either in a non-head position or not in the
* table. */
while (chain->next && !eql(chain->next->key, key)) {
chain = (upb_tabent*)chain->next;
}
if (chain->next) {
/* Found element to remove. */
upb_tabent *rm = (upb_tabent*)chain->next;
t->count--;
if (val) _upb_value_setval(val, chain->next->val.val);
if (removed) *removed = rm->key;
rm->key = 0; /* Make the slot empty. */
chain->next = rm->next;
return true;
} else {
/* Element to remove is not in the table. */
return false;
}
}
}
static size_t next(const upb_table *t, size_t i) {
do {
if (++i >= upb_table_size(t))
return SIZE_MAX - 1; /* Distinct from -1. */
} while(upb_tabent_isempty(&t->entries[i]));
return i;
}
static size_t begin(const upb_table *t) {
return next(t, -1);
}
/* upb_strtable ***************************************************************/
/* A simple "subclass" of upb_table that only adds a hash function for strings. */
static upb_tabkey strcopy(lookupkey_t k2, upb_arena *a) {
uint32_t len = (uint32_t) k2.str.len;
char *str = upb_arena_malloc(a, k2.str.len + sizeof(uint32_t) + 1);
if (str == NULL) return 0;
memcpy(str, &len, sizeof(uint32_t));
if (k2.str.len) memcpy(str + sizeof(uint32_t), k2.str.str, k2.str.len);
str[sizeof(uint32_t) + k2.str.len] = '\0';
return (uintptr_t)str;
}
static uint32_t table_hash(const char *p, size_t n) {
return wyhash(p, n, 0, _wyp);
}
static uint32_t strhash(upb_tabkey key) {
uint32_t len;
char *str = upb_tabstr(key, &len);
return table_hash(str, len);
}
static bool streql(upb_tabkey k1, lookupkey_t k2) {
uint32_t len;
char *str = upb_tabstr(k1, &len);
return len == k2.str.len && (len == 0 || memcmp(str, k2.str.str, len) == 0);
}
bool upb_strtable_init(upb_strtable *t, size_t expected_size, upb_arena *a) {
// Multiply by approximate reciprocal of MAX_LOAD (0.85), with pow2 denominator.
size_t need_entries = (expected_size + 1) * 1204 / 1024;
UPB_ASSERT(need_entries >= expected_size * 0.85);
int size_lg2 = _upb_lg2ceil(need_entries);
return init(&t->t, size_lg2, a);
}
void upb_strtable_clear(upb_strtable *t) {
size_t bytes = upb_table_size(&t->t) * sizeof(upb_tabent);
t->t.count = 0;
memset((char*)t->t.entries, 0, bytes);
}
bool upb_strtable_resize(upb_strtable *t, size_t size_lg2, upb_arena *a) {
upb_strtable new_table;
upb_strtable_iter i;
if (!init(&new_table.t, size_lg2, a))
return false;
upb_strtable_begin(&i, t);
for ( ; !upb_strtable_done(&i); upb_strtable_next(&i)) {
upb_strview key = upb_strtable_iter_key(&i);
upb_strtable_insert(&new_table, key.data, key.size,
upb_strtable_iter_value(&i), a);
}
*t = new_table;
return true;
}
bool upb_strtable_insert(upb_strtable *t, const char *k, size_t len,
upb_value v, upb_arena *a) {
lookupkey_t key;
upb_tabkey tabkey;
uint32_t hash;
if (isfull(&t->t)) {
/* Need to resize. New table of double the size, add old elements to it. */
if (!upb_strtable_resize(t, t->t.size_lg2 + 1, a)) {
return false;
}
}
key = strkey2(k, len);
tabkey = strcopy(key, a);
if (tabkey == 0) return false;
hash = table_hash(key.str.str, key.str.len);
insert(&t->t, key, tabkey, v, hash, &strhash, &streql);
return true;
}
bool upb_strtable_lookup2(const upb_strtable *t, const char *key, size_t len,
upb_value *v) {
uint32_t hash = table_hash(key, len);
return lookup(&t->t, strkey2(key, len), v, hash, &streql);
}
bool upb_strtable_remove(upb_strtable *t, const char *key, size_t len,
upb_value *val) {
uint32_t hash = table_hash(key, len);
upb_tabkey tabkey;
return rm(&t->t, strkey2(key, len), val, &tabkey, hash, &streql);
}
/* Iteration */
void upb_strtable_begin(upb_strtable_iter *i, const upb_strtable *t) {
i->t = t;
i->index = begin(&t->t);
}
void upb_strtable_next(upb_strtable_iter *i) {
i->index = next(&i->t->t, i->index);
}
bool upb_strtable_done(const upb_strtable_iter *i) {
if (!i->t) return true;
return i->index >= upb_table_size(&i->t->t) ||
upb_tabent_isempty(str_tabent(i));
}
upb_strview upb_strtable_iter_key(const upb_strtable_iter *i) {
upb_strview key;
uint32_t len;
UPB_ASSERT(!upb_strtable_done(i));
key.data = upb_tabstr(str_tabent(i)->key, &len);
key.size = len;
return key;
}
upb_value upb_strtable_iter_value(const upb_strtable_iter *i) {
UPB_ASSERT(!upb_strtable_done(i));
return _upb_value_val(str_tabent(i)->val.val);
}
void upb_strtable_iter_setdone(upb_strtable_iter *i) {
i->t = NULL;
i->index = SIZE_MAX;
}
bool upb_strtable_iter_isequal(const upb_strtable_iter *i1,
const upb_strtable_iter *i2) {
if (upb_strtable_done(i1) && upb_strtable_done(i2))
return true;
return i1->t == i2->t && i1->index == i2->index;
}
/* upb_inttable ***************************************************************/
/* For inttables we use a hybrid structure where small keys are kept in an
* array and large keys are put in the hash table. */
static uint32_t inthash(upb_tabkey key) { return upb_inthash(key); }
static bool inteql(upb_tabkey k1, lookupkey_t k2) {
return k1 == k2.num;
}
static upb_tabval *mutable_array(upb_inttable *t) {
return (upb_tabval*)t->array;
}
static upb_tabval *inttable_val(upb_inttable *t, uintptr_t key) {
if (key < t->array_size) {
return upb_arrhas(t->array[key]) ? &(mutable_array(t)[key]) : NULL;
} else {
upb_tabent *e =
findentry_mutable(&t->t, intkey(key), upb_inthash(key), &inteql);
return e ? &e->val : NULL;
}
}
static const upb_tabval *inttable_val_const(const upb_inttable *t,
uintptr_t key) {
return inttable_val((upb_inttable*)t, key);
}
size_t upb_inttable_count(const upb_inttable *t) {
return t->t.count + t->array_count;
}
static void check(upb_inttable *t) {
UPB_UNUSED(t);
#if defined(UPB_DEBUG_TABLE) && !defined(NDEBUG)
{
/* This check is very expensive (makes inserts/deletes O(N)). */
size_t count = 0;
upb_inttable_iter i;
upb_inttable_begin(&i, t);
for(; !upb_inttable_done(&i); upb_inttable_next(&i), count++) {
UPB_ASSERT(upb_inttable_lookup(t, upb_inttable_iter_key(&i), NULL));
}
UPB_ASSERT(count == upb_inttable_count(t));
}
#endif
}
bool upb_inttable_sizedinit(upb_inttable *t, size_t asize, int hsize_lg2,
upb_arena *a) {
size_t array_bytes;
if (!init(&t->t, hsize_lg2, a)) return false;
/* Always make the array part at least 1 long, so that we know key 0
* won't be in the hash part, which simplifies things. */
t->array_size = UPB_MAX(1, asize);
t->array_count = 0;
array_bytes = t->array_size * sizeof(upb_value);
t->array = upb_arena_malloc(a, array_bytes);
if (!t->array) {
return false;
}
memset(mutable_array(t), 0xff, array_bytes);
check(t);
return true;
}
bool upb_inttable_init(upb_inttable *t, upb_arena *a) {
return upb_inttable_sizedinit(t, 0, 4, a);
}
bool upb_inttable_insert(upb_inttable *t, uintptr_t key, upb_value val,
upb_arena *a) {
upb_tabval tabval;
tabval.val = val.val;
UPB_ASSERT(upb_arrhas(tabval)); /* This will reject (uint64_t)-1. Fix this. */
if (key < t->array_size) {
UPB_ASSERT(!upb_arrhas(t->array[key]));
t->array_count++;
mutable_array(t)[key].val = val.val;
} else {
if (isfull(&t->t)) {
/* Need to resize the hash part, but we re-use the array part. */
size_t i;
upb_table new_table;
if (!init(&new_table, t->t.size_lg2 + 1, a)) {
return false;
}
for (i = begin(&t->t); i < upb_table_size(&t->t); i = next(&t->t, i)) {
const upb_tabent *e = &t->t.entries[i];
uint32_t hash;
upb_value v;
_upb_value_setval(&v, e->val.val);
hash = upb_inthash(e->key);
insert(&new_table, intkey(e->key), e->key, v, hash, &inthash, &inteql);
}
UPB_ASSERT(t->t.count == new_table.count);
t->t = new_table;
}
insert(&t->t, intkey(key), key, val, upb_inthash(key), &inthash, &inteql);
}
check(t);
return true;
}
bool upb_inttable_lookup(const upb_inttable *t, uintptr_t key, upb_value *v) {
const upb_tabval *table_v = inttable_val_const(t, key);
if (!table_v) return false;
if (v) _upb_value_setval(v, table_v->val);
return true;
}
bool upb_inttable_replace(upb_inttable *t, uintptr_t key, upb_value val) {
upb_tabval *table_v = inttable_val(t, key);
if (!table_v) return false;
table_v->val = val.val;
return true;
}
bool upb_inttable_remove(upb_inttable *t, uintptr_t key, upb_value *val) {
bool success;
if (key < t->array_size) {
if (upb_arrhas(t->array[key])) {
upb_tabval empty = UPB_TABVALUE_EMPTY_INIT;
t->array_count--;
if (val) {
_upb_value_setval(val, t->array[key].val);
}
mutable_array(t)[key] = empty;
success = true;
} else {
success = false;
}
} else {
success = rm(&t->t, intkey(key), val, NULL, upb_inthash(key), &inteql);
}
check(t);
return success;
}
void upb_inttable_compact(upb_inttable *t, upb_arena *a) {
/* A power-of-two histogram of the table keys. */
size_t counts[UPB_MAXARRSIZE + 1] = {0};
/* The max key in each bucket. */
uintptr_t max[UPB_MAXARRSIZE + 1] = {0};
upb_inttable_iter i;
size_t arr_count;
int size_lg2;
upb_inttable new_t;
upb_inttable_begin(&i, t);
for (; !upb_inttable_done(&i); upb_inttable_next(&i)) {
uintptr_t key = upb_inttable_iter_key(&i);
int bucket = log2ceil(key);
max[bucket] = UPB_MAX(max[bucket], key);
counts[bucket]++;
}
/* Find the largest power of two that satisfies the MIN_DENSITY
* definition (while actually having some keys). */
arr_count = upb_inttable_count(t);
for (size_lg2 = ARRAY_SIZE(counts) - 1; size_lg2 > 0; size_lg2--) {
if (counts[size_lg2] == 0) {
/* We can halve again without losing any entries. */
continue;
} else if (arr_count >= (1 << size_lg2) * MIN_DENSITY) {
break;
}
arr_count -= counts[size_lg2];
}
UPB_ASSERT(arr_count <= upb_inttable_count(t));
{
/* Insert all elements into new, perfectly-sized table. */
size_t arr_size = max[size_lg2] + 1; /* +1 so arr[max] will fit. */
size_t hash_count = upb_inttable_count(t) - arr_count;
size_t hash_size = hash_count ? (hash_count / MAX_LOAD) + 1 : 0;
int hashsize_lg2 = log2ceil(hash_size);
upb_inttable_sizedinit(&new_t, arr_size, hashsize_lg2, a);
upb_inttable_begin(&i, t);
for (; !upb_inttable_done(&i); upb_inttable_next(&i)) {
uintptr_t k = upb_inttable_iter_key(&i);
upb_inttable_insert(&new_t, k, upb_inttable_iter_value(&i), a);
}
UPB_ASSERT(new_t.array_size == arr_size);
UPB_ASSERT(new_t.t.size_lg2 == hashsize_lg2);
}
*t = new_t;
}
/* Iteration. */
static const upb_tabent *int_tabent(const upb_inttable_iter *i) {
UPB_ASSERT(!i->array_part);
return &i->t->t.entries[i->index];
}
static upb_tabval int_arrent(const upb_inttable_iter *i) {
UPB_ASSERT(i->array_part);
return i->t->array[i->index];
}
void upb_inttable_begin(upb_inttable_iter *i, const upb_inttable *t) {
i->t = t;
i->index = -1;
i->array_part = true;
upb_inttable_next(i);
}
void upb_inttable_next(upb_inttable_iter *iter) {
const upb_inttable *t = iter->t;
if (iter->array_part) {
while (++iter->index < t->array_size) {
if (upb_arrhas(int_arrent(iter))) {
return;
}
}
iter->array_part = false;
iter->index = begin(&t->t);
} else {
iter->index = next(&t->t, iter->index);
}
}
bool upb_inttable_done(const upb_inttable_iter *i) {
if (!i->t) return true;
if (i->array_part) {
return i->index >= i->t->array_size ||
!upb_arrhas(int_arrent(i));
} else {
return i->index >= upb_table_size(&i->t->t) ||
upb_tabent_isempty(int_tabent(i));
}
}
uintptr_t upb_inttable_iter_key(const upb_inttable_iter *i) {
UPB_ASSERT(!upb_inttable_done(i));
return i->array_part ? i->index : int_tabent(i)->key;
}
upb_value upb_inttable_iter_value(const upb_inttable_iter *i) {
UPB_ASSERT(!upb_inttable_done(i));
return _upb_value_val(
i->array_part ? i->t->array[i->index].val : int_tabent(i)->val.val);
}
void upb_inttable_iter_setdone(upb_inttable_iter *i) {
i->t = NULL;
i->index = SIZE_MAX;
i->array_part = false;
}
bool upb_inttable_iter_isequal(const upb_inttable_iter *i1,
const upb_inttable_iter *i2) {
if (upb_inttable_done(i1) && upb_inttable_done(i2))
return true;
return i1->t == i2->t && i1->index == i2->index &&
i1->array_part == i2->array_part;
}