Move the map-related functions from msg_internal.h that are only used in generated code into map_gencode_util.h. Then move the rest of the map-related functions from msg_internal.h into map_internal.h. PiperOrigin-RevId: 486299140pull/13171/head
parent
a4779ef5f8
commit
fd040a8bff
12 changed files with 323 additions and 242 deletions
@ -0,0 +1,122 @@ |
||||
/*
|
||||
* Copyright (c) 2009-2021, Google LLC |
||||
* 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. |
||||
*/ |
||||
|
||||
// These functions are only used by generated code.
|
||||
|
||||
#ifndef UPB_COLLECTIONS_MAP_GENCODE_UTIL_H_ |
||||
#define UPB_COLLECTIONS_MAP_GENCODE_UTIL_H_ |
||||
|
||||
#include "upb/collections/map_internal.h" |
||||
|
||||
// Must be last.
|
||||
#include "upb/port_def.inc" |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
// Message map operations, these get the map from the message first.
|
||||
|
||||
UPB_INLINE size_t _upb_msg_map_size(const upb_Message* msg, size_t ofs) { |
||||
upb_Map* map = *UPB_PTR_AT(msg, ofs, upb_Map*); |
||||
return map ? _upb_Map_Size(map) : 0; |
||||
} |
||||
|
||||
UPB_INLINE bool _upb_msg_map_get(const upb_Message* msg, size_t ofs, |
||||
const void* key, size_t key_size, void* val, |
||||
size_t val_size) { |
||||
upb_Map* map = *UPB_PTR_AT(msg, ofs, upb_Map*); |
||||
if (!map) return false; |
||||
return _upb_Map_Get(map, key, key_size, val, val_size); |
||||
} |
||||
|
||||
UPB_INLINE void* _upb_msg_map_next(const upb_Message* msg, size_t ofs, |
||||
size_t* iter) { |
||||
upb_Map* map = *UPB_PTR_AT(msg, ofs, upb_Map*); |
||||
if (!map) return NULL; |
||||
return _upb_map_next(map, iter); |
||||
} |
||||
|
||||
UPB_INLINE bool _upb_msg_map_set(upb_Message* msg, size_t ofs, const void* key, |
||||
size_t key_size, void* val, size_t val_size, |
||||
upb_Arena* arena) { |
||||
upb_Map** map = UPB_PTR_AT(msg, ofs, upb_Map*); |
||||
if (!*map) { |
||||
*map = _upb_Map_New(arena, key_size, val_size); |
||||
} |
||||
return _upb_Map_Insert(*map, key, key_size, val, val_size, arena) != |
||||
kUpb_MapInsertStatus_OutOfMemory; |
||||
} |
||||
|
||||
UPB_INLINE bool _upb_msg_map_delete(upb_Message* msg, size_t ofs, |
||||
const void* key, size_t key_size) { |
||||
upb_Map* map = *UPB_PTR_AT(msg, ofs, upb_Map*); |
||||
if (!map) return false; |
||||
return _upb_Map_Delete(map, key, key_size); |
||||
} |
||||
|
||||
UPB_INLINE void _upb_msg_map_clear(upb_Message* msg, size_t ofs) { |
||||
upb_Map* map = *UPB_PTR_AT(msg, ofs, upb_Map*); |
||||
if (!map) return; |
||||
_upb_Map_Clear(map); |
||||
} |
||||
|
||||
UPB_INLINE void _upb_msg_map_key(const void* msg, void* key, size_t size) { |
||||
const upb_tabent* ent = (const upb_tabent*)msg; |
||||
uint32_t u32len; |
||||
upb_StringView k; |
||||
k.data = upb_tabstr(ent->key, &u32len); |
||||
k.size = u32len; |
||||
_upb_map_fromkey(k, key, size); |
||||
} |
||||
|
||||
UPB_INLINE void _upb_msg_map_value(const void* msg, void* val, size_t size) { |
||||
const upb_tabent* ent = (const upb_tabent*)msg; |
||||
upb_value v = {ent->val.val}; |
||||
_upb_map_fromvalue(v, val, size); |
||||
} |
||||
|
||||
UPB_INLINE void _upb_msg_map_set_value(void* msg, const void* val, |
||||
size_t size) { |
||||
upb_tabent* ent = (upb_tabent*)msg; |
||||
// This is like _upb_map_tovalue() except the entry already exists
|
||||
// so we can reuse the allocated upb_StringView for string fields.
|
||||
if (size == UPB_MAPTYPE_STRING) { |
||||
upb_StringView* strp = (upb_StringView*)(uintptr_t)ent->val.val; |
||||
memcpy(strp, val, sizeof(*strp)); |
||||
} else { |
||||
memcpy(&ent->val.val, val, size); |
||||
} |
||||
} |
||||
|
||||
#ifdef __cplusplus |
||||
} /* extern "C" */ |
||||
#endif |
||||
|
||||
#include "upb/port_undef.inc" |
||||
|
||||
#endif /* UPB_COLLECTIONS_MAP_GENCODE_UTIL_H_ */ |
@ -0,0 +1,163 @@ |
||||
/*
|
||||
* Copyright (c) 2009-2021, Google LLC |
||||
* 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. |
||||
*/ |
||||
|
||||
// EVERYTHING BELOW THIS LINE IS INTERNAL - DO NOT USE /////////////////////////
|
||||
|
||||
#ifndef UPB_COLLECTIONS_MAP_INTERNAL_H_ |
||||
#define UPB_COLLECTIONS_MAP_INTERNAL_H_ |
||||
|
||||
#include "upb/collections/map.h" |
||||
#include "upb/internal/table.h" |
||||
#include "upb/mem/arena.h" |
||||
#include "upb/string_view.h" |
||||
|
||||
// Must be last.
|
||||
#include "upb/port_def.inc" |
||||
|
||||
struct upb_Map { |
||||
// Size of key and val, based on the map type.
|
||||
// Strings are represented as '0' because they must be handled specially.
|
||||
char key_size; |
||||
char val_size; |
||||
|
||||
upb_strtable table; |
||||
}; |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
// Converting between internal table representation and user values.
|
||||
//
|
||||
// _upb_map_tokey() and _upb_map_fromkey() are inverses.
|
||||
// _upb_map_tovalue() and _upb_map_fromvalue() are inverses.
|
||||
//
|
||||
// These functions account for the fact that strings are treated differently
|
||||
// from other types when stored in a map.
|
||||
|
||||
UPB_INLINE upb_StringView _upb_map_tokey(const void* key, size_t size) { |
||||
if (size == UPB_MAPTYPE_STRING) { |
||||
return *(upb_StringView*)key; |
||||
} else { |
||||
return upb_StringView_FromDataAndSize((const char*)key, size); |
||||
} |
||||
} |
||||
|
||||
UPB_INLINE void _upb_map_fromkey(upb_StringView key, void* out, size_t size) { |
||||
if (size == UPB_MAPTYPE_STRING) { |
||||
memcpy(out, &key, sizeof(key)); |
||||
} else { |
||||
memcpy(out, key.data, size); |
||||
} |
||||
} |
||||
|
||||
UPB_INLINE bool _upb_map_tovalue(const void* val, size_t size, |
||||
upb_value* msgval, upb_Arena* a) { |
||||
if (size == UPB_MAPTYPE_STRING) { |
||||
upb_StringView* strp = (upb_StringView*)upb_Arena_Malloc(a, sizeof(*strp)); |
||||
if (!strp) return false; |
||||
*strp = *(upb_StringView*)val; |
||||
*msgval = upb_value_ptr(strp); |
||||
} else { |
||||
memcpy(msgval, val, size); |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
UPB_INLINE void _upb_map_fromvalue(upb_value val, void* out, size_t size) { |
||||
if (size == UPB_MAPTYPE_STRING) { |
||||
const upb_StringView* strp = (const upb_StringView*)upb_value_getptr(val); |
||||
memcpy(out, strp, sizeof(upb_StringView)); |
||||
} else { |
||||
memcpy(out, &val, size); |
||||
} |
||||
} |
||||
|
||||
UPB_INLINE void* _upb_map_next(const upb_Map* map, size_t* iter) { |
||||
upb_strtable_iter it; |
||||
it.t = &map->table; |
||||
it.index = *iter; |
||||
upb_strtable_next(&it); |
||||
*iter = it.index; |
||||
if (upb_strtable_done(&it)) return NULL; |
||||
return (void*)str_tabent(&it); |
||||
} |
||||
|
||||
UPB_INLINE void _upb_Map_Clear(upb_Map* map) { |
||||
upb_strtable_clear(&map->table); |
||||
} |
||||
|
||||
UPB_INLINE bool _upb_Map_Delete(upb_Map* map, const void* key, |
||||
size_t key_size) { |
||||
upb_StringView k = _upb_map_tokey(key, key_size); |
||||
return upb_strtable_remove2(&map->table, k.data, k.size, NULL); |
||||
} |
||||
|
||||
UPB_INLINE bool _upb_Map_Get(const upb_Map* map, const void* key, |
||||
size_t key_size, void* val, size_t val_size) { |
||||
upb_value tabval; |
||||
upb_StringView k = _upb_map_tokey(key, key_size); |
||||
bool ret = upb_strtable_lookup2(&map->table, k.data, k.size, &tabval); |
||||
if (ret && val) { |
||||
_upb_map_fromvalue(tabval, val, val_size); |
||||
} |
||||
return ret; |
||||
} |
||||
|
||||
UPB_INLINE upb_MapInsertStatus _upb_Map_Insert(upb_Map* map, const void* key, |
||||
size_t key_size, void* val, |
||||
size_t val_size, upb_Arena* a) { |
||||
upb_StringView strkey = _upb_map_tokey(key, key_size); |
||||
upb_value tabval = {0}; |
||||
if (!_upb_map_tovalue(val, val_size, &tabval, a)) { |
||||
return kUpb_MapInsertStatus_OutOfMemory; |
||||
} |
||||
|
||||
// TODO(haberman): add overwrite operation to minimize number of lookups.
|
||||
bool removed = |
||||
upb_strtable_remove2(&map->table, strkey.data, strkey.size, NULL); |
||||
if (!upb_strtable_insert(&map->table, strkey.data, strkey.size, tabval, a)) { |
||||
return kUpb_MapInsertStatus_OutOfMemory; |
||||
} |
||||
return removed ? kUpb_MapInsertStatus_Replaced |
||||
: kUpb_MapInsertStatus_Inserted; |
||||
} |
||||
|
||||
UPB_INLINE size_t _upb_Map_Size(const upb_Map* map) { |
||||
return map->table.t.count; |
||||
} |
||||
|
||||
// Creates a new map on the given arena with this key/value type.
|
||||
upb_Map* _upb_Map_New(upb_Arena* a, size_t key_size, size_t value_size); |
||||
|
||||
#ifdef __cplusplus |
||||
} /* extern "C" */ |
||||
#endif |
||||
|
||||
#include "upb/port_undef.inc" |
||||
|
||||
#endif /* UPB_COLLECTIONS_MAP_INTERNAL_H_ */ |
Loading…
Reference in new issue