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 #ifndef LISP_FEATURE_SB_SAFEPOINT
44 /* SIG_STOP_FOR_GC must be unblocked: else two threads racing here
45 * may deadlock: one will wait on the GC lock, and the other
46 * cannot stop the first one... */
47 check_gc_signals_unblocked_or_lose(0);
50 /* FIXME: OOAO violation: see arch_pseudo_* */
51 set_pseudo_atomic_atomic(th
);
52 result
= general_alloc(bytes
, page_type_flag
);
54 /* See how the runtime deals with GC being triggerred. */
55 if ((SymbolValue(GC_PENDING
,th
) == NIL
) &&
56 (SymbolValue(GC_INHIBIT
,th
) == NIL
) &&
57 (random() < RAND_MAX
/100)) {
58 SetSymbolValue(GC_PENDING
,T
,th
);
59 set_pseudo_atomic_interrupted(th
);
60 maybe_save_gc_mask_and_block_deferrables(NULL
);
63 clear_pseudo_atomic_atomic(th
);
65 if (get_pseudo_atomic_interrupted(th
)) {
66 /* WARNING KLUDGE FIXME: pa_alloc() is not pseudo-atomic on
67 * anything but x86[-64]. maybe_defer_handler doesn't defer
68 * interrupts if foreign_function_call_active
70 * If the C stack is not scavenged during GC, result needs to
71 * be protected against not being referred to by any roots, so
72 * we push it onto the lisp control stack, and read it back
73 * off after any potential GC has finished */
74 #ifndef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
75 #ifdef LISP_FEATURE_STACK_GROWS_DOWNWARD_NOT_UPWARD
76 #error "!C_STACK_IS_CONTROL_STACK and STACK_GROWS_DOWNWARD_NOT_UPWARD is not supported"
78 *access_control_stack_pointer(th
) = (lispobj
) result
;
79 access_control_stack_pointer(th
) += 1;
81 do_pending_interrupt();
82 #ifndef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
83 access_control_stack_pointer(th
) -= 1;
84 result
= (lispobj
*) *access_control_stack_pointer(th
);
91 pa_alloc(int bytes
, int page_type_flag
)
95 /* This is not pseudo atomic at all, but is called only from
96 * interrupt safe places like interrupt handlers. MG -
98 check_deferrables_blocked_or_lose(0);
100 result
= dynamic_space_free_pointer
;
102 /* Align up to next dual word boundary. */
103 bytes
= ALIGNED_SIZE(bytes
);
105 dynamic_space_free_pointer
= (lispobj
*)((char *)result
+ bytes
);
107 if (current_auto_gc_trigger
108 && dynamic_space_free_pointer
> current_auto_gc_trigger
) {
109 clear_auto_gc_trigger();
110 set_auto_gc_trigger((char *)dynamic_space_free_pointer
111 - (char *)current_dynamic_space
);
118 alloc_unboxed(int type
, int words
)
122 result
= pa_alloc(ALIGNED_SIZE((1 + words
) * sizeof(lispobj
)),
124 *result
= (lispobj
) (words
<< N_WIDETAG_BITS
) | type
;
129 alloc_vector(int type
, int length
, int size
, int page_type_flag
)
131 struct vector
*result
;
133 result
= (struct vector
*)
134 pa_alloc(ALIGNED_SIZE((2 + (length
*size
+ 31) / 32) * sizeof(lispobj
)),
137 result
->header
= type
;
138 result
->length
= make_fixnum(length
);
140 return make_lispobj(result
,OTHER_POINTER_LOWTAG
);
144 alloc_cons(lispobj car
, lispobj cdr
)
147 (struct cons
*)pa_alloc(ALIGNED_SIZE(sizeof(struct cons
)),
153 return make_lispobj(ptr
, LIST_POINTER_LOWTAG
);
157 alloc_number(sword_t n
)
161 if (-0x20000000 < n
&& n
< 0x20000000)
162 return make_fixnum(n
);
164 ptr
= (struct bignum
*)alloc_unboxed(BIGNUM_WIDETAG
, 1);
168 return make_lispobj(ptr
, OTHER_POINTER_LOWTAG
);
173 alloc_base_string(char *str
)
175 int len
= strlen(str
);
176 lispobj result
= alloc_vector(SIMPLE_BASE_STRING_WIDETAG
, len
+1, 8,
178 struct vector
*vec
= (struct vector
*)native_pointer(result
);
180 vec
->length
= make_fixnum(len
);
181 strcpy((char *)vec
->data
, str
);
191 alloc_unboxed((int)SAP_WIDETAG
, sizeof(struct sap
)/sizeof(lispobj
) -1);
193 return make_lispobj(sap
,OTHER_POINTER_LOWTAG
);
197 alloc_code_object (unsigned boxed
, unsigned unboxed
) {
199 /* boxed is the number of constants, add other slots, align it to
200 * two words, so that the code start is aligned, and convert it to
203 (offsetof(struct code
, constants
) >>
204 WORD_SHIFT
)) << WORD_SHIFT
;
205 boxed
&= ~LOWTAG_MASK
;
207 /* Unboxed is the size of instructions in bytes. It will be stored
208 * as is in the code_size slot, but it needs to be allocated with
209 * double-word alignment. */
210 unsigned unboxed_aligned
= (unboxed
+ LOWTAG_MASK
) & ~LOWTAG_MASK
;
212 code
= (struct code
*)pa_alloc(boxed
+ unboxed_aligned
, CODE_PAGE_FLAG
);
214 /* It used to be that even on gencgc builds the
215 * ALLOCATE-CODE-OBJECT VOP did all this initialization within
216 * pseudo atomic. Here, we rely on gc being inhibited. */
217 if (SymbolValue(GC_INHIBIT
, arch_os_get_current_thread()) == NIL
)
218 lose("alloc_code_object called with GC enabled.");
219 boxed
= boxed
<< (N_WIDETAG_BITS
- WORD_SHIFT
);
220 code
->header
= boxed
| CODE_HEADER_WIDETAG
;
221 code
->code_size
= make_fixnum(unboxed
);
222 code
->entry_points
= NIL
;
223 code
->debug_info
= NIL
;
224 return make_lispobj(code
, OTHER_POINTER_LOWTAG
);