io_buffer_pool.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "chromecast/net/io_buffer_pool.h"
  5. #include <new>
  6. #include "base/bits.h"
  7. #include "base/memory/aligned_memory.h"
  8. #include "base/synchronization/lock.h"
  9. namespace chromecast {
  10. // The IOBufferPool allocates IOBuffers and the associated data as a single
  11. // contiguous buffer. The buffer is laid out like this:
  12. // |------------Wrapper----------|---data buffer---|
  13. // |--IOBuffer--|--Internal ptr--|---data buffer---|
  14. //
  15. // The contiguous buffer is allocated as a character array, and then a Wrapper
  16. // instance is placement-newed into it. We return a pointer to the IOBuffer
  17. // within the Wrapper.
  18. //
  19. // When the IOBuffer is deleted (in operator delete), we get a pointer to the
  20. // beginning of storage for the IOBuffer, which is the same memory location
  21. // as the Wrapper instance (since the Wrapper has no vtable or base class, this
  22. // should be true for any compiler). We can therefore cast the "deleted"
  23. // pointer to a Wrapper* and then reclaim the buffer.
  24. //
  25. // All the actual data and logic for the buffer pool is held in the Internal
  26. // class, which is refcounted with 1 ref for the IOBufferPool owner, and 1 ref
  27. // for each buffer currently in use (ie, not in the free list). The Internal
  28. // instance is only deleted when its internal refcount drops to 0; this allows
  29. // buffers allocated from the pool to be safely used and deleted even after the
  30. // pool has been destroyed.
  31. //
  32. // Note that locking in the Internal methods is optional since it is only needed
  33. // when threadsafe operation is requested.
  34. class IOBufferPool::Internal {
  35. public:
  36. Internal(size_t buffer_size, size_t max_buffers, bool threadsafe);
  37. Internal(const Internal&) = delete;
  38. Internal& operator=(const Internal&) = delete;
  39. size_t num_allocated() const {
  40. base::AutoLockMaybe lock(lock_ptr_);
  41. return num_allocated_;
  42. }
  43. size_t num_free() const {
  44. base::AutoLockMaybe lock(lock_ptr_);
  45. return num_free_;
  46. }
  47. void Preallocate(size_t num_buffers);
  48. void OwnerDestroyed();
  49. scoped_refptr<net::IOBuffer> GetBuffer();
  50. private:
  51. class Buffer;
  52. class Wrapper;
  53. union Storage;
  54. static constexpr size_t kAlignment = 16;
  55. static void* AllocateAlignedSpace(size_t buffer_size);
  56. ~Internal();
  57. void Reclaim(Wrapper* wrapper);
  58. const size_t buffer_size_;
  59. const size_t max_buffers_;
  60. mutable base::Lock lock_;
  61. base::Lock* const lock_ptr_;
  62. Storage* free_buffers_;
  63. size_t num_allocated_;
  64. size_t num_free_;
  65. int refs_;
  66. };
  67. class IOBufferPool::Internal::Buffer : public net::IOBuffer {
  68. public:
  69. explicit Buffer(char* data) : net::IOBuffer(data) {}
  70. Buffer(const Buffer&) = delete;
  71. Buffer& operator=(const Buffer&) = delete;
  72. private:
  73. friend class Wrapper;
  74. ~Buffer() override { data_ = nullptr; }
  75. static void operator delete(void* ptr);
  76. };
  77. class IOBufferPool::Internal::Wrapper {
  78. public:
  79. Wrapper(char* data, IOBufferPool::Internal* pool)
  80. : buffer_(data), pool_(pool) {}
  81. Wrapper(const Wrapper&) = delete;
  82. Wrapper& operator=(const Wrapper&) = delete;
  83. ~Wrapper() = delete;
  84. static void operator delete(void*) = delete;
  85. Buffer* buffer() { return &buffer_; }
  86. void Reclaim() { pool_->Reclaim(this); }
  87. private:
  88. Buffer buffer_;
  89. IOBufferPool::Internal* const pool_;
  90. };
  91. union IOBufferPool::Internal::Storage {
  92. Storage* next; // Pointer to next free buffer.
  93. Wrapper wrapper;
  94. };
  95. void IOBufferPool::Internal::Buffer::operator delete(void* ptr) {
  96. Wrapper* wrapper = reinterpret_cast<Wrapper*>(ptr);
  97. wrapper->Reclaim();
  98. }
  99. IOBufferPool::Internal::Internal(size_t buffer_size,
  100. size_t max_buffers,
  101. bool threadsafe)
  102. : buffer_size_(buffer_size),
  103. max_buffers_(max_buffers),
  104. lock_ptr_(threadsafe ? &lock_ : nullptr),
  105. free_buffers_(nullptr),
  106. num_allocated_(0),
  107. num_free_(0),
  108. refs_(1) { // 1 ref for the owner.
  109. }
  110. IOBufferPool::Internal::~Internal() {
  111. while (free_buffers_) {
  112. char* data = reinterpret_cast<char*>(free_buffers_);
  113. free_buffers_ = free_buffers_->next;
  114. base::AlignedFree(data);
  115. }
  116. }
  117. // static
  118. void* IOBufferPool::Internal::AllocateAlignedSpace(size_t buffer_size) {
  119. size_t kAlignedStorageSize = base::bits::AlignUp(sizeof(Storage), kAlignment);
  120. return base::AlignedAlloc(kAlignedStorageSize + buffer_size, kAlignment);
  121. }
  122. void IOBufferPool::Internal::Preallocate(size_t num_buffers) {
  123. // We assume that this is uncontended in normal usage, so just lock for the
  124. // entire method.
  125. base::AutoLockMaybe lock(lock_ptr_);
  126. if (num_buffers > max_buffers_) {
  127. num_buffers = max_buffers_;
  128. }
  129. if (num_allocated_ >= num_buffers) {
  130. return;
  131. }
  132. size_t num_extra_buffers = num_buffers - num_allocated_;
  133. num_free_ += num_extra_buffers;
  134. num_allocated_ += num_extra_buffers;
  135. while (num_extra_buffers > 0) {
  136. void* ptr = AllocateAlignedSpace(buffer_size_);
  137. Storage* storage = reinterpret_cast<Storage*>(ptr);
  138. storage->next = free_buffers_;
  139. free_buffers_ = storage;
  140. --num_extra_buffers;
  141. }
  142. // No need to add refs here, since the newly allocated buffers are not in use.
  143. }
  144. void IOBufferPool::Internal::OwnerDestroyed() {
  145. bool deletable;
  146. {
  147. base::AutoLockMaybe lock(lock_ptr_);
  148. --refs_; // Remove the owner's ref.
  149. deletable = (refs_ == 0);
  150. }
  151. if (deletable) {
  152. delete this;
  153. }
  154. }
  155. scoped_refptr<net::IOBuffer> IOBufferPool::Internal::GetBuffer() {
  156. char* ptr = nullptr;
  157. {
  158. base::AutoLockMaybe lock(lock_ptr_);
  159. if (free_buffers_) {
  160. ptr = reinterpret_cast<char*>(free_buffers_);
  161. free_buffers_ = free_buffers_->next;
  162. --num_free_;
  163. } else {
  164. if (num_allocated_ == max_buffers_)
  165. return nullptr;
  166. ++num_allocated_;
  167. }
  168. ++refs_; // Add a ref for the now in-use buffer.
  169. }
  170. if (!ptr) {
  171. ptr = static_cast<char*>(AllocateAlignedSpace(buffer_size_));
  172. }
  173. size_t kAlignedStorageSize = base::bits::AlignUp(sizeof(Storage), kAlignment);
  174. char* data = ptr + kAlignedStorageSize;
  175. Wrapper* wrapper = new (ptr) Wrapper(data, this);
  176. return scoped_refptr<net::IOBuffer>(wrapper->buffer());
  177. }
  178. void IOBufferPool::Internal::Reclaim(Wrapper* wrapper) {
  179. Storage* storage = reinterpret_cast<Storage*>(wrapper);
  180. bool deletable;
  181. {
  182. base::AutoLockMaybe lock(lock_ptr_);
  183. storage->next = free_buffers_;
  184. free_buffers_ = storage;
  185. ++num_free_;
  186. --refs_; // Remove a ref since this buffer is no longer in use.
  187. deletable = (refs_ == 0);
  188. }
  189. if (deletable) {
  190. delete this;
  191. }
  192. }
  193. IOBufferPool::IOBufferPool(size_t buffer_size,
  194. size_t max_buffers,
  195. bool threadsafe)
  196. : buffer_size_(buffer_size),
  197. max_buffers_(max_buffers),
  198. threadsafe_(threadsafe),
  199. internal_(new Internal(buffer_size, max_buffers, threadsafe)) {}
  200. IOBufferPool::IOBufferPool(size_t buffer_size)
  201. : IOBufferPool(buffer_size, static_cast<size_t>(-1)) {}
  202. IOBufferPool::~IOBufferPool() {
  203. internal_->OwnerDestroyed();
  204. }
  205. size_t IOBufferPool::NumAllocatedForTesting() const {
  206. return internal_->num_allocated();
  207. }
  208. size_t IOBufferPool::NumFreeForTesting() const {
  209. return internal_->num_free();
  210. }
  211. void IOBufferPool::Preallocate(size_t num_buffers) {
  212. internal_->Preallocate(num_buffers);
  213. }
  214. scoped_refptr<net::IOBuffer> IOBufferPool::GetBuffer() {
  215. return internal_->GetBuffer();
  216. }
  217. } // namespace chromecast