vptr.hh revision 2080
1/*
2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef __ARCH_ALPHA_VPTR_HH__
30#define __ARCH_ALPHA_VPTR_HH__
31
32#include "targetarch/vtophys.hh"
33#include "arch/isa_traits.hh"
34
35class ExecContext;
36
37template <class T>
38class VPtr
39{
40  public:
41    typedef T Type;
42
43  private:
44    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