Protocol Buffers - Google's data interchange format (grpc依赖)
https://developers.google.com/protocol-buffers/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.4 KiB
55 lines
1.4 KiB
1 year ago
|
// Protocol Buffers - Google's data interchange format
|
||
|
// Copyright 2023 Google LLC. All rights reserved.
|
||
4 years ago
|
//
|
||
1 year ago
|
// Use of this source code is governed by a BSD-style
|
||
|
// license that can be found in the LICENSE file or at
|
||
|
// https://developers.google.com/open-source/licenses/bsd
|
||
5 years ago
|
|
||
1 year ago
|
#ifndef UPB_MEM_ARENA_HPP_
|
||
|
#define UPB_MEM_ARENA_HPP_
|
||
5 years ago
|
|
||
|
#include <memory>
|
||
|
|
||
1 year ago
|
#include "upb/mem/arena.h"
|
||
5 years ago
|
|
||
|
namespace upb {
|
||
|
|
||
|
class Arena {
|
||
|
public:
|
||
|
// A simple arena with no initial memory block and the default allocator.
|
||
3 years ago
|
Arena() : ptr_(upb_Arena_New(), upb_Arena_Free) {}
|
||
|
Arena(char* initial_block, size_t size)
|
||
|
: ptr_(upb_Arena_Init(initial_block, size, &upb_alloc_global),
|
||
|
upb_Arena_Free) {}
|
||
5 years ago
|
|
||
2 years ago
|
upb_Arena* ptr() const { return ptr_.get(); }
|
||
5 years ago
|
|
||
3 years ago
|
void Fuse(Arena& other) { upb_Arena_Fuse(ptr(), other.ptr()); }
|
||
5 years ago
|
|
||
2 years ago
|
protected:
|
||
3 years ago
|
std::unique_ptr<upb_Arena, decltype(&upb_Arena_Free)> ptr_;
|
||
5 years ago
|
};
|
||
|
|
||
|
// InlinedArena seeds the arenas with a predefined amount of memory. No
|
||
|
// heap memory will be allocated until the initial block is exceeded.
|
||
|
template <int N>
|
||
|
class InlinedArena : public Arena {
|
||
|
public:
|
||
4 years ago
|
InlinedArena() : Arena(initial_block_, N) {}
|
||
2 years ago
|
~InlinedArena() {
|
||
|
// Explicitly destroy the arena now so that it does not outlive
|
||
|
// initial_block_.
|
||
|
ptr_.reset();
|
||
|
}
|
||
5 years ago
|
|
||
|
private:
|
||
|
InlinedArena(const InlinedArena*) = delete;
|
||
|
InlinedArena& operator=(const InlinedArena*) = delete;
|
||
|
|
||
|
char initial_block_[N];
|
||
|
};
|
||
|
|
||
|
} // namespace upb
|
||
|
|
||
1 year ago
|
#endif // UPB_MEM_ARENA_HPP_
|