WPILibC++ 2027.0.0-alpha-3
Loading...
Searching...
No Matches
common.h
Go to the documentation of this file.
1// Protocol Buffers - Google's data interchange format
2// Copyright 2023 Google LLC. All rights reserved.
3//
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file or at
6// https://developers.google.com/open-source/licenses/bsd
7
8/*
9 * upb_table
10 *
11 * This header is INTERNAL-ONLY! Its interfaces are not public or stable!
12 * This file defines very fast int->upb_value (inttable) and string->upb_value
13 * (strtable) hash tables.
14 *
15 * The table uses chained scatter with Brent's variation (inspired by the Lua
16 * implementation of hash tables). The hash function for strings is Austin
17 * Appleby's "MurmurHash."
18 *
19 * The inttable uses uintptr_t as its key, which guarantees it can be used to
20 * store pointers or integers of at least 32 bits (upb isn't really useful on
21 * systems where sizeof(void*) < 4).
22 *
23 * The table must be homogeneous (all values of the same type). In debug
24 * mode, we check this on insert and lookup.
25 */
26
27#ifndef UPB_HASH_COMMON_H_
28#define UPB_HASH_COMMON_H_
29
30#include <stdint.h>
31#include <string.h>
32
34
35// Must be last.
36#include "upb/port/def.inc"
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42/* upb_value ******************************************************************/
43
44typedef struct {
45 uint64_t val;
46} upb_value;
47
48/* For each value ctype, define the following set of functions:
49 *
50 * // Get/set an int32 from a upb_value.
51 * int32_t upb_value_getint32(upb_value val);
52 * void upb_value_setint32(upb_value *val, int32_t cval);
53 *
54 * // Construct a new upb_value from an int32.
55 * upb_value upb_value_int32(int32_t val); */
56#define FUNCS(name, membername, type_t, converter) \
57 UPB_INLINE void upb_value_set##name(upb_value* val, type_t cval) { \
58 val->val = (uint64_t)cval; \
59 } \
60 UPB_INLINE upb_value upb_value_##name(type_t val) { \
61 upb_value ret; \
62 upb_value_set##name(&ret, val); \
63 return ret; \
64 } \
65 UPB_INLINE type_t upb_value_get##name(upb_value val) { \
66 return (type_t)(converter)val.val; \
67 }
68
69FUNCS(int32, int32, int32_t, int32_t)
70FUNCS(int64, int64, int64_t, int64_t)
71FUNCS(uint32, uint32, uint32_t, uint32_t)
72FUNCS(uint64, uint64, uint64_t, uint64_t)
73FUNCS(bool, _bool, bool, bool)
74FUNCS(cstr, cstr, char*, uintptr_t)
75FUNCS(uintptr, uptr, uintptr_t, uintptr_t)
76FUNCS(ptr, ptr, void*, uintptr_t)
77FUNCS(constptr, constptr, const void*, uintptr_t)
78
79#undef FUNCS
80
81UPB_INLINE void upb_value_setfloat(upb_value* val, float cval) {
82 memcpy(&val->val, &cval, sizeof(cval));
83}
84
85UPB_INLINE void upb_value_setdouble(upb_value* val, double cval) {
86 memcpy(&val->val, &cval, sizeof(cval));
87}
88
90 upb_value ret;
91 upb_value_setfloat(&ret, cval);
92 return ret;
93}
94
96 upb_value ret;
97 upb_value_setdouble(&ret, cval);
98 return ret;
99}
100
101/* upb_key *****************************************************************/
102
103// A uint32 size followed by that number of bytes stored contiguously.
104typedef struct {
105 uint32_t size;
106 const char data[];
108
109/* Either:
110 * 1. an actual integer key
111 * 2. a SizePrefixString*, owned by us.
112 *
113 * ...depending on whether this is a string table or an int table. */
114typedef union {
115 uintptr_t num;
117} upb_key;
118
122
123/* upb_table ******************************************************************/
124
125typedef struct _upb_tabent {
128
129 /* Internal chaining. This is const so we can create static initializers for
130 * tables. We cast away const sometimes, but *only* when the containing
131 * upb_table is known to be non-const. This requires a bit of care, but
132 * the subtlety is confined to table.c. */
133 const struct _upb_tabent* next;
135
136typedef struct {
138 /* Number of entries in the hash part. */
139 uint32_t count;
140
141 /* Mask to turn hash value -> bucket. The map's allocated size is mask + 1.*/
142 uint32_t mask;
143} upb_table;
144
145UPB_INLINE size_t upb_table_size(const upb_table* t) { return t->mask + 1; }
146
147// Internal-only functions, in .h file only out of necessity.
148
150 upb_key ret;
151 memset(&ret, 0, sizeof(upb_key));
152 return ret;
153}
154
156 upb_key key = e->key;
157 UPB_ASSERT(sizeof(key.num) == sizeof(key.str));
158 uintptr_t val;
159 memcpy(&val, &key, sizeof(val));
160 // Note: for upb_inttables a tab_key is a true integer key value, but the
161 // inttable maintains the invariant that 0 value is always stored in the
162 // compact table and never as a upb_tabent* so we can always use the 0
163 // key value to identify an empty tabent.
164 return val == 0;
165}
166
167uint32_t _upb_Hash(const void* p, size_t n, uint64_t seed);
168
169#ifdef __cplusplus
170} /* extern "C" */
171#endif
172
173#include "upb/port/undef.inc"
174
175#endif /* UPB_HASH_COMMON_H_ */
#define UPB_ASSERT(expr)
Definition def.inc:329
#define UPB_INLINE
Definition def.inc:144
T * p
Definition format.h:754
auto ptr(T p) -> const void *
Converts p to const void* for pointer formatting.
Definition format.h:3963
UPB_INLINE upb_key upb_key_empty(void)
Definition common.h:149
UPB_INLINE upb_StringView upb_key_strview(upb_key key)
Definition common.h:119
UPB_INLINE size_t upb_table_size(const upb_table *t)
Definition common.h:145
UPB_INLINE upb_value upb_value_float(float cval)
Definition common.h:89
struct _upb_tabent upb_tabent
UPB_INLINE bool upb_tabent_isempty(const upb_tabent *e)
Definition common.h:155
uint32_t _upb_Hash(const void *p, size_t n, uint64_t seed)
UPB_INLINE upb_value upb_value_double(double cval)
Definition common.h:95
UPB_INLINE void upb_value_setfloat(upb_value *val, float cval)
Definition common.h:81
UPB_INLINE void upb_value_setdouble(upb_value *val, double cval)
Definition common.h:85
#define FUNCS(name, membername, type_t, converter)
Definition common.h:56
UPB_API_INLINE upb_StringView upb_StringView_FromDataAndSize(const char *data, size_t size)
Definition string_view.h:32
Definition common.h:125
upb_key key
Definition common.h:127
const struct _upb_tabent * next
Definition common.h:133
upb_value val
Definition common.h:126
Definition common.h:104
uint32_t size
Definition common.h:105
const char data[]
Definition common.h:106
Definition string_view.h:23
Definition common.h:136
upb_tabent * entries
Definition common.h:137
uint32_t mask
Definition common.h:142
uint32_t count
Definition common.h:139
Definition common.h:44
uint64_t val
Definition common.h:45
Definition common.h:114
uintptr_t num
Definition common.h:115
const upb_SizePrefixString * str
Definition common.h:116