This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

proposal: explicit context pointers in addition to trampolines in C frontend


Hi,

I've been thinking about trampolines and nested functions in C the other
day. I really like using trampolines for callback functions, such as in

	http://svn.clifford.at/spl/trunk/spl_modules/mod_xml.c

see:

  static struct spl_node *handler_xml2tree(struct spl_task *task, void *data)
  {
	[...]

	void XMLCALL element_start_hdl(void *data, const char *el, const char **attr)
	{
		[...]
	}

	[ ... ]

	XML_SetElementHandler(p, element_start_hdl, element_end_hdl);
  }

sure - I could also pack all the stuff which should be shared between
handler_xml2tree() and its element_start_hdl() in a struct and pass a
pointer to that struct as first argument to element_start_hdl(). But it
simply is easier (faster to implement) to do that with the trampoline
mechanism.

But, on the other hand, trampolines aren't very portable and require
an executeable stack, which isn't nice. So, it would be cool to be able to
do something like the following:

  static struct spl_node *handler_xml2tree(struct spl_task *task, void *data)
  {
	[...]

	void XMLCALL element_start_hdl(void *data
			__attribute__ (( contextpointer(handler_xml2tree) )),
			const char *el, const char **attr)
	{
		[...]
	}

	[ ... ]

	XML_SetElementHandler(p, element_start_hdl, element_end_hdl);
	XML_SetUserData(p, __builtin_contextpointer(handler_xml2tree));
  }

so, because the 1st argument of element_start_hdl() has this new, fancy,
attributute, it is using it to access the stack frame of its
handler_xml2tree(). The __builtin_contextpointer() is used to generate this
context pointers and no trampoline is ever generated when using
element_start_hdl as function pointer.

It's not that I would really _need_ this feature. It just came to my mind
when thinking about the trampoline thread from some time ago and I didn't
want the idea to get lost...

"You can now flame me, I am full of love."  ;-)

yours,
 - clifford

-- 
/"\  ASCII Ribbon Campaign - against html email
\ /                        - against microsoft office attachments
 X                         - against text above fullquote below
/ \                        - against lines longer than 79 characters
 
"The generation of random numbers is too important to be left to chance."
 - Robert R. Coveyou, Oak Ridge National Laboratory.
 

Attachment: pgp00000.pgp
Description: PGP signature


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]