File indexing completed on 2026-09-26 09:15:11
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027 #ifndef UPB_HASH_COMMON_H_
0028 #define UPB_HASH_COMMON_H_
0029
0030 #include <stdint.h>
0031 #include <string.h>
0032
0033 #include "upb/base/string_view.h"
0034
0035
0036 #include "upb/port/def.inc"
0037
0038 #ifdef __cplusplus
0039 extern "C" {
0040 #endif
0041
0042
0043
0044 typedef struct {
0045 uint64_t val;
0046 } upb_value;
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056 #define FUNCS(name, membername, type_t, converter) \
0057 UPB_INLINE void upb_value_set##name(upb_value* val, type_t cval) { \
0058 val->val = (uint64_t)cval; \
0059 } \
0060 UPB_INLINE upb_value upb_value_##name(type_t val) { \
0061 upb_value ret; \
0062 upb_value_set##name(&ret, val); \
0063 return ret; \
0064 } \
0065 UPB_INLINE type_t upb_value_get##name(upb_value val) { \
0066 return (type_t)(converter)val.val; \
0067 }
0068
0069 FUNCS(int32, int32, int32_t, int32_t)
0070 FUNCS(int64, int64, int64_t, int64_t)
0071 FUNCS(uint32, uint32, uint32_t, uint32_t)
0072 FUNCS(uint64, uint64, uint64_t, uint64_t)
0073 FUNCS(bool, _bool, bool, bool)
0074 FUNCS(cstr, cstr, char*, uintptr_t)
0075 FUNCS(uintptr, uptr, uintptr_t, uintptr_t)
0076 FUNCS(ptr, ptr, void*, uintptr_t)
0077 FUNCS(constptr, constptr, const void*, uintptr_t)
0078
0079 #undef FUNCS
0080
0081 UPB_INLINE void upb_value_setfloat(upb_value* val, float cval) {
0082 memcpy(&val->val, &cval, sizeof(cval));
0083 }
0084
0085 UPB_INLINE void upb_value_setdouble(upb_value* val, double cval) {
0086 memcpy(&val->val, &cval, sizeof(cval));
0087 }
0088
0089 UPB_INLINE upb_value upb_value_float(float cval) {
0090 upb_value ret;
0091 upb_value_setfloat(&ret, cval);
0092 return ret;
0093 }
0094
0095 UPB_INLINE upb_value upb_value_double(double cval) {
0096 upb_value ret;
0097 upb_value_setdouble(&ret, cval);
0098 return ret;
0099 }
0100
0101
0102
0103
0104 typedef struct {
0105 uint32_t size;
0106 const char data[];
0107 } upb_SizePrefixString;
0108
0109
0110
0111
0112
0113
0114 typedef union {
0115 uintptr_t num;
0116 const upb_SizePrefixString* str;
0117 } upb_key;
0118
0119 UPB_INLINE upb_StringView upb_key_strview(upb_key key) {
0120 return upb_StringView_FromDataAndSize(key.str->data, key.str->size);
0121 }
0122
0123
0124
0125 typedef struct _upb_tabent {
0126 upb_value val;
0127 upb_key key;
0128
0129
0130
0131
0132
0133 const struct _upb_tabent* next;
0134 } upb_tabent;
0135
0136 typedef struct {
0137 upb_tabent* entries;
0138
0139 uint32_t count;
0140
0141
0142 uint32_t mask;
0143 } upb_table;
0144
0145 UPB_INLINE size_t upb_table_size(const upb_table* t) { return t->mask + 1; }
0146
0147
0148
0149 UPB_INLINE upb_key upb_key_empty(void) {
0150 upb_key ret;
0151 memset(&ret, 0, sizeof(upb_key));
0152 return ret;
0153 }
0154
0155 UPB_INLINE bool upb_tabent_isempty(const upb_tabent* e) {
0156 upb_key key = e->key;
0157 UPB_ASSERT(sizeof(key.num) == sizeof(key.str));
0158 uintptr_t val;
0159 memcpy(&val, &key, sizeof(val));
0160
0161
0162
0163
0164 return val == 0;
0165 }
0166
0167 uint32_t _upb_Hash(const void* p, size_t n, uint64_t seed);
0168
0169 #ifdef __cplusplus
0170 }
0171 #endif
0172
0173 #include "upb/port/undef.inc"
0174
0175 #endif