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