| 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. |
| 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 |
/* |
| 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. |
| 24 |
*/ |
| 25 |
|
| 26 |
#ifndef GC_TYPED_H |
| 27 |
#define GC_TYPED_H |
| 28 |
|
| 29 |
#ifndef GC_H |
| 30 |
# include "gc.h" |
| 31 |
#endif |
| 32 |
|
| 33 |
#ifdef __cplusplus |
| 34 |
extern "C" { |
| 35 |
#endif |
| 36 |
|
| 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. */ |
| 40 |
|
| 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) |
| 49 |
|
| 50 |
typedef GC_word GC_descr; |
| 51 |
|
| 52 |
GC_API GC_descr GC_CALL GC_make_descriptor(const GC_word * /* GC_bitmap bm */, |
| 53 |
size_t /* len (number_of_bits_in_bitmap) */); |
| 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. */ |
| 69 |
|
| 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)); */ |
| 79 |
|
| 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 size may NOT be less than the number of */ |
| 85 |
/* meaningful bits in the bitmap of d multiplied by */ |
| 86 |
/* sizeof GC_word. The returned object is cleared. */ |
| 87 |
/* The returned object may NOT be passed to GC_realloc. */ |
| 88 |
|
| 89 |
GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void * GC_CALL |
| 90 |
GC_malloc_explicitly_typed_ignore_off_page(size_t /* size_in_bytes */, |
| 91 |
GC_descr /* d */); |
| 92 |
|
| 93 |
GC_API GC_ATTR_MALLOC GC_ATTR_CALLOC_SIZE(1, 2) void * GC_CALL |
| 94 |
GC_calloc_explicitly_typed(size_t /* nelements */, |
| 95 |
size_t /* element_size_in_bytes */, |
| 96 |
GC_descr /* d */); |
| 97 |
/* Allocate an array of nelements elements, each of the */ |
| 98 |
/* given size, and with the given descriptor. */ |
| 99 |
/* The element size must be a multiple of the byte */ |
| 100 |
/* alignment required for pointers. E.g. on a 32-bit */ |
| 101 |
/* machine with 16-bit aligned pointers, size_in_bytes */ |
| 102 |
/* must be a multiple of 2. The element size may NOT */ |
| 103 |
/* be less than the number of meaningful bits in the */ |
| 104 |
/* bitmap of d multiplied by sizeof GC_word. */ |
| 105 |
/* Returned object is cleared. */ |
| 106 |
|
| 107 |
#ifdef GC_DEBUG |
| 108 |
# define GC_MALLOC_EXPLICITLY_TYPED(bytes, d) ((void)(d), GC_MALLOC(bytes)) |
| 109 |
# define GC_CALLOC_EXPLICITLY_TYPED(n, bytes, d) \ |
| 110 |
((void)(d), GC_MALLOC((n) * (bytes))) |
| 111 |
#else |
| 112 |
# define GC_MALLOC_EXPLICITLY_TYPED(bytes, d) \ |
| 113 |
GC_malloc_explicitly_typed(bytes, d) |
| 114 |
# define GC_CALLOC_EXPLICITLY_TYPED(n, bytes, d) \ |
| 115 |
GC_calloc_explicitly_typed(n, bytes, d) |
| 116 |
#endif |
| 117 |
|
| 118 |
#ifdef __cplusplus |
| 119 |
} /* extern "C" */ |
| 120 |
#endif |
| 121 |
|
| 122 |
#endif /* GC_TYPED_H */ |