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