chromium/v8/src/zone/zone-compact-set.h

// Copyright 2016 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_ZONE_ZONE_COMPACT_SET_H_
#define V8_ZONE_ZONE_COMPACT_SET_H_

#include <algorithm>
#include <initializer_list>
#include <type_traits>

#include "src/base/compiler-specific.h"
#include "src/base/pointer-with-payload.h"
#include "src/common/assert-scope.h"
#include "src/handles/handles.h"
#include "src/zone/zone-containers.h"
#include "src/zone/zone.h"

namespace v8 {
namespace internal {

template <typename T, typename Enable = void>
struct ZoneCompactSetTraits;

template <typename T>
struct ZoneCompactSetTraits<Handle<T>> {
  using handle_type = Handle<T>;
  using data_type = Address;

  static data_type* HandleToPointer(handle_type handle) {
    // Use address() instead of location() to get around handle access checks
    // (we're not actually dereferencing the handle so it's safe to read its
    // location)
    return reinterpret_cast<Address*>(handle.address());
  }
  static handle_type PointerToHandle(data_type* ptr) {
    return handle_type(ptr);
  }
};

// A Zone-allocated set which has a compact encoding of zero and one values.
// Two or more values will be stored as a sorted list, which is copied on write
// to keep the ZoneCompactSet copy constructor trivial. Note that this means
// that insertions past the first value will trigger an allocation and copy of
// the existing elements -- ZoneCompactSet should be preferred for cases where
// we mostly have only zero or one values.
//
// T must be a Handle-like type with a specialization of ZoneCompactSetTraits.
// In particular, it must be a trivial wrapper of a pointer to actual data --
// ZoneCompactSet will store this pointer rather than the T type.
template <typename T>
class ZoneCompactSet final {};

template <typename T>
std::ostream& operator<<(std::ostream& os, ZoneCompactSet<T> set) {}

template <typename T>
class ZoneCompactSet<T>::const_iterator {};

template <typename T>
typename ZoneCompactSet<T>::const_iterator ZoneCompactSet<T>::begin() const {}

template <typename T>
typename ZoneCompactSet<T>::const_iterator ZoneCompactSet<T>::end() const {}

ZoneHandleSet;

}  // namespace internal
}  // namespace v8

#endif  // V8_ZONE_ZONE_COMPACT_SET_H_