ref_counted_memory.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright (c) 2012 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. #ifndef BASE_MEMORY_REF_COUNTED_MEMORY_H_
  5. #define BASE_MEMORY_REF_COUNTED_MEMORY_H_
  6. #include <stddef.h>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/base_export.h"
  11. #include "base/macros.h"
  12. #include "base/memory/ref_counted.h"
  13. #include "base/memory/shared_memory_mapping.h"
  14. namespace base {
  15. class ReadOnlySharedMemoryRegion;
  16. // A generic interface to memory. This object is reference counted because most
  17. // of its subclasses own the data they carry, and this interface needs to
  18. // support heterogeneous containers of these different types of memory.
  19. class BASE_EXPORT RefCountedMemory
  20. : public RefCountedThreadSafe<RefCountedMemory> {
  21. public:
  22. // Retrieves a pointer to the beginning of the data we point to. If the data
  23. // is empty, this will return NULL.
  24. virtual const unsigned char* front() const = 0;
  25. // Size of the memory pointed to.
  26. virtual size_t size() const = 0;
  27. // Returns true if |other| is byte for byte equal.
  28. bool Equals(const scoped_refptr<RefCountedMemory>& other) const;
  29. // Handy method to simplify calling front() with a reinterpret_cast.
  30. template<typename T> const T* front_as() const {
  31. return reinterpret_cast<const T*>(front());
  32. }
  33. // Alias for front() to make it possible for RefCountedMemory to implicitly
  34. // convert to span.
  35. const unsigned char* data() const { return front(); }
  36. protected:
  37. friend class RefCountedThreadSafe<RefCountedMemory>;
  38. RefCountedMemory();
  39. virtual ~RefCountedMemory();
  40. };
  41. // An implementation of RefCountedMemory, where the ref counting does not
  42. // matter.
  43. class BASE_EXPORT RefCountedStaticMemory : public RefCountedMemory {
  44. public:
  45. RefCountedStaticMemory() : data_(nullptr), length_(0) {}
  46. RefCountedStaticMemory(const void* data, size_t length)
  47. : data_(static_cast<const unsigned char*>(length ? data : nullptr)),
  48. length_(length) {}
  49. // RefCountedMemory:
  50. const unsigned char* front() const override;
  51. size_t size() const override;
  52. private:
  53. ~RefCountedStaticMemory() override;
  54. const unsigned char* data_;
  55. size_t length_;
  56. DISALLOW_COPY_AND_ASSIGN(RefCountedStaticMemory);
  57. };
  58. // An implementation of RefCountedMemory, where the data is stored in a STL
  59. // vector.
  60. class BASE_EXPORT RefCountedBytes : public RefCountedMemory {
  61. public:
  62. RefCountedBytes();
  63. // Constructs a RefCountedBytes object by copying from |initializer|.
  64. explicit RefCountedBytes(const std::vector<unsigned char>& initializer);
  65. // Constructs a RefCountedBytes object by copying |size| bytes from |p|.
  66. RefCountedBytes(const unsigned char* p, size_t size);
  67. // Constructs a RefCountedBytes object by zero-initializing a new vector of
  68. // |size| bytes.
  69. explicit RefCountedBytes(size_t size);
  70. // Constructs a RefCountedBytes object by performing a swap. (To non
  71. // destructively build a RefCountedBytes, use the constructor that takes a
  72. // vector.)
  73. static scoped_refptr<RefCountedBytes> TakeVector(
  74. std::vector<unsigned char>* to_destroy);
  75. // RefCountedMemory:
  76. const unsigned char* front() const override;
  77. size_t size() const override;
  78. const std::vector<unsigned char>& data() const { return data_; }
  79. std::vector<unsigned char>& data() { return data_; }
  80. // Non-const versions of front() and front_as() that are simply shorthand for
  81. // data().data().
  82. unsigned char* front() { return data_.data(); }
  83. template <typename T>
  84. T* front_as() {
  85. return reinterpret_cast<T*>(front());
  86. }
  87. private:
  88. ~RefCountedBytes() override;
  89. std::vector<unsigned char> data_;
  90. DISALLOW_COPY_AND_ASSIGN(RefCountedBytes);
  91. };
  92. // An implementation of RefCountedMemory, where the bytes are stored in a STL
  93. // string. Use this if your data naturally arrives in that format.
  94. class BASE_EXPORT RefCountedString : public RefCountedMemory {
  95. public:
  96. RefCountedString();
  97. // Constructs a RefCountedString object by performing a swap. (To non
  98. // destructively build a RefCountedString, use the default constructor and
  99. // copy into object->data()).
  100. static scoped_refptr<RefCountedString> TakeString(std::string* to_destroy);
  101. // RefCountedMemory:
  102. const unsigned char* front() const override;
  103. size_t size() const override;
  104. const std::string& data() const { return data_; }
  105. std::string& data() { return data_; }
  106. private:
  107. ~RefCountedString() override;
  108. std::string data_;
  109. DISALLOW_COPY_AND_ASSIGN(RefCountedString);
  110. };
  111. // An implementation of RefCountedMemory, where the bytes are stored in
  112. // ReadOnlySharedMemoryMapping.
  113. class BASE_EXPORT RefCountedSharedMemoryMapping : public RefCountedMemory {
  114. public:
  115. // Constructs a RefCountedMemory object by taking ownership of an already
  116. // mapped ReadOnlySharedMemoryMapping object.
  117. explicit RefCountedSharedMemoryMapping(ReadOnlySharedMemoryMapping mapping);
  118. // Convenience method to map all of |region| and take ownership of the
  119. // mapping. Returns an empty scoped_refptr if the map operation fails.
  120. static scoped_refptr<RefCountedSharedMemoryMapping> CreateFromWholeRegion(
  121. const ReadOnlySharedMemoryRegion& region);
  122. // RefCountedMemory:
  123. const unsigned char* front() const override;
  124. size_t size() const override;
  125. private:
  126. ~RefCountedSharedMemoryMapping() override;
  127. const ReadOnlySharedMemoryMapping mapping_;
  128. const size_t size_;
  129. DISALLOW_COPY_AND_ASSIGN(RefCountedSharedMemoryMapping);
  130. };
  131. } // namespace base
  132. #endif // BASE_MEMORY_REF_COUNTED_MEMORY_H_