• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef UPB_MESSAGE_INTERNAL_MAP_H_
9 #define UPB_MESSAGE_INTERNAL_MAP_H_
10 
11 #include <stddef.h>
12 #include <string.h>
13 
14 #include "upb/base/descriptor_constants.h"
15 #include "upb/base/string_view.h"
16 #include "upb/hash/str_table.h"
17 #include "upb/mem/arena.h"
18 
19 // Must be last.
20 #include "upb/port/def.inc"
21 
22 typedef enum {
23   kUpb_MapInsertStatus_Inserted = 0,
24   kUpb_MapInsertStatus_Replaced = 1,
25   kUpb_MapInsertStatus_OutOfMemory = 2,
26 } upb_MapInsertStatus;
27 
28 // EVERYTHING BELOW THIS LINE IS INTERNAL - DO NOT USE /////////////////////////
29 
30 struct upb_Map {
31   // Size of key and val, based on the map type.
32   // Strings are represented as '0' because they must be handled specially.
33   char key_size;
34   char val_size;
35   bool UPB_PRIVATE(is_frozen);
36 
37   upb_strtable table;
38 };
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
UPB_PRIVATE(_upb_Map_ShallowFreeze)44 UPB_INLINE void UPB_PRIVATE(_upb_Map_ShallowFreeze)(struct upb_Map* map) {
45   map->UPB_PRIVATE(is_frozen) = true;
46 }
47 
upb_Map_IsFrozen(const struct upb_Map * map)48 UPB_API_INLINE bool upb_Map_IsFrozen(const struct upb_Map* map) {
49   return map->UPB_PRIVATE(is_frozen);
50 }
51 
52 // Converting between internal table representation and user values.
53 //
54 // _upb_map_tokey() and _upb_map_fromkey() are inverses.
55 // _upb_map_tovalue() and _upb_map_fromvalue() are inverses.
56 //
57 // These functions account for the fact that strings are treated differently
58 // from other types when stored in a map.
59 
_upb_map_tokey(const void * key,size_t size)60 UPB_INLINE upb_StringView _upb_map_tokey(const void* key, size_t size) {
61   if (size == UPB_MAPTYPE_STRING) {
62     return *(upb_StringView*)key;
63   } else {
64     return upb_StringView_FromDataAndSize((const char*)key, size);
65   }
66 }
67 
_upb_map_fromkey(upb_StringView key,void * out,size_t size)68 UPB_INLINE void _upb_map_fromkey(upb_StringView key, void* out, size_t size) {
69   if (size == UPB_MAPTYPE_STRING) {
70     memcpy(out, &key, sizeof(key));
71   } else {
72     memcpy(out, key.data, size);
73   }
74 }
75 
_upb_map_tovalue(const void * val,size_t size,upb_value * msgval,upb_Arena * a)76 UPB_INLINE bool _upb_map_tovalue(const void* val, size_t size,
77                                  upb_value* msgval, upb_Arena* a) {
78   if (size == UPB_MAPTYPE_STRING) {
79     upb_StringView* strp = (upb_StringView*)upb_Arena_Malloc(a, sizeof(*strp));
80     if (!strp) return false;
81     *strp = *(upb_StringView*)val;
82     *msgval = upb_value_ptr(strp);
83   } else {
84     memcpy(msgval, val, size);
85   }
86   return true;
87 }
88 
_upb_map_fromvalue(upb_value val,void * out,size_t size)89 UPB_INLINE void _upb_map_fromvalue(upb_value val, void* out, size_t size) {
90   if (size == UPB_MAPTYPE_STRING) {
91     const upb_StringView* strp = (const upb_StringView*)upb_value_getptr(val);
92     memcpy(out, strp, sizeof(upb_StringView));
93   } else {
94     memcpy(out, &val, size);
95   }
96 }
97 
_upb_map_next(const struct upb_Map * map,size_t * iter)98 UPB_INLINE void* _upb_map_next(const struct upb_Map* map, size_t* iter) {
99   upb_strtable_iter it;
100   it.t = &map->table;
101   it.index = *iter;
102   upb_strtable_next(&it);
103   *iter = it.index;
104   if (upb_strtable_done(&it)) return NULL;
105   return (void*)str_tabent(&it);
106 }
107 
_upb_Map_Clear(struct upb_Map * map)108 UPB_INLINE void _upb_Map_Clear(struct upb_Map* map) {
109   UPB_ASSERT(!upb_Map_IsFrozen(map));
110 
111   upb_strtable_clear(&map->table);
112 }
113 
_upb_Map_Delete(struct upb_Map * map,const void * key,size_t key_size,upb_value * val)114 UPB_INLINE bool _upb_Map_Delete(struct upb_Map* map, const void* key,
115                                 size_t key_size, upb_value* val) {
116   UPB_ASSERT(!upb_Map_IsFrozen(map));
117 
118   upb_StringView k = _upb_map_tokey(key, key_size);
119   return upb_strtable_remove2(&map->table, k.data, k.size, val);
120 }
121 
_upb_Map_Get(const struct upb_Map * map,const void * key,size_t key_size,void * val,size_t val_size)122 UPB_INLINE bool _upb_Map_Get(const struct upb_Map* map, const void* key,
123                              size_t key_size, void* val, size_t val_size) {
124   upb_value tabval;
125   upb_StringView k = _upb_map_tokey(key, key_size);
126   bool ret = upb_strtable_lookup2(&map->table, k.data, k.size, &tabval);
127   if (ret && val) {
128     _upb_map_fromvalue(tabval, val, val_size);
129   }
130   return ret;
131 }
132 
_upb_Map_Insert(struct upb_Map * map,const void * key,size_t key_size,void * val,size_t val_size,upb_Arena * a)133 UPB_INLINE upb_MapInsertStatus _upb_Map_Insert(struct upb_Map* map,
134                                                const void* key, size_t key_size,
135                                                void* val, size_t val_size,
136                                                upb_Arena* a) {
137   UPB_ASSERT(!upb_Map_IsFrozen(map));
138 
139   upb_StringView strkey = _upb_map_tokey(key, key_size);
140   upb_value tabval = {0};
141   if (!_upb_map_tovalue(val, val_size, &tabval, a)) {
142     return kUpb_MapInsertStatus_OutOfMemory;
143   }
144 
145   // TODO: add overwrite operation to minimize number of lookups.
146   bool removed =
147       upb_strtable_remove2(&map->table, strkey.data, strkey.size, NULL);
148   if (!upb_strtable_insert(&map->table, strkey.data, strkey.size, tabval, a)) {
149     return kUpb_MapInsertStatus_OutOfMemory;
150   }
151   return removed ? kUpb_MapInsertStatus_Replaced
152                  : kUpb_MapInsertStatus_Inserted;
153 }
154 
_upb_Map_Size(const struct upb_Map * map)155 UPB_INLINE size_t _upb_Map_Size(const struct upb_Map* map) {
156   return map->table.t.count;
157 }
158 
159 // Strings/bytes are special-cased in maps.
160 extern char _upb_Map_CTypeSizeTable[12];
161 
_upb_Map_CTypeSize(upb_CType ctype)162 UPB_INLINE size_t _upb_Map_CTypeSize(upb_CType ctype) {
163   return _upb_Map_CTypeSizeTable[ctype];
164 }
165 
166 // Creates a new map on the given arena with this key/value type.
167 struct upb_Map* _upb_Map_New(upb_Arena* a, size_t key_size, size_t value_size);
168 
169 #ifdef __cplusplus
170 } /* extern "C" */
171 #endif
172 
173 #include "upb/port/undef.inc"
174 
175 #endif /* UPB_MESSAGE_INTERNAL_MAP_H_ */
176