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.
134 lines
4.5 KiB
134 lines
4.5 KiB
16 years ago
|
/*
|
||
|
* upb - a minimalist implementation of protocol buffers.
|
||
|
*
|
||
|
* Copyright (c) 2009 Joshua Haberman. See LICENSE for details.
|
||
16 years ago
|
*
|
||
|
* This file defines very fast int->struct (inttable) and string->struct
|
||
|
* (strtable) hash tables. The struct can be of any size, and it is stored
|
||
|
* in the table itself, for cache-friendly performance.
|
||
|
*
|
||
|
* The table uses internal chaining with Brent's variation (inspired by the
|
||
|
* Lua implementation of hash tables). The hash function for strings is
|
||
|
* Austin Appleby's "MurmurHash."
|
||
16 years ago
|
*/
|
||
|
|
||
|
#ifndef UPB_TABLE_H_
|
||
|
#define UPB_TABLE_H_
|
||
|
|
||
16 years ago
|
#include <assert.h>
|
||
16 years ago
|
#include "upb.h"
|
||
15 years ago
|
#include "upb_string.h"
|
||
16 years ago
|
|
||
|
#ifdef __cplusplus
|
||
|
extern "C" {
|
||
|
#endif
|
||
|
|
||
16 years ago
|
/* Note: the key cannot be zero! Zero is used by the implementation. */
|
||
16 years ago
|
typedef uint32_t upb_inttable_key_t;
|
||
16 years ago
|
|
||
16 years ago
|
#define UPB_END_OF_CHAIN (uint32_t)0
|
||
16 years ago
|
#define UPB_EMPTY_ENTRY (uint32_t)0
|
||
16 years ago
|
|
||
15 years ago
|
typedef struct {
|
||
16 years ago
|
upb_inttable_key_t key;
|
||
16 years ago
|
uint32_t next; /* Internal chaining. */
|
||
15 years ago
|
} upb_inttable_entry;
|
||
16 years ago
|
|
||
15 years ago
|
// TODO: consider storing the hash in the entry. This would avoid the need to
|
||
|
// rehash on table resizes, but more importantly could possibly improve lookup
|
||
|
// performance by letting us compare hashes before comparing lengths or the
|
||
|
// strings themselves.
|
||
15 years ago
|
typedef struct {
|
||
15 years ago
|
upb_string *key; // We own a ref.
|
||
15 years ago
|
uint32_t next; // Internal chaining.
|
||
15 years ago
|
} upb_strtable_entry;
|
||
16 years ago
|
|
||
15 years ago
|
typedef struct {
|
||
16 years ago
|
void *entries;
|
||
16 years ago
|
uint32_t count; /* How many elements are currently in the table? */
|
||
|
uint16_t entry_size; /* How big is each entry? */
|
||
|
uint8_t size_lg2; /* The table is 2^size_lg2 in size. */
|
||
16 years ago
|
uint32_t mask;
|
||
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 {
|
||
|
upb_table t;
|
||
|
} upb_inttable;
|
||
16 years ago
|
|
||
|
/* Initialize and free a table, respectively. Specify the initial size
|
||
|
* with 'size' (the size will be increased as necessary). Entry size
|
||
|
* specifies how many bytes each entry in the table is. */
|
||
15 years ago
|
void upb_inttable_init(upb_inttable *table, uint32_t size, uint16_t entry_size);
|
||
|
void upb_inttable_free(upb_inttable *table);
|
||
|
void upb_strtable_init(upb_strtable *table, uint32_t size, uint16_t entry_size);
|
||
|
void upb_strtable_free(upb_strtable *table);
|
||
|
|
||
|
INLINE uint32_t upb_table_size(upb_table *t) { return 1 << t->size_lg2; }
|
||
|
INLINE uint32_t upb_inttable_size(upb_inttable *t) {
|
||
16 years ago
|
return upb_table_size(&t->t);
|
||
16 years ago
|
}
|
||
15 years ago
|
INLINE uint32_t upb_strtable_size(upb_strtable *t) {
|
||
16 years ago
|
return upb_table_size(&t->t);
|
||
16 years ago
|
}
|
||
|
|
||
15 years ago
|
INLINE uint32_t upb_table_count(upb_table *t) { return t->count; }
|
||
|
INLINE uint32_t upb_inttable_count(upb_inttable *t) {
|
||
16 years ago
|
return upb_table_count(&t->t);
|
||
|
}
|
||
15 years ago
|
INLINE uint32_t upb_strtable_count(upb_strtable *t) {
|
||
16 years ago
|
return upb_table_count(&t->t);
|
||
|
}
|
||
|
|
||
16 years ago
|
/* Inserts the given key into the hashtable with the given value. The key must
|
||
|
* not already exist in the hash table. The data will be copied from e into
|
||
|
* the hashtable (the amount of data copied comes from entry_size when the
|
||
|
* table was constructed). Therefore the data at val may be freed once the
|
||
|
* call returns. */
|
||
15 years ago
|
void upb_inttable_insert(upb_inttable *t, upb_inttable_entry *e);
|
||
|
void upb_strtable_insert(upb_strtable *t, upb_strtable_entry *e);
|
||
16 years ago
|
|
||
15 years ago
|
INLINE uint32_t upb_inttable_bucket(upb_inttable *t, upb_inttable_key_t k) {
|
||
16 years ago
|
return (k & t->t.mask) + 1; /* Identity hash for ints. */
|
||
16 years ago
|
}
|
||
|
|
||
15 years ago
|
/* Looks up key in this table. Inlined because this is in the critical path of
|
||
|
* decoding. We have the caller specify the entry_size because fixing this as
|
||
|
* a literal (instead of reading table->entry_size) gives the compiler more
|
||
|
* ability to optimize. */
|
||
|
INLINE void *upb_inttable_fastlookup(upb_inttable *t, uint32_t key,
|
||
|
uint32_t entry_size) {
|
||
16 years ago
|
assert(key != 0);
|
||
16 years ago
|
uint32_t bucket = upb_inttable_bucket(t, key);
|
||
15 years ago
|
upb_inttable_entry *e;
|
||
16 years ago
|
do {
|
||
15 years ago
|
e = (upb_inttable_entry*)UPB_INDEX(t->t.entries, bucket-1, entry_size);
|
||
16 years ago
|
if(e->key == key) return e;
|
||
|
} while((bucket = e->next) != UPB_END_OF_CHAIN);
|
||
|
return NULL; /* Not found. */
|
||
16 years ago
|
}
|
||
|
|
||
15 years ago
|
INLINE void *upb_inttable_lookup(upb_inttable *t, uint32_t key) {
|
||
|
return upb_inttable_fastlookup(t, key, t->t.entry_size);
|
||
16 years ago
|
}
|
||
|
|
||
15 years ago
|
void *upb_strtable_lookup(upb_strtable *t, upb_string *key);
|
||
16 years ago
|
|
||
16 years ago
|
/* Provides iteration over the table. The order in which the entries are
|
||
|
* returned is undefined. Insertions invalidate iterators. The _next
|
||
|
* functions return NULL when the end has been reached. */
|
||
15 years ago
|
void *upb_inttable_begin(upb_inttable *t);
|
||
|
void *upb_inttable_next(upb_inttable *t, upb_inttable_entry *cur);
|
||
16 years ago
|
|
||
15 years ago
|
void *upb_strtable_begin(upb_strtable *t);
|
||
|
void *upb_strtable_next(upb_strtable *t, upb_strtable_entry *cur);
|
||
16 years ago
|
|
||
16 years ago
|
#ifdef __cplusplus
|
||
|
} /* extern "C" */
|
||
|
#endif
|
||
|
|
||
|
#endif /* UPB_TABLE_H_ */
|