JRT Pascal User's Guide version 3.0 NOT FOR SALE -131- 11. Storage management This section discusses the initialization and structure of main storage in the JRT Pascal system during execution of Pascal programs. 11.1 Main storage When a Pascal program is started by entering the command "EXEC prog_name", the EXEC.COM file is loaded into main storage at address 100H by the CP/M operating system. After EXEC receives control from CP/M, it determines how much storage is available and formats this area. EXEC then loads the Pascal program module from disk. Processing of the Pascal program then begins. During program execution, there are four main regions of main storage. Starting from the lowest address, these are: 1. EXEC - The run-time environment. This region is fixed in size and contains the primary run-time support system. 2. Pascal program module - This region is fixed in size and contains the compiled Pascal program from an '.INT' file. 3. Data stack - This region is variable in size. It begins at the end of the Pascal program and grows toward higher addresses (and toward the dynamic storage region, discussed next). This region contains all static variables (those created by VAR declarations), parameters passed to procedures and procedure activation blocks. 4. Dynamic storage - This region is variable in size. It begins at the top of available storage and grows down toward lower addresses (and towards the data stack, previously discussed). This region contains dynamic variables (those created by the NEW procedure), input/output buffers, file control blocks, external procedures and EXEC control tables. Since the data stack and the dynamic storage regions grow toward each other, a collision between these areas is possible when storage is nearly full. To prevent this condition, the run-time system maintains a 64 byte cushion between the two areas. When the cushion becomes less than 64 bytes, the run-time system takes several actions to restore the cushion. Copy compliments of Merle Schnick SECTION 11: Storage management JRT Pascal User's Guide version 3.0 NOT FOR SALE -132- If there is less than 64 bytes of free space in main storage, the least-recently-used procedure will be deleted. Dynamic storage is then compressed (see section 11.2 of this manual). Processing will continue even if the cushion cannot be restored, although performance will gradually decrease. Only if there is actually a colllision between the data stack and the dynamic storage regions will the run-time system recognize an error condition and terminate processing. Copy compliments of Merle Schnick SECTION 11: Storage management JRT Pascal User's Guide version 3.0 NOT FOR SALE -133- Map of main storage use in JRT Pascal system: high ------------------------------- address I DYNAMIC STORAGE I I I I variable in size I I I I direction ! I I of growth ! I I V I I-----------------------------I I unused area (CUSHION) I 64 bytes I-----------------------------I I DATA STACK I I I I variable in size I I I I direction ^ I I of growth ! I I ! I I-----------------------------I I PASCAL PROGRAM I I INT module I I I I fixed in size I I-----------------------------I I EXEC.COM I I run-time system I I I I fixed in size I low I I address ------------------------------- 100H Copy compliments of Merle Schnick SECTION 11: Storage management JRT Pascal User's Guide version 3.0 NOT FOR SALE -134- 11.2 Dynamic storage The JRT Pascal run-time system provides TRUE dynamic storage with auto-compression. Virtual storage is supported for external procedures. The JRT Pascal Dynamic Storage Management System is designed to provide complete support for advanced features such as dynamic data structures (linked lists, trees, rings,...) and completely automatic virtual storage for external procedure and function code. Dynamic storage may contain these items: 1. external procedures/functions 2. dynamic variables created by the NEW procedure 3. input/output buffers 4. file control blocks 5. EXEC control blocks and pointer tables 6. a free list of de-allocated storage blocks All of these items are allocated as blocks of dynamic storage. Dynamic storage blocks are addressed indirectly in JRT Pascal in order to allow the blocks to be moved during compression by updating a pointer table. The value stored in a pointer variable by the execution of the NEW procedure is a "virtual address" rather than the real address of the block allocated. The virtual address is used to locate an entry in an internal table called a pointer table, which contains the size and real address of each storage block. There may be up to 32 pointer tables and each one contains up to 52 entries for storage blocks. During dynamic storage compression, the real address of a storage block may change, but the virtual address does not change. The dynamic storage manager performs these services: 1. Format dynamic storage and initialize pointer tables. 2. Maintain the free list. This is a linked list which contains blocks of storage which have been de-allocated (by the DISPOSE procedure, by closing a file, or by purging of an external procedure). 3. Allocate a storage block. When a storage block is requested (by the NEW procedure, opening a file, or loading an external procedure), the storage manager attempts to satisfy this request by searching the free list or extending the dynamic storage region. When scanning the free list for a block, the first block which is large enough is selected. If this block is much too large, it is split and the remainder returned to the free list. After a block has been found, its real address, size, and a flag field are entered in a pointer table. 4. Release a block of storage. This adds a de-allocated block to the free list and deletes the corresponding pointer table entries. Copy compliments of Merle Schnick SECTION 11: Storage management JRT Pascal User's Guide version 3.0 NOT FOR SALE -135- 5. Determine the amount of free space. The free space is the sum of the sizes of all blocks on the free list and the size of the gap between the data stack region and the dynamic storage region. 6. Compress dynamic storage. All of the allocated storage blocks are moved into the top of storage to eliminate free space. The free list is set to a null pointer. The pointer table entries of all blocks are updated. If external procedures were moved then their relocatable addresses are adjusted. If active external procedures were moved then the Pascal program counter and the procedure return addresses are adjusted. 7. Convert the virtual address of a block to a real address. Copy compliments of Merle Schnick SECTION 11: Storage management re adjusted. 7. Convert the virtual address of a block to a real address.