2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
4 * Copyright 1996 Silicon Graphics. All rights reserved.
6 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
7 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
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.
16 * Some simple primitives for allocation with explicit type information.
17 * Facilities for dynamic type inference may be added later.
18 * Should be used only for extremely performance critical applications,
19 * or if conservative collector leakage is otherwise a problem (unlikely).
20 * Note that this is implemented completely separately from the rest
21 * of the collector, and is not linked in unless referenced.
22 * This does not currently support GC_DEBUG in any interesting way.
36 typedef GC_word * GC_bitmap;
37 /* The least significant bit of the first word is one if */
38 /* the first word in the object may be a pointer. */
40 #define GC_WORDSZ (8 * sizeof(GC_word))
41 #define GC_get_bit(bm, index) \
42 (((bm)[(index) / GC_WORDSZ] >> ((index) % GC_WORDSZ)) & 1)
43 #define GC_set_bit(bm, index) \
44 ((bm)[(index) / GC_WORDSZ] |= (GC_word)1 << ((index) % GC_WORDSZ))
45 #define GC_WORD_OFFSET(t, f) (offsetof(t,f) / sizeof(GC_word))
46 #define GC_WORD_LEN(t) (sizeof(t) / sizeof(GC_word))
47 #define GC_BITMAP_SIZE(t) ((GC_WORD_LEN(t) + GC_WORDSZ - 1) / GC_WORDSZ)
49 typedef GC_word GC_descr;
51 GC_API GC_descr GC_CALL GC_make_descriptor(GC_bitmap /* bm */,
53 /* Return a type descriptor for the object whose layout */
54 /* is described by the argument. */
55 /* The least significant bit of the first word is one */
56 /* if the first word in the object may be a pointer. */
57 /* The second argument specifies the number of */
58 /* meaningful bits in the bitmap. The actual object */
59 /* may be larger (but not smaller). Any additional */
60 /* words in the object are assumed not to contain */
62 /* Returns a conservative approximation in the */
63 /* (unlikely) case of insufficient memory to build */
64 /* the descriptor. Calls to GC_make_descriptor */
65 /* may consume some amount of a finite resource. This */
66 /* is intended to be called once per type, not once */
69 /* It is possible to generate a descriptor for a C type T with */
70 /* word aligned pointer fields f1, f2, ... as follows: */
72 /* GC_descr T_descr; */
73 /* GC_word T_bitmap[GC_BITMAP_SIZE(T)] = {0}; */
74 /* GC_set_bit(T_bitmap, GC_WORD_OFFSET(T,f1)); */
75 /* GC_set_bit(T_bitmap, GC_WORD_OFFSET(T,f2)); */
77 /* T_descr = GC_make_descriptor(T_bitmap, GC_WORD_LEN(T)); */
79 GC_API void * GC_CALL GC_malloc_explicitly_typed(size_t /* size_in_bytes */,
81 /* Allocate an object whose layout is described by d. */
82 /* The resulting object MAY NOT BE PASSED TO REALLOC. */
83 /* The returned object is cleared. */
85 GC_API void * GC_CALL GC_malloc_explicitly_typed_ignore_off_page(
86 size_t /* size_in_bytes */,
89 GC_API void * GC_CALL GC_calloc_explicitly_typed(size_t /* nelements */,
90 size_t /* element_size_in_bytes */,
92 /* Allocate an array of nelements elements, each of the */
93 /* given size, and with the given descriptor. */
94 /* The element size must be a multiple of the byte */
95 /* alignment required for pointers. E.g. on a 32-bit */
96 /* machine with 16-bit aligned pointers, size_in_bytes */
97 /* must be a multiple of 2. */
98 /* Returned object is cleared. */
101 # define GC_MALLOC_EXPLICITLY_TYPED(bytes, d) GC_MALLOC(bytes)
102 # define GC_CALLOC_EXPLICITLY_TYPED(n, bytes, d) GC_MALLOC((n) * (bytes))
104 # define GC_MALLOC_EXPLICITLY_TYPED(bytes, d) \
105 GC_malloc_explicitly_typed(bytes, d)
106 # define GC_CALLOC_EXPLICITLY_TYPED(n, bytes, d) \
107 GC_calloc_explicitly_typed(n, bytes, d)
111 } /* matches extern "C" */
114 #endif /* GC_TYPED_H */