0a88829833eb4f9709ef6ea3350a816edc85c76d
[sbcl.git] / src / runtime / alloc.c
1 /*
2  * allocation routines
3  */
4
5 /*
6  * This software is part of the SBCL system. See the README file for
7  * more information.
8  *
9  * This software is derived from the CMU CL system, which was
10  * written at Carnegie Mellon University and released into the
11  * public domain. The software is in the public domain and is
12  * provided with absolutely no warranty. See the COPYING and CREDITS
13  * files for more information.
14  */
15
16 #include "runtime.h"
17 #include "sbcl.h"
18 #include "alloc.h"
19 #include "globals.h"
20 #include "gc.h"
21
22 #ifdef ibmrt
23 #define GET_FREE_POINTER() ((lispobj *)SymbolValue(ALLOCATION_POINTER))
24 #define SET_FREE_POINTER(new_value) \
25     (SetSymbolValue(ALLOCATION_POINTER,(lispobj)(new_value)))
26 #define GET_GC_TRIGGER() ((lispobj *)SymbolValue(INTERNAL_GC_TRIGGER))
27 #define SET_GC_TRIGGER(new_value) \
28     (SetSymbolValue(INTERNAL_GC_TRIGGER,(lispobj)(new_value)))
29 #else
30 #define GET_FREE_POINTER() dynamic_space_free_pointer
31 #define SET_FREE_POINTER(new_value) (dynamic_space_free_pointer = (new_value))
32 #define GET_GC_TRIGGER() current_auto_gc_trigger
33 #define SET_GC_TRIGGER(new_value) \
34     clear_auto_gc_trigger(); set_auto_gc_trigger(new_value);
35 #endif
36
37 #define ALIGNED_SIZE(n) (n+lowtag_Mask) & ~lowtag_Mask
38
39 #if defined(WANT_CGC) || defined(GENCGC)
40 extern lispobj *alloc(int bytes);
41 #else
42 static lispobj *alloc(int bytes)
43 {
44     lispobj *result;
45
46     /* Round to dual word boundary. */
47     bytes = (bytes + lowtag_Mask) & ~lowtag_Mask;
48
49     result = GET_FREE_POINTER();
50     SET_FREE_POINTER(result + (bytes / sizeof(lispobj)));
51
52     if (GET_GC_TRIGGER() && GET_FREE_POINTER() > GET_GC_TRIGGER()) {
53         SET_GC_TRIGGER((char *)GET_FREE_POINTER()
54                        - (char *)DYNAMIC_SPACE_START);
55     }
56
57     return result;
58 }
59 #endif
60
61 static lispobj *alloc_unboxed(int type, int words)
62 {
63     lispobj *result;
64
65     result = alloc(ALIGNED_SIZE((1 + words) * sizeof(lispobj)));
66
67     *result = (lispobj) (words << type_Bits) | type;
68
69     return result;
70 }
71
72 static lispobj alloc_vector(int type, int length, int size)
73 {
74     struct vector *result;
75
76     result = (struct vector *)
77       alloc(ALIGNED_SIZE((2 + (length*size + 31) / 32) * sizeof(lispobj)));
78
79     result->header = type;
80     result->length = make_fixnum(length);
81
82     return ((lispobj)result)|type_OtherPointer;
83 }
84
85 lispobj alloc_cons(lispobj car, lispobj cdr)
86 {
87     struct cons *ptr = (struct cons *)alloc(ALIGNED_SIZE(sizeof(struct cons)));
88
89     ptr->car = car;
90     ptr->cdr = cdr;
91
92     return (lispobj)ptr | type_ListPointer;
93 }
94
95 lispobj alloc_number(long n)
96 {
97     struct bignum *ptr;
98
99     if (-0x20000000 < n && n < 0x20000000)
100         return make_fixnum(n);
101     else {
102         ptr = (struct bignum *)alloc_unboxed(type_Bignum, 1);
103
104         ptr->digits[0] = n;
105
106         return (lispobj) ptr | type_OtherPointer;
107     }
108 }
109
110 lispobj alloc_string(char *str)
111 {
112     int len = strlen(str);
113     lispobj result = alloc_vector(type_SimpleString, len+1, 8);
114     struct vector *vec = (struct vector *)PTR(result);
115
116     vec->length = make_fixnum(len);
117     strcpy((char *)vec->data, str);
118
119     return result;
120 }
121
122 lispobj alloc_sap(void *ptr)
123 {
124     /* FIXME: It would probably be good to grep for "alpha" everywhere
125      * and replace this kind of weirdness with nicer parameterizations
126      * like N_WORDS_IN_POINTER. However, it might be hard to do this
127      * well enough to be useful without an Alpha to test on. What to do? */
128 #ifndef alpha
129     struct sap *sap = (struct sap *)alloc_unboxed(type_Sap, 1);
130 #else
131     struct sap *sap = (struct sap *)alloc_unboxed(type_Sap, 3);
132 #endif
133     sap->pointer = ptr;
134
135     return (lispobj) sap | type_OtherPointer;
136 }