blob: 71c4894a47bc8c352ff177e794f8f94e191b3233 [file] [log] [blame]
Adam Cozzette501ecec2023-09-26 14:36:20 -07001// Protocol Buffers - Google's data interchange format
2// Copyright 2023 Google LLC. All rights reserved.
Adam Cozzette501ecec2023-09-26 14:36:20 -07003//
Protobuf Team Bot0fab7732023-11-20 13:38:15 -08004// 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
Adam Cozzette501ecec2023-09-26 14:36:20 -07007
Eric Salo07fba1d2023-09-29 14:50:56 -07008#ifndef UPB_MESSAGE_INTERNAL_ARRAY_H_
9#define UPB_MESSAGE_INTERNAL_ARRAY_H_
Adam Cozzette501ecec2023-09-26 14:36:20 -070010
11#include <string.h>
12
Eric Salo07fba1d2023-09-29 14:50:56 -070013#include "upb/message/array.h"
Adam Cozzette501ecec2023-09-26 14:36:20 -070014
15// Must be last.
16#include "upb/port/def.inc"
17
Eric Salo8324c902023-11-17 17:15:35 -080018#define _UPB_ARRAY_MASK_IMM 0x4 // Frozen/immutable bit.
19#define _UPB_ARRAY_MASK_LG2 0x3 // Encoded elem size.
20#define _UPB_ARRAY_MASK_ALL (_UPB_ARRAY_MASK_IMM | _UPB_ARRAY_MASK_LG2)
21
Adam Cozzette501ecec2023-09-26 14:36:20 -070022#ifdef __cplusplus
23extern "C" {
24#endif
25
26// LINT.IfChange(struct_definition)
27// Our internal representation for repeated fields.
28struct upb_Array {
Eric Salo8324c902023-11-17 17:15:35 -080029 // This is a tagged pointer. Bits #0 and #1 encode the elem size as follows:
30 // 0 maps to elem size 1
31 // 1 maps to elem size 4
32 // 2 maps to elem size 8
33 // 3 maps to elem size 16
34 //
35 // Bit #2 contains the frozen/immutable flag (currently unimplemented).
36 uintptr_t data;
37
Eric Salo3ce2c572023-11-27 10:45:25 -080038 size_t size; // The number of elements in the array.
39 size_t UPB_PRIVATE(capacity); // Allocated storage. Measured in elements.
Adam Cozzette501ecec2023-09-26 14:36:20 -070040};
Adam Cozzette501ecec2023-09-26 14:36:20 -070041
Eric Salo3ce2c572023-11-27 10:45:25 -080042UPB_INLINE void UPB_PRIVATE(_upb_Array_SetTaggedPtr)(upb_Array* array,
43 void* data, size_t lg2) {
Eric Salo8324c902023-11-17 17:15:35 -080044 UPB_ASSERT(lg2 != 1);
45 UPB_ASSERT(lg2 <= 4);
46 const size_t bits = lg2 - (lg2 != 0);
Eric Salo3ce2c572023-11-27 10:45:25 -080047 array->data = (uintptr_t)data | bits;
Eric Salo8324c902023-11-17 17:15:35 -080048}
49
Eric Salo3ce2c572023-11-27 10:45:25 -080050UPB_INLINE size_t UPB_PRIVATE(_upb_Array_ElemSizeLg2)(const upb_Array* array) {
51 const size_t bits = array->data & _UPB_ARRAY_MASK_LG2;
Eric Salo8324c902023-11-17 17:15:35 -080052 const size_t lg2 = bits + (bits != 0);
53 return lg2;
Adam Cozzette501ecec2023-09-26 14:36:20 -070054}
55
Eric Salo3ce2c572023-11-27 10:45:25 -080056UPB_INLINE const void* _upb_array_constptr(const upb_Array* array) {
57 UPB_PRIVATE(_upb_Array_ElemSizeLg2)(array); // Check assertions.
58 return (void*)(array->data & ~(uintptr_t)_UPB_ARRAY_MASK_ALL);
Adam Cozzette501ecec2023-09-26 14:36:20 -070059}
60
Eric Salo3ce2c572023-11-27 10:45:25 -080061UPB_INLINE void* _upb_array_ptr(upb_Array* array) {
62 return (void*)_upb_array_constptr(array);
Adam Cozzette501ecec2023-09-26 14:36:20 -070063}
64
Eric Salo3ce2c572023-11-27 10:45:25 -080065UPB_INLINE upb_Array* UPB_PRIVATE(_upb_Array_New)(upb_Arena* arena,
66 size_t init_capacity,
67 int elem_size_lg2) {
Eric Salo8324c902023-11-17 17:15:35 -080068 UPB_ASSERT(elem_size_lg2 != 1);
Adam Cozzette501ecec2023-09-26 14:36:20 -070069 UPB_ASSERT(elem_size_lg2 <= 4);
Eric Salo3ce2c572023-11-27 10:45:25 -080070 const size_t array_size = UPB_ALIGN_UP(sizeof(upb_Array), UPB_MALLOC_ALIGN);
71 const size_t bytes = array_size + (init_capacity << elem_size_lg2);
72 upb_Array* array = (upb_Array*)upb_Arena_Malloc(arena, bytes);
73 if (!array) return NULL;
74 UPB_PRIVATE(_upb_Array_SetTaggedPtr)
75 (array, UPB_PTR_AT(array, array_size, void), elem_size_lg2);
76 array->size = 0;
77 array->UPB_PRIVATE(capacity) = init_capacity;
78 return array;
Adam Cozzette501ecec2023-09-26 14:36:20 -070079}
80
81// Resizes the capacity of the array to be at least min_size.
Eric Salo3ce2c572023-11-27 10:45:25 -080082bool UPB_PRIVATE(_upb_Array_Realloc)(upb_Array* array, size_t min_size,
83 upb_Arena* arena);
Adam Cozzette501ecec2023-09-26 14:36:20 -070084
Eric Salo3ce2c572023-11-27 10:45:25 -080085UPB_INLINE bool UPB_PRIVATE(_upb_Array_Reserve)(upb_Array* array, size_t size,
86 upb_Arena* arena) {
87 if (array->UPB_PRIVATE(capacity) < size)
88 return UPB_PRIVATE(_upb_Array_Realloc)(array, size, arena);
Adam Cozzette501ecec2023-09-26 14:36:20 -070089 return true;
90}
91
92// Resize without initializing new elements.
Eric Salo3ce2c572023-11-27 10:45:25 -080093UPB_INLINE bool _upb_Array_ResizeUninitialized(upb_Array* array, size_t size,
Adam Cozzette501ecec2023-09-26 14:36:20 -070094 upb_Arena* arena) {
Eric Salo3ce2c572023-11-27 10:45:25 -080095 UPB_ASSERT(size <= array->size || arena); // Allow NULL arena when shrinking.
96 if (!UPB_PRIVATE(_upb_Array_Reserve)(array, size, arena)) return false;
97 array->size = size;
Adam Cozzette501ecec2023-09-26 14:36:20 -070098 return true;
99}
100
101// This function is intended for situations where elem_size is compile-time
102// constant or a known expression of the form (1 << lg2), so that the expression
103// i*elem_size does not result in an actual multiplication.
Eric Salo3ce2c572023-11-27 10:45:25 -0800104UPB_INLINE void UPB_PRIVATE(_upb_Array_Set)(upb_Array* array, size_t i,
105 const void* data,
106 size_t elem_size) {
107 UPB_ASSERT(i < array->size);
108 UPB_ASSERT(elem_size == 1U << UPB_PRIVATE(_upb_Array_ElemSizeLg2)(array));
109 char* arr_data = (char*)_upb_array_ptr(array);
Adam Cozzette501ecec2023-09-26 14:36:20 -0700110 memcpy(arr_data + (i * elem_size), data, elem_size);
111}
112
Protobuf Team Bot2ff245e2023-12-01 11:06:30 -0800113// LINT.ThenChange(
114// GoogleInternalName1,
115// //depot/google3/third_party/upb/bits/typescript/array.ts
116//)
117
Adam Cozzette501ecec2023-09-26 14:36:20 -0700118#ifdef __cplusplus
119} /* extern "C" */
120#endif
121
Eric Salo8324c902023-11-17 17:15:35 -0800122#undef _UPB_ARRAY_MASK_IMM
123#undef _UPB_ARRAY_MASK_LG2
124#undef _UPB_ARRAY_MASK_ALL
125
Adam Cozzette501ecec2023-09-26 14:36:20 -0700126#include "upb/port/undef.inc"
127
Eric Salo07fba1d2023-09-29 14:50:56 -0700128#endif /* UPB_MESSAGE_INTERNAL_ARRAY_H_ */