This is the mail archive of the gcc-help@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]

alloca/setjmp in caller's context?


alloca/setjmp in caller's context?


Hey, odd question.
I know C pretty darn well and am stumped.
I don't know gcc extensions well, and am possibly
willing to use them.


I have a homebrow exception handling mechanism.
In a language that isn't C, but can and does occasionally call out to C.
?Yes, it'd be nice to not the current exception mechanism.
?? e.g. to use _Unwind_RaiseException.
?That is definitely being considered.
?
?
?In the meantime, we have code like this:
?
?
?struct { ..stuff.. jmp_buf* jb } Frame;
?void PushFrame(Frame* f) // This function is actually written in C and is where possible tricks could be.
?{
? stuff with pthread_getspecific, maintaining a thread local linked list of frames 
?}
?
?
?void F1()
?{
? "TRY" ...
? }
?
?
?
? // this is because the different parts are output
? // by different parts, and this isn't all C code,
? // and I won't want to know the jmp_buf size where the
? // alloca is output; but I link in a separate bit of C
? // It used to be we did know the jmpbuf size but it is a maintenance/portability heachace
? // and still leaves some of this question valid -- setjmp but not alloca
? extern int jmpbuf_size = sizeof(jmp_buf);
? 
? =>
? void F1()
? {
? Frame frame;
? 
? frame.jb = alloca(jmpbuf_size);
? setjmp(frame.jb);
? PushFrame(&frame);
? }
? 

? so, then, the cost of "try" is two function calls and alloca.
? all obviously very inefficient, I understand.
? alloca is inlined but large in code size, at least when not optimized.
?
?
? Ordinally, code size would be easy to improve here by making
? it just be one function call. However setjmp and alloca are
? so special, they can't be buried inside another function.
?
?
?Ideas for doing so??
?
?
? An easier form of this question is, is there some clever and/or
? gcc-specific way of writing something like:
? 
? char* strdup_alloca(const char* a)
? {
??? size_t length = strlen(a);
??? char* b = (char*)alloca(length + 1);
??? memcpy(b, a, length + 1);
??? return b;
? }
? 
? and/or

? int setjmp_and_other(jmp_buf jb)
? {
??? printf("calling setjmp...\n");
??? return setjmp(jb);
? }

?
but for which the alloca actually is valid in just the immediate caller,
or the setjmp within the caller's context?

Yeah yeah, I know about malloc.
And I know about macros. If source code size was an issue, I'd use them.


Thanks,
?? - Jay
 		 	   		  


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