1 //===- llvm/ADT/PointerIntPair.h - Pair for pointer and int -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the PointerIntPair class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_POINTERINTPAIR_H
15 #define LLVM_ADT_POINTERINTPAIR_H
21 /// PointerIntPair - This class implements a pair of a pointer and small
22 /// integer. It is designed to represet this in the space required by one
23 /// pointer by bitmangling the integer into the low part of the pointer. This
24 /// can only be done for small integers: typically up to 3 bits, but it depends
25 /// on the alignment returned by the allocator in use.
27 template <typename PointerTy, unsigned IntBits, typename IntType=unsigned>
28 class PointerIntPair {
31 PointerIntPair() : Value(0) {}
32 PointerIntPair(PointerTy Ptr, IntType Int) : Value(0) {
37 PointerTy getPointer() const {
38 return reinterpret_cast<PointerTy>(Value & ~((1 << IntBits)-1));
41 IntType getInt() const {
42 return (IntType)(Value & (1 << IntBits)-1);
45 void setPointer(PointerTy Ptr) {
46 intptr_t PtrVal = reinterpret_cast<intptr_t>(Ptr);
47 assert((PtrVal & (1 << IntBits)-1) == 0 &&
48 "Pointer is no sufficiently aligned");
49 Value = PtrVal | (intptr_t)getInt();
52 void setInt(IntType Int) {
53 assert(Int < (1 << IntBits) && "Integer too large for field");
54 Value |= reinterpret_cast<intptr_t>(getPointer()) | (intptr_t)Int;
57 void *getOpaqueValue() const { return reinterpret_cast<void*>(Value); }
58 void setFromOpaqueValue(void *Val) { Value = reinterpret_cast<intptr_t>(Val);}
60 bool operator==(const PointerIntPair &RHS) const {
61 return Value == RHS.Value;
63 bool operator!=(const PointerIntPair &RHS) const {
64 return Value != RHS.Value;
68 } // end namespace llvm