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_number(sword_t n
)
133 if (-0x20000000 < n
&& n
< 0x20000000)
134 return make_fixnum(n
);
136 ptr
= (struct bignum
*)alloc_unboxed(BIGNUM_WIDETAG
, 1);
140 return make_lispobj(ptr
, OTHER_POINTER_LOWTAG
);
145 alloc_code_object (unsigned boxed
, unsigned unboxed
) {
147 /* boxed is the number of constants, add other slots, align it to
148 * two words, so that the code start is aligned, and convert it to
151 (offsetof(struct code
, constants
) >>
152 WORD_SHIFT
)) << WORD_SHIFT
;
153 boxed
&= ~LOWTAG_MASK
;
155 /* Unboxed is the size of instructions in bytes. It will be stored
156 * as is in the code_size slot, but it needs to be allocated with
157 * double-word alignment. */
158 unsigned unboxed_aligned
= (unboxed
+ LOWTAG_MASK
) & ~LOWTAG_MASK
;
160 code
= (struct code
*)pa_alloc(boxed
+ unboxed_aligned
, CODE_PAGE_FLAG
);
162 /* It used to be that even on gencgc builds the
163 * ALLOCATE-CODE-OBJECT VOP did all this initialization within
164 * pseudo atomic. Here, we rely on gc being inhibited. */
165 if (SymbolValue(GC_INHIBIT
, arch_os_get_current_thread()) == NIL
)
166 lose("alloc_code_object called with GC enabled.");
167 boxed
= boxed
<< (N_WIDETAG_BITS
- WORD_SHIFT
);
168 code
->header
= boxed
| CODE_HEADER_WIDETAG
;
169 code
->code_size
= make_fixnum(unboxed
);
170 code
->entry_points
= NIL
;
171 code
->debug_info
= NIL
;
172 return make_lispobj(code
, OTHER_POINTER_LOWTAG
);