James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2021 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef FLATBUFFERS_DETACHED_BUFFER_H_ |
| 18 | #define FLATBUFFERS_DETACHED_BUFFER_H_ |
| 19 | |
| 20 | #include "flatbuffers/allocator.h" |
| 21 | #include "flatbuffers/base.h" |
| 22 | #include "flatbuffers/default_allocator.h" |
| 23 | |
| 24 | namespace flatbuffers { |
| 25 | |
| 26 | // DetachedBuffer is a finished flatbuffer memory region, detached from its |
| 27 | // builder. The original memory region and allocator are also stored so that |
| 28 | // the DetachedBuffer can manage the memory lifetime. |
| 29 | class DetachedBuffer { |
| 30 | public: |
| 31 | DetachedBuffer() |
| 32 | : allocator_(nullptr), |
| 33 | own_allocator_(false), |
| 34 | buf_(nullptr), |
| 35 | reserved_(0), |
| 36 | cur_(nullptr), |
| 37 | size_(0) {} |
| 38 | |
| 39 | DetachedBuffer(Allocator *allocator, bool own_allocator, uint8_t *buf, |
| 40 | size_t reserved, uint8_t *cur, size_t sz) |
| 41 | : allocator_(allocator), |
| 42 | own_allocator_(own_allocator), |
| 43 | buf_(buf), |
| 44 | reserved_(reserved), |
| 45 | cur_(cur), |
| 46 | size_(sz) {} |
| 47 | |
| 48 | DetachedBuffer(DetachedBuffer &&other) |
| 49 | : allocator_(other.allocator_), |
| 50 | own_allocator_(other.own_allocator_), |
| 51 | buf_(other.buf_), |
| 52 | reserved_(other.reserved_), |
| 53 | cur_(other.cur_), |
| 54 | size_(other.size_) { |
| 55 | other.reset(); |
| 56 | } |
| 57 | |
| 58 | DetachedBuffer &operator=(DetachedBuffer &&other) { |
| 59 | if (this == &other) return *this; |
| 60 | |
| 61 | destroy(); |
| 62 | |
| 63 | allocator_ = other.allocator_; |
| 64 | own_allocator_ = other.own_allocator_; |
| 65 | buf_ = other.buf_; |
| 66 | reserved_ = other.reserved_; |
| 67 | cur_ = other.cur_; |
| 68 | size_ = other.size_; |
| 69 | |
| 70 | other.reset(); |
| 71 | |
| 72 | return *this; |
| 73 | } |
| 74 | |
| 75 | ~DetachedBuffer() { destroy(); } |
| 76 | |
| 77 | const uint8_t *data() const { return cur_; } |
| 78 | |
| 79 | uint8_t *data() { return cur_; } |
| 80 | |
| 81 | size_t size() const { return size_; } |
| 82 | |
| 83 | // These may change access mode, leave these at end of public section |
| 84 | FLATBUFFERS_DELETE_FUNC(DetachedBuffer(const DetachedBuffer &other)); |
| 85 | FLATBUFFERS_DELETE_FUNC( |
| 86 | DetachedBuffer &operator=(const DetachedBuffer &other)); |
| 87 | |
| 88 | protected: |
| 89 | Allocator *allocator_; |
| 90 | bool own_allocator_; |
| 91 | uint8_t *buf_; |
| 92 | size_t reserved_; |
| 93 | uint8_t *cur_; |
| 94 | size_t size_; |
| 95 | |
| 96 | inline void destroy() { |
| 97 | if (buf_) Deallocate(allocator_, buf_, reserved_); |
| 98 | if (own_allocator_ && allocator_) { delete allocator_; } |
| 99 | reset(); |
| 100 | } |
| 101 | |
| 102 | inline void reset() { |
| 103 | allocator_ = nullptr; |
| 104 | own_allocator_ = false; |
| 105 | buf_ = nullptr; |
| 106 | reserved_ = 0; |
| 107 | cur_ = nullptr; |
| 108 | size_ = 0; |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | } // namespace flatbuffers |
| 113 | |
| 114 | #endif // FLATBUFFERS_DETACHED_BUFFER_H_ |