• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2009 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_REF_COUNTED_MEMORY_H_
6 #define BASE_REF_COUNTED_MEMORY_H_
7 
8 #include <vector>
9 
10 #include "base/ref_counted.h"
11 
12 // TODO(erg): The contents of this file should be in a namespace. This would
13 // require touching >100 files in chrome/ though.
14 
15 // A generic interface to memory. This object is reference counted because one
16 // of its two subclasses own the data they carry, and we need to have
17 // heterogeneous containers of these two types of memory.
18 class RefCountedMemory : public base::RefCountedThreadSafe<RefCountedMemory> {
19  public:
20   // Retrieves a pointer to the beginning of the data we point to. If the data
21   // is empty, this will return NULL.
22   virtual const unsigned char* front() const = 0;
23 
24   // Size of the memory pointed to.
25   virtual size_t size() const = 0;
26 
27  protected:
28   friend class base::RefCountedThreadSafe<RefCountedMemory>;
29 
~RefCountedMemory()30   virtual ~RefCountedMemory() {}
31 };
32 
33 // An implementation of RefCountedMemory, where the ref counting does not
34 // matter.
35 class RefCountedStaticMemory : public RefCountedMemory {
36  public:
RefCountedStaticMemory()37   RefCountedStaticMemory()
38       : data_(NULL), length_(0) {}
RefCountedStaticMemory(const unsigned char * data,size_t length)39   RefCountedStaticMemory(const unsigned char* data, size_t length)
40       : data_(data), length_(length) {}
41 
front()42   virtual const unsigned char* front() const { return data_; }
size()43   virtual size_t size() const { return length_; }
44 
45  private:
46   const unsigned char* data_;
47   size_t length_;
48 
49   DISALLOW_COPY_AND_ASSIGN(RefCountedStaticMemory);
50 };
51 
52 // An implementation of RefCountedMemory, where we own our the data in a
53 // vector.
54 class RefCountedBytes : public RefCountedMemory {
55  public:
56   // Constructs a RefCountedBytes object by performing a swap. (To non
57   // destructively build a RefCountedBytes, use the constructor that takes a
58   // vector.)
TakeVector(std::vector<unsigned char> * to_destroy)59   static RefCountedBytes* TakeVector(std::vector<unsigned char>* to_destroy) {
60     RefCountedBytes* bytes = new RefCountedBytes;
61     bytes->data.swap(*to_destroy);
62     return bytes;
63   }
64 
RefCountedBytes()65   RefCountedBytes() {}
66 
67   // Constructs a RefCountedBytes object by _copying_ from |initializer|.
RefCountedBytes(const std::vector<unsigned char> & initializer)68   RefCountedBytes(const std::vector<unsigned char>& initializer)
69       : data(initializer) {}
70 
front()71   virtual const unsigned char* front() const {
72     // STL will assert if we do front() on an empty vector, but calling code
73     // expects a NULL.
74     return size() ? &data.front() : NULL;
75   }
size()76   virtual size_t size() const { return data.size(); }
77 
78   std::vector<unsigned char> data;
79 
80  private:
81   DISALLOW_COPY_AND_ASSIGN(RefCountedBytes);
82 };
83 
84 #endif  // BASE_REF_COUNTED_MEMORY_H_
85