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;
30
// Returns true if |other| is byte for byte equal.
31
bool Equals(const scoped_refptr<RefCountedMemory>& other) const;
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());
39
friend class base::RefCountedThreadSafe<RefCountedMemory>;
41
virtual ~RefCountedMemory();
44
// An implementation of RefCountedMemory, where the ref counting does not
46
class BASE_EXPORT RefCountedStaticMemory : public RefCountedMemory {
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)),
54
// Overridden from RefCountedMemory:
55
virtual const unsigned char* front() const OVERRIDE;
56
virtual size_t size() const OVERRIDE;
59
virtual ~RefCountedStaticMemory();
61
const unsigned char* data_;
64
DISALLOW_COPY_AND_ASSIGN(RefCountedStaticMemory);
67
// An implementation of RefCountedMemory, where we own our the data in a
69
class BASE_EXPORT RefCountedBytes : public RefCountedMemory {
73
// Constructs a RefCountedBytes object by _copying_ from |initializer|.
74
explicit RefCountedBytes(const std::vector<unsigned char>& initializer);
76
// Constructs a RefCountedBytes object by performing a swap. (To non
77
// destructively build a RefCountedBytes, use the constructor that takes a
79
static RefCountedBytes* TakeVector(std::vector<unsigned char>* to_destroy);
81
// Overridden from RefCountedMemory:
82
virtual const unsigned char* front() const OVERRIDE;
83
virtual size_t size() const OVERRIDE;
85
const std::vector<unsigned char>& data() const { return data_; }
86
std::vector<unsigned char>& data() { return data_; }
89
virtual ~RefCountedBytes();
91
std::vector<unsigned char> data_;
93
DISALLOW_COPY_AND_ASSIGN(RefCountedBytes);
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 {
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);
107
// Overridden from RefCountedMemory:
108
virtual const unsigned char* front() const OVERRIDE;
109
virtual size_t size() const OVERRIDE;
111
const std::string& data() const { return data_; }
112
std::string& data() { return data_; }
115
virtual ~RefCountedString();
119
DISALLOW_COPY_AND_ASSIGN(RefCountedString);
122
// An implementation of RefCountedMemory that holds a chunk of memory
123
// previously allocated with malloc or calloc, and that therefore must be freed
125
class BASE_EXPORT RefCountedMallocedMemory : public base::RefCountedMemory {
127
RefCountedMallocedMemory(void* data, size_t length);
129
// Overridden from RefCountedMemory:
130
virtual const unsigned char* front() const OVERRIDE;
131
virtual size_t size() const OVERRIDE;
134
virtual ~RefCountedMallocedMemory();
136
unsigned char* data_;
139
DISALLOW_COPY_AND_ASSIGN(RefCountedMallocedMemory);
144
#endif // BASE_MEMORY_REF_COUNTED_MEMORY_H_