SharedBuffer.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (C) 2005 The Android Open Source Project
  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. * DEPRECATED. DO NOT USE FOR NEW CODE.
  18. */
  19. #ifndef ANDROID_SHARED_BUFFER_H
  20. #define ANDROID_SHARED_BUFFER_H
  21. #include <atomic>
  22. #include <stdint.h>
  23. #include <sys/types.h>
  24. // ---------------------------------------------------------------------------
  25. namespace android {
  26. class SharedBuffer
  27. {
  28. public:
  29. /* flags to use with release() */
  30. enum {
  31. eKeepStorage = 0x00000001
  32. };
  33. /*! allocate a buffer of size 'size' and acquire() it.
  34. * call release() to free it.
  35. */
  36. static SharedBuffer* alloc(size_t size);
  37. /*! free the memory associated with the SharedBuffer.
  38. * Fails if there are any users associated with this SharedBuffer.
  39. * In other words, the buffer must have been release by all its
  40. * users.
  41. */
  42. static void dealloc(const SharedBuffer* released);
  43. //! access the data for read
  44. inline const void* data() const;
  45. //! access the data for read/write
  46. inline void* data();
  47. //! get size of the buffer
  48. inline size_t size() const;
  49. //! get back a SharedBuffer object from its data
  50. static inline SharedBuffer* bufferFromData(void* data);
  51. //! get back a SharedBuffer object from its data
  52. static inline const SharedBuffer* bufferFromData(const void* data);
  53. //! get the size of a SharedBuffer object from its data
  54. static inline size_t sizeFromData(const void* data);
  55. //! edit the buffer (get a writtable, or non-const, version of it)
  56. SharedBuffer* edit() const;
  57. //! edit the buffer, resizing if needed
  58. SharedBuffer* editResize(size_t size) const;
  59. //! like edit() but fails if a copy is required
  60. SharedBuffer* attemptEdit() const;
  61. //! resize and edit the buffer, loose it's content.
  62. SharedBuffer* reset(size_t size) const;
  63. //! acquire/release a reference on this buffer
  64. void acquire() const;
  65. /*! release a reference on this buffer, with the option of not
  66. * freeing the memory associated with it if it was the last reference
  67. * returns the previous reference count
  68. */
  69. int32_t release(uint32_t flags = 0) const;
  70. //! returns wether or not we're the only owner
  71. inline bool onlyOwner() const;
  72. private:
  73. inline SharedBuffer() { }
  74. inline ~SharedBuffer() { }
  75. SharedBuffer(const SharedBuffer&);
  76. SharedBuffer& operator = (const SharedBuffer&);
  77. // Must be sized to preserve correct alignment.
  78. mutable std::atomic<int32_t> mRefs;
  79. size_t mSize;
  80. uint32_t mReserved[2];
  81. };
  82. static_assert(sizeof(SharedBuffer) % 8 == 0
  83. && (sizeof(size_t) > 4 || sizeof(SharedBuffer) == 16),
  84. "SharedBuffer has unexpected size");
  85. // ---------------------------------------------------------------------------
  86. const void* SharedBuffer::data() const {
  87. return this + 1;
  88. }
  89. void* SharedBuffer::data() {
  90. return this + 1;
  91. }
  92. size_t SharedBuffer::size() const {
  93. return mSize;
  94. }
  95. SharedBuffer* SharedBuffer::bufferFromData(void* data) {
  96. return data ? static_cast<SharedBuffer *>(data)-1 : nullptr;
  97. }
  98. const SharedBuffer* SharedBuffer::bufferFromData(const void* data) {
  99. return data ? static_cast<const SharedBuffer *>(data)-1 : nullptr;
  100. }
  101. size_t SharedBuffer::sizeFromData(const void* data) {
  102. return data ? bufferFromData(data)->mSize : 0;
  103. }
  104. bool SharedBuffer::onlyOwner() const {
  105. return (mRefs.load(std::memory_order_acquire) == 1);
  106. }
  107. } // namespace android
  108. // ---------------------------------------------------------------------------
  109. #endif // ANDROID_VECTOR_H