Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-27 09:30:24

0001 /*
0002  * Copyright 2021 Google Inc. All rights reserved.
0003  *
0004  * Licensed under the Apache License, Version 2.0 (the "License");
0005  * you may not use this file except in compliance with the License.
0006  * You may obtain a copy of the License at
0007  *
0008  *     http://www.apache.org/licenses/LICENSE-2.0
0009  *
0010  * Unless required by applicable law or agreed to in writing, software
0011  * distributed under the License is distributed on an "AS IS" BASIS,
0012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013  * See the License for the specific language governing permissions and
0014  * limitations under the License.
0015  */
0016 
0017 #ifndef FLATBUFFERS_DETACHED_BUFFER_H_
0018 #define FLATBUFFERS_DETACHED_BUFFER_H_
0019 
0020 #include "flatbuffers/allocator.h"
0021 #include "flatbuffers/base.h"
0022 #include "flatbuffers/default_allocator.h"
0023 
0024 namespace flatbuffers {
0025 
0026 // DetachedBuffer is a finished flatbuffer memory region, detached from its
0027 // builder. The original memory region and allocator are also stored so that
0028 // the DetachedBuffer can manage the memory lifetime.
0029 class DetachedBuffer {
0030  public:
0031   DetachedBuffer()
0032       : allocator_(nullptr),
0033         own_allocator_(false),
0034         buf_(nullptr),
0035         reserved_(0),
0036         cur_(nullptr),
0037         size_(0) {}
0038 
0039   DetachedBuffer(Allocator *allocator, bool own_allocator, uint8_t *buf,
0040                  size_t reserved, uint8_t *cur, size_t sz)
0041       : allocator_(allocator),
0042         own_allocator_(own_allocator),
0043         buf_(buf),
0044         reserved_(reserved),
0045         cur_(cur),
0046         size_(sz) {}
0047 
0048   DetachedBuffer(DetachedBuffer &&other) noexcept
0049       : allocator_(other.allocator_),
0050         own_allocator_(other.own_allocator_),
0051         buf_(other.buf_),
0052         reserved_(other.reserved_),
0053         cur_(other.cur_),
0054         size_(other.size_) {
0055     other.reset();
0056   }
0057 
0058   DetachedBuffer &operator=(DetachedBuffer &&other) noexcept {
0059     if (this == &other) return *this;
0060 
0061     destroy();
0062 
0063     allocator_ = other.allocator_;
0064     own_allocator_ = other.own_allocator_;
0065     buf_ = other.buf_;
0066     reserved_ = other.reserved_;
0067     cur_ = other.cur_;
0068     size_ = other.size_;
0069 
0070     other.reset();
0071 
0072     return *this;
0073   }
0074 
0075   ~DetachedBuffer() { destroy(); }
0076 
0077   const uint8_t *data() const { return cur_; }
0078 
0079   uint8_t *data() { return cur_; }
0080 
0081   size_t size() const { return size_; }
0082 
0083   uint8_t *begin() { return data(); }
0084   const uint8_t *begin() const { return data(); }
0085   uint8_t *end() { return data() + size(); }
0086   const uint8_t *end() const { return data() + size(); }
0087 
0088   // These may change access mode, leave these at end of public section
0089   FLATBUFFERS_DELETE_FUNC(DetachedBuffer(const DetachedBuffer &other));
0090   FLATBUFFERS_DELETE_FUNC(
0091       DetachedBuffer &operator=(const DetachedBuffer &other));
0092 
0093  protected:
0094   Allocator *allocator_;
0095   bool own_allocator_;
0096   uint8_t *buf_;
0097   size_t reserved_;
0098   uint8_t *cur_;
0099   size_t size_;
0100 
0101   inline void destroy() {
0102     if (buf_) Deallocate(allocator_, buf_, reserved_);
0103     if (own_allocator_ && allocator_) { delete allocator_; }
0104     reset();
0105   }
0106 
0107   inline void reset() {
0108     allocator_ = nullptr;
0109     own_allocator_ = false;
0110     buf_ = nullptr;
0111     reserved_ = 0;
0112     cur_ = nullptr;
0113     size_ = 0;
0114   }
0115 };
0116 
0117 }  // namespace flatbuffers
0118 
0119 #endif  // FLATBUFFERS_DETACHED_BUFFER_H_