(scm_cell, scm_double_cell): Do not check scm_gc_running_p, allocation
[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, 2004 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 #include "libguile/unif.h"
38
39
40 SCM_API SCM scm_cell (scm_t_bits car, scm_t_bits cdr);
41 SCM_API SCM scm_double_cell (scm_t_bits car, scm_t_bits cbr,
42 scm_t_bits ccr, scm_t_bits cdr);
43
44 SCM_API SCM scm_array_handle_ref (scm_t_array_handle *h, ssize_t pos);
45 SCM_API void scm_array_handle_set (scm_t_array_handle *h, ssize_t pos, SCM val);
46
47
48 #if defined SCM_C_INLINE || defined SCM_INLINE_C_INCLUDING_INLINE_H
49 /* either inlining, or being included from inline.c. We use (and
50 repeat) this long #if test here and below so that we don't have to
51 introduce any extraneous symbols into the public namespace. We
52 only need SCM_C_INLINE to be seen publically . */
53
54 extern unsigned scm_newcell2_count;
55 extern unsigned scm_newcell_count;
56
57 #if defined SCM_C_INLINE && ! defined SCM_INLINE_C_INCLUDING_INLINE_H
58 /* definitely inlining */
59 #ifdef __GNUC__
60 extern
61 #else
62 static
63 #endif
64 SCM_C_INLINE
65 #endif
66 SCM
67 scm_cell (scm_t_bits car, scm_t_bits cdr)
68 {
69 SCM z;
70 SCM *freelist = SCM_FREELIST_LOC (scm_i_freelist);
71
72 if (scm_is_null (*freelist))
73 z = scm_gc_for_newcell (&scm_i_master_freelist, freelist);
74 else
75 {
76 z = *freelist;
77 *freelist = SCM_FREE_CELL_CDR (*freelist);
78 }
79
80 /*
81 We update scm_cells_allocated from this function. If we don't
82 update this explicitly, we will have to walk a freelist somewhere
83 later on, which seems a lot more expensive.
84 */
85 scm_cells_allocated += 1;
86
87 #if (SCM_DEBUG_CELL_ACCESSES == 1)
88 if (scm_debug_cell_accesses_p)
89 {
90 if (SCM_GC_MARK_P (z))
91 {
92 fprintf(stderr, "scm_cell tried to allocate a marked cell.\n");
93 abort();
94 }
95 else if (SCM_GC_CELL_WORD(z, 0) != scm_tc_free_cell)
96 {
97 fprintf(stderr, "cell from freelist is not a free cell.\n");
98 abort();
99 }
100 }
101
102 /*
103 Always set mark. Otherwise cells that are alloced before
104 scm_debug_cell_accesses_p is toggled seem invalid.
105 */
106 SCM_SET_GC_MARK (z);
107
108 /*
109 TODO: figure out if this use of mark bits is valid with
110 threading. What if another thread is doing GC at this point
111 ... ?
112 */
113
114 #endif
115
116
117 /* Initialize the type slot last so that the cell is ignored by the
118 GC until it is completely initialized. This is only relevant
119 when the GC can actually run during this code, which it can't
120 since the GC only runs when all other threads are stopped.
121 */
122 SCM_GC_SET_CELL_WORD (z, 1, cdr);
123 SCM_GC_SET_CELL_WORD (z, 0, car);
124
125 #if (SCM_DEBUG_CELL_ACCESSES == 1)
126 if (scm_expensive_debug_cell_accesses_p )
127 scm_i_expensive_validation_check (z);
128 #endif
129
130 return z;
131 }
132
133 #if defined SCM_C_INLINE && ! defined SCM_INLINE_C_INCLUDING_INLINE_H
134 /* definitely inlining */
135 #ifdef __GNUC__
136 extern
137 #else
138 static
139 #endif
140 SCM_C_INLINE
141 #endif
142 SCM
143 scm_double_cell (scm_t_bits car, scm_t_bits cbr,
144 scm_t_bits ccr, scm_t_bits cdr)
145 {
146 SCM z;
147 SCM *freelist = SCM_FREELIST_LOC (scm_i_freelist2);
148
149 if (scm_is_null (*freelist))
150 z = scm_gc_for_newcell (&scm_i_master_freelist2, freelist);
151 else
152 {
153 z = *freelist;
154 *freelist = SCM_FREE_CELL_CDR (*freelist);
155 }
156
157 scm_cells_allocated += 2;
158
159 /* Initialize the type slot last so that the cell is ignored by the
160 GC until it is completely initialized. This is only relevant
161 when the GC can actually run during this code, which it can't
162 since the GC only runs when all other threads are stopped.
163 */
164 SCM_GC_SET_CELL_WORD (z, 1, cbr);
165 SCM_GC_SET_CELL_WORD (z, 2, ccr);
166 SCM_GC_SET_CELL_WORD (z, 3, cdr);
167 SCM_GC_SET_CELL_WORD (z, 0, car);
168
169 #if (SCM_DEBUG_CELL_ACCESSES == 1)
170 if (scm_debug_cell_accesses_p)
171 {
172 if (SCM_GC_MARK_P (z))
173 {
174 fprintf(stderr,
175 "scm_double_cell tried to allocate a marked cell.\n");
176 abort();
177 }
178 }
179
180 /* see above. */
181 SCM_SET_GC_MARK (z);
182
183 #endif
184
185 /* When this function is inlined, it's possible that the last
186 SCM_GC_SET_CELL_WORD above will be adjacent to a following
187 initialization of z. E.g., it occurred in scm_make_real. GCC
188 from around version 3 (e.g., certainly 3.2) began taking
189 advantage of strict C aliasing rules which say that it's OK to
190 interchange the initialization above and the one below when the
191 pointer types appear to differ sufficiently. We don't want that,
192 of course. GCC allows this behaviour to be disabled with the
193 -fno-strict-aliasing option, but would also need to be supplied
194 by Guile users. Instead, the following statements prevent the
195 reordering.
196 */
197 #ifdef __GNUC__
198 asm volatile ("" : : : "memory");
199 #else
200 /* portable version, just in case any other compiler does the same
201 thing. */
202 scm_remember_upto_here_1 (z);
203 #endif
204
205 return z;
206 }
207
208 #if defined SCM_C_INLINE && ! defined SCM_INLINE_C_INCLUDING_INLINE_H
209 /* definitely inlining */
210 #ifdef __GNUC__
211 extern
212 #else
213 static
214 #endif
215 SCM_C_INLINE
216 #endif
217 SCM
218 scm_array_handle_ref (scm_t_array_handle *h, ssize_t p)
219 {
220 return h->ref (h, p);
221 }
222
223 #if defined SCM_C_INLINE && ! defined SCM_INLINE_C_INCLUDING_INLINE_H
224 /* definitely inlining */
225 #ifdef __GNUC__
226 extern
227 #else
228 static
229 #endif
230 SCM_C_INLINE
231 #endif
232 void
233 scm_array_handle_set (scm_t_array_handle *h, ssize_t p, SCM v)
234 {
235 h->set (h, p, v);
236 }
237
238 #endif
239 #endif