vptr.hh revision 712
112334Sgabeblack@google.com/*
212334Sgabeblack@google.com * Copyright (c) 2003 The Regents of The University of Michigan
312334Sgabeblack@google.com * All rights reserved.
412334Sgabeblack@google.com *
512334Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
612334Sgabeblack@google.com * modification, are permitted provided that the following conditions are
712334Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
812334Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
912334Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1012334Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1112334Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1212334Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1312334Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1412334Sgabeblack@google.com * this software without specific prior written permission.
1512334Sgabeblack@google.com *
1612334Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1712334Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1812334Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1912334Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2012334Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2112334Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2212334Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2312334Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2412334Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2512334Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2612334Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2712334Sgabeblack@google.com */
2812334Sgabeblack@google.com
2912334Sgabeblack@google.com#ifndef __VPTR_HH__
3012334Sgabeblack@google.com#define __VPTR_HH__
3112334Sgabeblack@google.com
3212334Sgabeblack@google.com#include "arch/alpha/vtophys.hh"
3312334Sgabeblack@google.com
3412334Sgabeblack@google.comclass ExecContext;
3512334Sgabeblack@google.com
3612334Sgabeblack@google.comtemplate <class T>
3712334Sgabeblack@google.comclass VPtr
3812334Sgabeblack@google.com{
3912334Sgabeblack@google.com  public:
4012334Sgabeblack@google.com    typedef T Type;
4112334Sgabeblack@google.com
4212334Sgabeblack@google.com  private:
4312334Sgabeblack@google.com    ExecContext *xc;
4412334Sgabeblack@google.com    Addr ptr;
4512334Sgabeblack@google.com
4612334Sgabeblack@google.com  public:
4712334Sgabeblack@google.com    ExecContext *GetXC() const { return xc; }
4812334Sgabeblack@google.com    Addr GetPointer() const { return ptr; }
4912334Sgabeblack@google.com
5012334Sgabeblack@google.com  public:
5112334Sgabeblack@google.com    explicit VPtr(ExecContext *_xc, Addr p = 0) : xc(_xc), ptr(p) { }
5212334Sgabeblack@google.com    template <class U>
5312334Sgabeblack@google.com    VPtr(const VPtr<U> &vp) : xc(vp.GetXC()), ptr(vp.GetPointer()) {}
5412334Sgabeblack@google.com    ~VPtr() {}
5512334Sgabeblack@google.com
5612334Sgabeblack@google.com    bool operator!() const
5712334Sgabeblack@google.com    {
5812334Sgabeblack@google.com        return ptr == 0;
5912334Sgabeblack@google.com    }
6012334Sgabeblack@google.com
6112334Sgabeblack@google.com    VPtr<T> operator+(int offset)
6212334Sgabeblack@google.com    {
6312334Sgabeblack@google.com        VPtr<T> ptr(*this);
6412334Sgabeblack@google.com        ptr += offset;
6512334Sgabeblack@google.com
6612334Sgabeblack@google.com        return ptr;
6712334Sgabeblack@google.com    }
6812334Sgabeblack@google.com
6912334Sgabeblack@google.com    const VPtr<T> &operator+=(int offset)
7012334Sgabeblack@google.com    {
7112334Sgabeblack@google.com        ptr += offset;
7212334Sgabeblack@google.com        assert((ptr & (ALPHA_PGBYTES - 1)) + sizeof(T) < ALPHA_PGBYTES);
7312334Sgabeblack@google.com
7412334Sgabeblack@google.com        return *this;
7512334Sgabeblack@google.com    }
7612334Sgabeblack@google.com
7712334Sgabeblack@google.com    const VPtr<T> &operator=(Addr p)
7812334Sgabeblack@google.com    {
7912334Sgabeblack@google.com        assert((p & (ALPHA_PGBYTES - 1)) + sizeof(T) < ALPHA_PGBYTES);
8012334Sgabeblack@google.com        ptr = p;
8112334Sgabeblack@google.com
8212334Sgabeblack@google.com        return *this;
8312334Sgabeblack@google.com    }
8412334Sgabeblack@google.com
8512334Sgabeblack@google.com    template <class U>
8612334Sgabeblack@google.com    const VPtr<T> &operator=(const VPtr<U> &vp)
8712334Sgabeblack@google.com    {
8812334Sgabeblack@google.com        xc = vp.GetXC();
8912334Sgabeblack@google.com        ptr = vp.GetPointer();
9012334Sgabeblack@google.com
9112334Sgabeblack@google.com        return *this;
9212334Sgabeblack@google.com    }
9312334Sgabeblack@google.com
9412334Sgabeblack@google.com    operator T *()
9512334Sgabeblack@google.com    {
9612334Sgabeblack@google.com        void *addr = vtomem(xc, ptr, sizeof(T));
9712334Sgabeblack@google.com        return (T *)addr;
9812334Sgabeblack@google.com    }
9912334Sgabeblack@google.com
10012334Sgabeblack@google.com    T *operator->()
10112334Sgabeblack@google.com    {
10212334Sgabeblack@google.com        void *addr = vtomem(xc, ptr, sizeof(T));
10312334Sgabeblack@google.com        return (T *)addr;
10412334Sgabeblack@google.com    }
10512334Sgabeblack@google.com
10612334Sgabeblack@google.com    T &operator*()
10712334Sgabeblack@google.com    {
10812334Sgabeblack@google.com        void *addr = vtomem(xc, ptr, sizeof(T));
10912334Sgabeblack@google.com        return *(T *)addr;
11012334Sgabeblack@google.com    }
11112334Sgabeblack@google.com};
11212334Sgabeblack@google.com
11312334Sgabeblack@google.com#endif // __VPTR_HH__
11412334Sgabeblack@google.com