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.
352 lines
12 KiB
352 lines
12 KiB
16 years ago
|
/*
|
||
4 years ago
|
* Copyright (c) 2009-2021, Google LLC
|
||
4 years ago
|
* All rights reserved.
|
||
|
*
|
||
|
* Redistribution and use in source and binary forms, with or without
|
||
|
* modification, are permitted provided that the following conditions are met:
|
||
|
* * Redistributions of source code must retain the above copyright
|
||
|
* notice, this list of conditions and the following disclaimer.
|
||
|
* * Redistributions in binary form must reproduce the above copyright
|
||
|
* notice, this list of conditions and the following disclaimer in the
|
||
|
* documentation and/or other materials provided with the distribution.
|
||
|
* * Neither the name of Google LLC nor the
|
||
|
* names of its contributors may be used to endorse or promote products
|
||
|
* derived from this software without specific prior written permission.
|
||
|
*
|
||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||
|
* DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY
|
||
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
|
*/
|
||
|
|
||
|
/*
|
||
|
* upb_table
|
||
|
*
|
||
|
* This header is INTERNAL-ONLY! Its interfaces are not public or stable!
|
||
|
* This file defines very fast int->upb_value (inttable) and string->upb_value
|
||
|
* (strtable) hash tables.
|
||
|
*
|
||
|
* The table uses chained scatter with Brent's variation (inspired by the Lua
|
||
|
* implementation of hash tables). The hash function for strings is Austin
|
||
|
* Appleby's "MurmurHash."
|
||
|
*
|
||
|
* The inttable uses uintptr_t as its key, which guarantees it can be used to
|
||
|
* store pointers or integers of at least 32 bits (upb isn't really useful on
|
||
|
* systems where sizeof(void*) < 4).
|
||
|
*
|
||
|
* The table must be homogeneous (all values of the same type). In debug
|
||
|
* mode, we check this on insert and lookup.
|
||
|
*/
|
||
16 years ago
|
|
||
|
#ifndef UPB_TABLE_H_
|
||
|
#define UPB_TABLE_H_
|
||
|
|
||
11 years ago
|
#include <stdint.h>
|
||
10 years ago
|
#include <string.h>
|
||
10 years ago
|
#include "upb/upb.h"
|
||
16 years ago
|
|
||
6 years ago
|
#include "upb/port_def.inc"
|
||
|
|
||
7 years ago
|
#ifdef __cplusplus
|
||
|
extern "C" {
|
||
|
#endif
|
||
|
|
||
11 years ago
|
|
||
|
/* upb_value ******************************************************************/
|
||
|
|
||
|
typedef struct {
|
||
7 years ago
|
uint64_t val;
|
||
11 years ago
|
} upb_value;
|
||
|
|
||
10 years ago
|
/* Variant that works with a length-delimited rather than NULL-delimited string,
|
||
|
* as supported by strtable. */
|
||
4 years ago
|
char *upb_strdup2(const char *s, size_t len, upb_arena *a);
|
||
11 years ago
|
|
||
5 years ago
|
UPB_INLINE void _upb_value_setval(upb_value *v, uint64_t val) {
|
||
11 years ago
|
v->val = val;
|
||
|
}
|
||
|
|
||
10 years ago
|
/* For each value ctype, define the following set of functions:
|
||
|
*
|
||
|
* // Get/set an int32 from a upb_value.
|
||
|
* int32_t upb_value_getint32(upb_value val);
|
||
|
* void upb_value_setint32(upb_value *val, int32_t cval);
|
||
|
*
|
||
|
* // Construct a new upb_value from an int32.
|
||
|
* upb_value upb_value_int32(int32_t val); */
|
||
|
#define FUNCS(name, membername, type_t, converter, proto_type) \
|
||
11 years ago
|
UPB_INLINE void upb_value_set ## name(upb_value *val, type_t cval) { \
|
||
7 years ago
|
val->val = (converter)cval; \
|
||
11 years ago
|
} \
|
||
|
UPB_INLINE upb_value upb_value_ ## name(type_t val) { \
|
||
|
upb_value ret; \
|
||
|
upb_value_set ## name(&ret, val); \
|
||
|
return ret; \
|
||
|
} \
|
||
|
UPB_INLINE type_t upb_value_get ## name(upb_value val) { \
|
||
7 years ago
|
return (type_t)(converter)val.val; \
|
||
11 years ago
|
}
|
||
|
|
||
7 years ago
|
FUNCS(int32, int32, int32_t, int32_t, UPB_CTYPE_INT32)
|
||
|
FUNCS(int64, int64, int64_t, int64_t, UPB_CTYPE_INT64)
|
||
|
FUNCS(uint32, uint32, uint32_t, uint32_t, UPB_CTYPE_UINT32)
|
||
|
FUNCS(uint64, uint64, uint64_t, uint64_t, UPB_CTYPE_UINT64)
|
||
|
FUNCS(bool, _bool, bool, bool, UPB_CTYPE_BOOL)
|
||
|
FUNCS(cstr, cstr, char*, uintptr_t, UPB_CTYPE_CSTR)
|
||
|
FUNCS(ptr, ptr, void*, uintptr_t, UPB_CTYPE_PTR)
|
||
|
FUNCS(constptr, constptr, const void*, uintptr_t, UPB_CTYPE_CONSTPTR)
|
||
|
FUNCS(fptr, fptr, upb_func*, uintptr_t, UPB_CTYPE_FPTR)
|
||
11 years ago
|
|
||
|
#undef FUNCS
|
||
8 years ago
|
|
||
|
UPB_INLINE void upb_value_setfloat(upb_value *val, float cval) {
|
||
|
memcpy(&val->val, &cval, sizeof(cval));
|
||
|
}
|
||
|
|
||
|
UPB_INLINE void upb_value_setdouble(upb_value *val, double cval) {
|
||
|
memcpy(&val->val, &cval, sizeof(cval));
|
||
|
}
|
||
|
|
||
|
UPB_INLINE upb_value upb_value_float(float cval) {
|
||
|
upb_value ret;
|
||
|
upb_value_setfloat(&ret, cval);
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
UPB_INLINE upb_value upb_value_double(double cval) {
|
||
|
upb_value ret;
|
||
|
upb_value_setdouble(&ret, cval);
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
10 years ago
|
#undef SET_TYPE
|
||
11 years ago
|
|
||
|
|
||
10 years ago
|
/* upb_tabkey *****************************************************************/
|
||
|
|
||
|
/* Either:
|
||
|
* 1. an actual integer key, or
|
||
|
* 2. a pointer to a string prefixed by its uint32_t length, owned by us.
|
||
|
*
|
||
|
* ...depending on whether this is a string table or an int table. We would
|
||
|
* make this a union of those two types, but C89 doesn't support statically
|
||
|
* initializing a non-first union member. */
|
||
|
typedef uintptr_t upb_tabkey;
|
||
11 years ago
|
|
||
10 years ago
|
UPB_INLINE char *upb_tabstr(upb_tabkey key, uint32_t *len) {
|
||
|
char* mem = (char*)key;
|
||
|
if (len) memcpy(len, mem, sizeof(*len));
|
||
|
return mem + sizeof(*len);
|
||
|
}
|
||
12 years ago
|
|
||
4 years ago
|
UPB_INLINE upb_strview upb_tabstrview(upb_tabkey key) {
|
||
|
upb_strview ret;
|
||
|
uint32_t len;
|
||
|
ret.data = upb_tabstr(key, &len);
|
||
|
ret.size = len;
|
||
|
return ret;
|
||
|
}
|
||
10 years ago
|
|
||
|
/* upb_tabval *****************************************************************/
|
||
|
|
||
4 years ago
|
typedef struct upb_tabval {
|
||
7 years ago
|
uint64_t val;
|
||
10 years ago
|
} upb_tabval;
|
||
|
|
||
6 years ago
|
#define UPB_TABVALUE_EMPTY_INIT {-1}
|
||
10 years ago
|
|
||
|
/* upb_table ******************************************************************/
|
||
|
|
||
13 years ago
|
typedef struct _upb_tabent {
|
||
|
upb_tabkey key;
|
||
10 years ago
|
upb_tabval val;
|
||
10 years ago
|
|
||
10 years ago
|
/* Internal chaining. This is const so we can create static initializers for
|
||
|
* tables. We cast away const sometimes, but *only* when the containing
|
||
|
* upb_table is known to be non-const. This requires a bit of care, but
|
||
|
* the subtlety is confined to table.c. */
|
||
12 years ago
|
const struct _upb_tabent *next;
|
||
13 years ago
|
} upb_tabent;
|
||
16 years ago
|
|
||
15 years ago
|
typedef struct {
|
||
10 years ago
|
size_t count; /* Number of entries in the hash part. */
|
||
4 years ago
|
uint32_t mask; /* Mask to turn hash value -> bucket. */
|
||
|
uint32_t max_count; /* Max count before we hit our load limit. */
|
||
10 years ago
|
uint8_t size_lg2; /* Size of the hashtable part is 2^size_lg2 entries. */
|
||
4 years ago
|
upb_tabent *entries;
|
||
15 years ago
|
} upb_table;
|
||
16 years ago
|
|
||
15 years ago
|
typedef struct {
|
||
|
upb_table t;
|
||
|
} upb_strtable;
|
||
16 years ago
|
|
||
15 years ago
|
typedef struct {
|
||
10 years ago
|
upb_table t; /* For entries that don't fit in the array part. */
|
||
|
const upb_tabval *array; /* Array part of the table. See const note above. */
|
||
|
size_t array_size; /* Array part size. */
|
||
|
size_t array_count; /* Array part number of elements. */
|
||
15 years ago
|
} upb_inttable;
|
||
16 years ago
|
|
||
12 years ago
|
UPB_INLINE size_t upb_table_size(const upb_table *t) {
|
||
12 years ago
|
if (t->size_lg2 == 0)
|
||
|
return 0;
|
||
|
else
|
||
|
return 1 << t->size_lg2;
|
||
16 years ago
|
}
|
||
|
|
||
10 years ago
|
/* Internal-only functions, in .h file only out of necessity. */
|
||
12 years ago
|
UPB_INLINE bool upb_tabent_isempty(const upb_tabent *e) {
|
||
10 years ago
|
return e->key == 0;
|
||
12 years ago
|
}
|
||
|
|
||
10 years ago
|
/* Initialize and uninitialize a table, respectively. If memory allocation
|
||
|
* failed, false is returned that the table is uninitialized. */
|
||
4 years ago
|
bool upb_inttable_init(upb_inttable *table, upb_arena *a);
|
||
|
bool upb_strtable_init(upb_strtable *table, size_t expected_size, upb_arena *a);
|
||
16 years ago
|
|
||
10 years ago
|
/* Returns the number of values in the table. */
|
||
13 years ago
|
size_t upb_inttable_count(const upb_inttable *t);
|
||
12 years ago
|
UPB_INLINE size_t upb_strtable_count(const upb_strtable *t) {
|
||
|
return t->t.count;
|
||
|
}
|
||
16 years ago
|
|
||
5 years ago
|
void upb_strtable_clear(upb_strtable *t);
|
||
8 years ago
|
|
||
10 years ago
|
/* Inserts the given key into the hashtable with the given value. The key must
|
||
|
* not already exist in the hash table. For string tables, the key must be
|
||
|
* NULL-terminated, and the table will make an internal copy of the key.
|
||
|
* Inttables must not insert a value of UINTPTR_MAX.
|
||
|
*
|
||
|
* If a table resize was required but memory allocation failed, false is
|
||
|
* returned and the table is unchanged. */
|
||
4 years ago
|
bool upb_inttable_insert(upb_inttable *t, uintptr_t key, upb_value val,
|
||
|
upb_arena *a);
|
||
|
bool upb_strtable_insert(upb_strtable *t, const char *key, size_t len,
|
||
|
upb_value val, upb_arena *a);
|
||
13 years ago
|
|
||
10 years ago
|
/* Looks up key in this table, returning "true" if the key was found.
|
||
|
* If v is non-NULL, copies the value for this key into *v. */
|
||
12 years ago
|
bool upb_inttable_lookup(const upb_inttable *t, uintptr_t key, upb_value *v);
|
||
10 years ago
|
bool upb_strtable_lookup2(const upb_strtable *t, const char *key, size_t len,
|
||
|
upb_value *v);
|
||
|
|
||
10 years ago
|
/* For NULL-terminated strings. */
|
||
10 years ago
|
UPB_INLINE bool upb_strtable_lookup(const upb_strtable *t, const char *key,
|
||
|
upb_value *v) {
|
||
|
return upb_strtable_lookup2(t, key, strlen(key), v);
|
||
|
}
|
||
13 years ago
|
|
||
10 years ago
|
/* Removes an item from the table. Returns true if the remove was successful,
|
||
|
* and stores the removed item in *val if non-NULL. */
|
||
13 years ago
|
bool upb_inttable_remove(upb_inttable *t, uintptr_t key, upb_value *val);
|
||
4 years ago
|
bool upb_strtable_remove(upb_strtable *t, const char *key, size_t len,
|
||
|
upb_value *val);
|
||
12 years ago
|
|
||
10 years ago
|
/* Updates an existing entry in an inttable. If the entry does not exist,
|
||
|
* returns false and does nothing. Unlike insert/remove, this does not
|
||
|
* invalidate iterators. */
|
||
11 years ago
|
bool upb_inttable_replace(upb_inttable *t, uintptr_t key, upb_value val);
|
||
|
|
||
10 years ago
|
/* Optimizes the table for the current set of entries, for both memory use and
|
||
|
* lookup time. Client should call this after all entries have been inserted;
|
||
|
* inserting more entries is legal, but will likely require a table resize. */
|
||
4 years ago
|
void upb_inttable_compact(upb_inttable *t, upb_arena *a);
|
||
16 years ago
|
|
||
10 years ago
|
/* Exposed for testing only. */
|
||
4 years ago
|
bool upb_strtable_resize(upb_strtable *t, size_t size_lg2, upb_arena *a);
|
||
11 years ago
|
|
||
|
/* Iterators ******************************************************************/
|
||
|
|
||
10 years ago
|
/* Iterators for int and string tables. We are subject to some kind of unusual
|
||
|
* design constraints:
|
||
|
*
|
||
|
* For high-level languages:
|
||
|
* - we must be able to guarantee that we don't crash or corrupt memory even if
|
||
|
* the program accesses an invalidated iterator.
|
||
|
*
|
||
|
* For C++11 range-based for:
|
||
|
* - iterators must be copyable
|
||
|
* - iterators must be comparable
|
||
|
* - it must be possible to construct an "end" value.
|
||
|
*
|
||
|
* Iteration order is undefined.
|
||
|
*
|
||
|
* Modifying the table invalidates iterators. upb_{str,int}table_done() is
|
||
|
* guaranteed to work even on an invalidated iterator, as long as the table it
|
||
|
* is iterating over has not been freed. Calling next() or accessing data from
|
||
|
* an invalidated iterator yields unspecified elements from the table, but it is
|
||
|
* guaranteed not to crash and to return real table elements (except when done()
|
||
|
* is true). */
|
||
11 years ago
|
|
||
14 years ago
|
|
||
|
/* upb_strtable_iter **********************************************************/
|
||
|
|
||
10 years ago
|
/* upb_strtable_iter i;
|
||
|
* upb_strtable_begin(&i, t);
|
||
|
* for(; !upb_strtable_done(&i); upb_strtable_next(&i)) {
|
||
|
* const char *key = upb_strtable_iter_key(&i);
|
||
|
* const upb_value val = upb_strtable_iter_value(&i);
|
||
|
* // ...
|
||
|
* }
|
||
|
*/
|
||
11 years ago
|
|
||
14 years ago
|
typedef struct {
|
||
13 years ago
|
const upb_strtable *t;
|
||
11 years ago
|
size_t index;
|
||
14 years ago
|
} upb_strtable_iter;
|
||
|
|
||
13 years ago
|
void upb_strtable_begin(upb_strtable_iter *i, const upb_strtable *t);
|
||
14 years ago
|
void upb_strtable_next(upb_strtable_iter *i);
|
||
11 years ago
|
bool upb_strtable_done(const upb_strtable_iter *i);
|
||
5 years ago
|
upb_strview upb_strtable_iter_key(const upb_strtable_iter *i);
|
||
11 years ago
|
upb_value upb_strtable_iter_value(const upb_strtable_iter *i);
|
||
|
void upb_strtable_iter_setdone(upb_strtable_iter *i);
|
||
|
bool upb_strtable_iter_isequal(const upb_strtable_iter *i1,
|
||
|
const upb_strtable_iter *i2);
|
||
14 years ago
|
|
||
16 years ago
|
|
||
14 years ago
|
/* upb_inttable_iter **********************************************************/
|
||
16 years ago
|
|
||
10 years ago
|
/* upb_inttable_iter i;
|
||
|
* upb_inttable_begin(&i, t);
|
||
|
* for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
|
||
|
* uintptr_t key = upb_inttable_iter_key(&i);
|
||
|
* upb_value val = upb_inttable_iter_value(&i);
|
||
|
* // ...
|
||
|
* }
|
||
|
*/
|
||
11 years ago
|
|
||
14 years ago
|
typedef struct {
|
||
13 years ago
|
const upb_inttable *t;
|
||
11 years ago
|
size_t index;
|
||
14 years ago
|
bool array_part;
|
||
|
} upb_inttable_iter;
|
||
|
|
||
5 years ago
|
UPB_INLINE const upb_tabent *str_tabent(const upb_strtable_iter *i) {
|
||
|
return &i->t->t.entries[i->index];
|
||
|
}
|
||
|
|
||
13 years ago
|
void upb_inttable_begin(upb_inttable_iter *i, const upb_inttable *t);
|
||
|
void upb_inttable_next(upb_inttable_iter *i);
|
||
11 years ago
|
bool upb_inttable_done(const upb_inttable_iter *i);
|
||
|
uintptr_t upb_inttable_iter_key(const upb_inttable_iter *i);
|
||
|
upb_value upb_inttable_iter_value(const upb_inttable_iter *i);
|
||
|
void upb_inttable_iter_setdone(upb_inttable_iter *i);
|
||
|
bool upb_inttable_iter_isequal(const upb_inttable_iter *i1,
|
||
|
const upb_inttable_iter *i2);
|
||
|
|
||
7 years ago
|
|
||
|
#ifdef __cplusplus
|
||
|
} /* extern "C" */
|
||
|
#endif
|
||
16 years ago
|
|
||
6 years ago
|
#include "upb/port_undef.inc"
|
||
|
|
||
16 years ago
|
#endif /* UPB_TABLE_H_ */
|