blob: f37a86011af9f92063200b89e6105b6528f30c18 [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
avi9beac252015-12-24 08:44:478#include <stddef.h>
9
[email protected]1dda9772011-07-22 13:22:2310#include <string>
[email protected]6de27992009-10-19 17:04:4611#include <vector>
12
[email protected]0bea7252011-08-05 15:34:0013#include "base/base_export.h"
[email protected]1dda9772011-07-22 13:22:2314#include "base/compiler_specific.h"
avi9beac252015-12-24 08:44:4715#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1516#include "base/memory/ref_counted.h"
[email protected]6de27992009-10-19 17:04:4617
[email protected]68c7630b2012-05-02 22:37:4218namespace base {
[email protected]6de27992009-10-19 17:04:4619
20// A generic interface to memory. This object is reference counted because one
21// of its two subclasses own the data they carry, and we need to have
22// heterogeneous containers of these two types of memory.
[email protected]0bea7252011-08-05 15:34:0023class BASE_EXPORT RefCountedMemory
[email protected]9493ee95c2011-03-28 23:48:4424 : public base::RefCountedThreadSafe<RefCountedMemory> {
[email protected]6de27992009-10-19 17:04:4625 public:
[email protected]1e7fafc42009-10-27 16:24:1326 // Retrieves a pointer to the beginning of the data we point to. If the data
27 // is empty, this will return NULL.
[email protected]6de27992009-10-19 17:04:4628 virtual const unsigned char* front() const = 0;
29
30 // Size of the memory pointed to.
31 virtual size_t size() const = 0;
[email protected]877d55d2009-11-05 21:53:0832
[email protected]556531622013-01-14 18:59:5533 // Returns true if |other| is byte for byte equal.
34 bool Equals(const scoped_refptr<RefCountedMemory>& other) const;
35
[email protected]8df08cf2014-02-12 22:34:0836 // Handy method to simplify calling front() with a reinterpret_cast.
37 template<typename T> const T* front_as() const {
38 return reinterpret_cast<const T*>(front());
39 }
40
[email protected]877d55d2009-11-05 21:53:0841 protected:
42 friend class base::RefCountedThreadSafe<RefCountedMemory>;
[email protected]d4799a32010-09-28 22:54:5843 RefCountedMemory();
44 virtual ~RefCountedMemory();
[email protected]6de27992009-10-19 17:04:4645};
46
47// An implementation of RefCountedMemory, where the ref counting does not
48// matter.
[email protected]0bea7252011-08-05 15:34:0049class BASE_EXPORT RefCountedStaticMemory : public RefCountedMemory {
[email protected]6de27992009-10-19 17:04:4650 public:
51 RefCountedStaticMemory()
52 : data_(NULL), length_(0) {}
[email protected]8df08cf2014-02-12 22:34:0853 RefCountedStaticMemory(const void* data, size_t length)
54 : data_(static_cast<const unsigned char*>(length ? data : NULL)),
55 length_(length) {}
[email protected]6de27992009-10-19 17:04:4656
[email protected]1dda9772011-07-22 13:22:2357 // Overridden from RefCountedMemory:
dcheng56488182014-10-21 10:54:5158 const unsigned char* front() const override;
59 size_t size() const override;
[email protected]6de27992009-10-19 17:04:4660
61 private:
dcheng56488182014-10-21 10:54:5162 ~RefCountedStaticMemory() override;
[email protected]a9aaa9d12012-04-25 00:42:5163
[email protected]6de27992009-10-19 17:04:4664 const unsigned char* data_;
65 size_t length_;
66
67 DISALLOW_COPY_AND_ASSIGN(RefCountedStaticMemory);
68};
69
[email protected]6a497d72014-04-30 20:30:1870// An implementation of RefCountedMemory, where we own the data in a vector.
[email protected]0bea7252011-08-05 15:34:0071class BASE_EXPORT RefCountedBytes : public RefCountedMemory {
[email protected]6de27992009-10-19 17:04:4672 public:
[email protected]3071754432010-07-28 00:09:5473 RefCountedBytes();
[email protected]6de27992009-10-19 17:04:4674
75 // Constructs a RefCountedBytes object by _copying_ from |initializer|.
[email protected]f3c697c52013-01-15 10:52:1176 explicit RefCountedBytes(const std::vector<unsigned char>& initializer);
[email protected]6de27992009-10-19 17:04:4677
[email protected]6a497d72014-04-30 20:30:1878 // Constructs a RefCountedBytes object by copying |size| bytes from |p|.
79 RefCountedBytes(const unsigned char* p, size_t size);
80
[email protected]a502bbe72011-01-07 18:06:4581 // Constructs a RefCountedBytes object by performing a swap. (To non
82 // destructively build a RefCountedBytes, use the constructor that takes a
83 // vector.)
84 static RefCountedBytes* TakeVector(std::vector<unsigned char>* to_destroy);
85
[email protected]1dda9772011-07-22 13:22:2386 // Overridden from RefCountedMemory:
dcheng56488182014-10-21 10:54:5187 const unsigned char* front() const override;
88 size_t size() const override;
[email protected]6de27992009-10-19 17:04:4689
[email protected]1dda9772011-07-22 13:22:2390 const std::vector<unsigned char>& data() const { return data_; }
91 std::vector<unsigned char>& data() { return data_; }
[email protected]6de27992009-10-19 17:04:4692
[email protected]1dda9772011-07-22 13:22:2393 private:
dcheng56488182014-10-21 10:54:5194 ~RefCountedBytes() override;
[email protected]0484d7b2010-09-16 22:07:2695
[email protected]1dda9772011-07-22 13:22:2396 std::vector<unsigned char> data_;
97
[email protected]6de27992009-10-19 17:04:4698 DISALLOW_COPY_AND_ASSIGN(RefCountedBytes);
99};
100
[email protected]1dda9772011-07-22 13:22:23101// An implementation of RefCountedMemory, where the bytes are stored in an STL
102// string. Use this if your data naturally arrives in that format.
[email protected]0bea7252011-08-05 15:34:00103class BASE_EXPORT RefCountedString : public RefCountedMemory {
[email protected]1dda9772011-07-22 13:22:23104 public:
105 RefCountedString();
106
107 // Constructs a RefCountedString object by performing a swap. (To non
108 // destructively build a RefCountedString, use the default constructor and
109 // copy into object->data()).
estadeb6178e62016-01-07 17:19:39110 static scoped_refptr<RefCountedString> TakeString(std::string* to_destroy);
[email protected]1dda9772011-07-22 13:22:23111
112 // Overridden from RefCountedMemory:
dcheng56488182014-10-21 10:54:51113 const unsigned char* front() const override;
114 size_t size() const override;
[email protected]1dda9772011-07-22 13:22:23115
116 const std::string& data() const { return data_; }
117 std::string& data() { return data_; }
118
119 private:
dcheng56488182014-10-21 10:54:51120 ~RefCountedString() override;
[email protected]1dda9772011-07-22 13:22:23121
122 std::string data_;
123
124 DISALLOW_COPY_AND_ASSIGN(RefCountedString);
125};
126
127} // namespace base
128
[email protected]3b63f8f42011-03-28 01:54:15129#endif // BASE_MEMORY_REF_COUNTED_MEMORY_H_