File: box.c

package info (click to toggle)
nickle 2.68-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,336 kB
  • ctags: 3,288
  • sloc: ansic: 31,198; yacc: 1,860; lex: 858; sh: 830; makefile: 229
file content (200 lines) | stat: -rw-r--r-- 4,046 bytes parent folder | download | duplicates (2)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* $Header$ */

/*
 * Copyright © 1988-2004 Keith Packard and Bart Massey.
 * All Rights Reserved.  See the file COPYING in this directory
 * for licensing information.
 */

#include	"nickle.h"

static void
BoxMark (void *object)
{
    BoxPtr	box = object;
    Value	*elements;
    int		i;

    elements = BoxElements(box);
    if (box->replace)
	MemReference (box->u.replace);
    else if (box->homogeneous)
	MemReference (box->u.type);
    else
	MemReference (box->u.types);
    for (i = 0; i < box->nvalues; i++)
	MemReference (elements[i]);
}

DataType BoxType = { BoxMark, 0, "BoxType" };

BoxPtr
NewBox (Bool constant, Bool array, int nvalues, TypePtr type)
{
    ENTER ();
    BoxPtr  box;
    int	    i;

    box = ALLOCATE (&BoxType, sizeof (Box) + nvalues * sizeof (Value));
    box->constant = constant;
    box->homogeneous = True;
    box->replace = False;
    box->nvalues = nvalues;
    box->u.type = type;
    for (i = 0; i < nvalues; i++)
	BoxValueSet(box, i, 0);
    RETURN (box);
}

BoxPtr
NewTypedBox (Bool array, BoxTypesPtr bt)
{
    ENTER ();
    BoxPtr  box;
    int	    i;

    box = ALLOCATE (&BoxType, sizeof (Box) + bt->count * sizeof (Value));
    box->constant = False;
/*    box->array = array; */
    box->homogeneous = False;
    box->u.types = bt;
    box->nvalues = bt->count;
    for (i = 0; i < bt->count; i++)
	BoxValueSet (box, i, 0);
    RETURN (box);
}

#ifndef HAVE_C_INLINE
Value
BoxValue (BoxPtr box, int e)
{
    if (!BoxElements(box)[e].value)
    {
	RaiseStandardException (exception_uninitialized_value, 0);
	return (Void);
    }
    return (BoxElements(box)[e].value);
}
#endif

static void
MarkBoxReplace (void *object)
{
    BoxReplacePtr   replace = object;

    MemReference (replace->new);
}

DataType    BoxReplaceType = { MarkBoxReplace, 0, "BoxReplaceType" };

void
BoxSetReplace (BoxPtr old, BoxPtr new, int oldstride, int newstride)
{
    ENTER ();
    BoxReplacePtr   r = ALLOCATE (&BoxReplaceType, sizeof (BoxReplace));
    r->new = new;
    r->oldstride = oldstride;
    r->newstride = newstride;
    old->replace = True;
    old->u.replace = r;
    EXIT ();
}

BoxPtr
BoxRewrite (BoxPtr box, int *ep)
{
    int	e = *ep;
    
    while (box->replace)
    {
	BoxReplacePtr	r = box->u.replace;
	int		chunk, off;

	chunk = e / r->oldstride;
	off = e % r->oldstride;
	e = chunk * r->newstride + off;
	box = r->new;
    }
    /*
     * XXX oops.  References to previously available storage
     * should do something sensible instead of cratering.
     * The desired semantic is for them to persist, pointing
     * to whatever storage was there before the underlying object
     * was resized.  But, that's "hard".  This check will
     * at least prevent a seg fault.
     */
    if (e >= box->nvalues)
    {
	RaiseStandardException (exception_invalid_array_bounds, 2,
				Void, NewInt (e));
	e = 0;
	box = NewBox (True, False, 1, typePrim[rep_void]);
	BoxValueSet (box, 0, 0);
    }
    *ep = e;
    return box;
}

static void MarkBoxTypes (void *object)
{
    BoxTypesPtr	bt = object;
    int		i;

    for (i = 0; i < bt->count; i++)
	MemReference (BoxTypesValue(bt,i));
}

DataType    BoxTypesType = { MarkBoxTypes, 0, "BoxTypesType" };

#define BT_INCR	4

BoxTypesPtr
NewBoxTypes (int size)
{
    ENTER ();
    BoxTypesPtr    bt;

    bt = ALLOCATE (&BoxTypesType, sizeof (BoxTypes) + size * sizeof (Type *));
    bt->size = size;
    bt->count = 0;
    RETURN (bt);
}

int
AddBoxType (BoxTypesPtr *btp, Type *t)
{
    ENTER ();
    BoxTypesPtr bt, new;
    int		count, size;
    int		position;
    
    bt = *btp;
    if (!bt)
    {
	count = 0;
	size = 0;
    }
    else
    {
	count = bt->count;
	size = bt->size;
    }
    if (count == size)
    {
	size = size + BT_INCR;
	new = NewBoxTypes (size);
	if (count)
	{
	    memcpy (BoxTypesElements (new), BoxTypesElements (bt),
		    count * sizeof (Type *));
	}
	new->size = size;
	new->count = count;
	*btp = new;
	bt = new;
    }
    position = bt->count++;
    BoxTypesValue(bt,position) = t;
    EXIT ();
    return position;
}