Sorry for the long email, i find that i need to provide a whole lot of
history as to why i am doing things the way i am so people understand
what i am trying to ask...

If you want to know why i was doing things read on, otherwise i have
some simple questions at the end since i have discovered that the way i
was going about it was going to fail.


Mike Stump wrote:
> On Feb 11, 2007, at 1:17 PM, Brendon Costa wrote:
>> I am coding an extension for GCC and am having some difficulty with
>> pre-compiled headers. I dont know if my understanding of how they work
>> is completely correct and so my code is getting a segfault.
> 
> You _must_ have clean data structures and complete type safety.  You
> cannot have any pointers to GCed memory any place but GCed memory, or
> file data marked with GTY(()) markers.  The last rule is a good
> approximation, cpp for examples deviates from this general rule.
> 
>> So i also hook into c_common_no_more_pch()
> 
> Ick.  You seem to have thought too much about pch.  Instead, pretend it
> isn't there, except for doing the GTY(()) thing on all static scope data.
> 
I understand that for data managed in GCC you shouldn't have to think
about PCH as the dumping of data to the PCH file and the re-constructing
of the data upon importing a PCH (mmap) should all be done for you. My
problem is that I am trying to do a "quick hack" to get my code to work
alongside the PCH mechanism where my code does not make any use of the
GCC garbage collector. After thinking about it more the approach i was
going to try as a quick hack will not work anyway.


> c_common_no_more_pch seems like the wrong place.  You calculate things
> when you have the data to calculate from and someplace to put it, but
> the code there.  You want to do it at the end of file processing, put it
> there.  You want to do it from the parser, or optimizer, put it there.
> 

I calculate my data just before a function is gimplified. The problem is
that the resulting data structures i am producing from analyzing the
body of the function are not managed under the GCC garbage collector and
so do not get dumped to the PCH file and then "reconstructed"/mmap'ed
back into the GCC process upon importing a precompiled header. So my
plan was to place a list of FUNCTION_DECL nodes under a garbage
collected static root which will be dumped and reconstructed by the PCH
process as normal and then "recalculate" my data structures from the
reconstructed FUNCTION_DECL nodes.

It kinda works (The segfault was because i forgot to use TREE_VALUE on
the chained items to get the actual FUNCTION_DECL nodes). However the
problem i mentioned before about the lowering of the FUNCTION_DECL has
occurred and so in the end i cant successfully recalculate my data
structures from the FUNCTION_DECL nodes.

It was a quick hack idea to try and get around the big task of moving
all my data structures to be managed by the GCC garbage collector.

This was based on an idea from Mike in Nov 2006 when i needed to ensure
that the FUNCTION_DECL nodes were not collected by the garbage collector
while i was still using them. You mentioned that i could do the above to
get the garbage collector to think that the nodes are still being used.
I also assumed that it could be used for working around the PCH engine.


The problem is that not only is this a bad way of going about it (I
should just re-write my code to use the garbage collector to manage my
data structures, though it is a big job) but that the FUNCTION_DECL
nodes that would be dumped to the PCH and then reconstructed by the PCH
mechanism will have been lowered to GIMPLE or even RTL. I need to
process the body of the FUNCTION_DECL nodes before they are lowered.


>> 1) Is there a better place to hook into to know when the PCH engine
>> has completed reconstructing the tree nodes?
> 
> See why this is the wrong question?  The question is, I am doing this
> calculation, where should I put it?  The answer to that is, when you
> have all the data from which you need to do the calculation and a place
> to store it.
> 
>> 2) Is there something wrong in the way i understand the reconstruction
>> of data by the PCH engine? I assumed it would re-construct a full tree
>> hierarchy if i rooted a tree node.
> 
> Yes.
> 
> The mental model you should use for PCH is this, when processing a
> header, the compiler does a complete memory walk and dumps it to a
> file.  Upon `reading' the pch file, it mmaps all that memory back in,
> throwing out all previously allocated memory, and continues just after
> the #include.
> 
> If you have any code that knows about pch (other than GTY(())), you're
> probably trying to be too smart about pchness.  I can't tell if you are
> or not, unless you tell me why you want to be.
> 

Yes. I was trying to get my code to work with PCH without having to use
the GCC garbage collector.

-- Why You Might Ask --
In order to "correctly" support PCH and make use of the GCC garbage
collector i will need to re-write a lot of the code i have. This is a
big task and one i was going to tackle at a later date.

This is the first GCC hacking i have done. I started a couple of years
ago on this project and before i found out what all that GTY(()) stuff
was for I had been writing this extension for about a year. I now have a
working system that only fails with PCH files. I want to release soon
and I may yet just say that my system does not work with PCH files,
however I was looking for a "quick hack" that will enable this to work.

I hope that explains what i was trying to achieve and why.

Now i think i have resolved myself to the fact that if i want to support
PCH files, then I will need to modify my code to use the GTY markers and
GCC garbage collector.


I am using GCC 4.0.1






--------- Questions -----------
* Does GCC use the PCH feature to speed up compilation of its standard
libraries?

* If so is there a simple way of disabling this at configure time etc?

* Is it possible to use the GTY(()) flags inside macros?

For example:

A lot of my code makes use of some basic containers i created. This
makes memory management much easier for me. I created a "template array
class" in C as one example of a container. It looks something like:

#define DECLARE_ARRAY(DataTypeName, DataType) \
struct DataTypeName##ArrayStruct \
{ \
   size_t count; \
   size_t size; \
   DataType* array; \
}; \
typedef struct DataTypeName##ArrayStruct DataTypeName##Array; \
typedef int(*DataTypeName##CompareFunction)(const DataType*, \
   const DataType*); \
void DataTypeName##Array_Constructor(DataTypeName##Array* object); \
void DataTypeName##Array_Destructor(DataTypeName##Array* object); \
void DataTypeName##Array_Push(DataTypeName##Array* object, const
DataType* value); \

...ETC...

I would instanciate a declaration of this class using the above macro like:

DECLARE_ARRAY(Char, char)

and would in a .c file implement it with a similar macro:

#define char_Constructor(ArgOne) *ArgOne = 0
#define char_Destructor(ArgOne) *ArgOne = 0
#define char_Assign(ArgOne, ArgTwo) *ArgOne = *ArgTwo
IMPLEMENT_ARRAY(Char, char)


Now i use this "template class" for creating arrays of all sorts of
different data types including char for making variable sized strings.

So would it be possible to place the markers inside the macro or will i
have to extract the structures from the macros in order to place the GTY
markers in them?


* Is it possible to explicitly free garbage collected memory if i know i
will not be needing it any more (Yes this defeats the purpose of garbage
collection but can significantly reduce the runtime memory usage and i
have all the code there already to free the memory)?

For example when i need to expand the size of the array i will free the
old array and malloc a new one. It would be good to still be able to do
this with the garbage collector.


Thanks for the help.
Brendon.




Reply via email to