Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:13:18

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2023 Google LLC.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 #ifndef UPB_MINI_TABLE_EXTENSION_REGISTRY_H_
0009 #define UPB_MINI_TABLE_EXTENSION_REGISTRY_H_
0010 
0011 #include "upb/mem/arena.h"
0012 #include "upb/mini_table/extension.h"
0013 #include "upb/mini_table/message.h"
0014 
0015 // Must be last.
0016 #include "upb/port/def.inc"
0017 
0018 #ifdef __cplusplus
0019 extern "C" {
0020 #endif
0021 
0022 /* Extension registry: a dynamic data structure that stores a map of:
0023  *   (upb_MiniTable, number) -> extension info
0024  *
0025  * upb_decode() uses upb_ExtensionRegistry to look up extensions while parsing
0026  * binary format.
0027  *
0028  * upb_ExtensionRegistry is part of the mini-table (msglayout) family of
0029  * objects. Like all mini-table objects, it is suitable for reflection-less
0030  * builds that do not want to expose names into the binary.
0031  *
0032  * Unlike most mini-table types, upb_ExtensionRegistry requires dynamic memory
0033  * allocation and dynamic initialization:
0034  * * If reflection is being used, then upb_DefPool will construct an appropriate
0035  *   upb_ExtensionRegistry automatically.
0036  * * For a mini-table only build, the user must manually construct the
0037  *   upb_ExtensionRegistry and populate it with all of the extensions the user
0038  * cares about.
0039  * * A third alternative is to manually unpack relevant extensions after the
0040  *   main parse is complete, similar to how Any works. This is perhaps the
0041  *   nicest solution from the perspective of reducing dependencies, avoiding
0042  *   dynamic memory allocation, and avoiding the need to parse uninteresting
0043  *   extensions.  The downsides are:
0044  *     (1) parse errors are not caught during the main parse
0045  *     (2) the CPU hit of parsing comes during access, which could cause an
0046  *         undesirable stutter in application performance.
0047  *
0048  * Users cannot directly get or put into this map. Users can only add the
0049  * extensions from a generated module and pass the extension registry to the
0050  * binary decoder.
0051  *
0052  * A upb_DefPool provides a upb_ExtensionRegistry, so any users who use
0053  * reflection do not need to populate a upb_ExtensionRegistry directly.
0054  */
0055 
0056 typedef struct upb_ExtensionRegistry upb_ExtensionRegistry;
0057 
0058 // Creates a upb_ExtensionRegistry in the given arena.
0059 // The arena must outlive any use of the extreg.
0060 UPB_API upb_ExtensionRegistry* upb_ExtensionRegistry_New(upb_Arena* arena);
0061 
0062 UPB_API bool upb_ExtensionRegistry_Add(upb_ExtensionRegistry* r,
0063                                        const upb_MiniTableExtension* e);
0064 
0065 // Adds the given extension info for the array |e| of size |count| into the
0066 // registry. If there are any errors, the entire array is backed out.
0067 // The extensions must outlive the registry.
0068 // Possible errors include OOM or an extension number that already exists.
0069 // TODO: There is currently no way to know the exact reason for failure.
0070 bool upb_ExtensionRegistry_AddArray(upb_ExtensionRegistry* r,
0071                                     const upb_MiniTableExtension** e,
0072                                     size_t count);
0073 
0074 #ifdef UPB_LINKARR_DECLARE
0075 
0076 // Adds all extensions linked into the binary into the registry.  The set of
0077 // linked extensions is assembled by the linker using linker arrays.  This
0078 // will likely not work properly if the extensions are split across multiple
0079 // shared libraries.
0080 //
0081 // Returns true if all extensions were added successfully, false on out of
0082 // memory or if any extensions were already present.
0083 //
0084 // This API is currently not available on MSVC (though it *is* available on
0085 // Windows using clang-cl).
0086 UPB_API bool upb_ExtensionRegistry_AddAllLinkedExtensions(
0087     upb_ExtensionRegistry* r);
0088 
0089 #endif  // UPB_LINKARR_DECLARE
0090 
0091 // Looks up the extension (if any) defined for message type |t| and field
0092 // number |num|. Returns the extension if found, otherwise NULL.
0093 UPB_API const upb_MiniTableExtension* upb_ExtensionRegistry_Lookup(
0094     const upb_ExtensionRegistry* r, const upb_MiniTable* t, uint32_t num);
0095 
0096 #ifdef __cplusplus
0097 } /* extern "C" */
0098 #endif
0099 
0100 #include "upb/port/undef.inc"
0101 
0102 #endif /* UPB_MINI_TABLE_EXTENSION_REGISTRY_H_ */