34d09a4a8fc6e4d9a4172e3e2c193970f422df93
[bpt/guile.git] / libguile / inline.h
1 /* classes: h_files */
2
3 #ifndef SCM_INLINE_H
4 #define SCM_INLINE_H
5
6 /* Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23 /* This file is for inline functions. On platforms that don't support
24 inlining functions, they are turned into ordinary functions. See
25 "inline.c".
26 */
27
28 #include "libguile/__scm.h"
29
30 #if (SCM_DEBUG_CELL_ACCESSES == 1)
31 #include <stdio.h>
32 #endif
33
34 #include "libguile/pairs.h"
35 #include "libguile/gc.h"
36 #include "libguile/threads.h"
37
38
39 SCM_API SCM scm_cell (scm_t_bits car, scm_t_bits cdr);
40 SCM_API SCM scm_double_cell (scm_t_bits car, scm_t_bits cbr,
41 scm_t_bits ccr, scm_t_bits cdr);
42
43
44
45 #if defined SCM_C_INLINE || defined SCM_INLINE_C_INCLUDING_INLINE_H
46 /* either inlining, or being included from inline.c. We use (and
47 repeat) this long #if test here and below so that we don't have to
48 introduce any extraneous symbols into the public namespace. We
49 only need SCM_C_INLINE to be seen publically . */
50
51 extern unsigned scm_newcell2_count;
52 extern unsigned scm_newcell_count;
53
54 #if defined SCM_C_INLINE && ! defined SCM_INLINE_C_INCLUDING_INLINE_H
55 /* definitely inlining */
56 #ifdef __GNUC__
57 extern
58 #else
59 static
60 #endif
61 SCM_C_INLINE
62 #endif
63 SCM
64 scm_cell (scm_t_bits car, scm_t_bits cdr)
65 {
66 SCM z;
67 /* We retrieve the SCM pointer only once since the call to
68 SCM_FREELIST_LOC will be slightly expensive when we support
69 preemptive multithreading. SCM_FREELIST_LOC will then retrieve
70 the thread specific freelist.
71
72 Until then, SCM_FREELIST_DOC expands to (&scm_i_freelist) and the
73 following code will compile to the same as if we had worked
74 directly on the scm_i_freelist variable.
75 */
76 SCM *freelist = SCM_FREELIST_LOC (scm_i_freelist);
77
78 if (SCM_NULLP (*freelist))
79 z = scm_gc_for_newcell (&scm_i_master_freelist, freelist);
80 else
81 {
82 z = *freelist;
83 *freelist = SCM_FREE_CELL_CDR (*freelist);
84 }
85
86 /*
87 We update scm_cells_allocated from this function. If we don't
88 update this explicitly, we will have to walk a freelist somewhere
89 later on, which seems a lot more expensive.
90 */
91 scm_cells_allocated += 1;
92
93 #if (SCM_DEBUG_CELL_ACCESSES == 1)
94 if (scm_debug_cell_accesses_p)
95 {
96 if (SCM_GC_MARK_P (z))
97 {
98 fprintf(stderr, "scm_cell tried to allocate a marked cell.\n");
99 abort();
100 }
101 else if (SCM_GC_CELL_WORD(z, 0) != scm_tc_free_cell)
102 {
103 fprintf(stderr, "cell from freelist is not a free cell.\n");
104 abort();
105 }
106 }
107
108 /*
109 Always set mark. Otherwise cells that are alloced before
110 scm_debug_cell_accesses_p is toggled seem invalid.
111 */
112 SCM_SET_GC_MARK (z);
113
114 /*
115 TODO: figure out if this use of mark bits is valid with
116 threading. What if another thread is doing GC at this point
117 ... ?
118 */
119
120 #endif
121
122
123 /* Initialize the type slot last so that the cell is ignored by the
124 GC until it is completely initialized. This is only relevant
125 when the GC can actually run during this code, which it can't for
126 cooperating threads, but it might be important when we get true
127 preemptive threads.
128 */
129 SCM_GC_SET_CELL_WORD (z, 1, cdr);
130 SCM_GC_SET_CELL_WORD (z, 0, car);
131
132 #if 0 /*fixme* Hmm... let's consider this later. */
133 #if !defined(SCM_USE_COOP_THREADS) && !defined(SCM_USE_NULL_THREADS) && !defined(SCM_USE_COPT_THREADS)
134 /* When we are using preemtive threads, we might need to make
135 sure that the initial values for the slots are protected until
136 the cell is completely initialized.
137 */
138 #error review me
139 scm_remember_upto_here_1 (SCM_PACK (cdr));
140 #endif
141 #endif
142
143 #if (SCM_DEBUG_CELL_ACCESSES == 1)
144 if (scm_expensive_debug_cell_accesses_p )
145 scm_i_expensive_validation_check (z);
146 #endif
147
148 return z;
149 }
150
151 #if defined SCM_C_INLINE && ! defined SCM_INLINE_C_INCLUDING_INLINE_H
152 /* definitely inlining */
153 #ifdef __GNUC__
154 extern
155 #else
156 static
157 #endif
158 SCM_C_INLINE
159 #endif
160 SCM
161 scm_double_cell (scm_t_bits car, scm_t_bits cbr,
162 scm_t_bits ccr, scm_t_bits cdr)
163 {
164 SCM z;
165 SCM *freelist = SCM_FREELIST_LOC (scm_i_freelist2);
166
167 if (SCM_NULLP (*freelist))
168 z = scm_gc_for_newcell (&scm_i_master_freelist2, freelist);
169 else
170 {
171 z = *freelist;
172 *freelist = SCM_FREE_CELL_CDR (*freelist);
173 }
174
175 scm_cells_allocated += 2;
176
177 /* Initialize the type slot last so that the cell is ignored by the
178 GC until it is completely initialized. This is only relevant
179 when the GC can actually run during this code, which it can't for
180 cooperating threads, but it might be important when we get true
181 preemptive threads.
182 */
183 SCM_GC_SET_CELL_WORD (z, 1, cbr);
184 SCM_GC_SET_CELL_WORD (z, 2, ccr);
185 SCM_GC_SET_CELL_WORD (z, 3, cdr);
186 SCM_GC_SET_CELL_WORD (z, 0, car);
187
188 #if 0 /*fixme* Hmm... let's consider this later. */
189 #if !defined(SCM_USE_COOP_THREADS) && !defined(SCM_USE_NULL_THREADS) && !defined(SCM_USE_COPT_THREADS)
190 /* When we are using non-cooperating threads, we might need to make
191 sure that the initial values for the slots are protected until
192 the cell is completely initialized.
193 */
194 #error review me
195 scm_remember_upto_here_3 (SCM_PACK (cbr), SCM_PACK (ccr), SCM_PACK (cdr));
196 #endif
197 #endif
198
199
200 #if (SCM_DEBUG_CELL_ACCESSES == 1)
201 if (scm_debug_cell_accesses_p)
202 {
203 if (SCM_GC_MARK_P (z))
204 {
205 fprintf(stderr,
206 "scm_double_cell tried to allocate a marked cell.\n");
207 abort();
208 }
209 }
210
211 /* see above. */
212 SCM_SET_GC_MARK (z);
213
214 #endif
215
216 /* When this function is inlined, it's possible that the last
217 SCM_GC_SET_CELL_WORD above will be adjacent to a following
218 initialization of z. E.g., it occurred in scm_make_real. GCC
219 from around version 3 (e.g., certainly 3.2) began taking
220 advantage of strict C aliasing rules which say that it's OK to
221 interchange the initialization above and the one below when the
222 pointer types appear to differ sufficiently. We don't want that,
223 of course. GCC allows this behaviour to be disabled with the
224 -fno-strict-aliasing option, but would also need to be supplied
225 by Guile users. Instead, the following statements prevent the
226 reordering.
227 */
228 #ifdef __GNUC__
229 asm volatile ("" : : : "memory");
230 #else
231 /* portable version, just in case any other compiler does the same
232 thing. */
233 scm_remember_upto_here_1 (z);
234 #endif
235
236 return z;
237 }
238
239
240
241 #endif
242 #endif