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