TweetFollow Us on Twitter

Segment
Volume Number:2
Issue Number:11
Column Tag:Basic School

Using the Segment Manager from Basic

By Dave Kelly, MacTutor Editorial Board

This month we'll discuss Macintosh memory management (part 2, part 1 was discussed back in the June 1985 MacTutor). While the information back in June 1985 is still valid, there are some other (new?) considerations that I haven't mentioned yet. If you have the June '85 MacTutor go back and review that information as it will be useful here as we talk about it some more. It would also be helpful to read Inside Macintosh Vol. I chapter 3 on Memory Management and Vol II chapter 2 on the Segment Loader.

Now that we have as much memory available as we ever dreamed we might have (remember when it was so great to have 16K RAM?), we need to talk about the best ways to make use of it. In review, please recall that the key to memory management in MS Basic is the CLEAR statement. Even on a Macintosh Plus you should use CLEAR to make the best use of the available memory. The following procedure will help you to quickly set up the memory configuration:

1. Load your program and before running it type PRINT FRE(0) in the command window to determine the amount of memory used by your program listing. Record this number for later use.

2. Run the program. The intent here is to determine the amount of memory that your variables need. For some programs you may not be able to run enough of the program to declare all of the variables. In that case you may have to do some rough guessing here. Either after running your program or sometime during the program (this may be more convenient) you should print FRE(0) again. The difference between this number and the number you recorded in step 1 above is the amount of memory allocated to the data segment that is used for your variables. (FRE(-1) returns the number of bytes not used in the heap; FRE(-2) returns the number of bytes in the stack that have not been used; you may use these if it will help you).

3. Now it's time to guess. Try to decide if the program will ever need more memory than this in the future. If you need to increase the program size later or allocate more variables you will need to allow some space for this. If you are not sure you may want to just add 20% or so more memory to the length of the program. Whatever you decide here will be your data segment memory.

4. Now, if your program uses any Macintosh resources you will need to figure out how much of the heap to use. If you have extra memory like on the Mac plus it never hurts to have extra heap space. In fact, the heap may be more heavily used than the data segment depending on how many controls, or windows, etc. you may be using. Keep in mind also that if any desk accessories are opened they will need some heap space of their own. By printing FRE(-2) it will show the heap memory allocation. If you print FRE(-2) before running the program you will know the amount of heap available to the program. After the program is run and resources are loaded, the amount of heap will decrease. You need to know the difference between the FRE(-2) before and after running the program. Record this number.

5. Now you need to calculate the stack-size to use in the CLEAR statement. Many times it is not even necessary to change the data segment (leave it as large as the memory you have left). The number to use for the stack-size is calculated by stack-size = Total Ram - (data-segment-size + heap-size).

Fig. 1 Output of our Segment Program

Now how do you know when you need to adjust the memory allocation? Ans: Anytime that you notice an unusual amount of disk activity, especially just before displaying controls (edit fields, buttons, menus, windows etc.). It doesn't matter if you have a Mac Plus or not, if you don't adjust your memory properly, then the memory will not be used efficiently. In order to remain compatible with the 512K & 128K Mac, the heap and stack default sizes are the same on the Mac plus. This may explain why so many Mac programs need to read the disk so often even though you may think you have a lot of memory (1Meg really is plenty for Basic!!). Good memory management planning can improve your program tremendously. Another thing to keep in mind is that once the CLEAR statement has been used to change the memory allocation it will remain that way for the next program that you load into memory. A second CLEAR statement will not reallocate the memory properly. It is best to quit to the finder and start all over unless the new program uses the same memory configuration.

So much for MS Basic memory management. The comments above in addition to those in the June '85 MacTutor should give you all you need to handle most of your memory management. Memory management in other languages is handled through use of the Segment Loader. Access to the Segment Loader is not provided in MS Basic. ZBasic provides the SEGMENT and SEGMENT RETURN statements which use the Segment Loader. We'll talk about those in a moment. First a few words about the Segment Loader. The Segment Loader allows you to divide up the code of your application into several parts. When the Finder starts up your application the Segment Loader is called to load in the main segment of your program. Other segments are loaded in automatically as needed. After a segment is no longer needed, your application can call the segment loader to make the segment purgable. This way if you have only a small amount of memory available you may still run a large program. The maximum size of a segment is 32K. Any code that is not used often may be in a separate segment so that it may be swapped in whenever it is needed. Another function of the Segment Loader is in supplying information from the Finder.

Your ZBasic code will be compiled into code segments (resources of type 'CODE') automatically, no matter if you use the SEGMENT statements or not. When the code reaches a length of about 28K the segment will end and the next segment will be loaded. The SEGMENT RETURN statement marks the end of the CODE segment and returns to the calling routine (cannot be called from the same segment). You should arrange your application such that a segment consists of a subroutine. The routines you decide to separate into segments preferably should be routines which are only used occasionally (example: print routines).

The following example program demonstrates how to "segmentize" your programs. However, because the segments in the example are small (less than 100 bytes), the segment memory swapping is virtually non-existent on a Macintosh plus (and probably all the other size Macs too!). If you really want to try this out, you'll have to write some larger CODE segments and run on a smaller Mac. You can check the segment sizes by choosing the COMPILE option of the ZBasic command menu. The sizes of the segments will be displayed. The remaining memory can be found with the MEM(-1) function. MEM(-1) returns the maximum amount of available memory for the program and variables.

Another part of the Segment Loader is supported by ZBasic with the FINDERINFO function. The FINDERINFO function returns information to your application which tells the application how the application was selected from the Finder. The function returns variables with the number of files selected, the name of the files and volume number, and a message variable indicating if the file should be loaded or printed. This way the files may be printed (or whatever) without going through the full set of initialization routines that your application may have. If no files have been selected then your application should open a new untitled document and continue from there. The syntax is: message%= FINDERINFO (count%, var$[(n)], type%[(n)], volume%[(n)]). The message variable is the result of the FINDERINFO function. If the result is equal to zero then the file should be loaded otherwise the file should be printed. The Count% variable returns the number of files . The variable var$(n) is a string variable or string array with the names of the files which have been selected. The file type of the selected files is stored in a long integer word which must be converted to a string by using the MKI$ statement. The volume% variable is the the disk volume where the file is located.

To get the FINDERINFO function to work, first you must compile your program as an application. There are two or three statements that must be used properly in order to compile the program so that the function will work. First type CREATOR "ffff" where "ffff" is the creator name which will be given to the application. The other statement that must match this one is the DEFOPEN "ffffcccc" statement which sets up the file type ("ffff") and the file creator ("cccc") for the data files created by your application. In the demo program the creator is "DAVE" and the file type is "DATA". The DEFOPEN statement is used in the application program when opening up new data files. Now that the creator is the same for the application as for the data, we need to set the bundle bit with the BUNDLE 1 statement. Type BUNDLE 1 in the command window. Now it is time to compile the program into an application. Select RUN* from the menus to create the application. New data files which are created with the application will now be linked to each other. Selecting one or more data files while in the Finder and selecting PRINT or OPEN will open the application which the data files belong and send info to the application via the FINDERINFO statement. To see the results of this function, you may want to run the demo and create a few sample data files and see what happens when they are selected for printing or to be opened.

If you have problems getting the FINDERINFO information, be sure that you set the creator and bundle bit before compiling the application. You should also be sure to define all the variables in the function before calling it. More information on the Segment Loader is available in Vol 2, Chap. 2 of Inside Macintosh (The Segment Loader chapter).

WINDOW OFF
REM Segment Loader Example ZBasic 3.02
REM ©MacTutor 1986
REM by Dave Kelly
DIM 31 Name$(5),Vol%(5),Type&(5),Mes$(1)
REM up to 5 files can be selected.
DEFSTR LONG
Count%=2
Message%=FINDERINFO(Count%, Name$(0), Type&(0),    Vol%(0))
X&=MEM(-1)
WINDOW 1,"Segment Sample",(50,100)-(475,300),257
IF Count%<>0 THEN GOSUB "DisplayFINDERINFO"
MENU 1,0,1,"File"
MENU 1,1,1,"Create Data File"
MENU 1,2,1,"Quit"
MENU 2,0,1,"Segments"
MENU 2,1,1,"Main Segment"
MENU 2,2,1,"1st Segment"
MENU 2,3,1,"2nd Segment"
MENU 2,4,1,"3rd Segment"
MENU 2,5,1,"Check memory available and unload unused     segments"
ON MENU GOSUB "Menuevent"
MENU ON
"Loop":
GOTO "Loop"
"Menuevent":
Menunumber=MENU(0)
Menuitem=MENU(1)
MENU
MENU OFF
ON Menunumber GOSUB "Filemenu","Segmenu"
MENU ON
RETURN
"Filemenu":
IF Menuitem=1 THEN "Openfile"
IF Menuitem<>2 THEN RETURN
MENU RESET
END
"Openfile":
DEF OPEN "DATADAVE"
Filename$=FILES$(0,"Create new file as ..." ,"Segment    DATA")
IF Filename$="" THEN RETURN
OPEN O,#1,Filename$
PRINT "New file named ";Filename$;" has been created.":PRINT
PRINT "Quit the program and click on "; Filename$; " to see    how the"
PRINT "FINDERINFO function works.  Select PRINT or OPEN  from the Finder."
PRINT "The FINDERINFO function will indicate what has been     selected."
PRINT "You may want to create multiple data files and try      printing"
PRINT "or opening them to see what happens."
RETURN
"Segmenu":
ON Menuitem GOSUB "Main", "Seg1", "Seg2", "Seg3",  "Memory"
RETURN
"Main":
PRINT "This is part of the main segment"
RETURN
SEGMENT
"Seg1"
PRINT "This is part of the 1st segment"
SEGMENT RETURN
SEGMENT
"Seg2"
PRINT "This is part of the 2nd segment"
SEGMENT RETURN
SEGMENT
"Seg3"
PRINT "This is part of the 3rd segment"
SEGMENT RETURN
SEGMENT
"Memory"
X&=MEM(-1)
PRINT "Memory = ";X&
SEGMENT RETURN
SEGMENT
"DisplayFINDERINFO":
CLS
Mes$(0)="Data file(s) should be loaded"
Mes$(1)="Data file(s) should be printed"
PRINT "Message% = ";Message%;" Therefore... "; Mes$(     Message%)
PRINT "Count%   = ";Count%; "file(s) have been passed to       this application"
PRINT "Filenames are:"
PRINT"Name","Type","Vol"
FOR C=0 TO Count%-1
PRINT Name$(C),MKI$(Type&(C)),Vol%(C)
NEXT C
SEGMENT RETURN
 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Fresh From the Land Down Under – The Tou...
After a two week hiatus, we are back with another episode of The TouchArcade Show. Eli is fresh off his trip to Australia, which according to him is very similar to America but more upside down. Also kangaroos all over. Other topics this week... | Read more »
TouchArcade Game of the Week: ‘Dungeon T...
I’m a little conflicted on this week’s pick. Pretty much everyone knows the legend of Dungeon Raid, the match-3 RPG hybrid that took the world by storm way back in 2011. Everyone at the time was obsessed with it, but for whatever reason the... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for July 19th, 2024. In today’s article, we finish up the week with the unusual appearance of a review. I’ve spent my time with Hot Lap Racing, and I’m ready to give my verdict. After... | Read more »
Draknek Interview: Alan Hazelden on Thin...
Ever since I played my first release from Draknek & Friends years ago, I knew I wanted to sit down with Alan Hazelden and chat about the team, puzzle games, and much more. | Read more »
The Latest ‘Marvel Snap’ OTA Update Buff...
I don’t know about all of you, my fellow Marvel Snap (Free) players, but these days when I see a balance update I find myself clenching my… teeth and bracing for the impact to my decks. They’ve been pretty spicy of late, after all. How will the... | Read more »
‘Honkai Star Rail’ Version 2.4 “Finest D...
HoYoverse just announced the Honkai Star Rail (Free) version 2.4 “Finest Duel Under the Pristine Blue" update alongside a surprising collaboration. Honkai Star Rail 2.4 follows the 2.3 “Farewell, Penacony" update. Read about that here. | Read more »
‘Vampire Survivors+’ on Apple Arcade Wil...
Earlier this month, Apple revealed that poncle’s excellent Vampire Survivors+ () would be heading to Apple Arcade as a new App Store Great. I reached out to poncle to check in on the DLC for Vampire Survivors+ because only the first two DLCs were... | Read more »
Homerun Clash 2: Legends Derby opens for...
Since launching in 2018, Homerun Clash has performed admirably for HAEGIN, racking up 12 million players all eager to prove they could be the next baseball champions. Well, the title will soon be up for grabs again, as Homerun Clash 2: Legends... | Read more »
‘Neverness to Everness’ Is a Free To Pla...
Perfect World Games and Hotta Studio (Tower of Fantasy) announced a new free to play open world RPG in the form of Neverness to Everness a few days ago (via Gematsu). Neverness to Everness has an urban setting, and the two reveal trailers for it... | Read more »
Meditative Puzzler ‘Ouros’ Coming to iOS...
Ouros is a mediative puzzle game from developer Michael Kamm that launched on PC just a couple of months back, and today it has been revealed that the title is now heading to iOS and Android devices next month. Which is good news I say because this... | Read more »

Price Scanner via MacPrices.net

Amazon is still selling 16-inch MacBook Pros...
Prime Day in July is over, but Amazon is still selling 16-inch Apple MacBook Pros for $500-$600 off MSRP. Shipping is free. These are the lowest prices available this weekend for new 16″ Apple... Read more
Walmart continues to sell clearance 13-inch M...
Walmart continues to offer clearance, but new, Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBooks... Read more
Apple is offering steep discounts, up to $600...
Apple has standard-configuration 16″ M3 Max MacBook Pros available, Certified Refurbished, starting at $2969 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free,... Read more
Save up to $480 with these 14-inch M3 Pro/M3...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
Amazon has clearance 9th-generation WiFi iPad...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Apple is offering a $50 discount on 2nd-gener...
Apple has Certified Refurbished White and Midnight HomePods available for $249, Certified Refurbished. That’s $50 off MSRP and the lowest price currently available for a full-size Apple HomePod today... Read more
The latest MacBook Pro sale at Amazon: 16-inc...
Amazon is offering instant discounts on 16″ M3 Pro and 16″ M3 Max MacBook Pros ranging up to $400 off MSRP as part of their early July 4th sale. Shipping is free. These are the lowest prices... Read more
14-inch M3 Pro MacBook Pros with 36GB of RAM...
B&H Photo has 14″ M3 Pro MacBook Pros with 36GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 Pro MacBook Pro (... Read more
14-inch M3 MacBook Pros with 16GB of RAM on s...
B&H Photo has 14″ M3 MacBook Pros with 16GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $150-$200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 MacBook Pro (... Read more
Amazon is offering $170-$200 discounts on new...
Amazon is offering a $170-$200 discount on every configuration and color of Apple’s M3-powered 15″ MacBook Airs. Prices start at $1129 for models with 8GB of RAM and 256GB of storage: – 15″ M3... Read more

Jobs Board

*Apple* Systems Engineer - Chenega Corporati...
…LLC,** a **Chenega Professional Services** ' company, is looking for a ** Apple Systems Engineer** to support the Information Technology Operations and Maintenance Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
*Apple* / Mac Administrator - JAMF Pro - Ame...
Amentum is seeking an ** Apple / Mac Administrator - JAMF Pro** to provide support with the Apple Ecosystem to include hardware and software to join our team and Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.