• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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.
4 
5 #ifndef BASE_MEMORY_REF_COUNTED_MEMORY_H_
6 #define BASE_MEMORY_REF_COUNTED_MEMORY_H_
7 #pragma once
8 
9 #include <vector>
10 
11 #include "base/base_api.h"
12 #include "base/memory/ref_counted.h"
13 
14 // TODO(erg): The contents of this file should be in a namespace. This would
15 // require touching >100 files in chrome/ though.
16 
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_API 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;
26 
27   // Size of the memory pointed to.
28   virtual size_t size() const = 0;
29 
30  protected:
31   friend class base::RefCountedThreadSafe<RefCountedMemory>;
32   RefCountedMemory();
33   virtual ~RefCountedMemory();
34 };
35 
36 // An implementation of RefCountedMemory, where the ref counting does not
37 // matter.
38 class BASE_API RefCountedStaticMemory : public RefCountedMemory {
39  public:
RefCountedStaticMemory()40   RefCountedStaticMemory()
41       : data_(NULL), length_(0) {}
RefCountedStaticMemory(const unsigned char * data,size_t length)42   RefCountedStaticMemory(const unsigned char* data, size_t length)
43       : data_(data), length_(length) {}
44 
45   // Overriden from RefCountedMemory:
46   virtual const unsigned char* front() const;
47   virtual size_t size() const;
48 
49  private:
50   const unsigned char* data_;
51   size_t length_;
52 
53   DISALLOW_COPY_AND_ASSIGN(RefCountedStaticMemory);
54 };
55 
56 // An implementation of RefCountedMemory, where we own our the data in a
57 // vector.
58 class BASE_API RefCountedBytes : public RefCountedMemory {
59  public:
60   RefCountedBytes();
61 
62   // Constructs a RefCountedBytes object by _copying_ from |initializer|.
63   RefCountedBytes(const std::vector<unsigned char>& initializer);
64 
65   // Constructs a RefCountedBytes object by performing a swap. (To non
66   // destructively build a RefCountedBytes, use the constructor that takes a
67   // vector.)
68   static RefCountedBytes* TakeVector(std::vector<unsigned char>* to_destroy);
69 
70   // Overriden from RefCountedMemory:
71   virtual const unsigned char* front() const;
72   virtual size_t size() const;
73 
74   std::vector<unsigned char> data;
75 
76  protected:
77   friend class base::RefCountedThreadSafe<RefCountedBytes>;
78   virtual ~RefCountedBytes();
79 
80  private:
81   DISALLOW_COPY_AND_ASSIGN(RefCountedBytes);
82 };
83 
84 #endif  // BASE_MEMORY_REF_COUNTED_MEMORY_H_
85