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