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_
11 #include "base/base_export.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/ref_counted.h"
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
> {
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;
31 friend class base::RefCountedThreadSafe
<RefCountedMemory
>;
33 virtual ~RefCountedMemory();
36 // An implementation of RefCountedMemory, where the ref counting does not
38 class BASE_EXPORT RefCountedStaticMemory
: public RefCountedMemory
{
40 RefCountedStaticMemory()
41 : data_(NULL
), length_(0) {}
42 RefCountedStaticMemory(const unsigned char* data
, size_t length
)
43 : data_(length
? data
: NULL
), length_(length
) {}
45 // Overridden from RefCountedMemory:
46 virtual const unsigned char* front() const OVERRIDE
;
47 virtual size_t size() const OVERRIDE
;
50 virtual ~RefCountedStaticMemory();
52 const unsigned char* data_
;
55 DISALLOW_COPY_AND_ASSIGN(RefCountedStaticMemory
);
58 // An implementation of RefCountedMemory, where we own our the data in a
60 class BASE_EXPORT RefCountedBytes
: public RefCountedMemory
{
64 // Constructs a RefCountedBytes object by _copying_ from |initializer|.
65 RefCountedBytes(const std::vector
<unsigned char>& initializer
);
67 // Constructs a RefCountedBytes object by performing a swap. (To non
68 // destructively build a RefCountedBytes, use the constructor that takes a
70 static RefCountedBytes
* TakeVector(std::vector
<unsigned char>* to_destroy
);
72 // Overridden from RefCountedMemory:
73 virtual const unsigned char* front() const OVERRIDE
;
74 virtual size_t size() const OVERRIDE
;
76 const std::vector
<unsigned char>& data() const { return data_
; }
77 std::vector
<unsigned char>& data() { return data_
; }
80 virtual ~RefCountedBytes();
82 std::vector
<unsigned char> data_
;
84 DISALLOW_COPY_AND_ASSIGN(RefCountedBytes
);
87 // An implementation of RefCountedMemory, where the bytes are stored in an STL
88 // string. Use this if your data naturally arrives in that format.
89 class BASE_EXPORT RefCountedString
: public RefCountedMemory
{
93 // Constructs a RefCountedString object by performing a swap. (To non
94 // destructively build a RefCountedString, use the default constructor and
95 // copy into object->data()).
96 static RefCountedString
* TakeString(std::string
* to_destroy
);
98 // Overridden from RefCountedMemory:
99 virtual const unsigned char* front() const OVERRIDE
;
100 virtual size_t size() const OVERRIDE
;
102 const std::string
& data() const { return data_
; }
103 std::string
& data() { return data_
; }
106 virtual ~RefCountedString();
110 DISALLOW_COPY_AND_ASSIGN(RefCountedString
);
115 #endif // BASE_MEMORY_REF_COUNTED_MEMORY_H_