Revert 249576 "Cast: Wiring up the asyc initialization with the ..."
[chromium-blink-merge.git] / base / memory / ref_counted_memory.h
blobd2987c5d21bd3d1cb1c6ebd6bf3c7f827b341491
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.
5 #ifndef BASE_MEMORY_REF_COUNTED_MEMORY_H_
6 #define BASE_MEMORY_REF_COUNTED_MEMORY_H_
8 #include <string>
9 #include <vector>
11 #include "base/base_export.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/ref_counted.h"
15 namespace base {
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;
27 // Size of the memory pointed to.
28 virtual size_t size() const = 0;
30 // Returns true if |other| is byte for byte equal.
31 bool Equals(const scoped_refptr<RefCountedMemory>& other) const;
33 protected:
34 friend class base::RefCountedThreadSafe<RefCountedMemory>;
35 RefCountedMemory();
36 virtual ~RefCountedMemory();
39 // An implementation of RefCountedMemory, where the ref counting does not
40 // matter.
41 class BASE_EXPORT RefCountedStaticMemory : public RefCountedMemory {
42 public:
43 RefCountedStaticMemory()
44 : data_(NULL), length_(0) {}
45 RefCountedStaticMemory(const unsigned char* data, size_t length)
46 : data_(length ? data : NULL), length_(length) {}
48 // Overridden from RefCountedMemory:
49 virtual const unsigned char* front() const OVERRIDE;
50 virtual size_t size() const OVERRIDE;
52 private:
53 virtual ~RefCountedStaticMemory();
55 const unsigned char* data_;
56 size_t length_;
58 DISALLOW_COPY_AND_ASSIGN(RefCountedStaticMemory);
61 // An implementation of RefCountedMemory, where we own our the data in a
62 // vector.
63 class BASE_EXPORT RefCountedBytes : public RefCountedMemory {
64 public:
65 RefCountedBytes();
67 // Constructs a RefCountedBytes object by _copying_ from |initializer|.
68 explicit RefCountedBytes(const std::vector<unsigned char>& initializer);
70 // Constructs a RefCountedBytes object by performing a swap. (To non
71 // destructively build a RefCountedBytes, use the constructor that takes a
72 // vector.)
73 static RefCountedBytes* TakeVector(std::vector<unsigned char>* to_destroy);
75 // Overridden from RefCountedMemory:
76 virtual const unsigned char* front() const OVERRIDE;
77 virtual size_t size() const OVERRIDE;
79 const std::vector<unsigned char>& data() const { return data_; }
80 std::vector<unsigned char>& data() { return data_; }
82 private:
83 virtual ~RefCountedBytes();
85 std::vector<unsigned char> data_;
87 DISALLOW_COPY_AND_ASSIGN(RefCountedBytes);
90 // An implementation of RefCountedMemory, where the bytes are stored in an STL
91 // string. Use this if your data naturally arrives in that format.
92 class BASE_EXPORT RefCountedString : public RefCountedMemory {
93 public:
94 RefCountedString();
96 // Constructs a RefCountedString object by performing a swap. (To non
97 // destructively build a RefCountedString, use the default constructor and
98 // copy into object->data()).
99 static RefCountedString* TakeString(std::string* to_destroy);
101 // Overridden from RefCountedMemory:
102 virtual const unsigned char* front() const OVERRIDE;
103 virtual size_t size() const OVERRIDE;
105 const std::string& data() const { return data_; }
106 std::string& data() { return data_; }
108 private:
109 virtual ~RefCountedString();
111 std::string data_;
113 DISALLOW_COPY_AND_ASSIGN(RefCountedString);
116 // An implementation of RefCountedMemory that holds a chunk of memory
117 // previously allocated with malloc or calloc, and that therefore must be freed
118 // using free().
119 class BASE_EXPORT RefCountedMallocedMemory : public base::RefCountedMemory {
120 public:
121 RefCountedMallocedMemory(void* data, size_t length);
123 // Overridden from RefCountedMemory:
124 virtual const unsigned char* front() const OVERRIDE;
125 virtual size_t size() const OVERRIDE;
127 private:
128 virtual ~RefCountedMallocedMemory();
130 unsigned char* data_;
131 size_t length_;
133 DISALLOW_COPY_AND_ASSIGN(RefCountedMallocedMemory);
136 } // namespace base
138 #endif // BASE_MEMORY_REF_COUNTED_MEMORY_H_