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