vptr.hh revision 2170
15795Ssaidi@eecs.umich.edu/* 25795Ssaidi@eecs.umich.edu * Copyright (c) 2004-2005 The Regents of The University of Michigan 35795Ssaidi@eecs.umich.edu * All rights reserved. 45795Ssaidi@eecs.umich.edu * 55795Ssaidi@eecs.umich.edu * Redistribution and use in source and binary forms, with or without 65795Ssaidi@eecs.umich.edu * modification, are permitted provided that the following conditions are 75795Ssaidi@eecs.umich.edu * met: redistributions of source code must retain the above copyright 85795Ssaidi@eecs.umich.edu * notice, this list of conditions and the following disclaimer; 95795Ssaidi@eecs.umich.edu * redistributions in binary form must reproduce the above copyright 105795Ssaidi@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the 115795Ssaidi@eecs.umich.edu * documentation and/or other materials provided with the distribution; 125795Ssaidi@eecs.umich.edu * neither the name of the copyright holders nor the names of its 135795Ssaidi@eecs.umich.edu * contributors may be used to endorse or promote products derived from 145795Ssaidi@eecs.umich.edu * this software without specific prior written permission. 155795Ssaidi@eecs.umich.edu * 165795Ssaidi@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 175795Ssaidi@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 185795Ssaidi@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 195795Ssaidi@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 205795Ssaidi@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 215795Ssaidi@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 225795Ssaidi@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 235795Ssaidi@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 245795Ssaidi@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 255795Ssaidi@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 265795Ssaidi@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 275795Ssaidi@eecs.umich.edu */ 285795Ssaidi@eecs.umich.edu 295795Ssaidi@eecs.umich.edu#ifndef __ARCH_ALPHA_VPTR_HH__ 305795Ssaidi@eecs.umich.edu#define __ARCH_ALPHA_VPTR_HH__ 315795Ssaidi@eecs.umich.edu 328229Snate@binkert.org#include "arch/vtophys.hh" 335795Ssaidi@eecs.umich.edu#include "arch/isa_traits.hh" 345795Ssaidi@eecs.umich.edu 355795Ssaidi@eecs.umich.educlass ExecContext; 365795Ssaidi@eecs.umich.edu 375795Ssaidi@eecs.umich.edutemplate <class T> 385795Ssaidi@eecs.umich.educlass VPtr 395795Ssaidi@eecs.umich.edu{ 405795Ssaidi@eecs.umich.edu public: 415795Ssaidi@eecs.umich.edu typedef T Type; 425795Ssaidi@eecs.umich.edu 435795Ssaidi@eecs.umich.edu private: 445795Ssaidi@eecs.umich.edu ExecContext *xc; 45 Addr ptr; 46 47 public: 48 ExecContext *GetXC() const { return xc; } 49 Addr GetPointer() const { return ptr; } 50 51 public: 52 explicit VPtr(ExecContext *_xc, Addr p = 0) : xc(_xc), ptr(p) { } 53 template <class U> 54 VPtr(const VPtr<U> &vp) : xc(vp.GetXC()), ptr(vp.GetPointer()) {} 55 ~VPtr() {} 56 57 bool operator!() const 58 { 59 return ptr == 0; 60 } 61 62 VPtr<T> operator+(int offset) 63 { 64 VPtr<T> ptr(*this); 65 ptr += offset; 66 67 return ptr; 68 } 69 70 const VPtr<T> &operator+=(int offset) 71 { 72 ptr += offset; 73 assert((ptr & (TheISA::PageBytes - 1)) + sizeof(T) 74 < TheISA::PageBytes); 75 76 return *this; 77 } 78 79 const VPtr<T> &operator=(Addr p) 80 { 81 assert((p & (TheISA::PageBytes - 1)) + sizeof(T) 82 < TheISA::PageBytes); 83 ptr = p; 84 85 return *this; 86 } 87 88 template <class U> 89 const VPtr<T> &operator=(const VPtr<U> &vp) 90 { 91 xc = vp.GetXC(); 92 ptr = vp.GetPointer(); 93 94 return *this; 95 } 96 97 operator T *() 98 { 99 void *addr = vtomem(xc, ptr, sizeof(T)); 100 return (T *)addr; 101 } 102 103 T *operator->() 104 { 105 void *addr = vtomem(xc, ptr, sizeof(T)); 106 return (T *)addr; 107 } 108 109 T &operator*() 110 { 111 void *addr = vtomem(xc, ptr, sizeof(T)); 112 return *(T *)addr; 113 } 114}; 115 116#endif // __ARCH_ALPHA_VPTR_HH__ 117