• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 #include "base/memory/ref_counted_memory.h"
6 
7 #include <utility>
8 
9 #include "base/logging.h"
10 #include "base/memory/read_only_shared_memory_region.h"
11 
12 namespace base {
13 
Equals(const scoped_refptr<RefCountedMemory> & other) const14 bool RefCountedMemory::Equals(
15     const scoped_refptr<RefCountedMemory>& other) const {
16   return other.get() &&
17          size() == other->size() &&
18          (memcmp(front(), other->front(), size()) == 0);
19 }
20 
21 RefCountedMemory::RefCountedMemory() = default;
22 
23 RefCountedMemory::~RefCountedMemory() = default;
24 
front() const25 const unsigned char* RefCountedStaticMemory::front() const {
26   return data_;
27 }
28 
size() const29 size_t RefCountedStaticMemory::size() const {
30   return length_;
31 }
32 
33 RefCountedStaticMemory::~RefCountedStaticMemory() = default;
34 
35 RefCountedBytes::RefCountedBytes() = default;
36 
RefCountedBytes(const std::vector<unsigned char> & initializer)37 RefCountedBytes::RefCountedBytes(const std::vector<unsigned char>& initializer)
38     : data_(initializer) {
39 }
40 
RefCountedBytes(const unsigned char * p,size_t size)41 RefCountedBytes::RefCountedBytes(const unsigned char* p, size_t size)
42     : data_(p, p + size) {}
43 
RefCountedBytes(size_t size)44 RefCountedBytes::RefCountedBytes(size_t size) : data_(size, 0) {}
45 
TakeVector(std::vector<unsigned char> * to_destroy)46 scoped_refptr<RefCountedBytes> RefCountedBytes::TakeVector(
47     std::vector<unsigned char>* to_destroy) {
48   auto bytes = MakeRefCounted<RefCountedBytes>();
49   bytes->data_.swap(*to_destroy);
50   return bytes;
51 }
52 
front() const53 const unsigned char* RefCountedBytes::front() const {
54   // STL will assert if we do front() on an empty vector, but calling code
55   // expects a NULL.
56   return size() ? &data_.front() : nullptr;
57 }
58 
size() const59 size_t RefCountedBytes::size() const {
60   return data_.size();
61 }
62 
63 RefCountedBytes::~RefCountedBytes() = default;
64 
65 RefCountedString::RefCountedString() = default;
66 
67 RefCountedString::~RefCountedString() = default;
68 
69 // static
TakeString(std::string * to_destroy)70 scoped_refptr<RefCountedString> RefCountedString::TakeString(
71     std::string* to_destroy) {
72   auto self = MakeRefCounted<RefCountedString>();
73   to_destroy->swap(self->data_);
74   return self;
75 }
76 
front() const77 const unsigned char* RefCountedString::front() const {
78   return data_.empty() ? nullptr
79                        : reinterpret_cast<const unsigned char*>(data_.data());
80 }
81 
size() const82 size_t RefCountedString::size() const {
83   return data_.size();
84 }
85 
RefCountedSharedMemory(std::unique_ptr<SharedMemory> shm,size_t size)86 RefCountedSharedMemory::RefCountedSharedMemory(
87     std::unique_ptr<SharedMemory> shm,
88     size_t size)
89     : shm_(std::move(shm)), size_(size) {
90   DCHECK(shm_);
91   DCHECK(shm_->memory());
92   DCHECK_GT(size_, 0U);
93   DCHECK_LE(size_, shm_->mapped_size());
94 }
95 
96 RefCountedSharedMemory::~RefCountedSharedMemory() = default;
97 
front() const98 const unsigned char* RefCountedSharedMemory::front() const {
99   return static_cast<const unsigned char*>(shm_->memory());
100 }
101 
size() const102 size_t RefCountedSharedMemory::size() const {
103   return size_;
104 }
105 
RefCountedSharedMemoryMapping(ReadOnlySharedMemoryMapping mapping)106 RefCountedSharedMemoryMapping::RefCountedSharedMemoryMapping(
107     ReadOnlySharedMemoryMapping mapping)
108     : mapping_(std::move(mapping)), size_(mapping_.size()) {
109   DCHECK_GT(size_, 0U);
110 }
111 
112 RefCountedSharedMemoryMapping::~RefCountedSharedMemoryMapping() = default;
113 
front() const114 const unsigned char* RefCountedSharedMemoryMapping::front() const {
115   return static_cast<const unsigned char*>(mapping_.memory());
116 }
117 
size() const118 size_t RefCountedSharedMemoryMapping::size() const {
119   return size_;
120 }
121 
122 // static
123 scoped_refptr<RefCountedSharedMemoryMapping>
CreateFromWholeRegion(const ReadOnlySharedMemoryRegion & region)124 RefCountedSharedMemoryMapping::CreateFromWholeRegion(
125     const ReadOnlySharedMemoryRegion& region) {
126   ReadOnlySharedMemoryMapping mapping = region.Map();
127   if (!mapping.IsValid())
128     return nullptr;
129   return MakeRefCounted<RefCountedSharedMemoryMapping>(std::move(mapping));
130 }
131 
132 }  //  namespace base
133