Add `NEWS' entries for Andy's fixes.
[bpt/guile.git] / libguile / inline.h
CommitLineData
16ea9620
MV
1/* classes: h_files */
2
3#ifndef SCM_INLINE_H
4#define SCM_INLINE_H
5
3f520967 6/* Copyright (C) 2001, 2002, 2003, 2004, 2006, 2008 Free Software Foundation, Inc.
16ea9620 7 *
73be1d9e
MV
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.
16ea9620 12 *
73be1d9e 13 * This library is distributed in the hope that it will be useful,
16ea9620 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
16ea9620 17 *
73be1d9e
MV
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
92205699 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
73be1d9e 21 */
16ea9620
MV
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
60e7529a 28#include "libguile/__scm.h"
c8a1bdc4 29
1e71eafb
HWN
30#if (SCM_DEBUG_CELL_ACCESSES == 1)
31#include <stdio.h>
1e71eafb
HWN
32#endif
33
16ea9620
MV
34#include "libguile/pairs.h"
35#include "libguile/gc.h"
9bc4701c 36#include "libguile/threads.h"
9598a406 37#include "libguile/unif.h"
d5ad4aa6 38#include "libguile/pairs.h"
16ea9620 39
c8a1bdc4 40
979eade6
LC
41#ifndef SCM_INLINE_C_INCLUDING_INLINE_H
42
43/* GCC has `__inline__' in all modes, including strict ansi. GCC 4.3 and
44 above with `-std=c99' or `-std=gnu99' implements ISO C99 inline semantics,
45 unless `-fgnu89-inline' is used. Here we want GNU "extern inline"
46 semantics, hence the `__gnu_inline__' attribute, in accordance with:
47 http://gcc.gnu.org/gcc-4.3/porting_to.html .
48
49 With GCC 4.2, `__GNUC_STDC_INLINE__' is never defined (because C99 inline
50 semantics are not supported), but a warning is issued in C99 mode if
51 `__gnu_inline__' is not used. */
52
53# ifdef __GNUC__
54# if (defined __GNUC_STDC_INLINE__) || (__GNUC__ == 4 && __GNUC_MINOR__ == 2)
55# define SCM_C_EXTERN_INLINE \
56 extern __inline__ __attribute__ ((__gnu_inline__))
57# else
58# define SCM_C_EXTERN_INLINE extern __inline__
59# endif
60# elif (defined SCM_C_INLINE)
61# define SCM_C_EXTERN_INLINE static SCM_C_INLINE
62# endif
63
64#endif /* SCM_INLINE_C_INCLUDING_INLINE_H */
65
66
3f520967
LC
67#if ((!defined SCM_C_INLINE) && (!defined SCM_INLINE_C_INCLUDING_INLINE_H)) \
68 || (defined __GNUC__)
69
70/* The `extern' declarations. They should only appear when used from
71 "inline.c", when `inline' is not supported at all or when GCC's "extern
72 inline" is used. */
73
c8a1bdc4
HWN
74SCM_API SCM scm_cell (scm_t_bits car, scm_t_bits cdr);
75SCM_API SCM scm_double_cell (scm_t_bits car, scm_t_bits cbr,
76 scm_t_bits ccr, scm_t_bits cdr);
77
9598a406
MV
78SCM_API SCM scm_array_handle_ref (scm_t_array_handle *h, ssize_t pos);
79SCM_API void scm_array_handle_set (scm_t_array_handle *h, ssize_t pos, SCM val);
16ea9620 80
3f520967
LC
81SCM_API int scm_is_pair (SCM x);
82
83#endif
84
60e7529a
RB
85
86#if defined SCM_C_INLINE || defined SCM_INLINE_C_INCLUDING_INLINE_H
87/* either inlining, or being included from inline.c. We use (and
88 repeat) this long #if test here and below so that we don't have to
89 introduce any extraneous symbols into the public namespace. We
90 only need SCM_C_INLINE to be seen publically . */
c8a1bdc4
HWN
91
92extern unsigned scm_newcell2_count;
93extern unsigned scm_newcell_count;
94
979eade6
LC
95
96#if defined SCM_C_EXTERN_INLINE && ! defined SCM_INLINE_C_INCLUDING_INLINE_H
97SCM_C_EXTERN_INLINE
60e7529a 98#endif
c8a1bdc4 99SCM
228a24ef 100scm_cell (scm_t_bits car, scm_t_bits cdr)
16ea9620
MV
101{
102 SCM z;
9bc4701c 103 SCM *freelist = SCM_FREELIST_LOC (scm_i_freelist);
16ea9620 104
d2e53ed6 105 if (scm_is_null (*freelist))
9bc4701c 106 z = scm_gc_for_newcell (&scm_i_master_freelist, freelist);
c8a1bdc4 107 else
16ea9620 108 {
9bc4701c
MD
109 z = *freelist;
110 *freelist = SCM_FREE_CELL_CDR (*freelist);
16ea9620 111 }
c8a1bdc4
HWN
112
113 /*
114 We update scm_cells_allocated from this function. If we don't
115 update this explicitly, we will have to walk a freelist somewhere
116 later on, which seems a lot more expensive.
117 */
118 scm_cells_allocated += 1;
119
120#if (SCM_DEBUG_CELL_ACCESSES == 1)
121 if (scm_debug_cell_accesses_p)
1e71eafb
HWN
122 {
123 if (SCM_GC_MARK_P (z))
124 {
125 fprintf(stderr, "scm_cell tried to allocate a marked cell.\n");
126 abort();
127 }
6b69393d 128 else if (SCM_GC_CELL_WORD(z, 0) != scm_tc_free_cell)
1e71eafb
HWN
129 {
130 fprintf(stderr, "cell from freelist is not a free cell.\n");
131 abort();
132 }
133 }
134
135 /*
136 Always set mark. Otherwise cells that are alloced before
137 scm_debug_cell_accesses_p is toggled seem invalid.
138 */
139 SCM_SET_GC_MARK (z);
140
141 /*
142 TODO: figure out if this use of mark bits is valid with
143 threading. What if another thread is doing GC at this point
144 ... ?
145 */
c8a1bdc4 146
c8a1bdc4 147#endif
16ea9620 148
c8a1bdc4 149
16ea9620
MV
150 /* Initialize the type slot last so that the cell is ignored by the
151 GC until it is completely initialized. This is only relevant
b2a339f6
MV
152 when the GC can actually run during this code, which it can't
153 since the GC only runs when all other threads are stopped.
16ea9620 154 */
1fc8902f
DH
155 SCM_GC_SET_CELL_WORD (z, 1, cdr);
156 SCM_GC_SET_CELL_WORD (z, 0, car);
16ea9620 157
eab1b259
HWN
158#if (SCM_DEBUG_CELL_ACCESSES == 1)
159 if (scm_expensive_debug_cell_accesses_p )
160 scm_i_expensive_validation_check (z);
161#endif
c8a1bdc4 162
16ea9620
MV
163 return z;
164}
165
979eade6
LC
166#if defined SCM_C_EXTERN_INLINE && ! defined SCM_INLINE_C_INCLUDING_INLINE_H
167SCM_C_EXTERN_INLINE
60e7529a 168#endif
c8a1bdc4 169SCM
228a24ef
DH
170scm_double_cell (scm_t_bits car, scm_t_bits cbr,
171 scm_t_bits ccr, scm_t_bits cdr)
16ea9620 172{
5e1e20c8
MV
173 SCM z;
174 SCM *freelist = SCM_FREELIST_LOC (scm_i_freelist2);
175
d2e53ed6 176 if (scm_is_null (*freelist))
9bc4701c 177 z = scm_gc_for_newcell (&scm_i_master_freelist2, freelist);
16ea9620
MV
178 else
179 {
9bc4701c
MD
180 z = *freelist;
181 *freelist = SCM_FREE_CELL_CDR (*freelist);
16ea9620
MV
182 }
183
c8a1bdc4
HWN
184 scm_cells_allocated += 2;
185
16ea9620
MV
186 /* Initialize the type slot last so that the cell is ignored by the
187 GC until it is completely initialized. This is only relevant
b2a339f6
MV
188 when the GC can actually run during this code, which it can't
189 since the GC only runs when all other threads are stopped.
16ea9620 190 */
1fc8902f
DH
191 SCM_GC_SET_CELL_WORD (z, 1, cbr);
192 SCM_GC_SET_CELL_WORD (z, 2, ccr);
193 SCM_GC_SET_CELL_WORD (z, 3, cdr);
194 SCM_GC_SET_CELL_WORD (z, 0, car);
16ea9620 195
c8a1bdc4
HWN
196#if (SCM_DEBUG_CELL_ACCESSES == 1)
197 if (scm_debug_cell_accesses_p)
198 {
199 if (SCM_GC_MARK_P (z))
200 {
201 fprintf(stderr,
202 "scm_double_cell tried to allocate a marked cell.\n");
203 abort();
204 }
c8a1bdc4 205 }
4ad0814a
HWN
206
207 /* see above. */
208 SCM_SET_GC_MARK (z);
209
3553e1d1
GH
210#endif
211
212 /* When this function is inlined, it's possible that the last
213 SCM_GC_SET_CELL_WORD above will be adjacent to a following
214 initialization of z. E.g., it occurred in scm_make_real. GCC
215 from around version 3 (e.g., certainly 3.2) began taking
216 advantage of strict C aliasing rules which say that it's OK to
217 interchange the initialization above and the one below when the
218 pointer types appear to differ sufficiently. We don't want that,
219 of course. GCC allows this behaviour to be disabled with the
220 -fno-strict-aliasing option, but would also need to be supplied
221 by Guile users. Instead, the following statements prevent the
222 reordering.
223 */
224#ifdef __GNUC__
cb975c21 225 __asm__ volatile ("" : : : "memory");
3553e1d1
GH
226#else
227 /* portable version, just in case any other compiler does the same
228 thing. */
229 scm_remember_upto_here_1 (z);
c8a1bdc4 230#endif
6253f3f1 231
c8a1bdc4
HWN
232 return z;
233}
6253f3f1 234
979eade6
LC
235#if defined SCM_C_EXTERN_INLINE && ! defined SCM_INLINE_C_INCLUDING_INLINE_H
236SCM_C_EXTERN_INLINE
9598a406
MV
237#endif
238SCM
239scm_array_handle_ref (scm_t_array_handle *h, ssize_t p)
240{
241 return h->ref (h, p);
242}
eab1b259 243
979eade6
LC
244#if defined SCM_C_EXTERN_INLINE && ! defined SCM_INLINE_C_INCLUDING_INLINE_H
245SCM_C_EXTERN_INLINE
9598a406
MV
246#endif
247void
248scm_array_handle_set (scm_t_array_handle *h, ssize_t p, SCM v)
249{
250 h->set (h, p, v);
251}
eab1b259 252
979eade6
LC
253#if defined SCM_C_EXTERN_INLINE && ! defined SCM_INLINE_C_INCLUDING_INLINE_H
254SCM_C_EXTERN_INLINE
d5ad4aa6
MV
255#endif
256int
257scm_is_pair (SCM x)
258{
23f2b9a3
KR
259 /* The following "workaround_for_gcc_295" avoids bad code generated by
260 i386 gcc 2.95.4 (the Debian packaged 2.95.4-24 at least).
261
262 Under the default -O2 the inlined SCM_I_CONSP test gets "optimized" so
263 the fetch of the tag word from x is done before confirming it's a
264 non-immediate (SCM_NIMP). Needless to say that bombs badly if x is a
265 immediate. This was seen to afflict scm_srfi1_split_at and something
266 deep in the bowels of ceval(). In both cases segvs resulted from
267 deferencing a random immediate value. srfi-1.test exposes the problem
268 through a short list, the immediate being SCM_EOL in that case.
269 Something in syntax.test exposed the ceval() problem.
270
271 Just "volatile SCM workaround_for_gcc_295 = lst" is enough to avoid the
272 problem, without even using that variable. The "w=w" is just to
273 prevent a warning about it being unused.
274 */
275#if defined (__GNUC__) && __GNUC__ == 2 && __GNUC_MINOR__ == 95
276 volatile SCM workaround_for_gcc_295 = x;
277 workaround_for_gcc_295 = workaround_for_gcc_295;
278#endif
279
d5ad4aa6
MV
280 return SCM_I_CONSP (x);
281}
282
16ea9620 283#endif
16ea9620 284#endif