0.pre8.28
[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 "runtime.h"
21 #include "os.h"
22 #include "sbcl.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 GET_FREE_POINTER() dynamic_space_free_pointer
33 #define SET_FREE_POINTER(new_value) \
34     (dynamic_space_free_pointer = (new_value))
35 #define GET_GC_TRIGGER() current_auto_gc_trigger
36 #define SET_GC_TRIGGER(new_value) \
37     clear_auto_gc_trigger(); set_auto_gc_trigger(new_value);
38
39 #define ALIGNED_SIZE(n) (n+LOWTAG_MASK) & ~LOWTAG_MASK
40
41 #if defined LISP_FEATURE_GENCGC
42 extern lispobj *alloc(int bytes);
43 lispobj *
44 pa_alloc(int bytes) 
45 {
46     lispobj *result=0;
47     struct thread *th=arch_os_get_current_thread();
48     SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED, make_fixnum(0),th);
49     SetSymbolValue(PSEUDO_ATOMIC_ATOMIC, make_fixnum(1),th);
50     result=alloc(bytes);
51     SetSymbolValue(PSEUDO_ATOMIC_ATOMIC, make_fixnum(0),th);
52     if (SymbolValue(PSEUDO_ATOMIC_INTERRUPTED,th)) 
53         /* even if we gc at this point, the new allocation will be
54          * protected from being moved, because result is on the c stack
55          * and points to it */
56         do_pending_interrupt(); 
57     return result; 
58 }
59
60 #else
61 static lispobj *
62 pa_alloc(int bytes)
63 {
64     char *result;
65
66     /* Round to dual word boundary. */
67     bytes = (bytes + LOWTAG_MASK) & ~LOWTAG_MASK;
68
69     result = (char *)GET_FREE_POINTER();
70
71     SET_FREE_POINTER((lispobj *)(result + bytes));
72
73     if (GET_GC_TRIGGER() && GET_FREE_POINTER() > GET_GC_TRIGGER()) {
74         SET_GC_TRIGGER((char *)GET_FREE_POINTER()
75                        - (char *)current_dynamic_space);
76     }
77     return (lispobj *) result;
78 }
79 #endif
80
81
82 lispobj *
83 alloc_unboxed(int type, int words)
84 {
85     lispobj *result;
86
87     result = pa_alloc(ALIGNED_SIZE((1 + words) * sizeof(lispobj)));
88     *result = (lispobj) (words << N_WIDETAG_BITS) | type;
89     return result;
90 }
91
92 static lispobj
93 alloc_vector(int type, int length, int size)
94 {
95     struct vector *result;
96
97     result = (struct vector *)
98       pa_alloc(ALIGNED_SIZE((2 + (length*size + 31) / 32) * sizeof(lispobj)));
99
100     result->header = type;
101     result->length = make_fixnum(length);
102
103     return make_lispobj(result,OTHER_POINTER_LOWTAG);
104 }
105
106 lispobj
107 alloc_cons(lispobj car, lispobj cdr)
108 {
109     struct cons *ptr = (struct cons *)pa_alloc(ALIGNED_SIZE(sizeof(struct cons)));
110
111     ptr->car = car;
112     ptr->cdr = cdr;
113
114     return make_lispobj(ptr, LIST_POINTER_LOWTAG);
115 }
116
117 lispobj
118 alloc_number(long n)
119 {
120     struct bignum *ptr;
121
122     if (-0x20000000 < n && n < 0x20000000)
123         return make_fixnum(n);
124     else {
125         ptr = (struct bignum *)alloc_unboxed(BIGNUM_WIDETAG, 1);
126
127         ptr->digits[0] = n;
128
129         return make_lispobj(ptr, OTHER_POINTER_LOWTAG);
130     }
131 }
132
133 lispobj
134 alloc_string(char *str)
135 {
136     int len = strlen(str);
137     lispobj result = alloc_vector(SIMPLE_STRING_WIDETAG, len+1, 8);
138     struct vector *vec = (struct vector *)native_pointer(result);
139
140     vec->length = make_fixnum(len);
141     strcpy((char *)vec->data, str);
142
143     return result;
144 }
145
146 lispobj
147 alloc_sap(void *ptr)
148 {
149     struct sap *sap;
150     sap=(struct sap *)
151         alloc_unboxed((int)SAP_WIDETAG, sizeof(struct sap)/sizeof(lispobj) -1);
152     sap->pointer = ptr;
153     return make_lispobj(sap,OTHER_POINTER_LOWTAG);
154 }