detached_buffer.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #ifndef FLATBUFFERS_DETACHED_BUFFER_H_
  17. #define FLATBUFFERS_DETACHED_BUFFER_H_
  18. #include "flatbuffers/allocator.h"
  19. #include "flatbuffers/base.h"
  20. #include "flatbuffers/default_allocator.h"
  21. namespace flatbuffers {
  22. // DetachedBuffer is a finished flatbuffer memory region, detached from its
  23. // builder. The original memory region and allocator are also stored so that
  24. // the DetachedBuffer can manage the memory lifetime.
  25. class DetachedBuffer {
  26. public:
  27. DetachedBuffer()
  28. : allocator_(nullptr),
  29. own_allocator_(false),
  30. buf_(nullptr),
  31. reserved_(0),
  32. cur_(nullptr),
  33. size_(0) {}
  34. DetachedBuffer(Allocator *allocator, bool own_allocator, uint8_t *buf,
  35. size_t reserved, uint8_t *cur, size_t sz)
  36. : allocator_(allocator),
  37. own_allocator_(own_allocator),
  38. buf_(buf),
  39. reserved_(reserved),
  40. cur_(cur),
  41. size_(sz) {}
  42. DetachedBuffer(DetachedBuffer &&other)
  43. : allocator_(other.allocator_),
  44. own_allocator_(other.own_allocator_),
  45. buf_(other.buf_),
  46. reserved_(other.reserved_),
  47. cur_(other.cur_),
  48. size_(other.size_) {
  49. other.reset();
  50. }
  51. DetachedBuffer &operator=(DetachedBuffer &&other) {
  52. if (this == &other) return *this;
  53. destroy();
  54. allocator_ = other.allocator_;
  55. own_allocator_ = other.own_allocator_;
  56. buf_ = other.buf_;
  57. reserved_ = other.reserved_;
  58. cur_ = other.cur_;
  59. size_ = other.size_;
  60. other.reset();
  61. return *this;
  62. }
  63. ~DetachedBuffer() { destroy(); }
  64. const uint8_t *data() const { return cur_; }
  65. uint8_t *data() { return cur_; }
  66. size_t size() const { return size_; }
  67. // These may change access mode, leave these at end of public section
  68. FLATBUFFERS_DELETE_FUNC(DetachedBuffer(const DetachedBuffer &other));
  69. FLATBUFFERS_DELETE_FUNC(
  70. DetachedBuffer &operator=(const DetachedBuffer &other));
  71. protected:
  72. Allocator *allocator_;
  73. bool own_allocator_;
  74. uint8_t *buf_;
  75. size_t reserved_;
  76. uint8_t *cur_;
  77. size_t size_;
  78. inline void destroy() {
  79. if (buf_) Deallocate(allocator_, buf_, reserved_);
  80. if (own_allocator_ && allocator_) { delete allocator_; }
  81. reset();
  82. }
  83. inline void reset() {
  84. allocator_ = nullptr;
  85. own_allocator_ = false;
  86. buf_ = nullptr;
  87. reserved_ = 0;
  88. cur_ = nullptr;
  89. size_ = 0;
  90. }
  91. };
  92. } // namespace flatbuffers
  93. #endif // FLATBUFFERS_DETACHED_BUFFER_H_