13f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Use of this source code is governed by a BSD-style license that can be
3c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// found in the LICENSE file.
4c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
5ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#ifndef BASE_MEMORY_REF_COUNTED_MEMORY_H_
6ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#define BASE_MEMORY_REF_COUNTED_MEMORY_H_
73345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#pragma once
8c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
9c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <vector>
10c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
11ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/base_api.h"
12ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/memory/ref_counted.h"
13c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
14c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// TODO(erg): The contents of this file should be in a namespace. This would
15c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// require touching >100 files in chrome/ though.
16c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
17c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// A generic interface to memory. This object is reference counted because one
18c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// of its two subclasses own the data they carry, and we need to have
19c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// heterogeneous containers of these two types of memory.
20ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenclass BASE_API RefCountedMemory
21ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    : public base::RefCountedThreadSafe<RefCountedMemory> {
22c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public:
23c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Retrieves a pointer to the beginning of the data we point to. If the data
24c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // is empty, this will return NULL.
25c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  virtual const unsigned char* front() const = 0;
26c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
27c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Size of the memory pointed to.
28c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  virtual size_t size() const = 0;
29c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
30c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott protected:
31c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  friend class base::RefCountedThreadSafe<RefCountedMemory>;
323345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  RefCountedMemory();
333345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  virtual ~RefCountedMemory();
34c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott};
35c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
36c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// An implementation of RefCountedMemory, where the ref counting does not
37c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// matter.
38ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenclass BASE_API RefCountedStaticMemory : public RefCountedMemory {
39c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public:
40c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  RefCountedStaticMemory()
41c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      : data_(NULL), length_(0) {}
42c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  RefCountedStaticMemory(const unsigned char* data, size_t length)
43c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      : data_(data), length_(length) {}
44c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
453f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  // Overriden from RefCountedMemory:
463345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  virtual const unsigned char* front() const;
473345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  virtual size_t size() const;
48c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
49c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott private:
50c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  const unsigned char* data_;
51c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  size_t length_;
52c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
53c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  DISALLOW_COPY_AND_ASSIGN(RefCountedStaticMemory);
54c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott};
55c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
56c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// An implementation of RefCountedMemory, where we own our the data in a
57c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// vector.
58ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenclass BASE_API RefCountedBytes : public RefCountedMemory {
59c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public:
603345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  RefCountedBytes();
61c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
62c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Constructs a RefCountedBytes object by _copying_ from |initializer|.
633345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  RefCountedBytes(const std::vector<unsigned char>& initializer);
64c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
653f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  // Constructs a RefCountedBytes object by performing a swap. (To non
663f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  // destructively build a RefCountedBytes, use the constructor that takes a
673f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  // vector.)
683f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  static RefCountedBytes* TakeVector(std::vector<unsigned char>* to_destroy);
693f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen
703f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  // Overriden from RefCountedMemory:
713345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  virtual const unsigned char* front() const;
723345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  virtual size_t size() const;
73c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
74c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  std::vector<unsigned char> data;
75c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
763345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick protected:
773345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  friend class base::RefCountedThreadSafe<RefCountedBytes>;
783345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  virtual ~RefCountedBytes();
793345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
80c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott private:
81c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  DISALLOW_COPY_AND_ASSIGN(RefCountedBytes);
82c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott};
83c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
84ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#endif  // BASE_MEMORY_REF_COUNTED_MEMORY_H_
85