ref_counted_memory.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. #ifndef BASE_MEMORY_REF_COUNTED_MEMORY_H_
  5. #define BASE_MEMORY_REF_COUNTED_MEMORY_H_
  6. #include <stddef.h>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/base_export.h"
  11. #include "base/containers/span.h"
  12. #include "base/memory/ref_counted.h"
  13. #include "base/memory/shared_memory_mapping.h"
  14. namespace base {
  15. class ReadOnlySharedMemoryRegion;
  16. // A generic interface to memory. This object is reference counted because most
  17. // of its subclasses own the data they carry, and this interface needs to
  18. // support heterogeneous containers of these different types of memory.
  19. class BASE_EXPORT RefCountedMemory
  20. : public RefCountedThreadSafe<RefCountedMemory> {
  21. public:
  22. // Retrieves a pointer to the beginning of the data we point to. If the data
  23. // is empty, this will return NULL.
  24. virtual const unsigned char* front() const = 0;
  25. // Size of the memory pointed to.
  26. virtual size_t size() const = 0;
  27. // Returns true if |other| is byte for byte equal.
  28. bool Equals(const scoped_refptr<RefCountedMemory>& other) const;
  29. // Handy method to simplify calling front() with a reinterpret_cast.
  30. template<typename T> const T* front_as() const {
  31. return reinterpret_cast<const T*>(front());
  32. }
  33. // Alias for front() to make it possible for RefCountedMemory to implicitly
  34. // convert to span.
  35. const unsigned char* data() const { return front(); }
  36. protected:
  37. friend class RefCountedThreadSafe<RefCountedMemory>;
  38. RefCountedMemory();
  39. virtual ~RefCountedMemory();
  40. };
  41. // An implementation of RefCountedMemory, where the ref counting does not
  42. // matter.
  43. class BASE_EXPORT RefCountedStaticMemory : public RefCountedMemory {
  44. public:
  45. RefCountedStaticMemory() : data_(nullptr), length_(0) {}
  46. RefCountedStaticMemory(const void* data, size_t length)
  47. : data_(static_cast<const unsigned char*>(length ? data : nullptr)),
  48. length_(length) {}
  49. RefCountedStaticMemory(const RefCountedStaticMemory&) = delete;
  50. RefCountedStaticMemory& operator=(const RefCountedStaticMemory&) = delete;
  51. // RefCountedMemory:
  52. const unsigned char* front() const override;
  53. size_t size() const override;
  54. private:
  55. ~RefCountedStaticMemory() override;
  56. const unsigned char* data_;
  57. size_t length_;
  58. };
  59. // An implementation of RefCountedMemory, where the data is stored in a STL
  60. // vector.
  61. class BASE_EXPORT RefCountedBytes : public RefCountedMemory {
  62. public:
  63. RefCountedBytes();
  64. // Constructs a RefCountedBytes object by copying from |initializer|.
  65. explicit RefCountedBytes(const std::vector<unsigned char>& initializer);
  66. explicit RefCountedBytes(base::span<const unsigned char> initializer);
  67. // Constructs a RefCountedBytes object by copying |size| bytes from |p|.
  68. RefCountedBytes(const unsigned char* p, size_t size);
  69. // Constructs a RefCountedBytes object by zero-initializing a new vector of
  70. // |size| bytes.
  71. explicit RefCountedBytes(size_t size);
  72. RefCountedBytes(const RefCountedBytes&) = delete;
  73. RefCountedBytes& operator=(const RefCountedBytes&) = delete;
  74. // Constructs a RefCountedBytes object by performing a swap. (To non
  75. // destructively build a RefCountedBytes, use the constructor that takes a
  76. // vector.)
  77. static scoped_refptr<RefCountedBytes> TakeVector(
  78. std::vector<unsigned char>* to_destroy);
  79. // RefCountedMemory:
  80. const unsigned char* front() const override;
  81. size_t size() const override;
  82. const std::vector<unsigned char>& data() const { return data_; }
  83. std::vector<unsigned char>& data() { return data_; }
  84. // Non-const versions of front() and front_as() that are simply shorthand for
  85. // data().data().
  86. unsigned char* front() { return data_.data(); }
  87. template <typename T>
  88. T* front_as() {
  89. return reinterpret_cast<T*>(front());
  90. }
  91. private:
  92. ~RefCountedBytes() override;
  93. std::vector<unsigned char> data_;
  94. };
  95. // An implementation of RefCountedMemory, where the bytes are stored in a STL
  96. // string. Use this if your data naturally arrives in that format.
  97. class BASE_EXPORT RefCountedString : public RefCountedMemory {
  98. public:
  99. RefCountedString();
  100. RefCountedString(const RefCountedString&) = delete;
  101. RefCountedString& operator=(const RefCountedString&) = delete;
  102. // Constructs a RefCountedString object by performing a swap. (To non
  103. // destructively build a RefCountedString, use the default constructor and
  104. // copy into object->data()).
  105. static scoped_refptr<RefCountedString> TakeString(std::string* to_destroy);
  106. static scoped_refptr<RefCountedString> TakeString(std::string&& str);
  107. // RefCountedMemory:
  108. const unsigned char* front() const override;
  109. size_t size() const override;
  110. const std::string& data() const { return data_; }
  111. std::string& data() { return data_; }
  112. private:
  113. ~RefCountedString() override;
  114. std::string data_;
  115. };
  116. // An implementation of RefCountedMemory, where the bytes are stored in a
  117. // std::u16string.
  118. class BASE_EXPORT RefCountedString16 : public base::RefCountedMemory {
  119. public:
  120. RefCountedString16();
  121. RefCountedString16(const RefCountedString16&) = delete;
  122. RefCountedString16& operator=(const RefCountedString16&) = delete;
  123. // Constructs a RefCountedString16 object by performing a swap.
  124. static scoped_refptr<RefCountedString16> TakeString(
  125. std::u16string* to_destroy);
  126. static scoped_refptr<RefCountedString16> TakeString(std::u16string&& str);
  127. // RefCountedMemory:
  128. const unsigned char* front() const override;
  129. size_t size() const override;
  130. protected:
  131. ~RefCountedString16() override;
  132. private:
  133. std::u16string data_;
  134. };
  135. // An implementation of RefCountedMemory, where the bytes are stored in
  136. // ReadOnlySharedMemoryMapping.
  137. class BASE_EXPORT RefCountedSharedMemoryMapping : public RefCountedMemory {
  138. public:
  139. // Constructs a RefCountedMemory object by taking ownership of an already
  140. // mapped ReadOnlySharedMemoryMapping object.
  141. explicit RefCountedSharedMemoryMapping(ReadOnlySharedMemoryMapping mapping);
  142. RefCountedSharedMemoryMapping(const RefCountedSharedMemoryMapping&) = delete;
  143. RefCountedSharedMemoryMapping& operator=(
  144. const RefCountedSharedMemoryMapping&) = delete;
  145. // Convenience method to map all of |region| and take ownership of the
  146. // mapping. Returns an empty scoped_refptr if the map operation fails.
  147. static scoped_refptr<RefCountedSharedMemoryMapping> CreateFromWholeRegion(
  148. const ReadOnlySharedMemoryRegion& region);
  149. // RefCountedMemory:
  150. const unsigned char* front() const override;
  151. size_t size() const override;
  152. private:
  153. ~RefCountedSharedMemoryMapping() override;
  154. const ReadOnlySharedMemoryMapping mapping_;
  155. const size_t size_;
  156. };
  157. } // namespace base
  158. #endif // BASE_MEMORY_REF_COUNTED_MEMORY_H_