Initial revision
[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 /*
17  * $Header$
18  */
19
20 #include "runtime.h"
21 #include "sbcl.h"
22 #include "alloc.h"
23 #include "globals.h"
24 #include "gc.h"
25
26 #ifdef ibmrt
27 #define GET_FREE_POINTER() ((lispobj *)SymbolValue(ALLOCATION_POINTER))
28 #define SET_FREE_POINTER(new_value) \
29     (SetSymbolValue(ALLOCATION_POINTER,(lispobj)(new_value)))
30 #define GET_GC_TRIGGER() ((lispobj *)SymbolValue(INTERNAL_GC_TRIGGER))
31 #define SET_GC_TRIGGER(new_value) \
32     (SetSymbolValue(INTERNAL_GC_TRIGGER,(lispobj)(new_value)))
33 #else
34 #define GET_FREE_POINTER() current_dynamic_space_free_pointer
35 #define SET_FREE_POINTER(new_value) \
36     (current_dynamic_space_free_pointer = (new_value))
37 #define GET_GC_TRIGGER() current_auto_gc_trigger
38 #define SET_GC_TRIGGER(new_value) \
39     clear_auto_gc_trigger(); set_auto_gc_trigger(new_value);
40 #endif
41
42 #define ALIGNED_SIZE(n) (n+lowtag_Mask) & ~lowtag_Mask
43
44 #if defined(WANT_CGC) || defined(GENCGC)
45 extern lispobj *alloc(int bytes);
46 #else
47 static lispobj *alloc(int bytes)
48 {
49     lispobj *result;
50
51     /* Round to dual word boundary. */
52     bytes = (bytes + lowtag_Mask) & ~lowtag_Mask;
53
54     result = GET_FREE_POINTER();
55     SET_FREE_POINTER(result + (bytes / sizeof(lispobj)));
56
57     if (GET_GC_TRIGGER() && GET_FREE_POINTER() > GET_GC_TRIGGER()) {
58         SET_GC_TRIGGER((char *)GET_FREE_POINTER()
59                        - (char *)current_dynamic_space);
60     }
61
62     return result;
63 }
64 #endif
65
66 static lispobj *alloc_unboxed(int type, int words)
67 {
68     lispobj *result;
69
70     result = alloc(ALIGNED_SIZE((1 + words) * sizeof(lispobj)));
71
72     *result = (lispobj) (words << type_Bits) | type;
73
74     return result;
75 }
76
77 static lispobj alloc_vector(int type, int length, int size)
78 {
79     struct vector *result;
80
81     result = (struct vector *)
82       alloc(ALIGNED_SIZE((2 + (length*size + 31) / 32) * sizeof(lispobj)));
83
84     result->header = type;
85     result->length = make_fixnum(length);
86
87     return ((lispobj)result)|type_OtherPointer;
88 }
89
90 lispobj alloc_cons(lispobj car, lispobj cdr)
91 {
92     struct cons *ptr = (struct cons *)alloc(ALIGNED_SIZE(sizeof(struct cons)));
93
94     ptr->car = car;
95     ptr->cdr = cdr;
96
97     return (lispobj)ptr | type_ListPointer;
98 }
99
100 lispobj alloc_number(long n)
101 {
102     struct bignum *ptr;
103
104     if (-0x20000000 < n && n < 0x20000000)
105         return make_fixnum(n);
106     else {
107         ptr = (struct bignum *)alloc_unboxed(type_Bignum, 1);
108
109         ptr->digits[0] = n;
110
111         return (lispobj) ptr | type_OtherPointer;
112     }
113 }
114
115 lispobj alloc_string(char *str)
116 {
117     int len = strlen(str);
118     lispobj result = alloc_vector(type_SimpleString, len+1, 8);
119     struct vector *vec = (struct vector *)PTR(result);
120
121     vec->length = make_fixnum(len);
122     strcpy((char *)vec->data, str);
123
124     return result;
125 }
126
127 lispobj alloc_sap(void *ptr)
128 {
129     /* FIXME: It would probably be good to grep for "alpha" everywhere
130      * and replace this kind of weirdness with nicer parameterizations
131      * like N_WORDS_IN_POINTER. However, it might be hard to do this
132      * well enough to be useful without an Alpha to test on. What to do? */
133 #ifndef alpha
134     struct sap *sap = (struct sap *)alloc_unboxed(type_Sap, 1);
135 #else
136     struct sap *sap = (struct sap *)alloc_unboxed(type_Sap, 3);
137 #endif
138     sap->pointer = ptr;
139
140     return (lispobj) sap | type_OtherPointer;
141 }