File: reference.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 (289 lines) | stat: -rw-r--r-- 6,583 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* Copyright 1989 GROUPE BULL -- See license conditions in file COPYRIGHT
 * Copyright 1989 Massachusetts Institute of Technology
 */
/******************************************************\
* 						       *
* reference.c:					       *
* reference count management and other storage issues  *
* 						       *
\******************************************************/

#include        "EXTERN.h"
#include 	"wool.h"


/*
 * The memory management of WOOL is implemented via a differed reference
 * count. That, each time an object's reference count attains 0, it is put in
 * the zrt, which is polled at regular intervals
 */

/************************************\
* 				     *
* Zero_reference table module (zrt)  *
* 				     *
\************************************/

/*
 * The zrt (Zero Reference Table) global structure  is used to mark ALL wobs
 * that have at any moment be of REF 0, that is either being created or via
 * decrease_reference.  Then you can call zrt_gc at strategic moments, (ie in
 * no enclosing WOOL function) to free all the zero-referenced objects in the
 * zrt.
 */

#ifdef STATS
WOOL_OBJECT
zrtstats()
{
    wool_printf("Zero-reference-table has %d", zrt_size);
    wool_printf("/%d slots\n", zrt_limit);
    return NIL;
}
#endif /* STATS */

zrt_init()
{
    zrt_size = 0;
    zrt_limit = 63;	/* pow(2,n)/4 -1 */
    zrt = (WOOL_OBJECT *) Malloc(zrt_limit * sizeof(WOOL_OBJECT));
    zrt_last = zrt;
}

/*
 * disposes really of objects stacked in zrt. Be warned that a WOOL_free might
 * trigger zrt_put during the zrt_gc !
 */

static int WlZrtInGc = 0;

zrt_gc(from)
int	from;
{
    WOOL_OBJECT *zrt_from = zrt + from;

    WlZrtInGc = 1;
    while (zrt_last > zrt_from) {
	zrt_last--, zrt_size--;
	if (REF(*zrt_last)) {	/* ok, graduate to normal object */
	    (*zrt_last) -> reference_count |= 1;
	} else {		/* free it */
	    WOOL_send(WOOL_free, *zrt_last, (*zrt_last));
	}
    }
    WlZrtInGc = 0;
}

/*
 * Never call zrt_put if obj was already in it (sould not happen)
 */

WOOL_OBJECT
zrt_put(obj)
WOOL_OBJECT obj;
{
    WOOL_OBJECT	*old_zrt;

#ifdef DEBUG
    must_not_be_in_zrt(obj);
#endif					/* DEBUG */
    if (WlZrtInGc) {
	WOOL_send(WOOL_free, obj, (obj));
    } else {
	if(zrt_size == zrt_limit) {
	    zrt_limit = (zrt_limit + 1) * 2 -1;
	    old_zrt = zrt;
	    zrt = (WOOL_OBJECT *) Realloc(zrt, zrt_limit *
					  sizeof(WOOL_OBJECT));
	    zrt_last = zrt + (zrt_last - old_zrt);
	}
	zrt_size ++;
	obj -> reference_count = 0;
	return *zrt_last++ = obj;
    }
}

/* when an object is physically replaced by another, update its entry
 * in the zrt
 */

zrt_replace_element(old, new)
WOOL_OBJECT old;
WOOL_OBJECT new;
{
    WOOL_OBJECT	*zrt_ptr = zrt;

    while (zrt_ptr < zrt_last) {
	if (*zrt_ptr == old) {
	    *zrt_ptr = new;
	    return;
	}
	zrt_ptr++;
    }
#ifdef DEBUG
    wool_error("replaced object 0x%x was not in zrt!", old);
#endif /* DEBUG */
}

#ifdef DEBUG
/* checks that the element is not in fact already in the zrt...
 */

must_not_be_in_zrt(obj)
WOOL_OBJECT obj;
{
    WOOL_OBJECT	*zrt_ptr = zrt;

    while (zrt_ptr < zrt_last) {
	if (*zrt_ptr == obj) {
	    wool_printf("at zrt[%d]", zrt_last - zrt);
	    wool_printf(" and zrt[%d], type: ", zrt_ptr - zrt);
	    wool_print(wool_type(obj));
	    wool_puts(", obj: ");
	    wool_print(obj);
	    wool_newline();
	    wool_error("object 0x%x was already in zrt!", obj);
	}
	zrt_ptr++;
    }
}
#endif /* DEBUG	     */

/***********************\
* 		        *
* reference management  *
* 		        *
\***********************/

/* increase_reference is a macro (REF(x)++) */

#ifdef DEBUG			/* macro otherwise */
increase_reference(obj)
WOOL_OBJECT obj;	/* obj may be UNDEFINED */
{
    REF(obj) += 2;
}

decrease_reference(obj)
WOOL_OBJECT obj;	/* obj may be UNDEFINED */
{
    if (obj) {
	if (((obj -> reference_count) -= 2) == 1)
	    zrt_put(obj);
	else if (obj -> reference_count < 0) {
	    wool_print(obj);
	    wool_error(": reference_count became %d", obj ->
		       reference_count);
	}
    }
}

decrease_reference_non_null(obj)
WOOL_OBJECT obj;	/* obj must be non-nil */
{
	if (((obj -> reference_count) -= 2) == 1)
	    zrt_put(obj);
	else if (obj -> reference_count < 0) {
	    wool_print(obj);
	    wool_error(": reference_count became %d", obj ->
		       reference_count);
	}
}
#endif /* DEBUG */

/*
 * decrease_reference_in_list:
 * decrease reference count of all the elements of the list.
 * but doesn't free the list.
 */

decrease_reference_in_list(count, list)
int    count;
WOOL_OBJECT *list;
{
    WOOL_OBJECT *last = list + count;

    while (list < last){
	decrease_reference(*list);
	list++;
    }
}

/*
 * duplicate an array of objects, increasing the reference count,
 * and mallocing
 */

duplicate_n_objects(source, dest, n)
WOOL_OBJECT    *source;		/* source is the array */
WOOL_OBJECT   **dest;		/* while dest is a POINTER to the array */
int             n;		/* how many to copy */
{
    WOOL_OBJECT *p = source, *q, *last = source + n;

    q = *dest = (WOOL_OBJECT *) Malloc(sizeof(WOOL_OBJECT) * n);
    while (p < last)
        increase_reference(*q++ = *p++);
}

/*
 * duplicate an array of objects, increasing the reference count,
 * without mallocing (dest already points to an malloced aera)
 */

copy_n_objects(source, dest, n)
WOOL_OBJECT    *source;		/* source is the array */
WOOL_OBJECT    *dest;		/* dest is  the array */
int             n;		/* how many to copy */
{
    WOOL_OBJECT *p = source, *q = dest, *last = source + n;

    while (p < last)
	increase_reference(*q++ = *p++);
}


/************************************\
* 				     *
* Delayed Free table module (dft)    *
* 				     *
\************************************/

/* this table holds all memory chunks that should be freed only when at the
 * top-level
 */

#define INITIAL_DFT_SIZE 31	/* pow(2,n)/4 - 1 */

/* This table holds memory chunks that are to be freed at the top-level
 */

dft_init()
{
    dft = (char **) Malloc(INITIAL_DFT_SIZE * sizeof(char *));
    dft_last = dft;
    dft_last_allocated = dft + INITIAL_DFT_SIZE;
}

/* dft_gc disposes really of memory stacked in dft. Defined as a macro
 * while (dft_last > dft) Free(*(--dft_last)) in wool.h
 */

/* put in dft for later freeing
 */

DelayedFree(ptr)
char * ptr;
{
    ASSERT(ptr != NULL);
    if(dft_last == dft_last_allocated) {
	char	**old_dft;
	int size = dft_last - dft, old_size = size;
	size = (size + 1) * 2 -1;
	old_dft = dft;
	dft = (char **) Realloc(dft, size * sizeof(char *));
	dft_last = dft + old_size;
	dft_last_allocated = dft + size;
    }
    *dft_last++ = ptr;
}