Debian Codegraph @

gc/include/gc_inline.h

raw
1 /*
2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1995 by Xerox Corporation. All rights reserved.
4 * Copyright (c) 2005 Hewlett-Packard Development Company, L.P.
5 *
6 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
7 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
8 *
9 * Permission is hereby granted to use or copy this program
10 * for any purpose, provided the above notices are retained on all copies.
11 * Permission to modify the code and to distribute modified code is granted,
12 * provided the above notices are retained, and a notice that the code was
13 * modified is included with the above copyright notice.
14 */
15
16 #ifndef GC_INLINE_H
17 #define GC_INLINE_H
18
19 /* WARNING: */
20 /* Note that for these routines, it is the clients responsibility to */
21 /* add the extra byte at the end to deal with one-past-the-end pointers.*/
22 /* In the standard collector configuration, the collector assumes that */
23 /* such a byte has been added, and hence does not trace the last word */
24 /* in the resulting object. */
25 /* This is not an issue if the collector is compiled with */
26 /* DONT_ADD_BYTE_AT_END, or if GC_all_interior_pointers is not set. */
27 /* This interface is most useful for compilers that generate C. */
28 /* It is also used internally for thread-local allocation. */
29 /* Manual use is hereby discouraged. */
30 /* There is no debugging version of this allocation API. */
31
32 #include "gc.h"
33 #include "gc_tiny_fl.h"
34
35 #if GC_GNUC_PREREQ(3, 0)
36 # define GC_EXPECT(expr, outcome) __builtin_expect(expr,outcome)
37 /* Equivalent to (expr), but predict that usually (expr)==outcome. */
38 #else
39 # define GC_EXPECT(expr, outcome) (expr)
40 #endif
41
42 #ifndef GC_ASSERT
43 # ifdef NDEBUG
44 # define GC_ASSERT(expr) /* empty */
45 # else
46 # include <assert.h>
47 # define GC_ASSERT(expr) assert(expr)
48 # endif
49 #endif
50
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54
55 #ifndef GC_PREFETCH_FOR_WRITE
56 # if GC_GNUC_PREREQ(3, 0) && !defined(GC_NO_PREFETCH_FOR_WRITE)
57 # define GC_PREFETCH_FOR_WRITE(x) __builtin_prefetch((x), 1)
58 # else
59 # define GC_PREFETCH_FOR_WRITE(x) (void)0
60 # endif
61 #endif
62
63 /* Object kinds; must match PTRFREE, NORMAL in gc_priv.h. */
64 #define GC_I_PTRFREE 0
65 #define GC_I_NORMAL 1
66
67 /* Store a pointer to a list of newly allocated objects of kind k and */
68 /* size lb in *result. The caller must make sure that *result is */
69 /* traced even if objects are ptrfree. */
70 GC_API void GC_CALL GC_generic_malloc_many(size_t /* lb */, int /* k */,
71 void ** /* result */);
72
73 /* Generalized version of GC_malloc and GC_malloc_atomic. */
74 /* Uses appropriately the thread-local (if available) or the global */
75 /* free-list of the specified kind. */
76 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
77 GC_malloc_kind(size_t /* lb */, int /* k */);
78
79 #ifdef GC_THREADS
80 /* Same as above but uses only the global free-list. */
81 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
82 GC_malloc_kind_global(size_t /* lb */, int /* k */);
83 #else
84 # define GC_malloc_kind_global GC_malloc_kind
85 #endif
86
87 /* An internal macro to update the free list pointer atomically (if */
88 /* the AO primitives are available) to avoid race with the marker. */
89 #if defined(GC_THREADS) && defined(AO_HAVE_store)
90 # define GC_FAST_M_AO_STORE(my_fl, next) \
91 AO_store((volatile AO_t *)(my_fl), (AO_t)(next))
92 #else
93 # define GC_FAST_M_AO_STORE(my_fl, next) (void)(*(my_fl) = (next))
94 #endif
95
96 /* The ultimately general inline allocation macro. Allocate an object */
97 /* of size granules, putting the resulting pointer in result. Tiny_fl */
98 /* is a "tiny" free list array, which will be used first, if the size */
99 /* is appropriate. If granules argument is too large, we allocate with */
100 /* default_expr instead. If we need to refill the free list, we use */
101 /* GC_generic_malloc_many with the indicated kind. */
102 /* Tiny_fl should be an array of GC_TINY_FREELISTS void * pointers. */
103 /* If num_direct is nonzero, and the individual free list pointers */
104 /* are initialized to (void *)1, then we allocate num_direct granules */
105 /* directly using generic_malloc before putting multiple objects into */
106 /* the tiny_fl entry. If num_direct is zero, then the free lists may */
107 /* also be initialized to (void *)0. */
108 /* Note that we use the zeroth free list to hold objects 1 granule in */
109 /* size that are used to satisfy size 0 allocation requests. */
110 /* We rely on much of this hopefully getting optimized away in the */
111 /* num_direct = 0 case. */
112 /* Particularly, if granules argument is constant, this should generate */
113 /* a small amount of code. */
114 # define GC_FAST_MALLOC_GRANS(result,granules,tiny_fl,num_direct, \
115 kind,default_expr,init) \
116 do { \
117 if (GC_EXPECT((granules) >= GC_TINY_FREELISTS,0)) { \
118 result = (default_expr); \
119 } else { \
120 void **my_fl = (tiny_fl) + (granules); \
121 void *my_entry=*my_fl; \
122 void *next; \
123 \
124 for (;;) { \
125 if (GC_EXPECT((GC_word)my_entry \
126 > (num_direct) + GC_TINY_FREELISTS + 1, 1)) { \
127 next = *(void **)(my_entry); \
128 result = (void *)my_entry; \
129 GC_FAST_M_AO_STORE(my_fl, next); \
130 init; \
131 GC_PREFETCH_FOR_WRITE(next); \
132 if ((kind) != GC_I_PTRFREE) { \
133 GC_end_stubborn_change(my_fl); \
134 GC_reachable_here(next); \
135 } \
136 GC_ASSERT(GC_size(result) >= (granules)*GC_GRANULE_BYTES); \
137 GC_ASSERT((kind) == GC_I_PTRFREE \
138 || ((GC_word *)result)[1] == 0); \
139 break; \
140 } \
141 /* Entry contains counter or NULL */ \
142 if ((GC_signed_word)my_entry - (GC_signed_word)(num_direct) <= 0 \
143 /* (GC_word)my_entry <= (num_direct) */ \
144 && my_entry != 0 /* NULL */) { \
145 /* Small counter value, not NULL */ \
146 GC_FAST_M_AO_STORE(my_fl, (char *)my_entry \
147 + (granules) + 1); \
148 result = (default_expr); \
149 break; \
150 } else { \
151 /* Large counter or NULL */ \
152 GC_generic_malloc_many(((granules) == 0? GC_GRANULE_BYTES : \
153 GC_RAW_BYTES_FROM_INDEX(granules)), \
154 kind, my_fl); \
155 my_entry = *my_fl; \
156 if (my_entry == 0) { \
157 result = (*GC_get_oom_fn())((granules)*GC_GRANULE_BYTES); \
158 break; \
159 } \
160 } \
161 } \
162 } \
163 } while (0)
164
165 # define GC_WORDS_TO_WHOLE_GRANULES(n) \
166 GC_WORDS_TO_GRANULES((n) + GC_GRANULE_WORDS - 1)
167
168 /* Allocate n words (NOT BYTES). X is made to point to the result. */
169 /* This should really only be used if GC_all_interior_pointers is */
170 /* not set, or DONT_ADD_BYTE_AT_END is set. See above. */
171 /* Does not acquire lock. The caller is responsible for supplying */
172 /* a cleared tiny_fl free list array. For single-threaded */
173 /* applications, this may be a global array. */
174 # define GC_MALLOC_WORDS_KIND(result,n,tiny_fl,kind,init) \
175 do { \
176 size_t granules = GC_WORDS_TO_WHOLE_GRANULES(n); \
177 GC_FAST_MALLOC_GRANS(result, granules, tiny_fl, 0, kind, \
178 GC_malloc_kind(granules*GC_GRANULE_BYTES, kind), \
179 init); \
180 } while (0)
181
182 # define GC_MALLOC_WORDS(result,n,tiny_fl) \
183 GC_MALLOC_WORDS_KIND(result, n, tiny_fl, GC_I_NORMAL, \
184 *(void **)(result) = 0)
185
186 # define GC_MALLOC_ATOMIC_WORDS(result,n,tiny_fl) \
187 GC_MALLOC_WORDS_KIND(result, n, tiny_fl, GC_I_PTRFREE, (void)0)
188
189 /* And once more for two word initialized objects: */
190 # define GC_CONS(result, first, second, tiny_fl) \
191 do { \
192 void *l = (void *)(first); \
193 void *r = (void *)(second); \
194 GC_MALLOC_WORDS_KIND(result, 2, tiny_fl, GC_I_NORMAL, (void)0); \
195 if ((result) != 0 /* NULL */) { \
196 *(void **)(result) = l; \
197 GC_ptr_store_and_dirty((void **)(result) + 1, r); \
198 GC_reachable_here(l); \
199 } \
200 } while (0)
201
202 GC_API void GC_CALL GC_print_free_list(int /* kind */,
203 size_t /* sz_in_granules */);
204
205 #ifdef __cplusplus
206 } /* extern "C" */
207 #endif
208
209 #endif /* !GC_INLINE_H */