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.
27 #include "genesis/vector.h"
28 #include "genesis/cons.h"
29 #include "genesis/bignum.h"
30 #include "genesis/sap.h"
32 #define ALIGNED_SIZE(n) ((n) + LOWTAG_MASK) & ~LOWTAG_MASK
34 #ifdef LISP_FEATURE_GENCGC
35 extern lispobj *alloc(int bytes);
42 #ifdef LISP_FEATURE_GENCGC
43 struct thread *th = arch_os_get_current_thread();
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();
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;
60 /* Align up to next dual word boundary. */
61 bytes = ALIGNED_SIZE(bytes);
63 dynamic_space_free_pointer = (lispobj *)((char *)result + bytes);
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);
77 alloc_unboxed(int type, int words)
81 result = pa_alloc(ALIGNED_SIZE((1 + words) * sizeof(lispobj)));
82 *result = (lispobj) (words << N_WIDETAG_BITS) | type;
87 alloc_vector(int type, int length, int size)
89 struct vector *result;
91 result = (struct vector *)
92 pa_alloc(ALIGNED_SIZE((2 + (length*size + 31) / 32) * sizeof(lispobj)));
94 result->header = type;
95 result->length = make_fixnum(length);
97 return make_lispobj(result,OTHER_POINTER_LOWTAG);
101 alloc_cons(lispobj car, lispobj cdr)
103 struct cons *ptr = (struct cons *)pa_alloc(ALIGNED_SIZE(sizeof(struct cons)));
108 return make_lispobj(ptr, LIST_POINTER_LOWTAG);
116 if (-0x20000000 < n && n < 0x20000000)
117 return make_fixnum(n);
119 ptr = (struct bignum *)alloc_unboxed(BIGNUM_WIDETAG, 1);
123 return make_lispobj(ptr, OTHER_POINTER_LOWTAG);
128 alloc_base_string(char *str)
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);
134 vec->length = make_fixnum(len);
135 strcpy((char *)vec->data, str);
145 alloc_unboxed((int)SAP_WIDETAG, sizeof(struct sap)/sizeof(lispobj) -1);
147 return make_lispobj(sap,OTHER_POINTER_LOWTAG);