*** empty log message ***
[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
d2e53ed6 6/* Copyright (C) 2001, 2002, 2003, 2004 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
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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"
16ea9620 37
c8a1bdc4
HWN
38
39SCM_API SCM scm_cell (scm_t_bits car, scm_t_bits cdr);
40SCM_API SCM scm_double_cell (scm_t_bits car, scm_t_bits cbr,
41 scm_t_bits ccr, scm_t_bits cdr);
42
16ea9620 43
60e7529a
RB
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 . */
c8a1bdc4
HWN
50
51extern unsigned scm_newcell2_count;
52extern unsigned scm_newcell_count;
53
60e7529a
RB
54#if defined SCM_C_INLINE && ! defined SCM_INLINE_C_INCLUDING_INLINE_H
55/* definitely inlining */
2b2c6fca
MV
56#ifdef __GNUC__
57extern
58#else
59static
60#endif
61SCM_C_INLINE
60e7529a 62#endif
c8a1bdc4 63SCM
228a24ef 64scm_cell (scm_t_bits car, scm_t_bits cdr)
16ea9620
MV
65{
66 SCM z;
9bc4701c
MD
67 /* We retrieve the SCM pointer only once since the call to
68 SCM_FREELIST_LOC will be slightly expensive when we support
050fedc4 69 preemptive multithreading. SCM_FREELIST_LOC will then retrieve
9bc4701c
MD
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);
16ea9620 77
5e1e20c8
MV
78 if (scm_gc_running_p)
79 {
80 abort();
81 }
82
d2e53ed6 83 if (scm_is_null (*freelist))
9bc4701c 84 z = scm_gc_for_newcell (&scm_i_master_freelist, freelist);
c8a1bdc4 85 else
16ea9620 86 {
9bc4701c
MD
87 z = *freelist;
88 *freelist = SCM_FREE_CELL_CDR (*freelist);
16ea9620 89 }
c8a1bdc4
HWN
90
91 /*
92 We update scm_cells_allocated from this function. If we don't
93 update this explicitly, we will have to walk a freelist somewhere
94 later on, which seems a lot more expensive.
95 */
96 scm_cells_allocated += 1;
97
98#if (SCM_DEBUG_CELL_ACCESSES == 1)
99 if (scm_debug_cell_accesses_p)
1e71eafb
HWN
100 {
101 if (SCM_GC_MARK_P (z))
102 {
103 fprintf(stderr, "scm_cell tried to allocate a marked cell.\n");
104 abort();
105 }
6b69393d 106 else if (SCM_GC_CELL_WORD(z, 0) != scm_tc_free_cell)
1e71eafb
HWN
107 {
108 fprintf(stderr, "cell from freelist is not a free cell.\n");
109 abort();
110 }
111 }
112
113 /*
114 Always set mark. Otherwise cells that are alloced before
115 scm_debug_cell_accesses_p is toggled seem invalid.
116 */
117 SCM_SET_GC_MARK (z);
118
119 /*
120 TODO: figure out if this use of mark bits is valid with
121 threading. What if another thread is doing GC at this point
122 ... ?
123 */
c8a1bdc4 124
c8a1bdc4 125#endif
16ea9620 126
c8a1bdc4 127
16ea9620
MV
128 /* Initialize the type slot last so that the cell is ignored by the
129 GC until it is completely initialized. This is only relevant
130 when the GC can actually run during this code, which it can't for
131 cooperating threads, but it might be important when we get true
132 preemptive threads.
133 */
1fc8902f
DH
134 SCM_GC_SET_CELL_WORD (z, 1, cdr);
135 SCM_GC_SET_CELL_WORD (z, 0, car);
16ea9620 136
9bc4701c 137#if 0 /*fixme* Hmm... let's consider this later. */
60e7529a 138#if !defined(SCM_USE_COOP_THREADS) && !defined(SCM_USE_NULL_THREADS) && !defined(SCM_USE_COPT_THREADS)
389626c5 139 /* When we are using preemtive threads, we might need to make
16ea9620
MV
140 sure that the initial values for the slots are protected until
141 the cell is completely initialized.
142 */
143#error review me
144 scm_remember_upto_here_1 (SCM_PACK (cdr));
145#endif
9bc4701c 146#endif
c8a1bdc4 147
eab1b259
HWN
148#if (SCM_DEBUG_CELL_ACCESSES == 1)
149 if (scm_expensive_debug_cell_accesses_p )
150 scm_i_expensive_validation_check (z);
151#endif
c8a1bdc4 152
16ea9620
MV
153 return z;
154}
155
60e7529a
RB
156#if defined SCM_C_INLINE && ! defined SCM_INLINE_C_INCLUDING_INLINE_H
157/* definitely inlining */
2b2c6fca
MV
158#ifdef __GNUC__
159extern
160#else
161static
162#endif
163SCM_C_INLINE
60e7529a 164#endif
c8a1bdc4 165SCM
228a24ef
DH
166scm_double_cell (scm_t_bits car, scm_t_bits cbr,
167 scm_t_bits ccr, scm_t_bits cdr)
16ea9620 168{
5e1e20c8
MV
169 SCM z;
170 SCM *freelist = SCM_FREELIST_LOC (scm_i_freelist2);
171
497eb0b7
HWN
172 if (scm_gc_running_p)
173 {
174 abort();
175 }
176
d2e53ed6 177 if (scm_is_null (*freelist))
9bc4701c 178 z = scm_gc_for_newcell (&scm_i_master_freelist2, freelist);
16ea9620
MV
179 else
180 {
9bc4701c
MD
181 z = *freelist;
182 *freelist = SCM_FREE_CELL_CDR (*freelist);
16ea9620
MV
183 }
184
c8a1bdc4
HWN
185 scm_cells_allocated += 2;
186
16ea9620
MV
187 /* Initialize the type slot last so that the cell is ignored by the
188 GC until it is completely initialized. This is only relevant
189 when the GC can actually run during this code, which it can't for
190 cooperating threads, but it might be important when we get true
191 preemptive threads.
192 */
1fc8902f
DH
193 SCM_GC_SET_CELL_WORD (z, 1, cbr);
194 SCM_GC_SET_CELL_WORD (z, 2, ccr);
195 SCM_GC_SET_CELL_WORD (z, 3, cdr);
196 SCM_GC_SET_CELL_WORD (z, 0, car);
16ea9620 197
9bc4701c 198#if 0 /*fixme* Hmm... let's consider this later. */
60e7529a 199#if !defined(SCM_USE_COOP_THREADS) && !defined(SCM_USE_NULL_THREADS) && !defined(SCM_USE_COPT_THREADS)
16ea9620
MV
200 /* When we are using non-cooperating threads, we might need to make
201 sure that the initial values for the slots are protected until
202 the cell is completely initialized.
203 */
204#error review me
205 scm_remember_upto_here_3 (SCM_PACK (cbr), SCM_PACK (ccr), SCM_PACK (cdr));
206#endif
9bc4701c 207#endif
16ea9620 208
16ea9620 209
c8a1bdc4
HWN
210#if (SCM_DEBUG_CELL_ACCESSES == 1)
211 if (scm_debug_cell_accesses_p)
212 {
213 if (SCM_GC_MARK_P (z))
214 {
215 fprintf(stderr,
216 "scm_double_cell tried to allocate a marked cell.\n");
217 abort();
218 }
c8a1bdc4 219 }
4ad0814a
HWN
220
221 /* see above. */
222 SCM_SET_GC_MARK (z);
223
3553e1d1
GH
224#endif
225
226 /* When this function is inlined, it's possible that the last
227 SCM_GC_SET_CELL_WORD above will be adjacent to a following
228 initialization of z. E.g., it occurred in scm_make_real. GCC
229 from around version 3 (e.g., certainly 3.2) began taking
230 advantage of strict C aliasing rules which say that it's OK to
231 interchange the initialization above and the one below when the
232 pointer types appear to differ sufficiently. We don't want that,
233 of course. GCC allows this behaviour to be disabled with the
234 -fno-strict-aliasing option, but would also need to be supplied
235 by Guile users. Instead, the following statements prevent the
236 reordering.
237 */
238#ifdef __GNUC__
239 asm volatile ("" : : : "memory");
240#else
241 /* portable version, just in case any other compiler does the same
242 thing. */
243 scm_remember_upto_here_1 (z);
c8a1bdc4 244#endif
6253f3f1 245
c8a1bdc4
HWN
246 return z;
247}
6253f3f1 248
eab1b259
HWN
249
250
16ea9620 251#endif
16ea9620 252#endif