File: wl_coll.c

package info (click to toggle)
gwm 1.8d-2
  • links: PTS
  • area: main
  • in suites: potato, woody
  • size: 5,120 kB
  • ctags: 3,030
  • sloc: ansic: 19,617; makefile: 1,763; lisp: 437; sh: 321; ml: 21
file content (239 lines) | stat: -rw-r--r-- 4,748 bytes parent folder | download
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* Copyright 1989 GROUPE BULL -- See license conditions in file COPYRIGHT
 * Copyright 1989 Massachusetts Institute of Technology
 */
/**************************\
* 			   *
*  WOOL_OBJECT Collection  *
*  BODY			   *
* 			   *
\**************************/

#include "EXTERN.h"
#include <stdio.h>
#include "wool.h"
#include "wl_number.h"
#include "wl_atom.h"
#include "wl_list.h"
#include "INTERN.h"
#include "wl_coll.h"

WOOL_OBJECT WLCollection_release();

/*
 * wlcf (wool collection free)
 * 
 * management of an stack of free collections to avoid calls to malloc
 */

#define WLCF_LIMIT 32
#if WLCF_LIMIT
static WOOL_Collection wlcf[WLCF_LIMIT];
#else
static WOOL_Collection wlcf[1];
#endif
static WOOL_Collection  *wlcf_last = wlcf;
static int	wlcf_size, wlcf_limit = WLCF_LIMIT;

/* to be called once */

#ifdef STATS
WOOL_OBJECT
wlcfstats()
{
    wool_printf("wool-coll-free (wlcf)  has %d", wlcf_size);
    wool_printf("/%d slots\n", wlcf_limit);
    return NIL;
}
#endif /* STATS */

/* to release wlcf  */

wlcf_flush()
{
    WOOL_Collection *p = wlcf_last - 1;

    while (p >= wlcf) {
	WLCollection_release(*p);
        p--;
    }
    wlcf_size = 0;
    wlcf_last = wlcf;
}

/* put in wlcf */

WOOL_OBJECT 
WLCollection_free(col)
WOOL_Collection col;
{
    if (wlcf_size >= wlcf_limit) {
	WLCollection_release(col);
    } else {
	wlcf_size++;
	*wlcf_last++ = col;
	col -> size = 0;
    }
    return NULL;
}

/*
 * Constructor:
 * WLCollection_make
 * do a wlcf_get in fact...
 */

WOOL_Collection 
WLCollection_make()
{
    WOOL_Collection col;

    if (wlcf_size) {
	col = *(--wlcf_last);
	wlcf_size--;
	zrt_put(col);
    } else {
	col = (WOOL_Collection)
	    Malloc(sizeof(struct _WOOL_Collection));
	zrt_put(col);
	col -> type = WLCollection;
	col -> size = 0;
	col -> limit = INITIAL_COLLECTION_SIZE;
	col -> list = (WOOL_OBJECT *) Malloc(col -> limit * sizeof(WOOL_OBJECT));
    }
    return col;
}

/* 
 * WLCollection_print:
 * Normally, never to be called.
 */

WOOL_OBJECT 
WLCollection_print(obj)
WOOL_Collection obj;
{
    int             i;
    WOOL_OBJECT    *p = obj -> list;

    wool_puts("{COLLECTION ");
    for (i = 0; i < obj -> size; i++, p++) {
	if (i)
	    wool_putchar(' ');
	WOOL_send(WOOL_print, *p, (*p));
    }
    wool_putchar('}');
    return (WOOL_OBJECT) obj;
}

/*
 * WLCollection_free
 */

WOOL_OBJECT 
WLCollection_release(col)
WOOL_Collection col;
{
    Free(col -> list);
    Free(col);
    return NULL;
}

/*
 * trying to execute an collection is the same error than executing an atom.
 */

/*
 * WLCollection_add:
 * Adds arg2 to arg1, just catenating if there is room, increasing limit
 * of collection if not.
 * (we know we have 4 bytes of overhead, thats the reason for our
 * growing scheme: * 2 +4)
 * WARNING: since a zrt_gc cannot occur during parsing, we do not set
 * the reference count on the sons!
 */

WOOL_Collection 
WLCollection_add(col, obj)
WOOL_Collection col;
WOOL_OBJECT     obj;
{
    if (col -> size >= col -> limit) {
	WOOL_OBJECT *oldlist = col -> list;

	col -> limit = col -> limit << 1 + 1;
	col -> list = (WOOL_OBJECT *) Malloc((col -> limit) * sizeof(WOOL_OBJECT));
	bcopy(oldlist, col -> list, col -> size * sizeof(WOOL_OBJECT));
	Free(oldlist);
    }
    *(col -> list + (col -> size)++) = obj;
    return col;
}

/* makes a (progn <list>) of a collection
 */

WOOL_OBJECT
WLCollection_progn(col)
WOOL_Collection col;
{
    if (col -> size) {
	WOOL_List       object = wool_list_make(col->size +1);

	copy_n_objects(col -> list, object -> list + 1, col -> size);
	increase_reference(object -> list[0] = WA_progn);
	return (WOOL_OBJECT) object;
    } else {				/* a list of size 0 is just NIL */
	return NIL;
    }
}

/*******************************************************\
* 						        *
* QuotedExpr package for speeding up quoted constructs  *
* 						        *
\*******************************************************/

WOOL_QuotedExpr
WLQuotedExpr_make(expr)
WOOL_OBJECT	expr;
{
    WOOL_QuotedExpr object = (WOOL_QuotedExpr)
    Malloc(sizeof(struct _WOOL_QuotedExpr));

    zrt_put(object);
    object -> type = WLQuotedExpr;
    increase_reference(object -> expr = expr);
    return (WOOL_QuotedExpr) object;
}

WOOL_OBJECT
WLQuotedExpr_eval(obj)
WOOL_QuotedExpr obj;
{
    return (WOOL_OBJECT) obj -> expr;
}

WOOL_OBJECT
WLQuotedExpr_print(obj)
WOOL_QuotedExpr obj;
{
    wool_putchar('\'');
    WOOL_send(WOOL_print, obj -> expr, (obj -> expr));
    return (WOOL_OBJECT) obj;
}

WOOL_OBJECT
WLQuotedExpr_free(obj)
WOOL_QuotedExpr obj;
{
    decrease_reference(obj -> expr);
    Free(obj);
    return NULL;
}

WOOL_OBJECT
WLQuotedExpr_equal(o1, o2)
WOOL_QuotedExpr o1, o2;
{
    return WOOL_send(WOOL_equal, o1 -> expr, (o1 -> expr, o2 -> expr));
}