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