Home | History | Annotate | Download | only in memory
      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 
     14 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 
     25 const unsigned char* RefCountedStaticMemory::front() const {
     26   return data_;
     27 }
     28 
     29 size_t RefCountedStaticMemory::size() const {
     30   return length_;
     31 }
     32 
     33 RefCountedStaticMemory::~RefCountedStaticMemory() = default;
     34 
     35 RefCountedBytes::RefCountedBytes() = default;
     36 
     37 RefCountedBytes::RefCountedBytes(const std::vector<unsigned char>& initializer)
     38     : data_(initializer) {
     39 }
     40 
     41 RefCountedBytes::RefCountedBytes(const unsigned char* p, size_t size)
     42     : data_(p, p + size) {}
     43 
     44 RefCountedBytes::RefCountedBytes(size_t size) : data_(size, 0) {}
     45 
     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 
     53 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 
     59 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
     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 
     77 const unsigned char* RefCountedString::front() const {
     78   return data_.empty() ? nullptr
     79                        : reinterpret_cast<const unsigned char*>(data_.data());
     80 }
     81 
     82 size_t RefCountedString::size() const {
     83   return data_.size();
     84 }
     85 
     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 
     98 const unsigned char* RefCountedSharedMemory::front() const {
     99   return static_cast<const unsigned char*>(shm_->memory());
    100 }
    101 
    102 size_t RefCountedSharedMemory::size() const {
    103   return size_;
    104 }
    105 
    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 
    114 const unsigned char* RefCountedSharedMemoryMapping::front() const {
    115   return static_cast<const unsigned char*>(mapping_.memory());
    116 }
    117 
    118 size_t RefCountedSharedMemoryMapping::size() const {
    119   return size_;
    120 }
    121 
    122 // static
    123 scoped_refptr<RefCountedSharedMemoryMapping>
    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