xref: /aosp_15_r20/external/grpc-grpc/third_party/upb/upb/message/internal/extension.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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_EXTENSION_H_
9 #define UPB_MESSAGE_INTERNAL_EXTENSION_H_
10 
11 #include "upb/base/string_view.h"
12 #include "upb/mem/arena.h"
13 #include "upb/message/internal/message.h"
14 #include "upb/mini_table/extension.h"
15 
16 // Must be last.
17 #include "upb/port/def.inc"
18 
19 // The internal representation of an extension is self-describing: it contains
20 // enough information that we can serialize it to binary format without needing
21 // to look it up in a upb_ExtensionRegistry.
22 //
23 // This representation allocates 16 bytes to data on 64-bit platforms.
24 // This is rather wasteful for scalars (in the extreme case of bool,
25 // it wastes 15 bytes). We accept this because we expect messages to be
26 // the most common extension type.
27 struct upb_Extension {
28   const upb_MiniTableExtension* ext;
29   union {
30     upb_StringView str;
31     void* ptr;
32     char scalar_data[8];
33   } data;
34 };
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 // Adds the given extension data to the given message.
41 // |ext| is copied into the message instance.
42 // This logically replaces any previously-added extension with this number.
43 struct upb_Extension* UPB_PRIVATE(_upb_Message_GetOrCreateExtension)(
44     struct upb_Message* msg, const upb_MiniTableExtension* ext,
45     upb_Arena* arena);
46 
47 // Returns an array of extensions for this message.
48 // Note: the array is ordered in reverse relative to the order of creation.
49 const struct upb_Extension* UPB_PRIVATE(_upb_Message_Getexts)(
50     const struct upb_Message* msg, size_t* count);
51 
52 // Returns an extension for a message with a given mini table,
53 // or NULL if no extension exists with this mini table.
54 const struct upb_Extension* UPB_PRIVATE(_upb_Message_Getext)(
55     const struct upb_Message* msg, const upb_MiniTableExtension* ext);
56 
57 #ifdef __cplusplus
58 } /* extern "C" */
59 #endif
60 
61 #include "upb/port/undef.inc"
62 
63 #endif /* UPB_MESSAGE_INTERNAL_EXTENSION_H_ */
64