create collections/ subdir for all array and map code

PiperOrigin-RevId: 486013554
pull/13171/head
Eric Salo 2 years ago committed by Copybara-Service
parent 115b878a5c
commit fd14316f38
  1. 51
      BUILD
  2. 53
      upb/array.h
  3. 4
      upb/collections.h
  4. 62
      upb/collections/array.c
  5. 82
      upb/collections/array.h
  6. 16
      upb/collections/array_internal.h
  7. 2
      upb/collections/map.c
  8. 116
      upb/collections/map.h
  9. 2
      upb/collections/map_sorter.c
  10. 6
      upb/collections/map_sorter_internal.h
  11. 10
      upb/collections/message_value.h
  12. 89
      upb/internal/array.c
  13. 2
      upb/json/decode.c
  14. 2
      upb/json/encode.c
  15. 87
      upb/map.h
  16. 2
      upb/mini_table/accessors.c
  17. 2
      upb/mini_table/accessors.h
  18. 2
      upb/mini_table/accessors_test.cc
  19. 2
      upb/reflection/message.c
  20. 2
      upb/reflection/message.h
  21. 4
      upb/text/encode.c
  22. 2
      upb/util/required_fields.c
  23. 2
      upb/wire/decode.c
  24. 2
      upb/wire/decode_fast.c
  25. 4
      upb/wire/encode.c
  26. 4
      upbc/protoc-gen-upb.cc

51
BUILD

@ -109,11 +109,10 @@ cc_library(
cc_library(
name = "upb",
srcs = [
"upb/array.c",
"upb/collections/map_sorter_internal.h",
"upb/collections/message_value.h",
"upb/internal/table.h",
"upb/internal/unicode.h",
"upb/map_sorter.c",
"upb/map_sorter.h",
"upb/msg.c",
"upb/msg_internal.h",
"upb/status.c",
@ -125,12 +124,12 @@ cc_library(
"upb/alloc.h",
"upb/arena.h",
"upb/array.h",
"upb/collections/array.h",
"upb/decode.h",
"upb/encode.h",
"upb/extension_registry.h",
"upb/mem/alloc.h",
"upb/mem/arena.h",
"upb/message_value.h",
"upb/msg.h",
"upb/status.h",
"upb/string_view.h",
@ -142,7 +141,7 @@ cc_library(
copts = UPB_DEFAULT_COPTS,
visibility = ["//visibility:public"],
deps = [
":array_internal",
":collections_internal",
":extension_registry",
":fastdecode",
":mem",
@ -232,8 +231,8 @@ cc_library(
copts = UPB_DEFAULT_COPTS,
visibility = ["//visibility:public"],
deps = [
":array_internal",
":collections",
":collections_internal",
":mini_table",
":mini_table_internal",
":port",
@ -291,7 +290,7 @@ cc_library(
],
copts = UPB_DEFAULT_COPTS,
deps = [
":array_internal",
":collections_internal",
":extension_registry",
":mem_internal",
":port",
@ -309,10 +308,10 @@ cc_library(
cc_library(
name = "generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me",
hdrs = [
"upb/array.h",
"upb/collections/array.h",
"upb/collections/array_internal.h",
"upb/collections/message_value.h",
"upb/extension_registry.h",
"upb/internal/array.h",
"upb/message_value.h",
"upb/msg.h",
"upb/msg_internal.h",
"upb/port_def.inc",
@ -398,22 +397,28 @@ upb_proto_reflection_library(
cc_library(
name = "collections",
srcs = [
"upb/map.c",
"upb/collections/array.c",
"upb/collections/array_internal.h",
"upb/collections/map.c",
"upb/collections/map_sorter.c",
"upb/collections/message_value.h",
"upb/extension_registry.h",
"upb/msg.h",
"upb/msg_internal.h",
],
hdrs = [
"upb/array.h",
"upb/collections.h",
"upb/collections/array.h",
"upb/collections/map.h",
"upb/collections/map_sorter_internal.h",
"upb/map.h",
"upb/map_sorter.h",
"upb/message_value.h",
],
copts = UPB_DEFAULT_COPTS,
visibility = ["//visibility:public"],
deps = [
":mini_table",
":port",
":table_internal",
":upb",
],
)
@ -947,20 +952,20 @@ cc_library(
# Internal C/C++ libraries #####################################################
cc_library(
name = "array_internal",
name = "collections_internal",
srcs = [
"upb/array.h",
"upb/internal/array.c",
"upb/message_value.h",
"upb/collections/array.h",
"upb/collections/message_value.h",
"upb/msg.h",
"upb/status.h",
"upb/string_view.h",
"upb/upb.h",
],
hdrs = ["upb/internal/array.h"],
hdrs = ["upb/collections/array_internal.h"],
copts = UPB_DEFAULT_COPTS,
visibility = ["//:__subpackages__"],
deps = [
":collections",
":mem",
":port",
],
@ -1066,9 +1071,9 @@ upb_amalgamation(
"upb.h",
],
libs = [
":array_internal",
":atoi_internal",
":collections",
":collections_internal",
":descriptor_upb_proto",
":extension_registry",
":fastdecode",
@ -1099,9 +1104,9 @@ upb_amalgamation(
"php-upb.h",
],
libs = [
":array_internal",
":atoi_internal",
":collections",
":collections_internal",
":descriptor_upb_proto",
":descriptor_upb_proto_reflection",
":extension_registry",
@ -1138,9 +1143,9 @@ upb_amalgamation(
"ruby-upb.h",
],
libs = [
":array_internal",
":atoi_internal",
":collections",
":collections_internal",
":descriptor_upb_proto",
":extension_registry",
":fastdecode",

@ -25,58 +25,11 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// This header is deprecated, use upb/collections/array.h instead
#ifndef UPB_ARRAY_H_
#define UPB_ARRAY_H_
#include "upb/message_value.h"
// Must be last.
#include "upb/port_def.inc"
#ifdef __cplusplus
extern "C" {
#endif
/* Creates a new array on the given arena that holds elements of this type. */
upb_Array* upb_Array_New(upb_Arena* a, upb_CType type);
/* Returns the number of elements in the array. */
size_t upb_Array_Size(const upb_Array* arr);
/* Returns the given element, which must be within the array's current size. */
upb_MessageValue upb_Array_Get(const upb_Array* arr, size_t i);
/* Sets the given element, which must be within the array's current size. */
void upb_Array_Set(upb_Array* arr, size_t i, upb_MessageValue val);
/* Appends an element to the array. Returns false on allocation failure. */
bool upb_Array_Append(upb_Array* array, upb_MessageValue val, upb_Arena* arena);
/* Moves elements within the array using memmove(). Like memmove(), the source
* and destination elements may be overlapping. */
void upb_Array_Move(upb_Array* array, size_t dst_idx, size_t src_idx,
size_t count);
/* Inserts one or more empty elements into the array. Existing elements are
* shifted right. The new elements have undefined state and must be set with
* `upb_Array_Set()`.
* REQUIRES: `i <= upb_Array_Size(arr)` */
bool upb_Array_Insert(upb_Array* array, size_t i, size_t count,
upb_Arena* arena);
/* Deletes one or more elements from the array. Existing elements are shifted
* left.
* REQUIRES: `i + count <= upb_Array_Size(arr)` */
void upb_Array_Delete(upb_Array* array, size_t i, size_t count);
/* Changes the size of a vector. New elements are initialized to empty/0.
* Returns false on allocation failure. */
bool upb_Array_Resize(upb_Array* array, size_t size, upb_Arena* arena);
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port_undef.inc"
#include "upb/collections/array.h"
#endif /* UPB_ARRAY_H_ */

@ -30,7 +30,7 @@
// TODO(b/232091617): Delete this entire header which currently exists only for
// temporary backwards compatibility.
#include "upb/array.h"
#include "upb/map.h"
#include "upb/collections/array.h"
#include "upb/collections/map.h"
#endif /* UPB_COLLECTIONS_H_ */

@ -25,10 +25,10 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "upb/internal/array.h"
#include <string.h>
#include "upb/collections/array_internal.h"
// Must be last.
#include "upb/port_def.inc"
@ -111,3 +111,61 @@ void upb_Array_Delete(upb_Array* arr, size_t i, size_t count) {
bool upb_Array_Resize(upb_Array* arr, size_t size, upb_Arena* arena) {
return _upb_Array_Resize(arr, size, arena);
}
// EVERYTHING BELOW THIS LINE IS INTERNAL - DO NOT USE /////////////////////////
bool _upb_array_realloc(upb_Array* arr, size_t min_capacity, upb_Arena* arena) {
size_t new_capacity = UPB_MAX(arr->capacity, 4);
int elem_size_lg2 = arr->data & 7;
size_t old_bytes = arr->capacity << elem_size_lg2;
size_t new_bytes;
void* ptr = _upb_array_ptr(arr);
// Log2 ceiling of size.
while (new_capacity < min_capacity) new_capacity *= 2;
new_bytes = new_capacity << elem_size_lg2;
ptr = upb_Arena_Realloc(arena, ptr, old_bytes, new_bytes);
if (!ptr) {
return false;
}
arr->data = _upb_tag_arrptr(ptr, elem_size_lg2);
arr->capacity = new_capacity;
return true;
}
static upb_Array* getorcreate_array(upb_Array** arr_ptr, int elem_size_lg2,
upb_Arena* arena) {
upb_Array* arr = *arr_ptr;
if (!arr) {
arr = _upb_Array_New(arena, 4, elem_size_lg2);
if (!arr) return NULL;
*arr_ptr = arr;
}
return arr;
}
void* _upb_Array_Resize_fallback(upb_Array** arr_ptr, size_t size,
int elem_size_lg2, upb_Arena* arena) {
upb_Array* arr = getorcreate_array(arr_ptr, elem_size_lg2, arena);
return arr && _upb_Array_Resize(arr, size, arena) ? _upb_array_ptr(arr)
: NULL;
}
bool _upb_Array_Append_fallback(upb_Array** arr_ptr, const void* value,
int elem_size_lg2, upb_Arena* arena) {
upb_Array* arr = getorcreate_array(arr_ptr, elem_size_lg2, arena);
if (!arr) return false;
size_t elems = arr->size;
if (!_upb_Array_Resize(arr, elems + 1, arena)) {
return false;
}
char* data = _upb_array_ptr(arr);
memcpy(data + (elems << elem_size_lg2), value, 1 << elem_size_lg2);
return true;
}

@ -0,0 +1,82 @@
/*
* 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.
*/
#ifndef UPB_COLLECTIONS_ARRAY_H_
#define UPB_COLLECTIONS_ARRAY_H_
#include "upb/collections/message_value.h"
// Must be last.
#include "upb/port_def.inc"
#ifdef __cplusplus
extern "C" {
#endif
/* Creates a new array on the given arena that holds elements of this type. */
upb_Array* upb_Array_New(upb_Arena* a, upb_CType type);
/* Returns the number of elements in the array. */
size_t upb_Array_Size(const upb_Array* arr);
/* Returns the given element, which must be within the array's current size. */
upb_MessageValue upb_Array_Get(const upb_Array* arr, size_t i);
/* Sets the given element, which must be within the array's current size. */
void upb_Array_Set(upb_Array* arr, size_t i, upb_MessageValue val);
/* Appends an element to the array. Returns false on allocation failure. */
bool upb_Array_Append(upb_Array* array, upb_MessageValue val, upb_Arena* arena);
/* Moves elements within the array using memmove(). Like memmove(), the source
* and destination elements may be overlapping. */
void upb_Array_Move(upb_Array* array, size_t dst_idx, size_t src_idx,
size_t count);
/* Inserts one or more empty elements into the array. Existing elements are
* shifted right. The new elements have undefined state and must be set with
* `upb_Array_Set()`.
* REQUIRES: `i <= upb_Array_Size(arr)` */
bool upb_Array_Insert(upb_Array* array, size_t i, size_t count,
upb_Arena* arena);
/* Deletes one or more elements from the array. Existing elements are shifted
* left.
* REQUIRES: `i + count <= upb_Array_Size(arr)` */
void upb_Array_Delete(upb_Array* array, size_t i, size_t count);
/* Changes the size of a vector. New elements are initialized to empty/0.
* Returns false on allocation failure. */
bool upb_Array_Resize(upb_Array* array, size_t size, upb_Arena* arena);
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port_undef.inc"
#endif /* UPB_COLLECTIONS_ARRAY_H_ */

@ -25,12 +25,12 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef UPB_INTERNAL_ARRAY_H_
#define UPB_INTERNAL_ARRAY_H_
#ifndef UPB_INTERNAL_ARRAY_INTERNAL_H_
#define UPB_INTERNAL_ARRAY_INTERNAL_H_
#include <string.h>
#include "upb/array.h"
#include "upb/collections/array.h"
// Must be last.
#include "upb/port_def.inc"
@ -39,7 +39,7 @@
extern "C" {
#endif
/* Our internal representation for repeated fields. */
// Our internal representation for repeated fields.
struct upb_Array {
uintptr_t data; /* Tagged ptr: low 3 bits of ptr are lg2(elem size). */
size_t size; /* The number of elements in the array. */
@ -78,10 +78,10 @@ UPB_INLINE upb_Array* _upb_Array_New(upb_Arena* a, size_t init_capacity,
return arr;
}
/* Resizes the capacity of the array to be at least min_size. */
// Resizes the capacity of the array to be at least min_size.
bool _upb_array_realloc(upb_Array* arr, size_t min_size, upb_Arena* arena);
/* Fallback functions for when the accessors require a resize. */
// Fallback functions for when the accessors require a resize.
void* _upb_Array_Resize_fallback(upb_Array** arr_ptr, size_t size,
int elem_size_lg2, upb_Arena* arena);
bool _upb_Array_Append_fallback(upb_Array** arr_ptr, const void* value,
@ -157,7 +157,7 @@ UPB_INLINE bool _upb_Array_Append_accessor2(void* msg, size_t ofs,
return true;
}
/* Used by old generated code, remove once all code has been regenerated. */
// Used by old generated code, remove once all code has been regenerated.
UPB_INLINE int _upb_sizelg2(upb_CType type) {
switch (type) {
case kUpb_CType_Bool:
@ -200,4 +200,4 @@ UPB_INLINE bool _upb_Array_Append_accessor(void* msg, size_t ofs,
#include "upb/port_undef.inc"
#endif /* UPB_INTERNAL_ARRAY_H_ */
#endif /* UPB_INTERNAL_ARRAY_INTERNAL_H_ */

@ -25,7 +25,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "upb/map.h"
#include "upb/collections/map.h"
#include <string.h>

@ -0,0 +1,116 @@
/*
* 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.
*/
#ifndef UPB_COLLECTIONS_MAP_H_
#define UPB_COLLECTIONS_MAP_H_
#include "upb/collections/message_value.h"
// Must be last.
#include "upb/port_def.inc"
#ifdef __cplusplus
extern "C" {
#endif
/* Creates a new map on the given arena with the given key/value size. */
upb_Map* upb_Map_New(upb_Arena* a, upb_CType key_type, upb_CType value_type);
/* Returns the number of entries in the map. */
size_t upb_Map_Size(const upb_Map* map);
/* Stores a value for the given key into |*val| (or the zero value if the key is
* not present). Returns whether the key was present. The |val| pointer may be
* NULL, in which case the function tests whether the given key is present. */
bool upb_Map_Get(const upb_Map* map, upb_MessageValue key,
upb_MessageValue* val);
/* Removes all entries in the map. */
void upb_Map_Clear(upb_Map* map);
typedef enum {
// LINT.IfChange
kUpb_MapInsertStatus_Inserted = 0,
kUpb_MapInsertStatus_Replaced = 1,
kUpb_MapInsertStatus_OutOfMemory = 2,
// LINT.ThenChange(//depot/google3/third_party/upb/upb/msg_internal.h)
} upb_MapInsertStatus;
/* Sets the given key to the given value, returning whether the key was inserted
* or replaced. If the key was inserted, then any existing iterators will be
* invalidated. */
upb_MapInsertStatus upb_Map_Insert(upb_Map* map, upb_MessageValue key,
upb_MessageValue val, upb_Arena* arena);
/* Sets the given key to the given value. Returns false if memory allocation
* failed. If the key is newly inserted, then any existing iterators will be
* invalidated. */
UPB_INLINE bool upb_Map_Set(upb_Map* map, upb_MessageValue key,
upb_MessageValue val, upb_Arena* arena) {
return upb_Map_Insert(map, key, val, arena) !=
kUpb_MapInsertStatus_OutOfMemory;
}
/* Deletes this key from the table. Returns true if the key was present. */
bool upb_Map_Delete(upb_Map* map, upb_MessageValue key);
/* Map iteration:
*
* size_t iter = kUpb_Map_Begin;
* while (upb_MapIterator_Next(map, &iter)) {
* upb_MessageValue key = upb_MapIterator_Key(map, iter);
* upb_MessageValue val = upb_MapIterator_Value(map, iter);
*
* // If mutating is desired.
* upb_MapIterator_SetValue(map, iter, value2);
* }
*/
/* Advances to the next entry. Returns false if no more entries are present. */
bool upb_MapIterator_Next(const upb_Map* map, size_t* iter);
/* Returns true if the iterator still points to a valid entry, or false if the
* iterator is past the last element. It is an error to call this function with
* kUpb_Map_Begin (you must call next() at least once first). */
bool upb_MapIterator_Done(const upb_Map* map, size_t iter);
/* Returns the key and value for this entry of the map. */
upb_MessageValue upb_MapIterator_Key(const upb_Map* map, size_t iter);
upb_MessageValue upb_MapIterator_Value(const upb_Map* map, size_t iter);
/* Sets the value for this entry. The iterator must not be done, and the
* iterator must not have been initialized const. */
void upb_MapIterator_SetValue(upb_Map* map, size_t iter,
upb_MessageValue value);
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port_undef.inc"
#endif /* UPB_COLLECTIONS_MAP_H_ */

@ -25,7 +25,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "upb/map_sorter.h"
#include "upb/collections/map_sorter_internal.h"
// Must be last.
#include "upb/port_def.inc"

@ -27,8 +27,8 @@
// EVERYTHING BELOW THIS LINE IS INTERNAL - DO NOT USE /////////////////////////
#ifndef UPB_MAP_SORTER_H_
#define UPB_MAP_SORTER_H_
#ifndef UPB_COLLECTIONS_MAP_SORTER_INTERNAL_H_
#define UPB_COLLECTIONS_MAP_SORTER_INTERNAL_H_
#include "upb/msg_internal.h"
@ -90,4 +90,4 @@ bool _upb_mapsorter_pushmap(_upb_mapsorter* s, upb_FieldType key_type,
#include "upb/port_undef.inc"
#endif /* UPB_MAP_SORTER_H_ */
#endif /* UPB_COLLECTIONS_MAP_SORTER_INTERNAL_H_ */

@ -35,12 +35,6 @@
// Must be last.
#include "upb/port_def.inc"
#ifdef __cplusplus
extern "C" {
#endif
// Definitions common to both upb_Array and upb_Map.
typedef union {
bool bool_val;
float float_val;
@ -61,10 +55,6 @@ typedef union {
upb_Message* msg;
} upb_MutableMessageValue;
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port_undef.inc"
#endif /* UPB_MESSAGE_VALUE_H_ */

@ -1,89 +0,0 @@
/*
* 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.
*/
#include "upb/internal/array.h"
#include <string.h>
// Must be last.
#include "upb/port_def.inc"
bool _upb_array_realloc(upb_Array* arr, size_t min_capacity, upb_Arena* arena) {
size_t new_capacity = UPB_MAX(arr->capacity, 4);
int elem_size_lg2 = arr->data & 7;
size_t old_bytes = arr->capacity << elem_size_lg2;
size_t new_bytes;
void* ptr = _upb_array_ptr(arr);
/* Log2 ceiling of size. */
while (new_capacity < min_capacity) new_capacity *= 2;
new_bytes = new_capacity << elem_size_lg2;
ptr = upb_Arena_Realloc(arena, ptr, old_bytes, new_bytes);
if (!ptr) {
return false;
}
arr->data = _upb_tag_arrptr(ptr, elem_size_lg2);
arr->capacity = new_capacity;
return true;
}
static upb_Array* getorcreate_array(upb_Array** arr_ptr, int elem_size_lg2,
upb_Arena* arena) {
upb_Array* arr = *arr_ptr;
if (!arr) {
arr = _upb_Array_New(arena, 4, elem_size_lg2);
if (!arr) return NULL;
*arr_ptr = arr;
}
return arr;
}
void* _upb_Array_Resize_fallback(upb_Array** arr_ptr, size_t size,
int elem_size_lg2, upb_Arena* arena) {
upb_Array* arr = getorcreate_array(arr_ptr, elem_size_lg2, arena);
return arr && _upb_Array_Resize(arr, size, arena) ? _upb_array_ptr(arr)
: NULL;
}
bool _upb_Array_Append_fallback(upb_Array** arr_ptr, const void* value,
int elem_size_lg2, upb_Arena* arena) {
upb_Array* arr = getorcreate_array(arr_ptr, elem_size_lg2, arena);
if (!arr) return false;
size_t elems = arr->size;
if (!_upb_Array_Resize(arr, elems + 1, arena)) {
return false;
}
char* data = _upb_array_ptr(arr);
memcpy(data + (elems << elem_size_lg2), value, 1 << elem_size_lg2);
return true;
}

@ -35,9 +35,9 @@
#include <stdlib.h>
#include <string.h>
#include "upb/collections/map.h"
#include "upb/internal/atoi.h"
#include "upb/internal/unicode.h"
#include "upb/map.h"
#include "upb/reflection/message.h"
#include "upb/wire/encode.h"

@ -35,8 +35,8 @@
#include <stdio.h>
#include <string.h>
#include "upb/collections/map.h"
#include "upb/internal/vsnprintf_compat.h"
#include "upb/map.h"
#include "upb/reflection/message.h"
#include "upb/wire/decode.h"
#include "upb/wire/encode_internal.h"

@ -25,92 +25,11 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// This header is deprecated, use upb/collections/map.h instead
#ifndef UPB_MAP_H_
#define UPB_MAP_H_
#include "upb/message_value.h"
// Must be last.
#include "upb/port_def.inc"
#ifdef __cplusplus
extern "C" {
#endif
/* Creates a new map on the given arena with the given key/value size. */
upb_Map* upb_Map_New(upb_Arena* a, upb_CType key_type, upb_CType value_type);
/* Returns the number of entries in the map. */
size_t upb_Map_Size(const upb_Map* map);
/* Stores a value for the given key into |*val| (or the zero value if the key is
* not present). Returns whether the key was present. The |val| pointer may be
* NULL, in which case the function tests whether the given key is present. */
bool upb_Map_Get(const upb_Map* map, upb_MessageValue key,
upb_MessageValue* val);
/* Removes all entries in the map. */
void upb_Map_Clear(upb_Map* map);
typedef enum {
// LINT.IfChange
kUpb_MapInsertStatus_Inserted = 0,
kUpb_MapInsertStatus_Replaced = 1,
kUpb_MapInsertStatus_OutOfMemory = 2,
// LINT.ThenChange(//depot/google3/third_party/upb/upb/msg_internal.h)
} upb_MapInsertStatus;
/* Sets the given key to the given value, returning whether the key was inserted
* or replaced. If the key was inserted, then any existing iterators will be
* invalidated. */
upb_MapInsertStatus upb_Map_Insert(upb_Map* map, upb_MessageValue key,
upb_MessageValue val, upb_Arena* arena);
/* Sets the given key to the given value. Returns false if memory allocation
* failed. If the key is newly inserted, then any existing iterators will be
* invalidated. */
UPB_INLINE bool upb_Map_Set(upb_Map* map, upb_MessageValue key,
upb_MessageValue val, upb_Arena* arena) {
return upb_Map_Insert(map, key, val, arena) !=
kUpb_MapInsertStatus_OutOfMemory;
}
/* Deletes this key from the table. Returns true if the key was present. */
bool upb_Map_Delete(upb_Map* map, upb_MessageValue key);
/* Map iteration:
*
* size_t iter = kUpb_Map_Begin;
* while (upb_MapIterator_Next(map, &iter)) {
* upb_MessageValue key = upb_MapIterator_Key(map, iter);
* upb_MessageValue val = upb_MapIterator_Value(map, iter);
*
* // If mutating is desired.
* upb_MapIterator_SetValue(map, iter, value2);
* }
*/
/* Advances to the next entry. Returns false if no more entries are present. */
bool upb_MapIterator_Next(const upb_Map* map, size_t* iter);
/* Returns true if the iterator still points to a valid entry, or false if the
* iterator is past the last element. It is an error to call this function with
* kUpb_Map_Begin (you must call next() at least once first). */
bool upb_MapIterator_Done(const upb_Map* map, size_t iter);
/* Returns the key and value for this entry of the map. */
upb_MessageValue upb_MapIterator_Key(const upb_Map* map, size_t iter);
upb_MessageValue upb_MapIterator_Value(const upb_Map* map, size_t iter);
/* Sets the value for this entry. The iterator must not be done, and the
* iterator must not have been initialized const. */
void upb_MapIterator_SetValue(upb_Map* map, size_t iter,
upb_MessageValue value);
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port_undef.inc"
#include "upb/collections/map.h"
#endif /* UPB_MAP_H_ */

@ -27,7 +27,7 @@
#include "upb/mini_table/accessors.h"
#include "upb/internal/array.h"
#include "upb/collections/array_internal.h"
#include "upb/wire/decode.h"
#include "upb/wire/encode.h"

@ -28,7 +28,7 @@
#ifndef UPB_MINI_TABLE_ACCESSORS_H_
#define UPB_MINI_TABLE_ACCESSORS_H_
#include "upb/array.h"
#include "upb/collections/array.h"
#include "upb/mini_table/common.h"
// Must be last.

@ -36,7 +36,7 @@
#include "gtest/gtest.h"
#include "google/protobuf/test_messages_proto2.upb.h"
#include "google/protobuf/test_messages_proto3.upb.h"
#include "upb/array.h"
#include "upb/collections/array.h"
#include "upb/mini_table/decode.h"
#include "upb/mini_table/encode_internal.h"
#include "upb/test.upb.h"

@ -29,8 +29,8 @@
#include <string.h>
#include "upb/collections/map.h"
#include "upb/internal/table.h"
#include "upb/map.h"
#include "upb/msg.h"
#include "upb/reflection/def_pool.h"
#include "upb/reflection/def_type.h"

@ -28,7 +28,7 @@
#ifndef UPB_REFLECTION_MESSAGE_H_
#define UPB_REFLECTION_MESSAGE_H_
#include "upb/map.h"
#include "upb/collections/map.h"
#include "upb/reflection/common.h"
// Must be last.

@ -33,9 +33,9 @@
#include <stdarg.h>
#include <string.h>
#include "upb/collections/map.h"
#include "upb/collections/map_sorter_internal.h"
#include "upb/internal/vsnprintf_compat.h"
#include "upb/map.h"
#include "upb/map_sorter.h"
#include "upb/reflection/message.h"
#include "upb/wire/encode_internal.h"

@ -32,8 +32,8 @@
#include <stdarg.h>
#include <stdio.h>
#include "upb/collections/map.h"
#include "upb/internal/vsnprintf_compat.h"
#include "upb/map.h"
#include "upb/reflection/message.h"
// Must be last.

@ -29,7 +29,7 @@
#include <string.h>
#include "upb/internal/array.h"
#include "upb/collections/array_internal.h"
#include "upb/msg_internal.h"
#include "upb/upb.h"
#include "upb/wire/decode_internal.h"

@ -37,7 +37,7 @@
#include "upb/wire/decode_fast.h"
#include "upb/internal/array.h"
#include "upb/collections/array_internal.h"
#include "upb/wire/decode_internal.h"
// Must be last.

@ -31,9 +31,9 @@
#include <string.h>
#include "upb/collections/array_internal.h"
#include "upb/collections/map_sorter_internal.h"
#include "upb/extension_registry.h"
#include "upb/internal/array.h"
#include "upb/map_sorter.h"
#include "upb/msg_internal.h"
#include "upb/upb.h"

@ -883,7 +883,7 @@ void WriteHeader(const FileLayout& layout, Output& output) {
output(
"#ifndef $0_UPB_H_\n"
"#define $0_UPB_H_\n\n"
"#include \"upb/internal/array.h\"\n"
"#include \"upb/collections/array_internal.h\"\n"
"#include \"upb/msg_internal.h\"\n"
"#include \"upb/wire/decode.h\"\n"
"#include \"upb/wire/decode_fast.h\"\n"
@ -1502,7 +1502,7 @@ void WriteSource(const FileLayout& layout, Output& output,
output(
"#include <stddef.h>\n"
"#include \"upb/internal/array.h\"\n"
"#include \"upb/collections/array_internal.h\"\n"
"#include \"upb/msg_internal.h\"\n"
"#include \"$0\"\n",
HeaderFilename(file));

Loading…
Cancel
Save