/proc/self/cwd/extensions/protobuf/memory_manager.h
Line | Count | Source |
1 | | // Copyright 2022 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #ifndef THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_MEMORY_MANAGER_H_ |
16 | | #define THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_MEMORY_MANAGER_H_ |
17 | | |
18 | | #include <utility> |
19 | | |
20 | | #include "absl/base/attributes.h" |
21 | | #include "absl/base/nullability.h" |
22 | | #include "common/memory.h" |
23 | | #include "google/protobuf/arena.h" |
24 | | |
25 | | namespace cel::extensions { |
26 | | |
27 | | // Returns an appropriate `MemoryManagerRef` wrapping `google::protobuf::Arena`. The |
28 | | // lifetime of objects creating using the resulting `MemoryManagerRef` is tied |
29 | | // to that of `google::protobuf::Arena`. |
30 | | // |
31 | | // IMPORTANT: Passing `nullptr` here will result in getting |
32 | | // `MemoryManagerRef::ReferenceCounting()`. |
33 | | MemoryManager ProtoMemoryManager(google::protobuf::Arena* arena); |
34 | 0 | inline MemoryManager ProtoMemoryManagerRef(google::protobuf::Arena* arena) { |
35 | 0 | return ProtoMemoryManager(arena); |
36 | 0 | } |
37 | | |
38 | | // Gets the underlying `google::protobuf::Arena`. If `MemoryManager` was not created using |
39 | | // either `ProtoMemoryManagerRef` or `ProtoMemoryManager`, this returns |
40 | | // `nullptr`. |
41 | | google::protobuf::Arena* absl_nullable ProtoMemoryManagerArena( |
42 | | MemoryManager memory_manager); |
43 | | // Allocate and construct `T` using the `ProtoMemoryManager` provided as |
44 | | // `memory_manager`. `memory_manager` must be `ProtoMemoryManager` or behavior |
45 | | // is undefined. Unlike `MemoryManager::New`, this method supports arena-enabled |
46 | | // messages. |
47 | | template <typename T, typename... Args> |
48 | | ABSL_MUST_USE_RESULT T* NewInProtoArena(MemoryManager memory_manager, |
49 | | Args&&... args) { |
50 | | return google::protobuf::Arena::Create<T>(ProtoMemoryManagerArena(memory_manager), |
51 | | std::forward<Args>(args)...); |
52 | | } |
53 | | |
54 | | } // namespace cel::extensions |
55 | | |
56 | | #endif // THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_MEMORY_MANAGER_H_ |