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