re-entrant libraries - is C deficient?
Alan Grant Finlay
alanf at bruce.cs.monash.OZ.AU
Wed Oct 3 01:21:42 AEST 1990
I am writing a library of C procedures which need access to a global
data structure. I wish to write them in a portable manner (i.e. even
outside UNIX) and to be re-entrant.
Currently I am doing this by having a compulsary first parameter to all
the procedures which is a pointer to the global data structure (apart from
one procedure which allocates this data structure and returns the pointer).
This is fine but the inefficiency annoys me. A lot of these procedures call
each other so the pointer is popped off and onto the stack all the time. I
know popping it off doesn't cost anything but pushing on again does. What
I would like to do (and could do in assembly) is to push it on the stack
once (i.e. implicitly or explicitly dedicate a register to the task of
locating this pointer).
I'm sure there are other languages that provide a solution to this problem.
For example in Pascal I can have nested procedures (I know this is not quite
a solution since the application must be compiled in the scope of the nested
procedures - but it is part way to a solution).
My question is: have I overlooked something or do I need to descend into
assembly language to solve this problem in C (assuming a C compiler has a
"base" register, can do without a register or provides some means to know
where the stack base is)? I know there are solutions for each particular
operating system but I want a techique that can be ported without having to
worry about such matters (i.e if all else fails I'll stick with the wasted
parameter).
More information about the Comp.lang.c
mailing list