2 * allocation routines for C code. For allocation done by Lisp look
3 * instead at src/compiler/target/alloc.lisp and .../macros.lisp
7 * This software is part of the SBCL system. See the README file for
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.
20 #include "genesis/config.h"
28 #include "genesis/vector.h"
29 #include "genesis/cons.h"
30 #include "genesis/bignum.h"
31 #include "genesis/sap.h"
33 #define GET_FREE_POINTER() dynamic_space_free_pointer
34 #define SET_FREE_POINTER(new_value) \
35 (dynamic_space_free_pointer = (new_value))
36 #define GET_GC_TRIGGER() current_auto_gc_trigger
37 #define SET_GC_TRIGGER(new_value) \
38 clear_auto_gc_trigger(); set_auto_gc_trigger(new_value);
40 #define ALIGNED_SIZE(n) (n+LOWTAG_MASK) & ~LOWTAG_MASK
42 #if defined LISP_FEATURE_GENCGC
43 extern lispobj *alloc(int bytes);
48 struct thread *th=arch_os_get_current_thread();
49 SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED, make_fixnum(0),th);
50 SetSymbolValue(PSEUDO_ATOMIC_ATOMIC, make_fixnum(1),th);
52 SetSymbolValue(PSEUDO_ATOMIC_ATOMIC, make_fixnum(0),th);
53 if (SymbolValue(PSEUDO_ATOMIC_INTERRUPTED,th))
54 /* even if we gc at this point, the new allocation will be
55 * protected from being moved, because result is on the c stack
57 do_pending_interrupt();
67 /* Round to dual word boundary. */
68 bytes = (bytes + LOWTAG_MASK) & ~LOWTAG_MASK;
70 result = (char *)GET_FREE_POINTER();
72 SET_FREE_POINTER((lispobj *)(result + bytes));
74 if (GET_GC_TRIGGER() && GET_FREE_POINTER() > GET_GC_TRIGGER()) {
75 SET_GC_TRIGGER((char *)GET_FREE_POINTER()
76 - (char *)current_dynamic_space);
78 return (lispobj *) result;
84 alloc_unboxed(int type, int words)
88 result = pa_alloc(ALIGNED_SIZE((1 + words) * sizeof(lispobj)));
89 *result = (lispobj) (words << N_WIDETAG_BITS) | type;
94 alloc_vector(int type, int length, int size)
96 struct vector *result;
98 result = (struct vector *)
99 pa_alloc(ALIGNED_SIZE((2 + (length*size + 31) / 32) * sizeof(lispobj)));
101 result->header = type;
102 result->length = make_fixnum(length);
104 return make_lispobj(result,OTHER_POINTER_LOWTAG);
108 alloc_cons(lispobj car, lispobj cdr)
110 struct cons *ptr = (struct cons *)pa_alloc(ALIGNED_SIZE(sizeof(struct cons)));
115 return make_lispobj(ptr, LIST_POINTER_LOWTAG);
123 if (-0x20000000 < n && n < 0x20000000)
124 return make_fixnum(n);
126 ptr = (struct bignum *)alloc_unboxed(BIGNUM_WIDETAG, 1);
130 return make_lispobj(ptr, OTHER_POINTER_LOWTAG);
135 alloc_base_string(char *str)
137 int len = strlen(str);
138 lispobj result = alloc_vector(SIMPLE_BASE_STRING_WIDETAG, len+1, 8);
139 struct vector *vec = (struct vector *)native_pointer(result);
141 vec->length = make_fixnum(len);
142 strcpy((char *)vec->data, str);
152 alloc_unboxed((int)SAP_WIDETAG, sizeof(struct sap)/sizeof(lispobj) -1);
154 return make_lispobj(sap,OTHER_POINTER_LOWTAG);