OLD | NEW |
---|---|
(Empty) | |
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 virtual ~RefCountedMemory() {} | |
21 | |
22 // Retrieves a pointer to the beginning of the data we point to. | |
23 virtual const unsigned char* front() const = 0; | |
24 | |
25 // Size of the memory pointed to. | |
26 virtual size_t size() const = 0; | |
27 }; | |
28 | |
29 // An implementation of RefCountedMemory, where the ref counting does not | |
30 // matter. | |
31 class RefCountedStaticMemory : public RefCountedMemory { | |
32 public: | |
33 RefCountedStaticMemory() | |
34 : data_(NULL), length_(0) {} | |
35 RefCountedStaticMemory(const unsigned char* data, size_t length) | |
36 : data_(data), length_(length) {} | |
37 | |
38 virtual const unsigned char* front() const { return data_; } | |
39 virtual size_t size() const { return length_; } | |
40 | |
41 private: | |
42 const unsigned char* data_; | |
43 size_t length_; | |
44 | |
45 DISALLOW_COPY_AND_ASSIGN(RefCountedStaticMemory); | |
46 }; | |
47 | |
48 // An implementation of RefCountedMemory, where we own our the data in a | |
49 // vector. | |
50 class RefCountedBytes : public RefCountedMemory { | |
51 public: | |
52 // Constructs a RefCountedBytes object by performing a swap. (To non | |
53 // destructively build a RefCountedBytes, use the constructor that takes a | |
54 // vector.) | |
55 static RefCountedBytes* TakeVector(std::vector<unsigned char>* to_destroy) { | |
56 RefCountedBytes* bytes = new RefCountedBytes; | |
57 bytes->data.swap(*to_destroy); | |
58 return bytes; | |
59 } | |
60 | |
61 RefCountedBytes() {} | |
62 | |
63 // Constructs a RefCountedBytes object by _copying_ from |initializer|. | |
64 RefCountedBytes(const std::vector<unsigned char>& initializer) | |
65 : data(initializer) {} | |
66 | |
67 virtual const unsigned char* front() const { return &data.front(); } | |
68 virtual size_t size() const { return data.size(); } | |
69 | |
70 std::vector<unsigned char> data; | |
71 | |
72 DISALLOW_COPY_AND_ASSIGN(RefCountedBytes); | |
brettw
2009/10/16 23:03:12
It's not your fault, but can you fix this? Without
| |
73 }; | |
74 | |
75 #endif // BASE_REF_COUNTED_MEMORY_H_ | |
OLD | NEW |