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