xref: /aosp_15_r20/external/grpc-grpc/third_party/upb/upb/message/internal/extension.c (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 #include "upb/message/internal/extension.h"
9 
10 #include <string.h>
11 
12 #include "upb/mem/arena.h"
13 #include "upb/message/internal/extension.h"
14 #include "upb/message/internal/message.h"
15 #include "upb/message/internal/types.h"
16 #include "upb/mini_table/extension.h"
17 
18 // Must be last.
19 #include "upb/port/def.inc"
20 
UPB_PRIVATE(_upb_Message_Getext)21 const struct upb_Extension* UPB_PRIVATE(_upb_Message_Getext)(
22     const struct upb_Message* msg, const upb_MiniTableExtension* e) {
23   size_t n;
24   const struct upb_Extension* ext = UPB_PRIVATE(_upb_Message_Getexts)(msg, &n);
25 
26   // For now we use linear search exclusively to find extensions.
27   // If this becomes an issue due to messages with lots of extensions,
28   // we can introduce a table of some sort.
29   for (size_t i = 0; i < n; i++) {
30     if (ext[i].ext == e) {
31       return &ext[i];
32     }
33   }
34 
35   return NULL;
36 }
37 
UPB_PRIVATE(_upb_Message_Getexts)38 const struct upb_Extension* UPB_PRIVATE(_upb_Message_Getexts)(
39     const struct upb_Message* msg, size_t* count) {
40   upb_Message_Internal* in = msg->internal;
41   if (in) {
42     *count = (in->size - in->ext_begin) / sizeof(struct upb_Extension);
43     return UPB_PTR_AT(in, in->ext_begin, void);
44   } else {
45     *count = 0;
46     return NULL;
47   }
48 }
49 
UPB_PRIVATE(_upb_Message_GetOrCreateExtension)50 struct upb_Extension* UPB_PRIVATE(_upb_Message_GetOrCreateExtension)(
51     struct upb_Message* msg, const upb_MiniTableExtension* e, upb_Arena* a) {
52   struct upb_Extension* ext =
53       (struct upb_Extension*)UPB_PRIVATE(_upb_Message_Getext)(msg, e);
54   if (ext) return ext;
55   if (!UPB_PRIVATE(_upb_Message_Realloc)(msg, sizeof(struct upb_Extension), a))
56     return NULL;
57   upb_Message_Internal* in = msg->internal;
58   in->ext_begin -= sizeof(struct upb_Extension);
59   ext = UPB_PTR_AT(in, in->ext_begin, void);
60   memset(ext, 0, sizeof(struct upb_Extension));
61   ext->ext = e;
62   return ext;
63 }
64