1.0.26.15: interrupt.c refactoring
[sbcl.git] / src / runtime / alloc.c
1 /*
2  * allocation routines for C code.  For allocation done by Lisp look
3  * instead at src/compiler/target/alloc.lisp and .../macros.lisp
4  */
5
6 /*
7  * This software is part of the SBCL system. See the README file for
8  * more information.
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <string.h>
19
20 #include "sbcl.h"
21 #include "runtime.h"
22 #include "os.h"
23 #include "alloc.h"
24 #include "globals.h"
25 #include "gc.h"
26 #include "thread.h"
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"
33
34 #define ALIGNED_SIZE(n) ((n) + LOWTAG_MASK) & ~LOWTAG_MASK
35
36 #ifdef LISP_FEATURE_GENCGC
37 static lispobj *
38 pa_alloc(int bytes, int page_type_flag)
39 {
40     lispobj *result;
41     struct thread *th = arch_os_get_current_thread();
42
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
46      * one... */
47     check_gc_signals_unblocked_or_lose(0);
48
49     /* FIXME: OOAO violation: see arch_pseudo_* */
50     set_pseudo_atomic_atomic(th);
51     result = general_alloc(bytes, page_type_flag);
52 #if 0
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);
60     }
61 #endif
62     clear_pseudo_atomic_atomic(th);
63
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
68          *
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"
76 #endif
77         *current_control_stack_pointer = (lispobj) result;
78         current_control_stack_pointer += 1;
79 #endif
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;
84 #endif
85     }
86     return result;
87 }
88 #else
89 static lispobj *
90 pa_alloc(int bytes, int page_type_flag)
91 {
92     lispobj *result;
93
94     /* This is not pseudo atomic at all, but is called only from
95      * interrupt safe places like interrupt handlers. MG -
96      * 2005-08-09 */
97     check_deferrables_blocked_or_lose(0);
98
99     result = dynamic_space_free_pointer;
100
101     /* Align up to next dual word boundary. */
102     bytes = ALIGNED_SIZE(bytes);
103
104     dynamic_space_free_pointer = (lispobj *)((char *)result + bytes);
105
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);
111     }
112     return result;
113 }
114 #endif
115
116 static lispobj *
117 alloc_unboxed(int type, int words)
118 {
119     lispobj *result;
120
121     result = pa_alloc(ALIGNED_SIZE((1 + words) * sizeof(lispobj)),
122                       UNBOXED_PAGE_FLAG);
123     *result = (lispobj) (words << N_WIDETAG_BITS) | type;
124     return result;
125 }
126
127 static lispobj
128 alloc_vector(int type, int length, int size, int page_type_flag)
129 {
130     struct vector *result;
131
132     result = (struct vector *)
133         pa_alloc(ALIGNED_SIZE((2 + (length*size + 31) / 32) * sizeof(lispobj)),
134                  page_type_flag);
135
136     result->header = type;
137     result->length = make_fixnum(length);
138
139     return make_lispobj(result,OTHER_POINTER_LOWTAG);
140 }
141
142 lispobj
143 alloc_cons(lispobj car, lispobj cdr)
144 {
145     struct cons *ptr =
146         (struct cons *)pa_alloc(ALIGNED_SIZE(sizeof(struct cons)),
147                                 BOXED_PAGE_FLAG);
148
149     ptr->car = car;
150     ptr->cdr = cdr;
151
152     return make_lispobj(ptr, LIST_POINTER_LOWTAG);
153 }
154
155 lispobj
156 alloc_number(long n)
157 {
158     struct bignum *ptr;
159
160     if (-0x20000000 < n && n < 0x20000000)
161         return make_fixnum(n);
162     else {
163         ptr = (struct bignum *)alloc_unboxed(BIGNUM_WIDETAG, 1);
164
165         ptr->digits[0] = n;
166
167         return make_lispobj(ptr, OTHER_POINTER_LOWTAG);
168     }
169 }
170
171 lispobj
172 alloc_base_string(char *str)
173 {
174     int len = strlen(str);
175     lispobj result = alloc_vector(SIMPLE_BASE_STRING_WIDETAG, len+1, 8,
176                                   UNBOXED_PAGE_FLAG);
177     struct vector *vec = (struct vector *)native_pointer(result);
178
179     vec->length = make_fixnum(len);
180     strcpy((char *)vec->data, str);
181
182     return result;
183 }
184
185 lispobj
186 alloc_sap(void *ptr)
187 {
188     struct sap *sap;
189     sap=(struct sap *)
190         alloc_unboxed((int)SAP_WIDETAG, sizeof(struct sap)/sizeof(lispobj) -1);
191     sap->pointer = ptr;
192     return make_lispobj(sap,OTHER_POINTER_LOWTAG);
193 }
194
195 lispobj
196 alloc_code_object (unsigned boxed, unsigned unboxed) {
197     struct code * code;
198     /* Coming in, boxed is the number of boxed words requested.
199      * Converting it to a fixnum makes it measured in bytes. It's also
200      * rounded up to double word along the way. */
201     boxed = make_fixnum(boxed + 1 +
202                         (offsetof(struct code, trace_table_offset) >>
203                          WORD_SHIFT));
204     boxed &= ~LOWTAG_MASK;
205
206     /* Unboxed is in bytes, round it up to double word boundary. Now
207      * it's also a fixnum containing the number of unboxed words. */
208     unboxed += LOWTAG_MASK;
209     unboxed &= ~LOWTAG_MASK;
210
211     code = (struct code *)pa_alloc(boxed + unboxed, CODE_PAGE_FLAG);
212
213     /* It used to be that even on gencgc builds the
214      * ALLOCATE-CODE-OBJECT VOP did all this initialization within
215      * pseudo atomic. Here, we rely on gc being inhibited. */
216     if (SymbolValue(GC_INHIBIT, arch_os_get_current_thread()) == NIL)
217         lose("alloc_code_object called with GC enabled.");
218     boxed = boxed << (N_WIDETAG_BITS - WORD_SHIFT);
219     code->header = boxed | CODE_HEADER_WIDETAG;
220     code->code_size = unboxed;
221     code->entry_points = NIL;
222     code->debug_info = NIL;
223     return make_lispobj(code, OTHER_POINTER_LOWTAG);
224 }