chromium/base/memory/ref_counted_memory.h

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_MEMORY_REF_COUNTED_MEMORY_H_
#define BASE_MEMORY_REF_COUNTED_MEMORY_H_

#include <stddef.h>

#include <memory>
#include <string>
#include <vector>

#include "base/base_export.h"
#include "base/compiler_specific.h"
#include "base/containers/span.h"
#include "base/memory/ref_counted.h"
#include "base/memory/shared_memory_mapping.h"

namespace base {

class ReadOnlySharedMemoryRegion;

// A generic interface to memory. This object is reference counted because most
// of its subclasses own the data they carry, and this interface needs to
// support heterogeneous containers of these different types of memory.
//
// The RefCountedMemory class provides a const view of the data it holds, as it
// does not require all subclassing implementations to hold mutable data. If a
// mutable view is required, the code must maintain awareness of the subclass
// type, and can access it through there, such as:
// - RefCountedBytes provides `as_vector()` to give mutable access to its data.
// - RefCountedString provides `as_string()` to give mutable access to its data.
class BASE_EXPORT RefCountedMemory
    : public RefCountedThreadSafe<RefCountedMemory> {};

// An implementation of RefCountedMemory, for pointing to memory with a static
// lifetime. Since the memory exists for the life of the program, the class can
// not and does not need to take ownership of it.
class BASE_EXPORT RefCountedStaticMemory : public RefCountedMemory {};

// An implementation of RefCountedMemory, where the data is stored in a STL
// vector.
class BASE_EXPORT RefCountedBytes : public RefCountedMemory {};

// An implementation of RefCountedMemory, where the bytes are stored in a STL
// string. Use this if your data naturally arrives in that format.
class BASE_EXPORT RefCountedString : public RefCountedMemory {};

// An implementation of RefCountedMemory, where the bytes are stored in a
// std::u16string.
class BASE_EXPORT RefCountedString16 : public base::RefCountedMemory {};

// An implementation of RefCountedMemory, where the bytes are stored in
// ReadOnlySharedMemoryMapping.
class BASE_EXPORT RefCountedSharedMemoryMapping : public RefCountedMemory {};

}  // namespace base

#endif  // BASE_MEMORY_REF_COUNTED_MEMORY_H_