fix comment typo
[oota-llvm.git] / include / llvm / ADT / PointerIntPair.h
1 //===- llvm/ADT/PointerIntPair.h - Pair for pointer and int -----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the PointerIntPair class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_ADT_POINTERINTPAIR_H
15 #define LLVM_ADT_POINTERINTPAIR_H
16
17 #include <cassert>
18
19 namespace llvm {
20   
21 /// PointerIntPair - This class implements a pair of a pointer and small
22 /// integer.  It is designed to represent 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.
26 ///
27 template <typename PointerTy, unsigned IntBits, typename IntType=unsigned>
28 class PointerIntPair {
29   intptr_t Value;
30 public:
31   PointerIntPair() : Value(0) {}
32   PointerIntPair(PointerTy Ptr, IntType Int) : Value(0) {
33     setPointer(Ptr);
34     setInt(Int);
35   }
36
37   PointerTy getPointer() const {
38     return reinterpret_cast<PointerTy>(Value & ~((1 << IntBits)-1));
39   }
40   
41   IntType getInt() const {
42     return (IntType)(Value & (1 << IntBits)-1);
43   }
44   
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();
50   }
51   
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;
55   }
56   
57   void *getOpaqueValue() const { return reinterpret_cast<void*>(Value); }
58   void setFromOpaqueValue(void *Val) { Value = reinterpret_cast<intptr_t>(Val);}
59   
60   bool operator==(const PointerIntPair &RHS) const {
61     return Value == RHS.Value;
62   }
63   bool operator!=(const PointerIntPair &RHS) const {
64     return Value != RHS.Value;
65   }
66 };
67
68 } // end namespace llvm
69 #endif