2 * allocation routines for C code. For allocation done by Lisp look
3 * instead at src/compiler/target/alloc.lisp and .../macros.lisp
7 * This software is part of the SBCL system. See the README file for
10 * This software is derived from the CMU CL system, which was
11 * written at Carnegie Mellon University and released into the
12 * public domain. The software is in the public domain and is
13 * provided with absolutely no warranty. See the COPYING and CREDITS
14 * files for more information.
27 #include "pseudo-atomic.h"
28 #include "genesis/vector.h"
29 #include "genesis/cons.h"
30 #include "genesis/bignum.h"
31 #include "genesis/sap.h"
32 #include "genesis/code.h"
34 #define ALIGNED_SIZE(n) ((n) + LOWTAG_MASK) & ~LOWTAG_MASK
36 #ifdef LISP_FEATURE_GENCGC
38 pa_alloc(int bytes
, int page_type_flag
)
41 struct thread
*th
= arch_os_get_current_thread();
43 /* SIG_STOP_FOR_GC must be unblocked: else two threads racing here
44 * may deadlock: one will wait on the GC lock, and the other
45 * cannot stop the first one... */
46 check_gc_signals_unblocked_or_lose(0);
48 /* FIXME: OOAO violation: see arch_pseudo_* */
49 set_pseudo_atomic_atomic(th
);
50 result
= general_alloc(bytes
, page_type_flag
);
52 /* See how the runtime deals with GC being triggerred. */
53 if ((SymbolValue(GC_PENDING
,th
) == NIL
) &&
54 (SymbolValue(GC_INHIBIT
,th
) == NIL
) &&
55 (random() < RAND_MAX
/100)) {
56 SetSymbolValue(GC_PENDING
,T
,th
);
57 set_pseudo_atomic_interrupted(th
);
58 maybe_save_gc_mask_and_block_deferrables(NULL
);
61 clear_pseudo_atomic_atomic(th
);
63 if (get_pseudo_atomic_interrupted(th
)) {
64 /* WARNING KLUDGE FIXME: pa_alloc() is not pseudo-atomic on
65 * anything but x86[-64]. maybe_defer_handler doesn't defer
66 * interrupts if foreign_function_call_active
68 * If the C stack is not scavenged during GC, result needs to
69 * be protected against not being referred to by any roots, so
70 * we push it onto the lisp control stack, and read it back
71 * off after any potential GC has finished */
72 #ifndef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
73 #ifdef LISP_FEATURE_STACK_GROWS_DOWNWARD_NOT_UPWARD
74 #error "!C_STACK_IS_CONTROL_STACK and STACK_GROWS_DOWNWARD_NOT_UPWARD is not supported"
76 *current_control_stack_pointer
= (lispobj
) result
;
77 current_control_stack_pointer
+= 1;
79 do_pending_interrupt();
80 #ifndef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
81 current_control_stack_pointer
-= 1;
82 result
= (lispobj
*) *current_control_stack_pointer
;
89 pa_alloc(int bytes
, int page_type_flag
)
93 /* This is not pseudo atomic at all, but is called only from
94 * interrupt safe places like interrupt handlers. MG -
96 check_deferrables_blocked_or_lose(0);
98 result
= dynamic_space_free_pointer
;
100 /* Align up to next dual word boundary. */
101 bytes
= ALIGNED_SIZE(bytes
);
103 dynamic_space_free_pointer
= (lispobj
*)((char *)result
+ bytes
);
105 if (current_auto_gc_trigger
106 && dynamic_space_free_pointer
> current_auto_gc_trigger
) {
107 clear_auto_gc_trigger();
108 set_auto_gc_trigger((char *)dynamic_space_free_pointer
109 - (char *)current_dynamic_space
);
116 alloc_unboxed(int type
, int words
)
120 result
= pa_alloc(ALIGNED_SIZE((1 + words
) * sizeof(lispobj
)),
122 *result
= (lispobj
) (words
<< N_WIDETAG_BITS
) | type
;
127 alloc_vector(int type
, int length
, int size
, int page_type_flag
)
129 struct vector
*result
;
131 result
= (struct vector
*)
132 pa_alloc(ALIGNED_SIZE((2 + (length
*size
+ 31) / 32) * sizeof(lispobj
)),
135 result
->header
= type
;
136 result
->length
= make_fixnum(length
);
138 return make_lispobj(result
,OTHER_POINTER_LOWTAG
);
142 alloc_cons(lispobj car
, lispobj cdr
)
145 (struct cons
*)pa_alloc(ALIGNED_SIZE(sizeof(struct cons
)),
151 return make_lispobj(ptr
, LIST_POINTER_LOWTAG
);
159 if (-0x20000000 < n
&& n
< 0x20000000)
160 return make_fixnum(n
);
162 ptr
= (struct bignum
*)alloc_unboxed(BIGNUM_WIDETAG
, 1);
166 return make_lispobj(ptr
, OTHER_POINTER_LOWTAG
);
171 alloc_base_string(char *str
)
173 int len
= strlen(str
);
174 lispobj result
= alloc_vector(SIMPLE_BASE_STRING_WIDETAG
, len
+1, 8,
176 struct vector
*vec
= (struct vector
*)native_pointer(result
);
178 vec
->length
= make_fixnum(len
);
179 strcpy((char *)vec
->data
, str
);
189 alloc_unboxed((int)SAP_WIDETAG
, sizeof(struct sap
)/sizeof(lispobj
) -1);
191 return make_lispobj(sap
,OTHER_POINTER_LOWTAG
);
195 alloc_code_object (unsigned boxed
, unsigned unboxed
) {
197 /* Coming in, boxed is the number of boxed words requested.
198 * Converting it to a fixnum makes it measured in bytes. It's also
199 * rounded up to double word along the way. */
200 boxed
= make_fixnum(boxed
+ 1 +
201 (offsetof(struct code
, trace_table_offset
) >>
203 boxed
&= ~LOWTAG_MASK
;
205 /* Unboxed is in bytes, round it up to double word boundary. Now
206 * it's also a fixnum containing the number of unboxed words. */
207 unboxed
+= LOWTAG_MASK
;
208 unboxed
&= ~LOWTAG_MASK
;
210 code
= (struct code
*)pa_alloc(boxed
+ unboxed
, CODE_PAGE_FLAG
);
212 /* It used to be that even on gencgc builds the
213 * ALLOCATE-CODE-OBJECT VOP did all this initialization within
214 * pseudo atomic. Here, we rely on gc being inhibited. */
215 if (SymbolValue(GC_INHIBIT
, arch_os_get_current_thread()) == NIL
)
216 lose("alloc_code_object called with GC enabled.");
217 boxed
= boxed
<< (N_WIDETAG_BITS
- WORD_SHIFT
);
218 code
->header
= boxed
| CODE_HEADER_WIDETAG
;
219 code
->code_size
= unboxed
;
220 code
->entry_points
= NIL
;
221 code
->debug_info
= NIL
;
222 return make_lispobj(code
, OTHER_POINTER_LOWTAG
);