Hong Shin | 1b44ce0 | 2024-08-28 16:04:09 -0700 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2024 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 "google/protobuf/hpb/extension.h" |
| 9 | |
Hong Shin | 9bce530 | 2024-10-08 06:58:18 -0700 | [diff] [blame] | 10 | #include "absl/status/status.h" |
| 11 | #include "google/protobuf/hpb/internal/message_lock.h" |
| 12 | #include "google/protobuf/hpb/status.h" |
| 13 | #include "upb/mem/arena.h" |
| 14 | #include "upb/message/accessors.h" |
| 15 | #include "upb/message/message.h" |
| 16 | #include "upb/mini_table/extension.h" |
Hong Shin | 1b44ce0 | 2024-08-28 16:04:09 -0700 | [diff] [blame] | 17 | #include "upb/mini_table/extension_registry.h" |
| 18 | |
| 19 | namespace hpb { |
| 20 | namespace internal { |
| 21 | upb_ExtensionRegistry* GetUpbExtensions( |
| 22 | const ExtensionRegistry& extension_registry) { |
| 23 | return extension_registry.registry_; |
| 24 | } |
| 25 | |
Hong Shin | 9bce530 | 2024-10-08 06:58:18 -0700 | [diff] [blame] | 26 | absl::Status MoveExtension(upb_Message* message, upb_Arena* message_arena, |
| 27 | const upb_MiniTableExtension* ext, |
| 28 | upb_Message* extension, upb_Arena* extension_arena) { |
| 29 | if (message_arena != extension_arena && |
| 30 | // Try fuse, if fusing is not allowed or fails, create copy of extension. |
| 31 | !upb_Arena_Fuse(message_arena, extension_arena)) { |
| 32 | extension = DeepClone(extension, upb_MiniTableExtension_GetSubMessage(ext), |
| 33 | message_arena); |
| 34 | } |
| 35 | return upb_Message_SetExtension(message, ext, &extension, message_arena) |
| 36 | ? absl::OkStatus() |
| 37 | : MessageAllocationError(); |
| 38 | } |
| 39 | |
| 40 | absl::Status SetExtension(upb_Message* message, upb_Arena* message_arena, |
| 41 | const upb_MiniTableExtension* ext, |
| 42 | const upb_Message* extension) { |
| 43 | // Clone extension into target message arena. |
| 44 | extension = DeepClone(extension, upb_MiniTableExtension_GetSubMessage(ext), |
| 45 | message_arena); |
| 46 | return upb_Message_SetExtension(message, ext, &extension, message_arena) |
| 47 | ? absl::OkStatus() |
| 48 | : MessageAllocationError(); |
| 49 | } |
| 50 | |
Hong Shin | 1b44ce0 | 2024-08-28 16:04:09 -0700 | [diff] [blame] | 51 | } // namespace internal |
| 52 | } // namespace hpb |