1/*
2    Title:  save_vec.cpp - The save vector holds temporary values that may move as
3    the result of a garbage collection.
4
5    Copyright (c) 2006, 2010 David C.J. Matthews
6
7    This library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Lesser General Public
9    License as published by the Free Software Foundation; either
10    version 2.1 of the License, or (at your option) any later version.
11
12    This library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Lesser General Public License for more details.
16
17    You should have received a copy of the GNU Lesser General Public
18    License along with this library; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21*/
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#elif defined(_WIN32)
26#include "winconfig.h"
27#else
28#error "No configuration file"
29#endif
30
31#ifdef HAVE_ASSERT_H
32#include <assert.h>
33#define ASSERT(x)   assert(x)
34#else
35#define ASSERT(x)
36#endif
37
38#include "globals.h"
39#include "save_vec.h"
40#include "check_objects.h"
41#include "scanaddrs.h"
42#include "memmgr.h"
43
44
45#define SVEC_SIZE 1000
46
47SaveVec::SaveVec()
48{
49    save_vec = new SaveVecEntry[SVEC_SIZE];
50    save_vec_addr = save_vec;
51}
52
53SaveVec::~SaveVec()
54{
55    delete[](save_vec);
56}
57
58// DCJM - I've used this in a few cases where we iterate over a list
59// and want to avoid overflowing the save vec.  I've assumed that simply
60// resetting the list doesn't actually destroy the entry on the save vec
61// and it's safe to still use it provided that doesn't result in allocation.
62void SaveVec::reset(Handle old_value)
63{
64    ASSERT(old_value >= save_vec && old_value <= save_vec_addr);
65    save_vec_addr = old_value;
66}
67
68Handle SaveVec::push(PolyWord valu) /* Push a PolyWord onto the save vec. */
69{
70    ASSERT(save_vec_addr < save_vec+SVEC_SIZE);
71
72    Check(valu);
73
74    *save_vec_addr = SaveVecEntry(valu);
75    return save_vec_addr++;
76}
77
78void SaveVec::gcScan(ScanAddress *process)
79/* Ensures that all the objects are retained and their addresses updated. */
80{
81    for (Handle sv = save_vec; sv < save_vec_addr; sv++)
82        process->ScanRuntimeWord(&sv->m_Handle);
83}
84
85// We just have one of these.
86static SaveVec save;
87
88SaveVec *gSaveVec = &save;
89