ref_counted_memory.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright (c) 2012 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 "base/memory/ref_counted_memory.h"
  5. #include <utility>
  6. #include "base/check_op.h"
  7. #include "base/memory/read_only_shared_memory_region.h"
  8. namespace base {
  9. bool RefCountedMemory::Equals(
  10. const scoped_refptr<RefCountedMemory>& other) const {
  11. return other.get() &&
  12. size() == other->size() &&
  13. (memcmp(front(), other->front(), size()) == 0);
  14. }
  15. RefCountedMemory::RefCountedMemory() = default;
  16. RefCountedMemory::~RefCountedMemory() = default;
  17. const unsigned char* RefCountedStaticMemory::front() const {
  18. return data_;
  19. }
  20. size_t RefCountedStaticMemory::size() const {
  21. return length_;
  22. }
  23. RefCountedStaticMemory::~RefCountedStaticMemory() = default;
  24. RefCountedBytes::RefCountedBytes() = default;
  25. RefCountedBytes::RefCountedBytes(const std::vector<unsigned char>& initializer)
  26. : data_(initializer) {}
  27. RefCountedBytes::RefCountedBytes(base::span<const unsigned char> initializer)
  28. : data_(initializer.begin(), initializer.end()) {}
  29. RefCountedBytes::RefCountedBytes(const unsigned char* p, size_t size)
  30. : data_(p, p + size) {}
  31. RefCountedBytes::RefCountedBytes(size_t size) : data_(size, 0) {}
  32. scoped_refptr<RefCountedBytes> RefCountedBytes::TakeVector(
  33. std::vector<unsigned char>* to_destroy) {
  34. auto bytes = MakeRefCounted<RefCountedBytes>();
  35. bytes->data_.swap(*to_destroy);
  36. return bytes;
  37. }
  38. const unsigned char* RefCountedBytes::front() const {
  39. // STL will assert if we do front() on an empty vector, but calling code
  40. // expects a NULL.
  41. return size() ? &data_.front() : nullptr;
  42. }
  43. size_t RefCountedBytes::size() const {
  44. return data_.size();
  45. }
  46. RefCountedBytes::~RefCountedBytes() = default;
  47. RefCountedString::RefCountedString() = default;
  48. RefCountedString::~RefCountedString() = default;
  49. // static
  50. scoped_refptr<RefCountedString> RefCountedString::TakeString(
  51. std::string* to_destroy) {
  52. return TakeString(std::move(*to_destroy));
  53. }
  54. // static
  55. scoped_refptr<RefCountedString> RefCountedString::TakeString(
  56. std::string&& str) {
  57. auto self = MakeRefCounted<RefCountedString>();
  58. str.swap(self->data_);
  59. return self;
  60. }
  61. const unsigned char* RefCountedString::front() const {
  62. return data_.empty() ? nullptr
  63. : reinterpret_cast<const unsigned char*>(data_.data());
  64. }
  65. size_t RefCountedString::size() const {
  66. return data_.size();
  67. }
  68. RefCountedString16::RefCountedString16() = default;
  69. RefCountedString16::~RefCountedString16() = default;
  70. // static
  71. scoped_refptr<RefCountedString16> RefCountedString16::TakeString(
  72. std::u16string* to_destroy) {
  73. return TakeString(std::move(*to_destroy));
  74. }
  75. // static
  76. scoped_refptr<RefCountedString16> RefCountedString16::TakeString(
  77. std::u16string&& str) {
  78. auto self = MakeRefCounted<RefCountedString16>();
  79. str.swap(self->data_);
  80. return self;
  81. }
  82. const unsigned char* RefCountedString16::front() const {
  83. return reinterpret_cast<const unsigned char*>(data_.data());
  84. }
  85. size_t RefCountedString16::size() const {
  86. return data_.size() * sizeof(char16_t);
  87. }
  88. RefCountedSharedMemoryMapping::RefCountedSharedMemoryMapping(
  89. ReadOnlySharedMemoryMapping mapping)
  90. : mapping_(std::move(mapping)), size_(mapping_.size()) {
  91. DCHECK_GT(size_, 0U);
  92. }
  93. RefCountedSharedMemoryMapping::~RefCountedSharedMemoryMapping() = default;
  94. const unsigned char* RefCountedSharedMemoryMapping::front() const {
  95. return static_cast<const unsigned char*>(mapping_.memory());
  96. }
  97. size_t RefCountedSharedMemoryMapping::size() const {
  98. return size_;
  99. }
  100. // static
  101. scoped_refptr<RefCountedSharedMemoryMapping>
  102. RefCountedSharedMemoryMapping::CreateFromWholeRegion(
  103. const ReadOnlySharedMemoryRegion& region) {
  104. ReadOnlySharedMemoryMapping mapping = region.Map();
  105. if (!mapping.IsValid())
  106. return nullptr;
  107. return MakeRefCounted<RefCountedSharedMemoryMapping>(std::move(mapping));
  108. }
  109. } // namespace base