vptr.hh revision 8229:78bf55f23338
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 * Authors: Nathan Binkert
29 */
30
31#ifndef __ARCH_ALPHA_VPTR_HH__
32#define __ARCH_ALPHA_VPTR_HH__
33
34#include "arch/isa_traits.hh"
35#include "arch/vtophys.hh"
36#include "mem/vport.hh"
37
38class ThreadContext;
39
40template <class T>
41class VPtr
42{
43  public:
44    typedef T Type;
45
46  protected:
47    ThreadContext *tc;
48    Addr ptr;
49    Addr buffer[(sizeof(T)-1)/sizeof(Addr) + 1];
50
51  public:
52    explicit VPtr(ThreadContext *_tc, Addr p = 0)
53        : tc(_tc), ptr(p)
54    {
55        refresh();
56    }
57
58    template <class U>
59    VPtr(const VPtr<U> &vp)
60        : tc(vp.tc), ptr(vp.ptr)
61    {
62        refresh();
63    }
64
65    ~VPtr()
66    {}
67
68    void
69    refresh()
70    {
71        if (!ptr)
72            return;
73
74        VirtualPort *port = tc->getVirtPort();
75        port->readBlob(ptr, buffer, sizeof(T));
76    }
77
78    bool
79    operator!() const
80    {
81        return ptr == 0;
82    }
83
84    VPtr<T>
85    operator+(int offset)
86    {
87        return VPtr<T>(tc, ptr + offset);
88    }
89
90    const VPtr<T> &
91    operator+=(int offset)
92    {
93        ptr += offset;
94        refresh();
95
96        return *this;
97    }
98
99    const VPtr<T> &
100    operator=(Addr p)
101    {
102        ptr = p;
103        refresh();
104
105        return *this;
106    }
107
108    template <class U>
109    const VPtr<T> &
110    operator=(const VPtr<U> &vp)
111    {
112        tc = vp.tc;
113        ptr = vp.ptr;
114        refresh();
115
116        return *this;
117    }
118
119    operator T *()
120    {
121        return (T *)buffer;
122    }
123
124    T *
125    operator->()
126    {
127        return (T *)buffer;
128    }
129
130    T &
131    operator*()
132    {
133        return *(T *)buffer;
134    }
135};
136
137#endif // __ARCH_ALPHA_VPTR_HH__
138