io_buffer.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright (c) 2011 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 "net/base/io_buffer.h"
  5. #include "base/check_op.h"
  6. #include "base/numerics/safe_math.h"
  7. namespace net {
  8. // TODO(eroman): IOBuffer is being converted to require buffer sizes and offsets
  9. // be specified as "size_t" rather than "int" (crbug.com/488553). To facilitate
  10. // this move (since LOTS of code needs to be updated), both "size_t" and "int
  11. // are being accepted. When using "size_t" this function ensures that it can be
  12. // safely converted to an "int" without truncation.
  13. void IOBuffer::AssertValidBufferSize(size_t size) {
  14. base::CheckedNumeric<int>(size).ValueOrDie();
  15. }
  16. void IOBuffer::AssertValidBufferSize(int size) {
  17. CHECK_GE(size, 0);
  18. }
  19. IOBuffer::IOBuffer() : data_(nullptr) {}
  20. IOBuffer::IOBuffer(size_t buffer_size) {
  21. AssertValidBufferSize(buffer_size);
  22. data_ = new char[buffer_size];
  23. }
  24. IOBuffer::IOBuffer(char* data)
  25. : data_(data) {
  26. }
  27. IOBuffer::~IOBuffer() {
  28. data_.ClearAndDeleteArray();
  29. }
  30. IOBufferWithSize::IOBufferWithSize(size_t size) : IOBuffer(size), size_(size) {
  31. // Note: Size check is done in superclass' constructor.
  32. }
  33. IOBufferWithSize::IOBufferWithSize(char* data, size_t size)
  34. : IOBuffer(data), size_(size) {
  35. AssertValidBufferSize(size);
  36. }
  37. IOBufferWithSize::~IOBufferWithSize() = default;
  38. StringIOBuffer::StringIOBuffer(const std::string& s)
  39. : IOBuffer(static_cast<char*>(nullptr)), string_data_(s) {
  40. AssertValidBufferSize(s.size());
  41. data_ = const_cast<char*>(string_data_.data());
  42. }
  43. StringIOBuffer::StringIOBuffer(std::unique_ptr<std::string> s)
  44. : IOBuffer(static_cast<char*>(nullptr)) {
  45. AssertValidBufferSize(s->size());
  46. string_data_.swap(*s.get());
  47. data_ = const_cast<char*>(string_data_.data());
  48. }
  49. StringIOBuffer::~StringIOBuffer() {
  50. // We haven't allocated the buffer, so remove it before the base class
  51. // destructor tries to delete[] it.
  52. data_ = nullptr;
  53. }
  54. DrainableIOBuffer::DrainableIOBuffer(scoped_refptr<IOBuffer> base, int size)
  55. : IOBuffer(base->data()), base_(std::move(base)), size_(size) {
  56. AssertValidBufferSize(size);
  57. }
  58. DrainableIOBuffer::DrainableIOBuffer(scoped_refptr<IOBuffer> base, size_t size)
  59. : IOBuffer(base->data()), base_(std::move(base)), size_(size) {
  60. AssertValidBufferSize(size);
  61. }
  62. void DrainableIOBuffer::DidConsume(int bytes) {
  63. SetOffset(used_ + bytes);
  64. }
  65. int DrainableIOBuffer::BytesRemaining() const {
  66. return size_ - used_;
  67. }
  68. // Returns the number of consumed bytes.
  69. int DrainableIOBuffer::BytesConsumed() const {
  70. return used_;
  71. }
  72. void DrainableIOBuffer::SetOffset(int bytes) {
  73. DCHECK_GE(bytes, 0);
  74. DCHECK_LE(bytes, size_);
  75. used_ = bytes;
  76. data_ = base_->data() + used_;
  77. }
  78. DrainableIOBuffer::~DrainableIOBuffer() {
  79. // The buffer is owned by the |base_| instance.
  80. data_ = nullptr;
  81. }
  82. GrowableIOBuffer::GrowableIOBuffer() = default;
  83. void GrowableIOBuffer::SetCapacity(int capacity) {
  84. DCHECK_GE(capacity, 0);
  85. // this will get reset in `set_offset`.
  86. data_ = nullptr;
  87. // realloc will crash if it fails.
  88. real_data_.reset(static_cast<char*>(realloc(real_data_.release(), capacity)));
  89. capacity_ = capacity;
  90. if (offset_ > capacity)
  91. set_offset(capacity);
  92. else
  93. set_offset(offset_); // The pointer may have changed.
  94. }
  95. void GrowableIOBuffer::set_offset(int offset) {
  96. DCHECK_GE(offset, 0);
  97. DCHECK_LE(offset, capacity_);
  98. offset_ = offset;
  99. data_ = real_data_.get() + offset;
  100. }
  101. int GrowableIOBuffer::RemainingCapacity() {
  102. return capacity_ - offset_;
  103. }
  104. char* GrowableIOBuffer::StartOfBuffer() {
  105. return real_data_.get();
  106. }
  107. GrowableIOBuffer::~GrowableIOBuffer() {
  108. data_ = nullptr;
  109. }
  110. PickledIOBuffer::PickledIOBuffer() : IOBuffer() {
  111. }
  112. void PickledIOBuffer::Done() {
  113. data_ = const_cast<char*>(static_cast<const char*>(pickle_.data()));
  114. }
  115. PickledIOBuffer::~PickledIOBuffer() {
  116. data_ = nullptr;
  117. }
  118. WrappedIOBuffer::WrappedIOBuffer(const char* data)
  119. : IOBuffer(const_cast<char*>(data)) {
  120. }
  121. WrappedIOBuffer::~WrappedIOBuffer() {
  122. data_ = nullptr;
  123. }
  124. } // namespace net