* *.[ch]: Whitespace changes -- added space after SCM_VALIDATE_*
[bpt/guile.git] / libguile / fluids.c
1 /* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
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
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
45
46 #include "_scm.h"
47 #include "print.h"
48 #include "smob.h"
49 #include "genio.h"
50 #include "dynwind.h"
51 #include "fluids.h"
52 #include "alist.h"
53 #include "eval.h"
54
55 #define INITIAL_FLUIDS 10
56 #include "scm_validate.h"
57
58 static volatile int n_fluids;
59 long scm_tc16_fluid;
60
61 SCM
62 scm_make_initial_fluids ()
63 {
64 return scm_make_vector (SCM_MAKINUM (INITIAL_FLUIDS),
65 SCM_BOOL_F);
66 }
67
68 static void
69 grow_fluids (scm_root_state *root_state,int new_length)
70 {
71 SCM old_fluids, new_fluids;
72 int old_length, i;
73
74 old_fluids = root_state->fluids;
75 old_length = SCM_LENGTH (old_fluids);
76 new_fluids = scm_make_vector (SCM_MAKINUM (new_length), SCM_BOOL_F);
77 i = 0;
78 while (i < old_length)
79 {
80 SCM_VELTS(new_fluids)[i] = SCM_VELTS(old_fluids)[i];
81 i++;
82 }
83 while (i < new_length)
84 {
85 SCM_VELTS(new_fluids)[i] = SCM_BOOL_F;
86 i++;
87 }
88
89 root_state->fluids = new_fluids;
90 }
91
92 void
93 scm_copy_fluids (scm_root_state *root_state)
94 {
95 grow_fluids (root_state, SCM_LENGTH(root_state->fluids));
96 }
97
98 static int
99 print_fluid (SCM exp, SCM port, scm_print_state *pstate)
100 {
101 scm_puts ("#<fluid ", port);
102 scm_intprint (SCM_FLUID_NUM (exp), 10, port);
103 scm_putc ('>', port);
104 return 1;
105 }
106
107 static
108 int next_fluid_num ()
109 {
110 int n;
111 #ifdef USE_THREADS
112 SCM_THREAD_CRITICAL_SECTION_START;
113 #endif
114 n = n_fluids++;
115 #ifdef USE_THREADS
116 SCM_THREAD_CRITICAL_SECTION_END;
117 #endif
118 return n;
119 }
120
121 SCM_DEFINE (scm_make_fluid, "make-fluid", 0, 0, 0,
122 (),
123 "")
124 #define FUNC_NAME s_scm_make_fluid
125 {
126 int n;
127
128 SCM_DEFER_INTS;
129 n = next_fluid_num ();
130 SCM_RETURN_NEWSMOB (scm_tc16_fluid, n);
131 }
132 #undef FUNC_NAME
133
134 SCM_DEFINE (scm_fluid_p, "fluid?", 1, 0, 0,
135 (SCM fl),
136 "")
137 #define FUNC_NAME s_scm_fluid_p
138 {
139 return SCM_BOOL(SCM_FLUIDP (fl));
140 }
141 #undef FUNC_NAME
142
143 SCM_DEFINE (scm_fluid_ref, "fluid-ref", 1, 0, 0,
144 (SCM fl),
145 "")
146 #define FUNC_NAME s_scm_fluid_ref
147 {
148 int n;
149
150 SCM_VALIDATE_FLUID (1,fl);
151
152 n = SCM_FLUID_NUM (fl);
153
154 if (SCM_LENGTH (scm_root->fluids) <= n)
155 grow_fluids (scm_root, n+1);
156 return SCM_VELTS(scm_root->fluids)[n];
157 }
158 #undef FUNC_NAME
159
160 SCM_DEFINE (scm_fluid_set_x, "fluid-set!", 2, 0, 0,
161 (SCM fl, SCM val),
162 "")
163 #define FUNC_NAME s_scm_fluid_set_x
164 {
165 int n;
166
167 SCM_VALIDATE_FLUID (1,fl);
168 n = SCM_FLUID_NUM (fl);
169
170 if (SCM_LENGTH (scm_root->fluids) <= n)
171 grow_fluids (scm_root, n+1);
172 SCM_VELTS(scm_root->fluids)[n] = val;
173 return val;
174 }
175 #undef FUNC_NAME
176
177 void
178 scm_swap_fluids (SCM fluids, SCM vals)
179 {
180 while (SCM_NIMP (fluids))
181 {
182 SCM fl = SCM_CAR (fluids);
183 SCM old_val = scm_fluid_ref (fl);
184 scm_fluid_set_x (fl, SCM_CAR (vals));
185 SCM_SETCAR (vals, old_val);
186 fluids = SCM_CDR (fluids);
187 vals = SCM_CDR (vals);
188 }
189 }
190
191 /* Swap the fluid values in reverse order. This is important when the
192 same fluid appears multiple times in the fluids list. */
193
194 void
195 scm_swap_fluids_reverse (SCM fluids, SCM vals)
196 {
197 if (SCM_NIMP (fluids))
198 {
199 SCM fl, old_val;
200
201 scm_swap_fluids_reverse (SCM_CDR (fluids), SCM_CDR (vals));
202 fl = SCM_CAR (fluids);
203 old_val = scm_fluid_ref (fl);
204 scm_fluid_set_x (fl, SCM_CAR (vals));
205 SCM_SETCAR (vals, old_val);
206 }
207 }
208
209
210 static SCM
211 apply_thunk (void *thunk)
212 {
213 return scm_apply ((SCM) thunk, SCM_EOL, SCM_EOL);
214 }
215
216 SCM_DEFINE (scm_with_fluids, "with-fluids*", 3, 0, 0,
217 (SCM fluids, SCM vals, SCM thunk),
218 "")
219 #define FUNC_NAME s_scm_with_fluids
220 {
221 return scm_internal_with_fluids (fluids, vals, apply_thunk, (void *)thunk);
222 }
223 #undef FUNC_NAME
224
225 SCM
226 scm_internal_with_fluids (SCM fluids, SCM vals, SCM (*cproc) (), void *cdata)
227 {
228 SCM ans;
229
230 int flen = scm_ilength (fluids);
231 int vlen = scm_ilength (vals);
232 SCM_ASSERT (flen >= 0, fluids, SCM_ARG1, s_scm_with_fluids);
233 SCM_ASSERT (vlen >= 0, vals, SCM_ARG2, s_scm_with_fluids);
234 if (flen != vlen)
235 scm_out_of_range (s_scm_with_fluids, vals);
236
237 scm_swap_fluids (fluids, vals);
238 scm_dynwinds = scm_acons (fluids, vals, scm_dynwinds);
239 ans = cproc (cdata);
240 scm_dynwinds = SCM_CDR (scm_dynwinds);
241 scm_swap_fluids_reverse (fluids, vals);
242 return ans;
243 }
244
245
246
247 void
248 scm_init_fluids ()
249 {
250 scm_tc16_fluid = scm_make_smob_type_mfpe ("fluid", 0,
251 NULL, NULL, print_fluid, NULL);
252 #include "fluids.x"
253 }