Simplify smob and port marking; set the mark bit in the generic
[bpt/guile.git] / libguile / fluids.c
CommitLineData
b7f3516f 1/* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
9482a297
MV
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
41
9482a297
MV
42#include "_scm.h"
43#include "print.h"
44#include "smob.h"
b3460a50 45#include "dynwind.h"
9482a297 46#include "fluids.h"
b3460a50
MV
47#include "alist.h"
48#include "eval.h"
9482a297
MV
49
50#define INITIAL_FLUIDS 10
51
52static volatile int n_fluids;
53long scm_tc16_fluid;
54
55SCM
56scm_make_initial_fluids ()
57{
58 return scm_make_vector (SCM_MAKINUM (INITIAL_FLUIDS),
a8741caa 59 SCM_BOOL_F);
9482a297
MV
60}
61
62static void grow_fluids SCM_P ((scm_root_state *, int new_length));
63static void
64grow_fluids (root_state, new_length)
65 scm_root_state *root_state;
66 int new_length;
67{
68 SCM old_fluids, new_fluids;
69 int old_length, i;
70
71 old_fluids = root_state->fluids;
9482a297 72 old_length = SCM_LENGTH (old_fluids);
a8741caa 73 new_fluids = scm_make_vector (SCM_MAKINUM (new_length), SCM_BOOL_F);
9482a297
MV
74 i = 0;
75 while (i < old_length)
76 {
77 SCM_VELTS(new_fluids)[i] = SCM_VELTS(old_fluids)[i];
78 i++;
79 }
80 while (i < new_length)
81 {
82 SCM_VELTS(new_fluids)[i] = SCM_BOOL_F;
83 i++;
84 }
85
86 root_state->fluids = new_fluids;
87}
88
89void
90scm_copy_fluids (root_state)
91 scm_root_state *root_state;
92{
93 grow_fluids (root_state, SCM_LENGTH(root_state->fluids));
94}
95
96static int print_fluid SCM_P ((SCM exp, SCM port, scm_print_state *pstate));
97static int
98print_fluid (exp, port, pstate)
99 SCM exp;
100 SCM port;
101 scm_print_state *pstate;
102{
b7f3516f 103 scm_puts ("#<fluid ", port);
9482a297 104 scm_intprint (SCM_FLUID_NUM (exp), 10, port);
b7f3516f 105 scm_putc ('>', port);
9482a297
MV
106 return 1;
107}
108
109static scm_smobfuns fluid_smob = {
dc53f026 110 0,
9482a297
MV
111 scm_free0,
112 print_fluid
113};
114
115static
116int next_fluid_num ()
117{
118 int n;
eadd48de 119#ifdef USE_THREADS
9482a297 120 SCM_THREAD_CRITICAL_SECTION_START;
eadd48de 121#endif
9482a297 122 n = n_fluids++;
eadd48de 123#ifdef USE_THREADS
9482a297 124 SCM_THREAD_CRITICAL_SECTION_END;
eadd48de 125#endif
9482a297
MV
126 return n;
127}
128
129SCM_PROC (s_make_fluid, "make-fluid", 0, 0, 0, scm_make_fluid);
130
131SCM
132scm_make_fluid ()
133{
134 SCM z;
135 int n;
136
137 SCM_DEFER_INTS;
138 n = next_fluid_num ();
139 SCM_NEWCELL (z);
140 SCM_SETCAR (z, scm_tc16_fluid);
141 SCM_SETCDR (z, n);
142 SCM_ALLOW_INTS;
143
144 return z;
145}
146
b3460a50
MV
147SCM_PROC (s_fluid_p, "fluid?", 1, 0, 0, scm_fluid_p);
148
149SCM
150scm_fluid_p (fl)
151 SCM fl;
152{
153 return (SCM_NIMP (fl) && SCM_FLUIDP (fl))? SCM_BOOL_T : SCM_BOOL_F;
154}
155
9482a297
MV
156SCM_PROC (s_fluid_ref, "fluid-ref", 1, 0, 0, scm_fluid_ref);
157
158SCM
159scm_fluid_ref (fl)
160 SCM fl;
161{
162 int n;
163
b3460a50 164 SCM_ASSERT (SCM_NIMP (fl) && SCM_FLUIDP (fl), fl, SCM_ARG1, s_fluid_ref);
9482a297
MV
165
166 n = SCM_FLUID_NUM (fl);
9482a297
MV
167
168 if (SCM_LENGTH (scm_root->fluids) <= n)
169 grow_fluids (scm_root, n+1);
170 return SCM_VELTS(scm_root->fluids)[n];
171}
172
173SCM_PROC (s_fluid_set_x, "fluid-set!", 2, 0, 0, scm_fluid_set_x);
174
175SCM
176scm_fluid_set_x (fl, val)
177 SCM fl;
178 SCM val;
179{
180 int n;
181
b3460a50 182 SCM_ASSERT (SCM_NIMP (fl) && SCM_FLUIDP (fl), fl, SCM_ARG1, s_fluid_set_x);
9482a297
MV
183
184 n = SCM_FLUID_NUM (fl);
9482a297
MV
185
186 if (SCM_LENGTH (scm_root->fluids) <= n)
187 grow_fluids (scm_root, n+1);
188 SCM_VELTS(scm_root->fluids)[n] = val;
189 return val;
190}
191
b3460a50
MV
192void
193scm_swap_fluids (fluids, vals)
194 SCM fluids, vals;
195{
196 while (SCM_NIMP (fluids))
197 {
198 SCM fl = SCM_CAR (fluids);
199 SCM old_val = scm_fluid_ref (fl);
200 scm_fluid_set_x (fl, SCM_CAR (vals));
201 SCM_SETCAR (vals, old_val);
202 fluids = SCM_CDR (fluids);
203 vals = SCM_CDR (vals);
204 }
205}
206
207/* Swap the fluid values in reverse order. This is important when the
208same fluid appears multiple times in the fluids list. */
209
210void
211scm_swap_fluids_reverse (fluids, vals)
212 SCM fluids, vals;
213{
214 if (SCM_NIMP (fluids))
215 {
216 SCM fl, old_val;
217
218 scm_swap_fluids_reverse (SCM_CDR (fluids), SCM_CDR (vals));
219 fl = SCM_CAR (fluids);
220 old_val = scm_fluid_ref (fl);
221 scm_fluid_set_x (fl, SCM_CAR (vals));
222 SCM_SETCAR (vals, old_val);
223 }
224}
225
226SCM_PROC (s_with_fluids, "with-fluids*", 3, 0, 0, scm_with_fluids);
227
228SCM
229scm_internal_with_fluids (fluids, vals, cproc, cdata)
230 SCM fluids, vals;
231 SCM (*cproc) ();
232 void *cdata;
233{
234 SCM ans;
235
236 int flen = scm_ilength (fluids);
237 int vlen = scm_ilength (vals);
238 SCM_ASSERT (flen >= 0, fluids, SCM_ARG1, s_with_fluids);
239 SCM_ASSERT (vlen >= 0, vals, SCM_ARG2, s_with_fluids);
240 if (flen != vlen)
241 scm_out_of_range (s_with_fluids, vals);
242
243 scm_swap_fluids (fluids, vals);
244 scm_dynwinds = scm_acons (fluids, vals, scm_dynwinds);
245 ans = cproc (cdata);
246 scm_dynwinds = SCM_CDR (scm_dynwinds);
247 scm_swap_fluids_reverse (fluids, vals);
248 return ans;
249}
250
251static SCM
252apply_thunk (void *thunk)
253{
254 return scm_apply ((SCM) thunk, SCM_EOL, SCM_EOL);
255}
256
257SCM
258scm_with_fluids (fluids, vals, thunk)
259 SCM fluids, vals, thunk;
260{
261 return scm_internal_with_fluids (fluids, vals, apply_thunk, (void *)thunk);
262}
263
9482a297
MV
264void
265scm_init_fluids ()
266{
267 scm_tc16_fluid = scm_newsmob(&fluid_smob);
268#include "fluids.x"
269}