vptr.hh revision 1354
113511Sgabeblack@google.com/*
213511Sgabeblack@google.com * Copyright (c) 2004 The Regents of The University of Michigan
313511Sgabeblack@google.com * All rights reserved.
413511Sgabeblack@google.com *
513511Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
613511Sgabeblack@google.com * modification, are permitted provided that the following conditions are
713511Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
813511Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
913511Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1013511Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1113511Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1213511Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1313511Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1413511Sgabeblack@google.com * this software without specific prior written permission.
1513511Sgabeblack@google.com *
1613511Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1713511Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1813511Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1913511Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2013511Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2113511Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2213511Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2313511Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2413511Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2513511Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2613511Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2713511Sgabeblack@google.com */
2813511Sgabeblack@google.com
2913511Sgabeblack@google.com#ifndef __ARCH_ALPHA_VPTR_HH__
3013511Sgabeblack@google.com#define __ARCH_ALPHA_VPTR_HH__
3113511Sgabeblack@google.com
3213511Sgabeblack@google.com#include "arch/alpha/vtophys.hh"
3313511Sgabeblack@google.com
3413511Sgabeblack@google.comclass ExecContext;
3513511Sgabeblack@google.com
3613511Sgabeblack@google.comtemplate <class T>
3713511Sgabeblack@google.comclass VPtr
3813511Sgabeblack@google.com{
3913511Sgabeblack@google.com  public:
4013511Sgabeblack@google.com    typedef T Type;
4113511Sgabeblack@google.com
4213511Sgabeblack@google.com  private:
4313511Sgabeblack@google.com    ExecContext *xc;
4413511Sgabeblack@google.com    Addr ptr;
4513511Sgabeblack@google.com
4613511Sgabeblack@google.com  public:
4713511Sgabeblack@google.com    ExecContext *GetXC() const { return xc; }
4813511Sgabeblack@google.com    Addr GetPointer() const { return ptr; }
4913511Sgabeblack@google.com
5013511Sgabeblack@google.com  public:
5113511Sgabeblack@google.com    explicit VPtr(ExecContext *_xc, Addr p = 0) : xc(_xc), ptr(p) { }
5213511Sgabeblack@google.com    template <class U>
5313511Sgabeblack@google.com    VPtr(const VPtr<U> &vp) : xc(vp.GetXC()), ptr(vp.GetPointer()) {}
5413511Sgabeblack@google.com    ~VPtr() {}
5513511Sgabeblack@google.com
5613511Sgabeblack@google.com    bool operator!() const
5713511Sgabeblack@google.com    {
5813511Sgabeblack@google.com        return ptr == 0;
5913511Sgabeblack@google.com    }
6013511Sgabeblack@google.com
6113511Sgabeblack@google.com    VPtr<T> operator+(int offset)
6213511Sgabeblack@google.com    {
6313511Sgabeblack@google.com        VPtr<T> ptr(*this);
6413511Sgabeblack@google.com        ptr += offset;
6513511Sgabeblack@google.com
6613511Sgabeblack@google.com        return ptr;
6713511Sgabeblack@google.com    }
6813511Sgabeblack@google.com
6913511Sgabeblack@google.com    const VPtr<T> &operator+=(int offset)
7013511Sgabeblack@google.com    {
7113511Sgabeblack@google.com        ptr += offset;
7213511Sgabeblack@google.com        assert((ptr & (AlphaISA::PageBytes - 1)) + sizeof(T)
7313511Sgabeblack@google.com               < AlphaISA::PageBytes);
7413511Sgabeblack@google.com
7513511Sgabeblack@google.com        return *this;
7613511Sgabeblack@google.com    }
7713511Sgabeblack@google.com
7813511Sgabeblack@google.com    const VPtr<T> &operator=(Addr p)
7913511Sgabeblack@google.com    {
8013511Sgabeblack@google.com        assert((p & (AlphaISA::PageBytes)) + sizeof(T) < AlphaISA::PageBytes);
8113511Sgabeblack@google.com        ptr = p;
8213511Sgabeblack@google.com
8313511Sgabeblack@google.com        return *this;
8413511Sgabeblack@google.com    }
8513511Sgabeblack@google.com
8613511Sgabeblack@google.com    template <class U>
8713511Sgabeblack@google.com    const VPtr<T> &operator=(const VPtr<U> &vp)
8813511Sgabeblack@google.com    {
8913511Sgabeblack@google.com        xc = vp.GetXC();
9013511Sgabeblack@google.com        ptr = vp.GetPointer();
9113511Sgabeblack@google.com
9213511Sgabeblack@google.com        return *this;
9313511Sgabeblack@google.com    }
9413511Sgabeblack@google.com
9513511Sgabeblack@google.com    operator T *()
9613511Sgabeblack@google.com    {
9713511Sgabeblack@google.com        void *addr = vtomem(xc, ptr, sizeof(T));
9813511Sgabeblack@google.com        return (T *)addr;
9913511Sgabeblack@google.com    }
10013511Sgabeblack@google.com
10113511Sgabeblack@google.com    T *operator->()
10213511Sgabeblack@google.com    {
10313511Sgabeblack@google.com        void *addr = vtomem(xc, ptr, sizeof(T));
10413511Sgabeblack@google.com        return (T *)addr;
10513511Sgabeblack@google.com    }
10613511Sgabeblack@google.com
10713511Sgabeblack@google.com    T &operator*()
10813511Sgabeblack@google.com    {
10913511Sgabeblack@google.com        void *addr = vtomem(xc, ptr, sizeof(T));
11013511Sgabeblack@google.com        return *(T *)addr;
11113511Sgabeblack@google.com    }
11213511Sgabeblack@google.com};
11313511Sgabeblack@google.com
11413511Sgabeblack@google.com#endif // __ARCH_ALPHA_VPTR_HH__
11513511Sgabeblack@google.com