OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_REF_COUNTED_MEMORY_H_ | 5 #ifndef BASE_REF_COUNTED_MEMORY_H_ |
6 #define BASE_REF_COUNTED_MEMORY_H_ | 6 #define BASE_REF_COUNTED_MEMORY_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/ref_counted.h" | 11 #include "base/ref_counted.h" |
(...skipping 21 matching lines...) Expand all Loading... |
33 | 33 |
34 // An implementation of RefCountedMemory, where the ref counting does not | 34 // An implementation of RefCountedMemory, where the ref counting does not |
35 // matter. | 35 // matter. |
36 class RefCountedStaticMemory : public RefCountedMemory { | 36 class RefCountedStaticMemory : public RefCountedMemory { |
37 public: | 37 public: |
38 RefCountedStaticMemory() | 38 RefCountedStaticMemory() |
39 : data_(NULL), length_(0) {} | 39 : data_(NULL), length_(0) {} |
40 RefCountedStaticMemory(const unsigned char* data, size_t length) | 40 RefCountedStaticMemory(const unsigned char* data, size_t length) |
41 : data_(data), length_(length) {} | 41 : data_(data), length_(length) {} |
42 | 42 |
43 virtual const unsigned char* front() const { return data_; } | 43 virtual const unsigned char* front() const; |
44 virtual size_t size() const { return length_; } | 44 virtual size_t size() const; |
45 | 45 |
46 private: | 46 private: |
47 const unsigned char* data_; | 47 const unsigned char* data_; |
48 size_t length_; | 48 size_t length_; |
49 | 49 |
50 DISALLOW_COPY_AND_ASSIGN(RefCountedStaticMemory); | 50 DISALLOW_COPY_AND_ASSIGN(RefCountedStaticMemory); |
51 }; | 51 }; |
52 | 52 |
53 // An implementation of RefCountedMemory, where we own our the data in a | 53 // An implementation of RefCountedMemory, where we own our the data in a |
54 // vector. | 54 // vector. |
55 class RefCountedBytes : public RefCountedMemory { | 55 class RefCountedBytes : public RefCountedMemory { |
56 public: | 56 public: |
57 // Constructs a RefCountedBytes object by performing a swap. (To non | 57 // Constructs a RefCountedBytes object by performing a swap. (To non |
58 // destructively build a RefCountedBytes, use the constructor that takes a | 58 // destructively build a RefCountedBytes, use the constructor that takes a |
59 // vector.) | 59 // vector.) |
60 static RefCountedBytes* TakeVector(std::vector<unsigned char>* to_destroy) { | 60 static RefCountedBytes* TakeVector(std::vector<unsigned char>* to_destroy); |
61 RefCountedBytes* bytes = new RefCountedBytes; | |
62 bytes->data.swap(*to_destroy); | |
63 return bytes; | |
64 } | |
65 | 61 |
66 RefCountedBytes() {} | 62 RefCountedBytes(); |
67 | 63 |
68 // Constructs a RefCountedBytes object by _copying_ from |initializer|. | 64 // Constructs a RefCountedBytes object by _copying_ from |initializer|. |
69 RefCountedBytes(const std::vector<unsigned char>& initializer) | 65 RefCountedBytes(const std::vector<unsigned char>& initializer); |
70 : data(initializer) {} | |
71 | 66 |
72 virtual const unsigned char* front() const { | 67 virtual const unsigned char* front() const; |
73 // STL will assert if we do front() on an empty vector, but calling code | 68 virtual size_t size() const; |
74 // expects a NULL. | |
75 return size() ? &data.front() : NULL; | |
76 } | |
77 virtual size_t size() const { return data.size(); } | |
78 | 69 |
79 std::vector<unsigned char> data; | 70 std::vector<unsigned char> data; |
80 | 71 |
81 private: | 72 private: |
82 DISALLOW_COPY_AND_ASSIGN(RefCountedBytes); | 73 DISALLOW_COPY_AND_ASSIGN(RefCountedBytes); |
83 }; | 74 }; |
84 | 75 |
85 #endif // BASE_REF_COUNTED_MEMORY_H_ | 76 #endif // BASE_REF_COUNTED_MEMORY_H_ |
OLD | NEW |