Implement base::RefCountedSharedMemory.
This base::RefCountedMemory subclass owns a base::SharedMemory instance.
Use this class to easily access data in shared memory via the
base::RefCountedMemory interface without having to copy it out to a
std::string or std::vector.
Use this in printing code to avoid a bunch of copying.
Change-Id: I275eacb3af0250d352ec909429d8563b28b1b57e
Reviewed-on: https://chromium-review.googlesource.com/978641
Commit-Queue: Lei Zhang <[email protected]>
Reviewed-by: Wei Li <[email protected]>
Reviewed-by: Rebekah Potter <[email protected]>
Reviewed-by: Reilly Grant <[email protected]>
Cr-Commit-Position: refs/heads/master@{#547048}
diff --git a/base/memory/ref_counted_memory.cc b/base/memory/ref_counted_memory.cc
index be981e32..7999d90 100644
--- a/base/memory/ref_counted_memory.cc
+++ b/base/memory/ref_counted_memory.cc
@@ -4,6 +4,8 @@
#include "base/memory/ref_counted_memory.h"
+#include <utility>
+
#include "base/logging.h"
namespace base {
@@ -80,4 +82,24 @@
return data_.size();
}
+RefCountedSharedMemory::RefCountedSharedMemory(
+ std::unique_ptr<SharedMemory> shm,
+ size_t size)
+ : shm_(std::move(shm)), size_(size) {
+ DCHECK(shm_);
+ DCHECK(shm_->memory());
+ DCHECK_GT(size_, 0U);
+ DCHECK_LE(size_, shm_->mapped_size());
+}
+
+RefCountedSharedMemory::~RefCountedSharedMemory() = default;
+
+const unsigned char* RefCountedSharedMemory::front() const {
+ return reinterpret_cast<const unsigned char*>(shm_->memory());
+}
+
+size_t RefCountedSharedMemory::size() const {
+ return size_;
+}
+
} // namespace base