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