1.0.25.24: x86/x86-64 runtime pseudo atomic fixes
[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     /* FIXME: OOAO violation: see arch_pseudo_* */
44     clear_pseudo_atomic_interrupted(th);
45     set_pseudo_atomic_atomic(th);
46     result = general_alloc(bytes, page_type_flag);
47 #if 0
48     /* See how the runtime deals with GC being triggerred. */
49     if ((SymbolValue(GC_PENDING,th) == NIL) &&
50         (SymbolValue(GC_INHIBIT,th) == NIL) &&
51         (random() < RAND_MAX/100)) {
52         SetSymbolValue(GC_PENDING,T,th);
53         set_pseudo_atomic_interrupted(th);
54         maybe_save_gc_mask_and_block_deferrables(NULL);
55     }
56 #endif
57     clear_pseudo_atomic_atomic(th);
58
59     if (get_pseudo_atomic_interrupted(th)) {
60         /* WARNING KLUDGE FIXME: pa_alloc() is not pseudo-atomic on
61          * anything but x86[-64]. maybe_defer_handler doesn't defer
62          * interrupts if foreign_function_call_active
63          *
64          * If the C stack is not scavenged during GC, result needs to
65          * be protected against not being referred to by any roots, so
66          * we push it onto the lisp control stack, and read it back
67          * off after any potential GC has finished */
68 #ifndef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
69 #ifdef LISP_FEATURE_STACK_GROWS_DOWNWARD_NOT_UPWARD
70 #error "!C_STACK_IS_CONTROL_STACK and STACK_GROWS_DOWNWARD_NOT_UPWARD is not supported"
71 #endif
72         *current_control_stack_pointer = (lispobj) result;
73         current_control_stack_pointer += 1;
74 #endif
75         do_pending_interrupt();
76 #ifndef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
77         current_control_stack_pointer -= 1;
78         result = (lispobj *) *current_control_stack_pointer;
79 #endif
80     }
81     return result;
82 }
83 #else
84 static lispobj *
85 pa_alloc(int bytes, int page_type_flag)
86 {
87     lispobj *result;
88
89     /* This is not pseudo atomic at all, but is called only from
90      * interrupt safe places like interrupt handlers. MG -
91      * 2005-08-09 */
92     check_deferrables_blocked_or_lose();
93
94     result = dynamic_space_free_pointer;
95
96     /* Align up to next dual word boundary. */
97     bytes = ALIGNED_SIZE(bytes);
98
99     dynamic_space_free_pointer = (lispobj *)((char *)result + bytes);
100
101     if (current_auto_gc_trigger
102         && dynamic_space_free_pointer > current_auto_gc_trigger) {
103         clear_auto_gc_trigger();
104         set_auto_gc_trigger((char *)dynamic_space_free_pointer
105                             - (char *)current_dynamic_space);
106     }
107     return result;
108 }
109 #endif
110
111 static lispobj *
112 alloc_unboxed(int type, int words)
113 {
114     lispobj *result;
115
116     result = pa_alloc(ALIGNED_SIZE((1 + words) * sizeof(lispobj)),
117                       UNBOXED_PAGE_FLAG);
118     *result = (lispobj) (words << N_WIDETAG_BITS) | type;
119     return result;
120 }
121
122 static lispobj
123 alloc_vector(int type, int length, int size, int page_type_flag)
124 {
125     struct vector *result;
126
127     result = (struct vector *)
128         pa_alloc(ALIGNED_SIZE((2 + (length*size + 31) / 32) * sizeof(lispobj)),
129                  page_type_flag);
130
131     result->header = type;
132     result->length = make_fixnum(length);
133
134     return make_lispobj(result,OTHER_POINTER_LOWTAG);
135 }
136
137 lispobj
138 alloc_cons(lispobj car, lispobj cdr)
139 {
140     struct cons *ptr =
141         (struct cons *)pa_alloc(ALIGNED_SIZE(sizeof(struct cons)),
142                                 BOXED_PAGE_FLAG);
143
144     ptr->car = car;
145     ptr->cdr = cdr;
146
147     return make_lispobj(ptr, LIST_POINTER_LOWTAG);
148 }
149
150 lispobj
151 alloc_number(long n)
152 {
153     struct bignum *ptr;
154
155     if (-0x20000000 < n && n < 0x20000000)
156         return make_fixnum(n);
157     else {
158         ptr = (struct bignum *)alloc_unboxed(BIGNUM_WIDETAG, 1);
159
160         ptr->digits[0] = n;
161
162         return make_lispobj(ptr, OTHER_POINTER_LOWTAG);
163     }
164 }
165
166 lispobj
167 alloc_base_string(char *str)
168 {
169     int len = strlen(str);
170     lispobj result = alloc_vector(SIMPLE_BASE_STRING_WIDETAG, len+1, 8,
171                                   UNBOXED_PAGE_FLAG);
172     struct vector *vec = (struct vector *)native_pointer(result);
173
174     vec->length = make_fixnum(len);
175     strcpy((char *)vec->data, str);
176
177     return result;
178 }
179
180 lispobj
181 alloc_sap(void *ptr)
182 {
183     struct sap *sap;
184     sap=(struct sap *)
185         alloc_unboxed((int)SAP_WIDETAG, sizeof(struct sap)/sizeof(lispobj) -1);
186     sap->pointer = ptr;
187     return make_lispobj(sap,OTHER_POINTER_LOWTAG);
188 }
189
190 lispobj
191 alloc_code_object (unsigned boxed, unsigned unboxed) {
192     struct code * code;
193     /* 4 == trace_table_offset offset in words */
194     boxed = make_fixnum(boxed + 1 + 4);
195     boxed &= ~LOWTAG_MASK;
196
197     unboxed += LOWTAG_MASK;
198     unboxed &= ~LOWTAG_MASK;
199
200     code = (struct code *)pa_alloc(ALIGNED_SIZE((boxed + unboxed) *
201                                                 sizeof(lispobj)),
202                                     CODE_PAGE_FLAG);
203
204     boxed = boxed << (N_WIDETAG_BITS - WORD_SHIFT);
205     code->header = boxed | CODE_HEADER_WIDETAG;
206     code->code_size = unboxed;
207     code->entry_points = NIL;
208     code->debug_info = NIL;
209     return make_lispobj(code, OTHER_POINTER_LOWTAG);
210 }