/* [<][>][^][v][top][bottom][index][help] */
1 /*
2 * gauche/memory.h - internal macros for memory-related stuff
3 *
4 * Copyright (c) 2000-2003 Shiro Kawai, All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the authors nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * $Id: memory.h,v 1.7 2004/12/18 04:11:13 shirok Exp $
34 */
35
36 #ifndef GAUCHE_MEM_H
37 #define GAUCHE_MEM_H
38
39 /*
40 * This file defines some macros to inline memory allocation.
41 * The macro have to interact with some internal gc stuff, so they should
42 * be used with care. By including this file you'll get whole bunch
43 * of internal macros of gc, which may conflict with your code if used
44 * carelessly.
45 *
46 * If SCM_INLINE_MALLOC_PRIMITIVES is not defined, these macros are
47 * replaced for "safer" function calls.
48 */
49
50 #ifdef SCM_INLINE_MALLOC_PRIMITIVES
51
52 /* These #define's must match the ones libgc.a was compiled with. */
53 /* NB: when cross-compiling, SMALL_CONFIG is defined in GC. */
54 #define DONT_ADD_BYTE_AT_END
55 #define ALL_INTERIOR_POINTERS
56
57 /* gc_priv.h included by gc_inline.h redefines FALSE and TRUE without
58 checking prior definition. This is a workaround. */
59 #undef TRUE
60 #undef FALSE
61 #include "gc_inline.h"
62
63 /* Basic macros. Allocate N words obj. N must be smaller than MAXOBJSZ. */
64 #define SCM_MALLOC_WORDS(p, n, type) \
65 do { \
66 void *SCMMW__tmpptr; \
67 GC_MALLOC_WORDS(SCMMW__tmpptr, n); \
68 p = (type)SCMMW__tmpptr; \
69 } while (0)
70
71 #define SCM_MALLOC_ATOMIC_WORDS(p, n) \
72 do { \
73 void *SCMMW__tmpptr; \
74 GC_MALLOC_ATOMIC_WORDS(SCMMW__tmpptr, n); \
75 p = (type)SCMMW__tmpptr; \
76 } while (0)
77
78 /* Type-specific macros */
79 #define SCM_NEW_PAIR(p, car_, cdr_) \
80 do { \
81 SCM_MALLOC_WORDS(p, sizeof(ScmPair)/sizeof(GC_word), ScmPair *); \
82 SCM_SET_CLASS(p, SCM_CLASS_PAIR); \
83 SCM_SET_CAR(p, car_); \
84 SCM_SET_CDR(p, cdr_); \
85 } while (0)
86
87 #define SCM_NEW_LIST1(p, obj0) \
88 SCM_NEW_PAIR(p, obj0, SCM_NIL)
89
90 #define SCM_NEW_LIST2(p, obj0, obj1) \
91 do { \
92 ScmObj SCML2__tmpptr; \
93 SCM_NEW_PAIR(SCML2__tmpptr, obj1, SCM_NIL); \
94 SCM_NEW_PAIR(p, obj0, SCM_OBJ(SCML2__tmpptr)); \
95 } while (0)
96
97 #define SCM_NEW_LIST3(p, obj0, obj1, obj2) \
98 do { \
99 ScmObj SCML3__tmpptr0, SCML3__tmpptr1; \
100 SCM_NEW_PAIR(SCML3__tmpptr0, obj2, SCM_NIL); \
101 SCM_NEW_PAIR(SCML3__tmpptr1, obj1, SCM_OBJ(SCML3__tmpptr0)); \
102 SCM_NEW_PAIR(p, obj0, SCM_OBJ(SCML3__tmpptr1)); \
103 } while (0)
104
105 #else /* !SCM_INLINE_MALLOC_PRIMITIVES */
106
107 #define SCM_MALLOC_WORDS(p, n, type) \
108 (p = (type)SCM_MALLOC(n * sizeof(GC_word)))
109
110 #define SCM_MALLOC_ATOMIC_WORDS(p, n, type) \
111 (p = (type)SCM_MALLOC_ATOMIC(n * sizeof(GC_word)))
112
113 #define SCM_NEW_PAIR(p, car_, cdr_) (p = (ScmPair*)Scm_Cons(car_, cdr_))
114
115 #define SCM_NEW_LIST1(p, obj0) (p = Scm_Cons(obj0, SCM_NIL))
116 #define SCM_NEW_LIST2(p, obj0, obj1) \
117 (p = Scm_Cons(obj0, Scm_Cons(obj1, SCM_NIL))
118 #define SCM_NEW_LIST3(p, obj0, obj1, obj2) \
119 (p = Scm_Cons(obj0, Scm_Cons(obj1, Scm_Cons(obj2, SCM_NIL))))
120
121 #endif /* SCM_INLINE_MALLOC_PRIMITIVES */
122
123 #endif /* GAUCHE_MEM_H */