📄 gcc_support.c
字号:
/***************************************************************************Interface between g++ and Boehm GC Copyright (c) 1991-1995 by Xerox Corporation. All rights reserved. THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED OR IMPLIED. ANY USE IS AT YOUR OWN RISK. Permission is hereby granted to copy this code for any purpose, provided the above notices are retained on all copies. Last modified on Sun Jul 16 23:21:14 PDT 1995 by ellisThis module provides runtime support for implementing theEllis/Detlefs GC proposal, "Safe, Efficient Garbage Collection forC++", within g++, using its -fgc-keyword extension. It definesversions of __builtin_new, __builtin_new_gc, __builtin_vec_new,__builtin_vec_new_gc, __builtin_delete, and __builtin_vec_delete thatinvoke the Bohem GC. It also implements the WeakPointer.h interface.This module assumes the following configuration options of the Boehm GC: -DALL_INTERIOR_POINTERS -DDONT_ADD_BYTE_AT_END This module adds its own required padding to the end of objects tosupport C/C++ "one-past-the-object" pointer semantics.****************************************************************************/#include <stddef.h>#include "gc.h"#if defined(__STDC__) # define PROTO( args ) args#else# define PROTO( args ) ()# endif#define BITSPERBYTE 8 /* What's the portable way to do this? */typedef void (*vfp) PROTO(( void ));extern vfp __new_handler;extern void __default_new_handler PROTO(( void ));/* A destructor_proc is the compiler generated procedure representing a C++ destructor. The "flag" argument is a hidden argument following somecompiler convention. */typedef (*destructor_proc) PROTO(( void* this, int flag ));/***************************************************************************A BI_header is the header the compiler adds to the front ofnew-allocated arrays of objects with destructors. The header ispadded out to a double, because that's what the compiler does toensure proper alignment of array elements on some architectures. int NUM_ARRAY_ELEMENTS (void* o) returns the number of array elements for array object o.char* FIRST_ELEMENT_P (void* o) returns the address of the first element of array object o.***************************************************************************/typedef struct BI_header { int nelts; char padding [sizeof( double ) - sizeof( int )]; /* Better way to do this? */} BI_header;#define NUM_ARRAY_ELEMENTS( o ) \ (((BI_header*) o)->nelts)#define FIRST_ELEMENT_P( o ) \ ((char*) o + sizeof( BI_header ))/***************************************************************************The __builtin_new routines add a descriptor word to the end of eachobject. The descriptor serves two purposes. First, the descriptor acts as padding, implementing C/C++ pointersemantics. C and C++ allow a valid array pointer to be incrementedone past the end of an object. The extra padding ensures that thecollector will recognize that such a pointer points to the object andnot the next object in memory.Second, the descriptor stores three extra pieces of information,whether an object has a registered finalizer (destructor), whether itmay have any weak pointers referencing it, and for collectible arrays,the element size of the array. The element size is required for thearray's finalizer to iterate through the elements of the array. (Analternative design would have the compiler generate a finalizerprocedure for each different array type. But given the overhead offinalization, there isn't any efficiency to be gained by that.)The descriptor must be added to non-collectible as well as collectibleobjects, since the Ellis/Detlefs proposal allows "pointer to gc T" tobe assigned to a "pointer to T", which could then be deleted. Thus,__builtin_delete must determine at runtime whether an object iscollectible, whether it has weak pointers referencing it, and whetherit may have a finalizer that needs unregistering. ThoughGC_REGISTER_FINALIZER doesn't care if you ask it to unregister afinalizer for an object that doesn't have one, it is a non-trivialprocedure that does a hash look-up, etc. The descriptor trades alittle extra space for a significant increase in time on the fast paththrough delete. (A similar argument applies toGC_UNREGISTER_DISAPPEARING_LINK).For non-array types, the space for the descriptor could be shrunk to asingle byte for storing the "has finalizer" flag. But this would savespace only on arrays of char (whose size is not a multiple of the wordsize) and structs whose largest member is less than a word in size(very infrequent). And it would require that programmers actuallyremember to call "delete[]" instead of "delete" (which they should,but there are probably lots of buggy programs out there). For themoment, the space savings seems not worthwhile, especially consideringthat the Boehm GC is already quite space competitive with othermalloc's.Given a pointer o to the base of an object:Descriptor* DESCRIPTOR (void* o) returns a pointer to the descriptor for o.The implementation of descriptors relies on the fact that the GCimplementation allocates objects in units of the machine's naturalword size (e.g. 32 bits on a SPARC, 64 bits on an Alpha).**************************************************************************/typedef struct Descriptor { unsigned has_weak_pointers: 1; unsigned has_finalizer: 1; unsigned element_size: BITSPERBYTE * sizeof( unsigned ) - 2; } Descriptor;#define DESCRIPTOR( o ) \ ((Descriptor*) ((char*)(o) + GC_size( o ) - sizeof( Descriptor )))/**************************************************************************Implementations of global operator new() and operator delete()***************************************************************************/void* __builtin_new( size ) size_t size; /* For non-gc non-array types, the compiler generates calls to __builtin_new, which allocates non-collected storage via GC_MALLOC_UNCOLLECTABLE. This ensures that the non-collected storage will be part of the collector's root set, required by the Ellis/Detlefs semantics. */{ vfp handler = __new_handler ? __new_handler : __default_new_handler; while (1) { void* o = GC_MALLOC_UNCOLLECTABLE( size + sizeof( Descriptor ) ); if (o != 0) return o; (*handler) ();}}void* __builtin_vec_new( size ) size_t size; /* For non-gc array types, the compiler generates calls to __builtin_vec_new. */{ return __builtin_new( size );}void* __builtin_new_gc( size ) size_t size; /* For gc non-array types, the compiler generates calls to __builtin_new_gc, which allocates collected storage via GC_MALLOC. */{ vfp handler = __new_handler ? __new_handler : __default_new_handler; while (1) { void* o = GC_MALLOC( size + sizeof( Descriptor ) ); if (o != 0) return o; (*handler) ();}}void* __builtin_new_gc_a( size ) size_t size; /* For non-pointer-containing gc non-array types, the compiler generates calls to __builtin_new_gc_a, which allocates collected storage via GC_MALLOC_ATOMIC. */{ vfp handler = __new_handler ? __new_handler : __default_new_handler; while (1) { void* o = GC_MALLOC_ATOMIC( size + sizeof( Descriptor ) ); if (o != 0) return o; (*handler) ();}}void* __builtin_vec_new_gc( size ) size_t size; /* For gc array types, the compiler generates calls to __builtin_vec_new_gc. */{ return __builtin_new_gc( size );}void* __builtin_vec_new_gc_a( size ) size_t size; /* For non-pointer-containing gc array types, the compiler generates calls to __builtin_vec_new_gc_a. */{ return __builtin_new_gc_a( size );}static void call_destructor( o, data ) void* o; void* data; /* call_destructor is the GC finalizer proc registered for non-array gc objects with destructors. Its client data is the destructor proc, which it calls with the magic integer 2, a special flag obeying the compiler convention for destructors. */{ ((destructor_proc) data)( o, 2 );}void* __builtin_new_gc_dtor( o, d ) void* o; destructor_proc d; /* The compiler generates a call to __builtin_new_gc_dtor to register the destructor "d" of a non-array gc object "o" as a GC finalizer. The destructor is registered via GC_REGISTER_FINALIZER_IGNORE_SELF, which causes the collector to ignore pointers from the object to itself when determining when the object can be finalized. This is necessary due to the self pointers used in the internal representation of multiply-inherited objects. */{ Descriptor* desc = DESCRIPTOR( o ); GC_REGISTER_FINALIZER_IGNORE_SELF( o, call_destructor, d, 0, 0 );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -