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