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 needs to be enabled before we can call lisp:
44 * otherwise two threads racing here may deadlock: the other will
45 * wait on the GC lock, and the other cannot stop the first
47 check_gc_signals_unblocked_or_lose();
49 /* FIXME: OOAO violation: see arch_pseudo_* */
50 set_pseudo_atomic_atomic(th);
51 result = general_alloc(bytes, page_type_flag);
53 /* See how the runtime deals with GC being triggerred. */
54 if ((SymbolValue(GC_PENDING,th) == NIL) &&
55 (SymbolValue(GC_INHIBIT,th) == NIL) &&
56 (random() < RAND_MAX/100)) {
57 SetSymbolValue(GC_PENDING,T,th);
58 set_pseudo_atomic_interrupted(th);
59 maybe_save_gc_mask_and_block_deferrables(NULL);
62 clear_pseudo_atomic_atomic(th);
64 if (get_pseudo_atomic_interrupted(th)) {
65 /* WARNING KLUDGE FIXME: pa_alloc() is not pseudo-atomic on
66 * anything but x86[-64]. maybe_defer_handler doesn't defer
67 * interrupts if foreign_function_call_active
69 * If the C stack is not scavenged during GC, result needs to
70 * be protected against not being referred to by any roots, so
71 * we push it onto the lisp control stack, and read it back
72 * off after any potential GC has finished */
73 #ifndef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
74 #ifdef LISP_FEATURE_STACK_GROWS_DOWNWARD_NOT_UPWARD
75 #error "!C_STACK_IS_CONTROL_STACK and STACK_GROWS_DOWNWARD_NOT_UPWARD is not supported"
77 *current_control_stack_pointer = (lispobj) result;
78 current_control_stack_pointer += 1;
80 do_pending_interrupt();
81 #ifndef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
82 current_control_stack_pointer -= 1;
83 result = (lispobj *) *current_control_stack_pointer;
90 pa_alloc(int bytes, int page_type_flag)
94 /* This is not pseudo atomic at all, but is called only from
95 * interrupt safe places like interrupt handlers. MG -
97 check_deferrables_blocked_or_lose();
99 result = dynamic_space_free_pointer;
101 /* Align up to next dual word boundary. */
102 bytes = ALIGNED_SIZE(bytes);
104 dynamic_space_free_pointer = (lispobj *)((char *)result + bytes);
106 if (current_auto_gc_trigger
107 && dynamic_space_free_pointer > current_auto_gc_trigger) {
108 clear_auto_gc_trigger();
109 set_auto_gc_trigger((char *)dynamic_space_free_pointer
110 - (char *)current_dynamic_space);
117 alloc_unboxed(int type, int words)
121 result = pa_alloc(ALIGNED_SIZE((1 + words) * sizeof(lispobj)),
123 *result = (lispobj) (words << N_WIDETAG_BITS) | type;
128 alloc_vector(int type, int length, int size, int page_type_flag)
130 struct vector *result;
132 result = (struct vector *)
133 pa_alloc(ALIGNED_SIZE((2 + (length*size + 31) / 32) * sizeof(lispobj)),
136 result->header = type;
137 result->length = make_fixnum(length);
139 return make_lispobj(result,OTHER_POINTER_LOWTAG);
143 alloc_cons(lispobj car, lispobj cdr)
146 (struct cons *)pa_alloc(ALIGNED_SIZE(sizeof(struct cons)),
152 return make_lispobj(ptr, LIST_POINTER_LOWTAG);
160 if (-0x20000000 < n && n < 0x20000000)
161 return make_fixnum(n);
163 ptr = (struct bignum *)alloc_unboxed(BIGNUM_WIDETAG, 1);
167 return make_lispobj(ptr, OTHER_POINTER_LOWTAG);
172 alloc_base_string(char *str)
174 int len = strlen(str);
175 lispobj result = alloc_vector(SIMPLE_BASE_STRING_WIDETAG, len+1, 8,
177 struct vector *vec = (struct vector *)native_pointer(result);
179 vec->length = make_fixnum(len);
180 strcpy((char *)vec->data, str);
190 alloc_unboxed((int)SAP_WIDETAG, sizeof(struct sap)/sizeof(lispobj) -1);
192 return make_lispobj(sap,OTHER_POINTER_LOWTAG);
196 alloc_code_object (unsigned boxed, unsigned unboxed) {
198 /* 4 == trace_table_offset offset in words */
199 boxed = make_fixnum(boxed + 1 + 4);
200 boxed &= ~LOWTAG_MASK;
202 unboxed += LOWTAG_MASK;
203 unboxed &= ~LOWTAG_MASK;
205 code = (struct code *)pa_alloc(ALIGNED_SIZE((boxed + unboxed) *
209 boxed = boxed << (N_WIDETAG_BITS - WORD_SHIFT);
210 code->header = boxed | CODE_HEADER_WIDETAG;
211 code->code_size = unboxed;
212 code->entry_points = NIL;
213 code->debug_info = NIL;
214 return make_lispobj(code, OTHER_POINTER_LOWTAG);