[comp.sys.ibm.pc] Mac 32K segmentation and program porting.

werner@aecom.yu.edu (Craig Werner) (11/28/89)

	I have the unenviable task of porting a program I wrote under
Unix to microcomputers.  It is a simple program. Basically, it takes
stdin, digests it based on command line arguments, and sends it to
stdout. Porting from Unix to MSDOS involved changing several ints to long
ints, and that was it.  I expected some trouble going to the Mac (such as
lack of stdin, a command line, and stdout) but I am absolutely stumped by
the first glitch, and that is the "code segment too big" error.
Apparently, the Macintosh operating system limits code segments to 32K.
I've heard of segment complaints with the Intel architecture, but in that
case it is the compiler's worry, not mine.  The THINK C documentation is
extremely unenlightening in how to get around this.

	So I appeal to those who may have done this before.  What is the
easiest way to split a program so that all the code segments are
acceptable to the Macintosh.  I'm asking for quick and dirty guidelines,
shortcuts and tricks.  Surely somebody in one of these groups has made
the transition before.  I don't want to become a Macintosh programmer. I
just want to make a program work.


-- 
	        Craig Werner   (future MD/PhD, 4.5 years down, 2.5 to go)
	     werner@aecom.YU.EDU -- Albert Einstein College of Medicine
              (1935-14E Eastchester Rd., Bronx NY 10461, 212-931-2517)
           "Someone write me a letter. I need to know that I'm still alive."

vallon@sbcs.sunysb.edu (Justin Vallon) (11/29/89)

In article <2627@aecom.yu.edu> werner@aecom.yu.edu (Craig Werner) writes:
>[Porting a Unix program to Mac.  Think C complained about code segment]
>[greater than 32K.]

>[How do I fix it?]

Boy, this one is easy.  The reason that the Mac has a 32K code resource
limit is because code resources are relocatable, and the 68000 relocatable
address mode can only take an offset of 16-bits (+32767 to -32768).  To
make sure that every instruction can reference the other within a single
code seg, code segments are (usually) limited to 32K.  Some environments
(like Aztec C, I think) can create code segments greater than 32K.

But, your problem is easily solved.  What you want to do is create another
code segment.  Two code segments give 64K of code.  You can create as
many segments as you want.  However, don't go crazy.  Keep the size to
about 25K each, although of course that's an opinion.  There is no real
difference (speed or otherwise) between placing two files in the same, or
different segments.

How to, in Think C:  Open the Project window.  You'll see a dark gray bar
going down the left side of the window.  This denotes segment one (although
numbers really don't matter).  To create another, drag one of the source
file entries (ie: foo.c  4590) down past the last entry.  Now, you'll see
foo.c is in a different segment, because it will have it's own bar next
to it.  Now, you can drag more files into that segment by clicking on it,
and dragging it right on top of foo.c.  Or, you could create a third seg
by dragging it past foo.c, into the empty area.

Here is some background, if you want it.  You don't have to read or under-
stand this to write a segmented Mac program.  When Think C generates code
for a call to a routine within the same segment, (I believe) relative
addressing is used.  When generating call for a routine in another segment,
it calls the routine by calling a compiler-generated Jump-Table Entry.
The Jump-Table Entry does one of two things.  If the segment is loaded,
then it just calls the routine that is specified.  If the segment is not
loaded, it loads the segment, and then calls the routine.

When you program launches, only one segment is loaded [in Think C, it's
probably the one with main()].  As you call routines in other segs, the
Jump-Table entry loads the segment, and sets a flag that insures that
(a) it isn't loaded again, and (b) all other references to routines in that
segment will not bother loading the segment.

Therefore, if your program contains 4 segments, there will be only three
(don't count that main seg) times when the performance of any routine is
affected.  Loading a segment is quite fast (on the order of 1/2 second),
so don't worry about it slowing down the program.

If you want to learn more, read Inside Macintosh, Segment Loader chapter
(I forget which volume, probably III).

>	        Craig Werner   (future MD/PhD, 4.5 years down, 2.5 to go)
>	     werner@aecom.YU.EDU -- Albert Einstein College of Medicine
>              (1935-14E Eastchester Rd., Bronx NY 10461, 212-931-2517)
>           "Someone write me a letter. I need to know that I'm still alive."

-Justin
vallon@sbcs.sunysb.edu