Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers gc_typed.h Source File

gc_typed.h

00001 /* 
00002  * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
00003  * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
00004  * Copyright 1996 Silicon Graphics.  All rights reserved.
00005  *
00006  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
00007  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
00008  *
00009  * Permission is hereby granted to use or copy this program
00010  * for any purpose,  provided the above notices are retained on all copies.
00011  * Permission to modify the code and to distribute modified code is granted,
00012  * provided the above notices are retained, and a notice that the code was
00013  * modified is included with the above copyright notice.
00014  */
00015 /*
00016  * Some simple primitives for allocation with explicit type information.
00017  * Facilities for dynamic type inference may be added later.
00018  * Should be used only for extremely performance critical applications,
00019  * or if conservative collector leakage is otherwise a problem (unlikely).
00020  * Note that this is implemented completely separately from the rest
00021  * of the collector, and is not linked in unless referenced.
00022  * This does not currently support GC_DEBUG in any interesting way.
00023  */
00024 /* Boehm, May 19, 1994 2:13 pm PDT */
00025 
00026 #ifndef _GC_TYPED_H
00027 # define _GC_TYPED_H
00028 # ifndef _GC_H
00029 #   include "gc.h"
00030 # endif
00031 
00032 #ifdef __cplusplus
00033   extern "C" {
00034 #endif
00035 typedef GC_word * GC_bitmap;
00036     /* The least significant bit of the first word is one if    */
00037     /* the first word in the object may be a pointer.       */
00038     
00039 # define GC_WORDSZ (8*sizeof(GC_word))
00040 # define GC_get_bit(bm, index) \
00041         (((bm)[index/GC_WORDSZ] >> (index%GC_WORDSZ)) & 1)
00042 # define GC_set_bit(bm, index) \
00043         (bm)[index/GC_WORDSZ] |= ((GC_word)1 << (index%GC_WORDSZ))
00044 # define GC_WORD_OFFSET(t, f) (offsetof(t,f)/sizeof(GC_word))
00045 # define GC_WORD_LEN(t) (sizeof(t)/ sizeof(GC_word))
00046 # define GC_BITMAP_SIZE(t) ((GC_WORD_LEN(t) + GC_WORDSZ-1)/GC_WORDSZ)
00047 
00048 typedef GC_word GC_descr;
00049 
00050 GC_API GC_descr GC_make_descriptor GC_PROTO((GC_bitmap bm, size_t len));
00051         /* Return a type descriptor for the object whose layout */
00052         /* is described by the argument.            */
00053         /* The least significant bit of the first word is one   */
00054         /* if the first word in the object may be a pointer.    */
00055         /* The second argument specifies the number of      */
00056         /* meaningful bits in the bitmap.  The actual object    */
00057         /* may be larger (but not smaller).  Any additional */
00058         /* words in the object are assumed not to contain   */
00059         /* pointers.                        */
00060         /* Returns a conservative approximation in the      */
00061         /* (unlikely) case of insufficient memory to build  */
00062         /* the descriptor.  Calls to GC_make_descriptor     */
00063         /* may consume some amount of a finite resource.  This  */
00064         /* is intended to be called once per type, not once */
00065         /* per allocation.                  */
00066 
00067 /* It is possible to generate a descriptor for a C type T with  */
00068 /* word aligned pointer fields f1, f2, ... as follows:          */
00069 /*                                  */
00070 /* GC_descr T_descr;                                                    */
00071 /* GC_word T_bitmap[GC_BITMAP_SIZE(T)] = {0};               */
00072 /* GC_set_bit(T_bitmap, GC_WORD_OFFSET(T,f1));              */
00073 /* GC_set_bit(T_bitmap, GC_WORD_OFFSET(T,f2));              */
00074 /* ...                                  */
00075 /* T_descr = GC_make_descriptor(T_bitmap, GC_WORD_LEN(T));      */
00076 
00077 GC_API GC_PTR GC_malloc_explicitly_typed
00078             GC_PROTO((size_t size_in_bytes, GC_descr d));
00079         /* Allocate an object whose layout is described by d.   */
00080         /* The resulting object MAY NOT BE PASSED TO REALLOC.   */
00081         /* The returned object is cleared.          */
00082 
00083 GC_API GC_PTR GC_malloc_explicitly_typed_ignore_off_page
00084                         GC_PROTO((size_t size_in_bytes, GC_descr d));
00085         
00086 GC_API GC_PTR GC_calloc_explicitly_typed
00087             GC_PROTO((size_t nelements,
00088                   size_t element_size_in_bytes,
00089                   GC_descr d));
00090     /* Allocate an array of nelements elements, each of the */
00091     /* given size, and with the given descriptor.       */
00092     /* The elemnt size must be a multiple of the byte   */
00093     /* alignment required for pointers.  E.g. on a 32-bit   */
00094     /* machine with 16-bit aligned pointers, size_in_bytes  */
00095     /* must be a multiple of 2.             */
00096     /* Returned object is cleared.              */
00097 
00098 #ifdef GC_DEBUG
00099 #   define GC_MALLOC_EXPLICITLY_TYPED(bytes, d) GC_MALLOC(bytes)
00100 #   define GC_CALLOC_EXPLICITLY_TYPED(n, bytes, d) GC_MALLOC(n*bytes)
00101 #else
00102 #  define GC_MALLOC_EXPLICITLY_TYPED(bytes, d) \
00103     GC_malloc_explicitly_typed(bytes, d)
00104 #  define GC_CALLOC_EXPLICITLY_TYPED(n, bytes, d) \
00105     GC_calloc_explicitly_typed(n, bytes, d)
00106 #endif /* !GC_DEBUG */
00107 
00108 #ifdef __cplusplus
00109   } /* matches extern "C" */
00110 #endif
00111 
00112 #endif /* _GC_TYPED_H */
00113