blob: b99871bb124c92637d60a6f71aa2d7dc044e3b56 [file] [log] [blame]
[email protected]ebbccb952012-04-20 09:51:311// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]6de27992009-10-19 17:04:462// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]3b63f8f42011-03-28 01:54:155#ifndef BASE_MEMORY_REF_COUNTED_MEMORY_H_
6#define BASE_MEMORY_REF_COUNTED_MEMORY_H_
[email protected]6de27992009-10-19 17:04:467
[email protected]1dda9772011-07-22 13:22:238#include <string>
[email protected]6de27992009-10-19 17:04:469#include <vector>
10
[email protected]0bea7252011-08-05 15:34:0011#include "base/base_export.h"
[email protected]1dda9772011-07-22 13:22:2312#include "base/compiler_specific.h"
[email protected]3b63f8f42011-03-28 01:54:1513#include "base/memory/ref_counted.h"
[email protected]6de27992009-10-19 17:04:4614
[email protected]68c7630b2012-05-02 22:37:4215namespace base {
[email protected]6de27992009-10-19 17:04:4616
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.
[email protected]0bea7252011-08-05 15:34:0020class BASE_EXPORT RefCountedMemory
[email protected]9493ee95c2011-03-28 23:48:4421 : public base::RefCountedThreadSafe<RefCountedMemory> {
[email protected]6de27992009-10-19 17:04:4622 public:
[email protected]1e7fafc42009-10-27 16:24:1323 // Retrieves a pointer to the beginning of the data we point to. If the data
24 // is empty, this will return NULL.
[email protected]6de27992009-10-19 17:04:4625 virtual const unsigned char* front() const = 0;
26
27 // Size of the memory pointed to.
28 virtual size_t size() const = 0;
[email protected]877d55d2009-11-05 21:53:0829
[email protected]556531622013-01-14 18:59:5530 // Returns true if |other| is byte for byte equal.
31 bool Equals(const scoped_refptr<RefCountedMemory>& other) const;
32
[email protected]877d55d2009-11-05 21:53:0833 protected:
34 friend class base::RefCountedThreadSafe<RefCountedMemory>;
[email protected]d4799a32010-09-28 22:54:5835 RefCountedMemory();
36 virtual ~RefCountedMemory();
[email protected]6de27992009-10-19 17:04:4637};
38
39// An implementation of RefCountedMemory, where the ref counting does not
40// matter.
[email protected]0bea7252011-08-05 15:34:0041class BASE_EXPORT RefCountedStaticMemory : public RefCountedMemory {
[email protected]6de27992009-10-19 17:04:4642 public:
43 RefCountedStaticMemory()
44 : data_(NULL), length_(0) {}
45 RefCountedStaticMemory(const unsigned char* data, size_t length)
[email protected]1dda9772011-07-22 13:22:2346 : data_(length ? data : NULL), length_(length) {}
[email protected]6de27992009-10-19 17:04:4647
[email protected]1dda9772011-07-22 13:22:2348 // Overridden from RefCountedMemory:
49 virtual const unsigned char* front() const OVERRIDE;
50 virtual size_t size() const OVERRIDE;
[email protected]6de27992009-10-19 17:04:4651
52 private:
[email protected]a9aaa9d12012-04-25 00:42:5153 virtual ~RefCountedStaticMemory();
54
[email protected]6de27992009-10-19 17:04:4655 const unsigned char* data_;
56 size_t length_;
57
58 DISALLOW_COPY_AND_ASSIGN(RefCountedStaticMemory);
59};
60
61// An implementation of RefCountedMemory, where we own our the data in a
62// vector.
[email protected]0bea7252011-08-05 15:34:0063class BASE_EXPORT RefCountedBytes : public RefCountedMemory {
[email protected]6de27992009-10-19 17:04:4664 public:
[email protected]3071754432010-07-28 00:09:5465 RefCountedBytes();
[email protected]6de27992009-10-19 17:04:4666
67 // Constructs a RefCountedBytes object by _copying_ from |initializer|.
[email protected]3071754432010-07-28 00:09:5468 RefCountedBytes(const std::vector<unsigned char>& initializer);
[email protected]6de27992009-10-19 17:04:4669
[email protected]a502bbe72011-01-07 18:06:4570 // 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);
74
[email protected]1dda9772011-07-22 13:22:2375 // Overridden from RefCountedMemory:
76 virtual const unsigned char* front() const OVERRIDE;
77 virtual size_t size() const OVERRIDE;
[email protected]6de27992009-10-19 17:04:4678
[email protected]1dda9772011-07-22 13:22:2379 const std::vector<unsigned char>& data() const { return data_; }
80 std::vector<unsigned char>& data() { return data_; }
[email protected]6de27992009-10-19 17:04:4681
[email protected]1dda9772011-07-22 13:22:2382 private:
[email protected]0484d7b2010-09-16 22:07:2683 virtual ~RefCountedBytes();
84
[email protected]1dda9772011-07-22 13:22:2385 std::vector<unsigned char> data_;
86
[email protected]6de27992009-10-19 17:04:4687 DISALLOW_COPY_AND_ASSIGN(RefCountedBytes);
88};
89
[email protected]1dda9772011-07-22 13:22:2390// An implementation of RefCountedMemory, where the bytes are stored in an STL
91// string. Use this if your data naturally arrives in that format.
[email protected]0bea7252011-08-05 15:34:0092class BASE_EXPORT RefCountedString : public RefCountedMemory {
[email protected]1dda9772011-07-22 13:22:2393 public:
94 RefCountedString();
95
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);
100
101 // Overridden from RefCountedMemory:
102 virtual const unsigned char* front() const OVERRIDE;
103 virtual size_t size() const OVERRIDE;
104
105 const std::string& data() const { return data_; }
106 std::string& data() { return data_; }
107
108 private:
[email protected]1dda9772011-07-22 13:22:23109 virtual ~RefCountedString();
110
111 std::string data_;
112
113 DISALLOW_COPY_AND_ASSIGN(RefCountedString);
114};
115
116} // namespace base
117
[email protected]3b63f8f42011-03-28 01:54:15118#endif // BASE_MEMORY_REF_COUNTED_MEMORY_H_