Website Summary Files Commits Branches @commit @diff @stats
path: root / src / frames.cpp
blob: 8b7c891082ab7706135bf22266f45521b920690d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <algorithm>

#include "types.hpp"

#include "frames.hpp"

namespace SpasmImpl
{
FrameStack::FrameStack() : base_ptr(NULL), frame_ptr(NULL), g_size(0), c_size(0)
{
}

FrameStack::FrameStack(const FrameStack& fs)
    : g_size(fs.g_size), c_size(fs.c_size), fstack(fs.fstack)
{
    copy_fstack(fs);
}

FrameStack::~FrameStack()
{
    delete_fstack();
}

FrameStack& FrameStack::operator=(const FrameStack& fs)
{
    if (this != &fs)
    {
        delete_fstack();
        copy_fstack(fs);
        g_size = fs.g_size;
        c_size = fs.c_size;
        fstack = fs.fstack;
    }
    return *this;
}

/*!
** Deletes the frame stack
*/
void FrameStack::delete_fstack()
{
    if (base_ptr)
        delete[] base_ptr;
}

/*!
** Copies the frame stack fs
**
** \param fs - the frame stack to be copied
*/
void FrameStack::copy_fstack(const FrameStack& fs)
{
    base_ptr = new data_t[fs.g_size];
    std::copy(fs.base_ptr, fs.frame_ptr + fs.c_size, base_ptr);
    frame_ptr = base_ptr + (fs.frame_ptr - fs.base_ptr);
}

/*!
** Returns a data_t object from the current frame
**
** \param index - index of the data_t object
** \return const reference to the object
*/
const data_t& FrameStack::operator[](size_t index) const
{
    return frame_ptr[index];
}

/*!
** Returns a data_t object from the current frame
**
** \param index - index of the data_t object
** \return reference to the object
*/
data_t& FrameStack::operator[](size_t index)
{
    return frame_ptr[index];
}

/*!
** Removes the current frame from the stack.
** It is called when a function does return.
*/
void FrameStack::pop_frame()
{
    if (fstack.empty())
    {
        frame_ptr = base_ptr;
        c_size = 0;
    }
    else
    {
        data_t* new_frame_ptr = base_ptr + fstack.top();
        fstack.pop();
        c_size = frame_ptr - new_frame_ptr;
        frame_ptr = new_frame_ptr;
    }
}

/*!
** Creates (pushes on the stack) new frame of fsize data_t objects
**
** \param fsize - size in data_t objects of the new frame
*/
void FrameStack::new_frame(size_t fsize)
{
    if (frame_ptr + c_size + fsize > base_ptr + g_size)
    {
        size_t new_size = g_size + std::max(g_size, c_size + fsize);
        data_t* new_base = new data_t[new_size];
        if (base_ptr)
        {
            std::copy(base_ptr, frame_ptr + c_size, new_base);
            delete[] base_ptr;
        }
        frame_ptr = new_base + (frame_ptr - base_ptr);
        base_ptr = new_base;
    }
    fstack.push(frame_ptr - base_ptr);
    c_size = fsize;
}

/*!
** \return true if the stack is empty
*/
bool FrameStack::empty() const
{
    return frame_ptr == base_ptr && c_size == 0;
}
}  // namespace SpasmImpl