OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/ref_counted.h" | 10 #include "base/ref_counted.h" |
11 | 11 |
12 // TODO(erg): The contents of this file should be in a namespace. This would | 12 // TODO(erg): The contents of this file should be in a namespace. This would |
13 // require touching >100 files in chrome/ though. | 13 // require touching >100 files in chrome/ though. |
14 | 14 |
15 // A generic interface to memory. This object is reference counted because one | 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 | 16 // of its two subclasses own the data they carry, and we need to have |
17 // heterogeneous containers of these two types of memory. | 17 // heterogeneous containers of these two types of memory. |
18 class RefCountedMemory : public base::RefCountedThreadSafe< RefCountedMemory > { | 18 class RefCountedMemory : public base::RefCountedThreadSafe< RefCountedMemory > { |
19 public: | 19 public: |
20 virtual ~RefCountedMemory() {} | 20 virtual ~RefCountedMemory() {} |
21 | 21 |
22 // Retrieves a pointer to the beginning of the data we point to. | 22 // Retrieves a pointer to the beginning of the data we point to. If the data |
| 23 // is empty, this will return NULL. |
23 virtual const unsigned char* front() const = 0; | 24 virtual const unsigned char* front() const = 0; |
24 | 25 |
25 // Size of the memory pointed to. | 26 // Size of the memory pointed to. |
26 virtual size_t size() const = 0; | 27 virtual size_t size() const = 0; |
27 }; | 28 }; |
28 | 29 |
29 // An implementation of RefCountedMemory, where the ref counting does not | 30 // An implementation of RefCountedMemory, where the ref counting does not |
30 // matter. | 31 // matter. |
31 class RefCountedStaticMemory : public RefCountedMemory { | 32 class RefCountedStaticMemory : public RefCountedMemory { |
32 public: | 33 public: |
(...skipping 24 matching lines...) Expand all Loading... |
57 bytes->data.swap(*to_destroy); | 58 bytes->data.swap(*to_destroy); |
58 return bytes; | 59 return bytes; |
59 } | 60 } |
60 | 61 |
61 RefCountedBytes() {} | 62 RefCountedBytes() {} |
62 | 63 |
63 // Constructs a RefCountedBytes object by _copying_ from |initializer|. | 64 // Constructs a RefCountedBytes object by _copying_ from |initializer|. |
64 RefCountedBytes(const std::vector<unsigned char>& initializer) | 65 RefCountedBytes(const std::vector<unsigned char>& initializer) |
65 : data(initializer) {} | 66 : data(initializer) {} |
66 | 67 |
67 virtual const unsigned char* front() const { return &data.front(); } | 68 virtual const unsigned char* front() const { |
| 69 // STL will assert if we do front() on an empty vector, but calling code |
| 70 // expects a NULL. |
| 71 return size() ? &data.front() : NULL; |
| 72 } |
68 virtual size_t size() const { return data.size(); } | 73 virtual size_t size() const { return data.size(); } |
69 | 74 |
70 std::vector<unsigned char> data; | 75 std::vector<unsigned char> data; |
71 | 76 |
72 private: | 77 private: |
73 DISALLOW_COPY_AND_ASSIGN(RefCountedBytes); | 78 DISALLOW_COPY_AND_ASSIGN(RefCountedBytes); |
74 }; | 79 }; |
75 | 80 |
76 #endif // BASE_REF_COUNTED_MEMORY_H_ | 81 #endif // BASE_REF_COUNTED_MEMORY_H_ |
OLD | NEW |