chromium/v8/src/base/pointer-with-payload.h

// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_BASE_POINTER_WITH_PAYLOAD_H_
#define V8_BASE_POINTER_WITH_PAYLOAD_H_

#include <cstdint>
#include <type_traits>

#include "src/base/logging.h"

namespace v8 {
namespace base {

template <typename PointerType>
struct PointerWithPayloadTraits {};

// Assume void* has the same payloads as void**, under the assumption that it's
// used for classes that contain at least one pointer.
template <>
struct PointerWithPayloadTraits<void> : public PointerWithPayloadTraits<void*> {};

// PointerWithPayload combines a PointerType* an a small PayloadType into
// one. The bits of the storage type get packed into the lower bits of the
// pointer that are free due to alignment. The user needs to specify how many
// bits are needed to store the PayloadType, allowing Types that by default are
// larger to be stored.
//
// Example:
//   PointerWithPayload<int *, bool, 1> data_and_flag;
//
//   Here we store a bool that needs 1 bit of storage state into the lower bits
//   of int *, which points to some int data;
template <typename PointerType, typename PayloadType, int NumPayloadBits>
class PointerWithPayload {};

template <typename PointerType, typename PayloadType, int NumPayloadBits>
bool operator==(
    PointerWithPayload<PointerType, PayloadType, NumPayloadBits> lhs,
    PointerWithPayload<PointerType, PayloadType, NumPayloadBits> rhs) {}

}  // namespace base
}  // namespace v8

#endif  // V8_BASE_POINTER_WITH_PAYLOAD_H_