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