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 5 #ifndef BASE_MEMORY_REF_COUNTED_MEMORY_H_ 6 #define BASE_MEMORY_REF_COUNTED_MEMORY_H_ 7 8 #include <stddef.h> 9 10 #include <string> 11 #include <vector> 12 13 #include "base/base_export.h" 14 #include "base/compiler_specific.h" 15 #include "base/macros.h" 16 #include "base/memory/ref_counted.h" 17 18 namespace base { 19 20 // A generic interface to memory. This object is reference counted because one 21 // of its two subclasses own the data they carry, and we need to have 22 // heterogeneous containers of these two types of memory. 23 class BASE_EXPORT RefCountedMemory 24 : public base::RefCountedThreadSafe<RefCountedMemory> { 25 public: 26 // Retrieves a pointer to the beginning of the data we point to. If the data 27 // is empty, this will return NULL. 28 virtual const unsigned char* front() const = 0; 29 30 // Size of the memory pointed to. 31 virtual size_t size() const = 0; 32 33 // Returns true if |other| is byte for byte equal. 34 bool Equals(const scoped_refptr<RefCountedMemory>& other) const; 35 36 // Handy method to simplify calling front() with a reinterpret_cast. front_as()37 template<typename T> const T* front_as() const { 38 return reinterpret_cast<const T*>(front()); 39 } 40 41 protected: 42 friend class base::RefCountedThreadSafe<RefCountedMemory>; 43 RefCountedMemory(); 44 virtual ~RefCountedMemory(); 45 }; 46 47 // An implementation of RefCountedMemory, where the ref counting does not 48 // matter. 49 class BASE_EXPORT RefCountedStaticMemory : public RefCountedMemory { 50 public: RefCountedStaticMemory()51 RefCountedStaticMemory() 52 : data_(NULL), length_(0) {} RefCountedStaticMemory(const void * data,size_t length)53 RefCountedStaticMemory(const void* data, size_t length) 54 : data_(static_cast<const unsigned char*>(length ? data : NULL)), 55 length_(length) {} 56 57 // Overridden from RefCountedMemory: 58 const unsigned char* front() const override; 59 size_t size() const override; 60 61 private: 62 ~RefCountedStaticMemory() override; 63 64 const unsigned char* data_; 65 size_t length_; 66 67 DISALLOW_COPY_AND_ASSIGN(RefCountedStaticMemory); 68 }; 69 70 // An implementation of RefCountedMemory, where we own the data in a vector. 71 class BASE_EXPORT RefCountedBytes : public RefCountedMemory { 72 public: 73 RefCountedBytes(); 74 75 // Constructs a RefCountedBytes object by _copying_ from |initializer|. 76 explicit RefCountedBytes(const std::vector<unsigned char>& initializer); 77 78 // Constructs a RefCountedBytes object by copying |size| bytes from |p|. 79 RefCountedBytes(const unsigned char* p, size_t size); 80 81 // Constructs a RefCountedBytes object by performing a swap. (To non 82 // destructively build a RefCountedBytes, use the constructor that takes a 83 // vector.) 84 static RefCountedBytes* TakeVector(std::vector<unsigned char>* to_destroy); 85 86 // Overridden from RefCountedMemory: 87 const unsigned char* front() const override; 88 size_t size() const override; 89 data()90 const std::vector<unsigned char>& data() const { return data_; } data()91 std::vector<unsigned char>& data() { return data_; } 92 93 private: 94 ~RefCountedBytes() override; 95 96 std::vector<unsigned char> data_; 97 98 DISALLOW_COPY_AND_ASSIGN(RefCountedBytes); 99 }; 100 101 // An implementation of RefCountedMemory, where the bytes are stored in an STL 102 // string. Use this if your data naturally arrives in that format. 103 class BASE_EXPORT RefCountedString : public RefCountedMemory { 104 public: 105 RefCountedString(); 106 107 // Constructs a RefCountedString object by performing a swap. (To non 108 // destructively build a RefCountedString, use the default constructor and 109 // copy into object->data()). 110 static scoped_refptr<RefCountedString> TakeString(std::string* to_destroy); 111 112 // Overridden from RefCountedMemory: 113 const unsigned char* front() const override; 114 size_t size() const override; 115 data()116 const std::string& data() const { return data_; } data()117 std::string& data() { return data_; } 118 119 private: 120 ~RefCountedString() override; 121 122 std::string data_; 123 124 DISALLOW_COPY_AND_ASSIGN(RefCountedString); 125 }; 126 127 } // namespace base 128 129 #endif // BASE_MEMORY_REF_COUNTED_MEMORY_H_ 130