Import boehm-gc snapshot, taken from
[official-gcc.git] / boehm-gc / include / gc_typed.h
blob8261cd7b1c81d28d89ccc60ff2f7f42b51d8c22f
1 /*
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.
17 * Some simple primitives for allocation with explicit type information.
18 * Facilities for dynamic type inference may be added later.
19 * Should be used only for extremely performance critical applications,
20 * or if conservative collector leakage is otherwise a problem (unlikely).
21 * Note that this is implemented completely separately from the rest
22 * of the collector, and is not linked in unless referenced.
23 * This does not currently support GC_DEBUG in any interesting way.
26 #ifndef GC_TYPED_H
27 #define GC_TYPED_H
29 #ifndef GC_H
30 # include "gc.h"
31 #endif
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
37 typedef GC_word * GC_bitmap;
38 /* The least significant bit of the first word is one if */
39 /* the first word in the object may be a pointer. */
41 #define GC_WORDSZ (8 * sizeof(GC_word))
42 #define GC_get_bit(bm, index) \
43 (((bm)[(index) / GC_WORDSZ] >> ((index) % GC_WORDSZ)) & 1)
44 #define GC_set_bit(bm, index) \
45 ((bm)[(index) / GC_WORDSZ] |= (GC_word)1 << ((index) % GC_WORDSZ))
46 #define GC_WORD_OFFSET(t, f) (offsetof(t,f) / sizeof(GC_word))
47 #define GC_WORD_LEN(t) (sizeof(t) / sizeof(GC_word))
48 #define GC_BITMAP_SIZE(t) ((GC_WORD_LEN(t) + GC_WORDSZ - 1) / GC_WORDSZ)
50 typedef GC_word GC_descr;
52 GC_API GC_descr GC_CALL GC_make_descriptor(const GC_word * /* GC_bitmap bm */,
53 size_t /* len */);
54 /* Return a type descriptor for the object whose layout */
55 /* is described by the argument. */
56 /* The least significant bit of the first word is one */
57 /* if the first word in the object may be a pointer. */
58 /* The second argument specifies the number of */
59 /* meaningful bits in the bitmap. The actual object */
60 /* may be larger (but not smaller). Any additional */
61 /* words in the object are assumed not to contain */
62 /* pointers. */
63 /* Returns a conservative approximation in the */
64 /* (unlikely) case of insufficient memory to build */
65 /* the descriptor. Calls to GC_make_descriptor */
66 /* may consume some amount of a finite resource. This */
67 /* is intended to be called once per type, not once */
68 /* per allocation. */
70 /* It is possible to generate a descriptor for a C type T with */
71 /* word aligned pointer fields f1, f2, ... as follows: */
72 /* */
73 /* GC_descr T_descr; */
74 /* GC_word T_bitmap[GC_BITMAP_SIZE(T)] = {0}; */
75 /* GC_set_bit(T_bitmap, GC_WORD_OFFSET(T,f1)); */
76 /* GC_set_bit(T_bitmap, GC_WORD_OFFSET(T,f2)); */
77 /* ... */
78 /* T_descr = GC_make_descriptor(T_bitmap, GC_WORD_LEN(T)); */
80 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
81 GC_malloc_explicitly_typed(size_t /* size_in_bytes */,
82 GC_descr /* d */);
83 /* Allocate an object whose layout is described by d. */
84 /* The resulting object MAY NOT BE PASSED TO REALLOC. */
85 /* The returned object is cleared. */
87 GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL
88 GC_malloc_explicitly_typed_ignore_off_page(size_t /* size_in_bytes */,
89 GC_descr /* d */);
91 GC_API GC_ATTR_MALLOC void * GC_CALL
92 GC_calloc_explicitly_typed(size_t /* nelements */,
93 size_t /* element_size_in_bytes */,
94 GC_descr /* d */);
95 /* Allocate an array of nelements elements, each of the */
96 /* given size, and with the given descriptor. */
97 /* The element size must be a multiple of the byte */
98 /* alignment required for pointers. E.g. on a 32-bit */
99 /* machine with 16-bit aligned pointers, size_in_bytes */
100 /* must be a multiple of 2. */
101 /* Returned object is cleared. */
103 #ifdef GC_DEBUG
104 # define GC_MALLOC_EXPLICITLY_TYPED(bytes, d) GC_MALLOC(bytes)
105 # define GC_CALLOC_EXPLICITLY_TYPED(n, bytes, d) GC_MALLOC((n) * (bytes))
106 #else
107 # define GC_MALLOC_EXPLICITLY_TYPED(bytes, d) \
108 GC_malloc_explicitly_typed(bytes, d)
109 # define GC_CALLOC_EXPLICITLY_TYPED(n, bytes, d) \
110 GC_calloc_explicitly_typed(n, bytes, d)
111 #endif
113 #ifdef __cplusplus
114 } /* matches extern "C" */
115 #endif
117 #endif /* GC_TYPED_H */