Debian Codegraph @

external/bdwgc/include/new_gc_alloc.h

raw
1 /*
2 * Copyright (c) 1996-1998 by Silicon Graphics. All rights reserved.
3 *
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
6 *
7 * Permission is hereby granted to use or copy this program
8 * for any purpose, provided the above notices are retained on all copies.
9 * Permission to modify the code and to distribute modified code is granted,
10 * provided the above notices are retained, and a notice that the code was
11 * modified is included with the above copyright notice.
12 */
13
14 //
15 // This is a revision of gc_allocator.h for SGI STL versions > 3.0.
16 // Unlike earlier versions, it supplements the standard (STL) alloc.h
17 // instead of replacing it.
18 //
19 // This is sloppy about variable names used in header files.
20 // It also doesn't yet understand the new header file names or
21 // namespaces.
22 //
23 // This assumes the collector has been compiled with -DGC_ATOMIC_UNCOLLECTABLE.
24 // The user should also consider -DREDIRECT_MALLOC=GC_uncollectable_malloc,
25 // to ensure that object allocated through malloc are traced.
26 //
27 // Some of this could be faster in the explicit deallocation case.
28 // In particular, we spend too much time clearing objects on the
29 // free lists. That could be avoided.
30 //
31 // This uses template classes with static members, and hence does not work
32 // with g++ 2.7.2 and earlier.
33 //
34 // Unlike its predecessor, this one simply defines
35 // gc_alloc
36 // single_client_gc_alloc
37 // traceable_alloc
38 // single_client_traceable_alloc
39 //
40 // It does not redefine alloc. Nor does it change the default allocator,
41 // though the user may wish to do so. (The argument against changing
42 // the default allocator is that it may introduce subtle link compatibility
43 // problems. The argument for changing it is that the usual default
44 // allocator is usually a very bad choice for a garbage collected environment.)
45 //
46
47 #ifndef GC_ALLOC_H
48
49 #include "gc.h"
50
51 #if GC_GNUC_PREREQ(3, 0)
52 # include <bits/stl_alloc.h>
53 # ifndef __STL_BEGIN_NAMESPACE
54 # define __STL_BEGIN_NAMESPACE namespace std {
55 # define __STL_END_NAMESPACE };
56 # endif
57 # ifndef __STL_USE_STD_ALLOCATORS
58 # define __STL_USE_STD_ALLOCATORS
59 # endif
60 #else
61 # include <stack> // A more portable way to get stl_alloc.h file.
62 #endif
63
64 /* A hack to deal with gcc 3.1. If you are using gcc3.1 and later, */
65 /* you should probably really use gc_allocator.h instead. */
66 #if GC_GNUC_PREREQ(3, 1)
67 # define simple_alloc __simple_alloc
68 #endif
69
70 #define GC_ALLOC_H
71
72 #include <stddef.h>
73 #include <string.h>
74
75 // We can't include gc_priv.h, since that pulls in way too much stuff.
76 #include "gc_alloc_ptrs.h"
77
78 #define GC_generic_malloc_words_small(lw, k) \
79 GC_generic_malloc((lw) * sizeof(GC_word), k)
80
81 // Object kinds; must match PTRFREE, NORMAL, UNCOLLECTABLE, and
82 // AUNCOLLECTABLE in gc_priv.h.
83
84 enum { GC_PTRFREE = 0, GC_NORMAL = 1, GC_UNCOLLECTABLE = 2,
85 GC_AUNCOLLECTABLE = 3 };
86
87 enum { GC_max_fast_bytes = 255 };
88
89 enum { GC_byte_alignment = 8 };
90
91 #if defined(CPPCHECK)
92 const unsigned GC_bytes_per_word = sizeof(char *);
93 const unsigned GC_word_alignment = GC_byte_alignment/GC_bytes_per_word;
94 #else
95 enum { GC_bytes_per_word = sizeof(char *) };
96 enum { GC_word_alignment = GC_byte_alignment/GC_bytes_per_word };
97 #endif
98
99 inline void * &GC_obj_link(void * p)
100 { return *reinterpret_cast<void **>(p); }
101
102 // Compute a number of words >= n+1 bytes.
103 // The +1 allows for pointers one past the end.
104 inline size_t GC_round_up(size_t n)
105 {
106 return ((n + GC_byte_alignment)/GC_byte_alignment)*GC_word_alignment;
107 }
108
109 // The same but don't allow for extra byte.
110 inline size_t GC_round_up_uncollectable(size_t n)
111 {
112 return ((n + GC_byte_alignment - 1)/GC_byte_alignment)*GC_word_alignment;
113 }
114
115 template <int dummy>
116 class GC_aux_template {
117 public:
118 // File local count of allocated words. Occasionally this is
119 // added into the global count. A separate count is necessary since the
120 // real one must be updated with a procedure call.
121 static size_t GC_bytes_recently_allocd;
122
123 // Same for uncollectible memory. Not yet reflected in either
124 // GC_bytes_recently_allocd or GC_non_gc_bytes.
125 static size_t GC_uncollectable_bytes_recently_allocd;
126
127 // Similar counter for explicitly deallocated memory.
128 static size_t GC_bytes_recently_freed;
129
130 // Again for uncollectible memory.
131 static size_t GC_uncollectable_bytes_recently_freed;
132
133 static void * GC_out_of_line_malloc(size_t nwords, int kind);
134 };
135
136 template <int dummy>
137 size_t GC_aux_template<dummy>::GC_bytes_recently_allocd = 0;
138
139 template <int dummy>
140 size_t GC_aux_template<dummy>::GC_uncollectable_bytes_recently_allocd = 0;
141
142 template <int dummy>
143 size_t GC_aux_template<dummy>::GC_bytes_recently_freed = 0;
144
145 template <int dummy>
146 size_t GC_aux_template<dummy>::GC_uncollectable_bytes_recently_freed = 0;
147
148 template <int dummy>
149 void * GC_aux_template<dummy>::GC_out_of_line_malloc(size_t nwords, int kind)
150 {
151 GC_bytes_recently_allocd += GC_uncollectable_bytes_recently_allocd;
152 GC_non_gc_bytes +=
153 GC_uncollectable_bytes_recently_allocd;
154 GC_uncollectable_bytes_recently_allocd = 0;
155
156 GC_bytes_recently_freed += GC_uncollectable_bytes_recently_freed;
157 GC_non_gc_bytes -= GC_uncollectable_bytes_recently_freed;
158 GC_uncollectable_bytes_recently_freed = 0;
159
160 GC_incr_bytes_allocd(GC_bytes_recently_allocd);
161 GC_bytes_recently_allocd = 0;
162
163 GC_incr_bytes_freed(GC_bytes_recently_freed);
164 GC_bytes_recently_freed = 0;
165
166 return GC_generic_malloc_words_small(nwords, kind);
167 }
168
169 typedef GC_aux_template<0> GC_aux;
170
171 // A fast, single-threaded, garbage-collected allocator
172 // We assume the first word will be immediately overwritten.
173 // In this version, deallocation is not a no-op, and explicit
174 // deallocation is likely to help performance.
175 template <int dummy>
176 class single_client_gc_alloc_template {
177 public:
178 static void * allocate(size_t n)
179 {
180 size_t nwords = GC_round_up(n);
181 void ** flh;
182 void * op;
183
184 if (n > GC_max_fast_bytes) return GC_malloc(n);
185 flh = &GC_objfreelist_ptr[nwords];
186 op = *flh;
187 if (0 == op) {
188 return GC_aux::GC_out_of_line_malloc(nwords, GC_NORMAL);
189 }
190 *flh = GC_obj_link(op);
191 GC_aux::GC_bytes_recently_allocd += nwords * GC_bytes_per_word;
192 return op;
193 }
194 static void * ptr_free_allocate(size_t n)
195 {
196 size_t nwords = GC_round_up(n);
197 void ** flh;
198 void * op;
199
200 if (n > GC_max_fast_bytes) return GC_malloc_atomic(n);
201 flh = &GC_aobjfreelist_ptr[nwords];
202 op = *flh;
203 if (0 == op) {
204 return GC_aux::GC_out_of_line_malloc(nwords, GC_PTRFREE);
205 }
206 *flh = GC_obj_link(op);
207 GC_aux::GC_bytes_recently_allocd += nwords * GC_bytes_per_word;
208 return op;
209 }
210 static void deallocate(void *p, size_t n)
211 {
212 if (n > GC_max_fast_bytes) {
213 GC_free(p);
214 } else {
215 size_t nwords = GC_round_up(n);
216 void ** flh = &GC_objfreelist_ptr[nwords];
217
218 GC_obj_link(p) = *flh;
219 memset(reinterpret_cast<char *>(p) + GC_bytes_per_word, 0,
220 GC_bytes_per_word * (nwords - 1));
221 *flh = p;
222 GC_aux::GC_bytes_recently_freed += nwords * GC_bytes_per_word;
223 }
224 }
225 static void ptr_free_deallocate(void *p, size_t n)
226 {
227 if (n > GC_max_fast_bytes) {
228 GC_free(p);
229 } else {
230 size_t nwords = GC_round_up(n);
231 void ** flh = &GC_aobjfreelist_ptr[nwords];
232
233 GC_obj_link(p) = *flh;
234 *flh = p;
235 GC_aux::GC_bytes_recently_freed += nwords * GC_bytes_per_word;
236 }
237 }
238 };
239
240 typedef single_client_gc_alloc_template<0> single_client_gc_alloc;
241
242 // Once more, for uncollectible objects.
243 template <int dummy>
244 class single_client_traceable_alloc_template {
245 public:
246 static void * allocate(size_t n)
247 {
248 size_t nwords = GC_round_up_uncollectable(n);
249 void ** flh;
250 void * op;
251
252 if (n > GC_max_fast_bytes) return GC_malloc_uncollectable(n);
253 flh = &GC_uobjfreelist_ptr[nwords];
254 op = *flh;
255 if (0 == op) {
256 return GC_aux::GC_out_of_line_malloc(nwords, GC_UNCOLLECTABLE);
257 }
258 *flh = GC_obj_link(op);
259 GC_aux::GC_uncollectable_bytes_recently_allocd +=
260 nwords * GC_bytes_per_word;
261 return op;
262 }
263 static void * ptr_free_allocate(size_t n)
264 {
265 size_t nwords = GC_round_up_uncollectable(n);
266 void ** flh;
267 void * op;
268
269 if (n > GC_max_fast_bytes) return GC_malloc_atomic_uncollectable(n);
270 flh = &GC_auobjfreelist_ptr[nwords];
271 op = *flh;
272 if (0 == op) {
273 return GC_aux::GC_out_of_line_malloc(nwords, GC_AUNCOLLECTABLE);
274 }
275 *flh = GC_obj_link(op);
276 GC_aux::GC_uncollectable_bytes_recently_allocd +=
277 nwords * GC_bytes_per_word;
278 return op;
279 }
280 static void deallocate(void *p, size_t n)
281 {
282 if (n > GC_max_fast_bytes) {
283 GC_free(p);
284 } else {
285 size_t nwords = GC_round_up_uncollectable(n);
286 void ** flh = &GC_uobjfreelist_ptr[nwords];
287
288 GC_obj_link(p) = *flh;
289 *flh = p;
290 GC_aux::GC_uncollectable_bytes_recently_freed +=
291 nwords * GC_bytes_per_word;
292 }
293 }
294 static void ptr_free_deallocate(void *p, size_t n)
295 {
296 if (n > GC_max_fast_bytes) {
297 GC_free(p);
298 } else {
299 size_t nwords = GC_round_up_uncollectable(n);
300 void ** flh = &GC_auobjfreelist_ptr[nwords];
301
302 GC_obj_link(p) = *flh;
303 *flh = p;
304 GC_aux::GC_uncollectable_bytes_recently_freed +=
305 nwords * GC_bytes_per_word;
306 }
307 }
308 };
309
310 typedef single_client_traceable_alloc_template<0> single_client_traceable_alloc;
311
312 template < int dummy >
313 class gc_alloc_template {
314 public:
315 static void * allocate(size_t n) { return GC_malloc(n); }
316 static void * ptr_free_allocate(size_t n)
317 { return GC_malloc_atomic(n); }
318 static void deallocate(void *, size_t) { }
319 static void ptr_free_deallocate(void *, size_t) { }
320 };
321
322 typedef gc_alloc_template < 0 > gc_alloc;
323
324 template < int dummy >
325 class traceable_alloc_template {
326 public:
327 static void * allocate(size_t n) { return GC_malloc_uncollectable(n); }
328 static void * ptr_free_allocate(size_t n)
329 { return GC_malloc_atomic_uncollectable(n); }
330 static void deallocate(void *p, size_t) { GC_free(p); }
331 static void ptr_free_deallocate(void *p, size_t) { GC_free(p); }
332 };
333
334 typedef traceable_alloc_template < 0 > traceable_alloc;
335
336 // We want to specialize simple_alloc so that it does the right thing
337 // for all pointer-free types. At the moment there is no portable way to
338 // even approximate that. The following approximation should work for
339 // SGI compilers, and recent versions of g++.
340
341 // GC_SPECIALIZE() is used internally.
342 #define GC_SPECIALIZE(T,alloc) \
343 class simple_alloc<T, alloc> { \
344 public: \
345 static T *allocate(size_t n) \
346 { return 0 == n? 0 : \
347 reinterpret_cast<T*>(alloc::ptr_free_allocate(n * sizeof(T))); } \
348 static T *allocate(void) \
349 { return reinterpret_cast<T*>(alloc::ptr_free_allocate(sizeof(T))); } \
350 static void deallocate(T *p, size_t n) \
351 { if (0 != n) alloc::ptr_free_deallocate(p, n * sizeof(T)); } \
352 static void deallocate(T *p) \
353 { alloc::ptr_free_deallocate(p, sizeof(T)); } \
354 };
355
356 __STL_BEGIN_NAMESPACE
357
358 GC_SPECIALIZE(char, gc_alloc)
359 GC_SPECIALIZE(int, gc_alloc)
360 GC_SPECIALIZE(unsigned, gc_alloc)
361 GC_SPECIALIZE(float, gc_alloc)
362 GC_SPECIALIZE(double, gc_alloc)
363
364 GC_SPECIALIZE(char, traceable_alloc)
365 GC_SPECIALIZE(int, traceable_alloc)
366 GC_SPECIALIZE(unsigned, traceable_alloc)
367 GC_SPECIALIZE(float, traceable_alloc)
368 GC_SPECIALIZE(double, traceable_alloc)
369
370 GC_SPECIALIZE(char, single_client_gc_alloc)
371 GC_SPECIALIZE(int, single_client_gc_alloc)
372 GC_SPECIALIZE(unsigned, single_client_gc_alloc)
373 GC_SPECIALIZE(float, single_client_gc_alloc)
374 GC_SPECIALIZE(double, single_client_gc_alloc)
375
376 GC_SPECIALIZE(char, single_client_traceable_alloc)
377 GC_SPECIALIZE(int, single_client_traceable_alloc)
378 GC_SPECIALIZE(unsigned, single_client_traceable_alloc)
379 GC_SPECIALIZE(float, single_client_traceable_alloc)
380 GC_SPECIALIZE(double, single_client_traceable_alloc)
381
382 __STL_END_NAMESPACE
383
384 #ifdef __STL_USE_STD_ALLOCATORS
385
386 __STL_BEGIN_NAMESPACE
387
388 template <class _Tp>
389 struct _Alloc_traits<_Tp, gc_alloc >
390 {
391 static const bool _S_instanceless = true;
392 typedef simple_alloc<_Tp, gc_alloc > _Alloc_type;
393 typedef __allocator<_Tp, gc_alloc > allocator_type;
394 };
395
396 inline bool operator==(const gc_alloc&,
397 const gc_alloc&)
398 {
399 return true;
400 }
401
402 inline bool operator!=(const gc_alloc&,
403 const gc_alloc&)
404 {
405 return false;
406 }
407
408 template <class _Tp>
409 struct _Alloc_traits<_Tp, single_client_gc_alloc >
410 {
411 static const bool _S_instanceless = true;
412 typedef simple_alloc<_Tp, single_client_gc_alloc > _Alloc_type;
413 typedef __allocator<_Tp, single_client_gc_alloc > allocator_type;
414 };
415
416 inline bool operator==(const single_client_gc_alloc&,
417 const single_client_gc_alloc&)
418 {
419 return true;
420 }
421
422 inline bool operator!=(const single_client_gc_alloc&,
423 const single_client_gc_alloc&)
424 {
425 return false;
426 }
427
428 template <class _Tp>
429 struct _Alloc_traits<_Tp, traceable_alloc >
430 {
431 static const bool _S_instanceless = true;
432 typedef simple_alloc<_Tp, traceable_alloc > _Alloc_type;
433 typedef __allocator<_Tp, traceable_alloc > allocator_type;
434 };
435
436 inline bool operator==(const traceable_alloc&,
437 const traceable_alloc&)
438 {
439 return true;
440 }
441
442 inline bool operator!=(const traceable_alloc&,
443 const traceable_alloc&)
444 {
445 return false;
446 }
447
448 template <class _Tp>
449 struct _Alloc_traits<_Tp, single_client_traceable_alloc >
450 {
451 static const bool _S_instanceless = true;
452 typedef simple_alloc<_Tp, single_client_traceable_alloc > _Alloc_type;
453 typedef __allocator<_Tp, single_client_traceable_alloc > allocator_type;
454 };
455
456 inline bool operator==(const single_client_traceable_alloc&,
457 const single_client_traceable_alloc&)
458 {
459 return true;
460 }
461
462 inline bool operator!=(const single_client_traceable_alloc&,
463 const single_client_traceable_alloc&)
464 {
465 return false;
466 }
467
468 __STL_END_NAMESPACE
469
470 #endif /* __STL_USE_STD_ALLOCATORS */
471
472 #endif /* GC_ALLOC_H */