1 /* tuples.c -- management of `tuples' (car and two values)
2
3 Copyright (C) 1993, 1994, 2000 John Harper <john@dcs.warwick.ac.uk>
4
5 $Id$
6
7 This file is part of librep.
8
9 librep is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 librep is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with librep; see the file COPYING. If not, write to
21 the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22
23 #define _GNU_SOURCE
24
25 #include "repint.h"
26
27 #define rep_TUPLEBLK_SIZE 680 /* ~8k */
28
29 /* Symbol allocation blocks */
30 typedef struct rep_tuple_block_struct rep_tuple_block;
31 struct rep_tuple_block_struct {
32 rep_tuple_block *next;
33 rep_ALIGN_CELL(rep_tuple tuples[rep_TUPLEBLK_SIZE]);
34 };
35
36 static rep_tuple_block *tuple_block_chain;
37 static rep_tuple *tuple_freelist;
38 int rep_allocated_tuples, rep_used_tuples;
39
40 repv
rep_make_tuple(repv car,repv a,repv b)41 rep_make_tuple (repv car, repv a, repv b)
42 {
43 rep_tuple *t;
44 if (tuple_freelist == 0)
45 {
46 rep_tuple_block *sb = rep_ALLOC_CELL (sizeof (rep_tuple_block));
47 if (sb != 0)
48 {
49 int i;
50 rep_allocated_tuples += rep_TUPLEBLK_SIZE;
51 sb->next = tuple_block_chain;
52 tuple_block_chain = sb;
53 for (i = 0; i < (rep_TUPLEBLK_SIZE - 1); i++)
54 {
55 sb->tuples[i].a = rep_VAL (&sb->tuples[i + 1]);
56 sb->tuples[i].car = 0;
57 }
58 sb->tuples[i].a = rep_VAL (tuple_freelist);
59 sb->tuples[i].car = 0;
60 tuple_freelist = sb->tuples;
61 }
62 else
63 abort ();
64 }
65 t = tuple_freelist;
66 tuple_freelist = rep_TUPLE (t->a);
67 t->car = car;
68 t->a = a;
69 t->b = b;
70 rep_used_tuples++;
71 rep_data_after_gc += sizeof (rep_tuple);
72 return rep_VAL (t);
73 }
74
75 void
rep_mark_tuple(repv t)76 rep_mark_tuple (repv t)
77 {
78 rep_MARKVAL (rep_TUPLE (t)->a);
79 rep_MARKVAL (rep_TUPLE (t)->b);
80 }
81
82 void
rep_sweep_tuples(void)83 rep_sweep_tuples (void)
84 {
85 rep_tuple_block *sb;
86 rep_tuple *tem_freelist = 0;
87 int tem_used = 0;
88 for (sb = tuple_block_chain; sb != 0; sb = sb->next)
89 {
90 rep_tuple *this = sb->tuples;
91 rep_tuple *last = &(sb->tuples[rep_TUPLEBLK_SIZE]);
92 while (this < last)
93 {
94 if (!rep_GC_CELL_MARKEDP (rep_VAL (this)))
95 {
96 this->a = rep_VAL (tem_freelist);
97 tem_freelist = this;
98 }
99 else
100 {
101 rep_GC_CLR_CELL (rep_VAL (this));
102 tem_used++;
103 }
104 this++;
105 }
106 }
107 tuple_freelist = tem_freelist;
108 rep_used_tuples = tem_used;
109 }
110
111 void
rep_tuples_kill(void)112 rep_tuples_kill(void)
113 {
114 rep_tuple_block *sb = tuple_block_chain;
115 while (sb != 0)
116 {
117 rep_tuple_block *nxt = sb->next;
118 rep_FREE_CELL (sb);
119 sb = nxt;
120 }
121 tuple_block_chain = NULL;
122 }
123