Joy of Joys (porting C-shell)

Stuart Thompson stuart at hcr.UUCP
Tue Jun 28 04:19:39 AEST 1988


The C-shell has some VAX machine dependent code which makes some very basic
assumptions about the address space of the VAX machine to control memory
allocation/dealloction.  Due to the way the C-shell mixes pointers to data
from both the data segment (static data) and the break area (dynamic data),
a special function called xfree() is used to determine which data area on
the stack a pointer refers to before memory is deallocated (using free()).

In the figure below, the data segment, which includes both the "bss" and
"data" areas of the stack has a lower address space than that of the break
area (shown as "sbrk area" the figure).  The xfree() function does the
following:

xfree(cp)
char *cp;
{
	extern int end;	/* end of data segment */

	if (cp >= end && cp < (char *)&cp)
		free();
}

The if statement in xfree() checks that:
	1) cp points above the data segment, i.e. not in the data segment
	2) the address for the actual data for cp is below the current
	   stack frame, i.e. is not local in the current stack frame

           VAX  STACK
hi addr _________________
        |               |
        |               | <- pointer to data (&cp)
        |===============| current stack frame
        |               |
        |---------------|
        |               |
        |     sbrk      |
        |     area      |
        |               | <- address of data (cp)
        |               |
        |---------------| (end)
        |     bss       |
        |---------------|
        |     data      |
lo addr -----------------


It should be noted that the if statement in xfree() will only work on machines
with a stack structure similar to that of the VAX.  The onlyread() function
in the C-shell also contains the same sort of machine dependent assumptions.

For many other machine architectures whose address space does not correspond
to that of a VAX, porting the C-shell requires making interesting workarounds.
One such implementation is in the VX/VE UNIX emulation on CDC CYBER machines.
Segments on CYBERs are quite different than on a VAX, the break area resides
in an extensible segment which has no fixed relation to either the stack segment
or data segments.  In this case, we must modify xfree() and onlyread() to
compare segment numbers between pointers rather than relative stack addresses.

A truly portable C-shell should use non-machine dependent mechanisms to
distinguish between pointers to dynamic data and pointers to static data.

_____
Stuart E. Thompson
Just another techie
{utzoo,utcsri}!hcr!stuart



More information about the Comp.unix.wizards mailing list