[email protected] | ebbccb95 | 2012-04-20 09:51:31 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 6de2799 | 2009-10-19 17:04:46 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 5 | #ifndef BASE_MEMORY_REF_COUNTED_MEMORY_H_ |
| 6 | #define BASE_MEMORY_REF_COUNTED_MEMORY_H_ |
[email protected] | 6de2799 | 2009-10-19 17:04:46 | [diff] [blame] | 7 | |
avi | 9beac25 | 2015-12-24 08:44:47 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
[email protected] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 10 | #include <string> |
[email protected] | 6de2799 | 2009-10-19 17:04:46 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 13 | #include "base/base_export.h" |
[email protected] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 14 | #include "base/compiler_specific.h" |
avi | 9beac25 | 2015-12-24 08:44:47 | [diff] [blame] | 15 | #include "base/macros.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 16 | #include "base/memory/ref_counted.h" |
[email protected] | 6de2799 | 2009-10-19 17:04:46 | [diff] [blame] | 17 | |
[email protected] | 68c7630b | 2012-05-02 22:37:42 | [diff] [blame] | 18 | namespace base { |
[email protected] | 6de2799 | 2009-10-19 17:04:46 | [diff] [blame] | 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. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 23 | class BASE_EXPORT RefCountedMemory |
[email protected] | 9493ee95c | 2011-03-28 23:48:44 | [diff] [blame] | 24 | : public base::RefCountedThreadSafe<RefCountedMemory> { |
[email protected] | 6de2799 | 2009-10-19 17:04:46 | [diff] [blame] | 25 | public: |
[email protected] | 1e7fafc4 | 2009-10-27 16:24:13 | [diff] [blame] | 26 | // Retrieves a pointer to the beginning of the data we point to. If the data |
| 27 | // is empty, this will return NULL. |
[email protected] | 6de2799 | 2009-10-19 17:04:46 | [diff] [blame] | 28 | virtual const unsigned char* front() const = 0; |
| 29 | |
| 30 | // Size of the memory pointed to. |
| 31 | virtual size_t size() const = 0; |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame] | 32 | |
[email protected] | 55653162 | 2013-01-14 18:59:55 | [diff] [blame] | 33 | // Returns true if |other| is byte for byte equal. |
| 34 | bool Equals(const scoped_refptr<RefCountedMemory>& other) const; |
| 35 | |
[email protected] | 8df08cf | 2014-02-12 22:34:08 | [diff] [blame] | 36 | // Handy method to simplify calling front() with a reinterpret_cast. |
| 37 | template<typename T> const T* front_as() const { |
| 38 | return reinterpret_cast<const T*>(front()); |
| 39 | } |
| 40 | |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame] | 41 | protected: |
| 42 | friend class base::RefCountedThreadSafe<RefCountedMemory>; |
[email protected] | d4799a3 | 2010-09-28 22:54:58 | [diff] [blame] | 43 | RefCountedMemory(); |
| 44 | virtual ~RefCountedMemory(); |
[email protected] | 6de2799 | 2009-10-19 17:04:46 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | // An implementation of RefCountedMemory, where the ref counting does not |
| 48 | // matter. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 49 | class BASE_EXPORT RefCountedStaticMemory : public RefCountedMemory { |
[email protected] | 6de2799 | 2009-10-19 17:04:46 | [diff] [blame] | 50 | public: |
| 51 | RefCountedStaticMemory() |
| 52 | : data_(NULL), length_(0) {} |
[email protected] | 8df08cf | 2014-02-12 22:34:08 | [diff] [blame] | 53 | RefCountedStaticMemory(const void* data, size_t length) |
| 54 | : data_(static_cast<const unsigned char*>(length ? data : NULL)), |
| 55 | length_(length) {} |
[email protected] | 6de2799 | 2009-10-19 17:04:46 | [diff] [blame] | 56 | |
[email protected] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 57 | // Overridden from RefCountedMemory: |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 58 | const unsigned char* front() const override; |
| 59 | size_t size() const override; |
[email protected] | 6de2799 | 2009-10-19 17:04:46 | [diff] [blame] | 60 | |
| 61 | private: |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 62 | ~RefCountedStaticMemory() override; |
[email protected] | a9aaa9d1 | 2012-04-25 00:42:51 | [diff] [blame] | 63 | |
[email protected] | 6de2799 | 2009-10-19 17:04:46 | [diff] [blame] | 64 | const unsigned char* data_; |
| 65 | size_t length_; |
| 66 | |
| 67 | DISALLOW_COPY_AND_ASSIGN(RefCountedStaticMemory); |
| 68 | }; |
| 69 | |
[email protected] | 6a497d7 | 2014-04-30 20:30:18 | [diff] [blame] | 70 | // An implementation of RefCountedMemory, where we own the data in a vector. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 71 | class BASE_EXPORT RefCountedBytes : public RefCountedMemory { |
[email protected] | 6de2799 | 2009-10-19 17:04:46 | [diff] [blame] | 72 | public: |
[email protected] | 307175443 | 2010-07-28 00:09:54 | [diff] [blame] | 73 | RefCountedBytes(); |
[email protected] | 6de2799 | 2009-10-19 17:04:46 | [diff] [blame] | 74 | |
| 75 | // Constructs a RefCountedBytes object by _copying_ from |initializer|. |
[email protected] | f3c697c5 | 2013-01-15 10:52:11 | [diff] [blame] | 76 | explicit RefCountedBytes(const std::vector<unsigned char>& initializer); |
[email protected] | 6de2799 | 2009-10-19 17:04:46 | [diff] [blame] | 77 | |
[email protected] | 6a497d7 | 2014-04-30 20:30:18 | [diff] [blame] | 78 | // Constructs a RefCountedBytes object by copying |size| bytes from |p|. |
| 79 | RefCountedBytes(const unsigned char* p, size_t size); |
| 80 | |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 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 | |
[email protected] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 86 | // Overridden from RefCountedMemory: |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 87 | const unsigned char* front() const override; |
| 88 | size_t size() const override; |
[email protected] | 6de2799 | 2009-10-19 17:04:46 | [diff] [blame] | 89 | |
[email protected] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 90 | const std::vector<unsigned char>& data() const { return data_; } |
| 91 | std::vector<unsigned char>& data() { return data_; } |
[email protected] | 6de2799 | 2009-10-19 17:04:46 | [diff] [blame] | 92 | |
[email protected] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 93 | private: |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 94 | ~RefCountedBytes() override; |
[email protected] | 0484d7b | 2010-09-16 22:07:26 | [diff] [blame] | 95 | |
[email protected] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 96 | std::vector<unsigned char> data_; |
| 97 | |
[email protected] | 6de2799 | 2009-10-19 17:04:46 | [diff] [blame] | 98 | DISALLOW_COPY_AND_ASSIGN(RefCountedBytes); |
| 99 | }; |
| 100 | |
[email protected] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 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. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 103 | class BASE_EXPORT RefCountedString : public RefCountedMemory { |
[email protected] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 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()). |
estade | b6178e6 | 2016-01-07 17:19:39 | [diff] [blame] | 110 | static scoped_refptr<RefCountedString> TakeString(std::string* to_destroy); |
[email protected] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 111 | |
| 112 | // Overridden from RefCountedMemory: |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 113 | const unsigned char* front() const override; |
| 114 | size_t size() const override; |
[email protected] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 115 | |
| 116 | const std::string& data() const { return data_; } |
| 117 | std::string& data() { return data_; } |
| 118 | |
| 119 | private: |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 120 | ~RefCountedString() override; |
[email protected] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 121 | |
| 122 | std::string data_; |
| 123 | |
| 124 | DISALLOW_COPY_AND_ASSIGN(RefCountedString); |
| 125 | }; |
| 126 | |
| 127 | } // namespace base |
| 128 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 129 | #endif // BASE_MEMORY_REF_COUNTED_MEMORY_H_ |