0.6.12.7.flaky1.1:
[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 #include <stdio.h>
22
23 #ifdef ibmrt
24 #define GET_FREE_POINTER() ((lispobj *)SymbolValue(ALLOCATION_POINTER))
25 #define SET_FREE_POINTER(new_value) \
26     (SetSymbolValue(ALLOCATION_POINTER,(lispobj)(new_value)))
27 #define GET_GC_TRIGGER() ((lispobj *)SymbolValue(INTERNAL_GC_TRIGGER))
28 #define SET_GC_TRIGGER(new_value) \
29     (SetSymbolValue(INTERNAL_GC_TRIGGER,(lispobj)(new_value)))
30 #else
31 #define GET_FREE_POINTER() dynamic_space_free_pointer
32 #define SET_FREE_POINTER(new_value) \
33   (dynamic_space_free_pointer = (new_value))
34 #define GET_GC_TRIGGER() current_auto_gc_trigger
35 #define SET_GC_TRIGGER(new_value) \
36     clear_auto_gc_trigger(); set_auto_gc_trigger(new_value);
37 #endif
38
39 #define ALIGNED_SIZE(n) (n+lowtag_Mask) & ~lowtag_Mask
40
41 #if defined(WANT_CGC) || defined(GENCGC)
42 extern lispobj *alloc(int bytes);
43 #else
44 static lispobj *
45 alloc(int bytes)
46 {
47     lispobj *result;
48
49     /* Round to dual word boundary. */
50     bytes = (bytes + lowtag_Mask) & ~lowtag_Mask;
51
52     result = GET_FREE_POINTER();
53
54     SET_FREE_POINTER(result + (bytes / sizeof(lispobj)));
55
56     if (GET_GC_TRIGGER() && GET_FREE_POINTER() > GET_GC_TRIGGER()) {
57         SET_GC_TRIGGER((char *)GET_FREE_POINTER()
58                        - (char *)current_dynamic_space);
59     }
60     return result;
61 }
62 #endif
63
64 static lispobj *
65 alloc_unboxed(int type, int words)
66 {
67     lispobj *result;
68
69     result = alloc(ALIGNED_SIZE((1 + words) * sizeof(lispobj)));
70     *result = (lispobj) (words << type_Bits) | type;
71     return result;
72 }
73
74 static lispobj
75 alloc_vector(int type, int length, int size)
76 {
77     struct vector *result;
78
79     result = (struct vector *)
80       alloc(ALIGNED_SIZE((2 + (length*size + 31) / 32) * sizeof(lispobj)));
81
82     result->header = type;
83     result->length = make_fixnum(length);
84
85     return ((lispobj)result)|type_OtherPointer;
86 }
87
88 lispobj
89 alloc_cons(lispobj car, lispobj cdr)
90 {
91     struct cons *ptr = (struct cons *)alloc(ALIGNED_SIZE(sizeof(struct cons)));
92
93     ptr->car = car;
94     ptr->cdr = cdr;
95
96     return (lispobj)ptr | type_ListPointer;
97 }
98
99 lispobj
100 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
116 alloc_string(char *str)
117 {
118     int len = strlen(str);
119     lispobj result = alloc_vector(type_SimpleString, len+1, 8);
120     struct vector *vec = (struct vector *)PTR(result);
121
122     vec->length = make_fixnum(len);
123     strcpy((char *)vec->data, str);
124
125     return result;
126 }
127
128 lispobj
129 alloc_sap(void *ptr)
130 {
131     int n_words_to_alloc =
132         (sizeof(struct sap) - sizeof(lispobj)) / sizeof(u32);
133     struct sap *sap =
134         (struct sap *)alloc_unboxed((int)type_Sap, n_words_to_alloc);
135     sap->pointer = ptr;
136     return (lispobj) sap | type_OtherPointer;
137 }