Mapped Access to Files (NM) (new) [ Introduction to MPE XL for MPE V Programmers Migration Guide ] MPE/iX 5.0 Documentation
Introduction to MPE XL for MPE V Programmers Migration Guide
Mapped Access to Files (NM)
(new)
A major enhancement to the MPE XL file system is mapped access to files,
an option that allows you to access a disk file directly through memory
load and store instructions. You choose the mapped access option in NM
(Native Mode programming environment) through two HPFOPEN intrinsic
parameters:
* The short mapped option returns a 32-bit value of type address.
* The long mapped option returns a 64-bit value of type address.
Using the mapped option to access files requires the use of pointers, a
datatype available in Pascal or C.
Accessing With Mapped Option
To use the mapped option to access files, declare a short (32-bit) or
long (64-bit) pointer variable within a program, and pass that variable
to the appropriate HPFOPEN parameter. When HPFOPEN returns the variable,
it is pointing to the beginning of the data area of the opened file.
After HPFOPEN returns the address of the file, you can simply reference
the pointer as an array.
Some files restrict the type of access, like READ or READ/WRITE, that
will be allowed with the mapped access option. Some can not be opened at
all.
The following file types are allowed any type of access when they are
opened using a mapped access option:
* standard files with fixed-length record formats.
* standard files with undefined-length record formats.
The following file types are restricted to READ-ONLY access when they are
opened using a mapped access option:
* standard files with variable-length record formats.
* KSAM files opened with copy mode option enabled.
* standard files with variable-length record formats.
The following file types are not allowed to be opened at all with a
mapped access option:
* circular (CIR) files.
* device files.
* message (MSG) files.
* relative input/output (RIO) files.
* any files with a negative file code, such as privileged files like
Turboimage databases.
You can use all applicable file system intrinsics with a file that has
been opened with the mapped access option, including all data transfer
intrinsics. However, when mixing file system data transfer intrinsic
calls (such as FREAD and FWRITE) with a mapped access option, you must
consider the data type, the record format, and the record size of the
file. Otherwise, the data you write to the file using the option may not
make sense when it is read by FREAD.
When you open a file using a mapped access option and write data to that
file, you must use the FPOINT and FCONTROL intrinsics to reset the EOF
before you close the file. Otherwise, all data you write to the file
after the EOF will be lost when you close the file. In the case of a
newly created file, the EOF initially points to record zero.
NOTE When you access a file via mapped access option you are bypassing
certain file system services that set various file system pointers
automatically, including the EOF and the logical record pointer.
You are responsible for resetting the EOF prior to closing a file
you have accessed with map option.
You are also bypassing file system posting, so if data recovery is
needed you should use FCONTROL controlcodes to post data and update
the EOF periodically. Heavy use of the FCONTROL intrinsic to post
data and set the EOF will degrade performance due to the overhead
of the extra posting.
Advantages of
Mapped Access
Option
There are two perspectives you can take on mapped access to files:
* A file is accessible as virtual memory. The advantages from this
perspective are high performance and fast response time from the file
system.
* Virtual memory is accessed through the file system. Three advantages
from this perspective are that virtual memory can be easily and
permanently saved, it can be "checkpointed", and it can be easily
shared through a common naming convention.
Accessing a file with a mapped option can be faster than accessing it
through normal file system intrinsics. This is especially true when you
are accessing a smaller file randomly rather than sequentially. With a
mapped access option, there is no file system overhead associated with a
specific reference to the file.
The only differences between using the mapped access option and using
normal memory are the locality of the access and the protection strategy.
Disadvantages of
Mapped Access
Option
It is possible to show a degradation of performance if an application
which accesses files sequentially is modified to access files via mapped
option. Normal file system reads are buffered to pre-fetch multiple
records per read. The mapped access option does minimal pre-fetching of
data, and consequently some performance penalty is paid by additional
overhead on page faults.
You cannot access a loaded program file or a loaded library file using
mapped access option. In addition, you cannot load or execute a program
file that is currently being accessed with the mapped options.
Short or Long
Mapped Access?
The short mapped option is available in the HPFOPEN intrinsic to provide
you with shared virtual memory. A short pointer is returned in the
optional item parameter. You can use the pointer as a large array of any
type to efficiently access the file.
The long mapped option is available in the HPFOPEN intrinsic to provide
you with access to shared virtual memory. You can use the pointer as a
large array of any type to efficiently access the file.
A file opened using the short mapped option can be up to four megabytes
in size. A process can have up to six megabytes of files open at the
same time that were created using the short mapped option. If you need
more, use the long mapped option. A file opened using the long mapped
option can be up to two gigabytes in size.
Using the long mapped access option has two advantages over the short
mapped option. You can access much larger files. You can open files
that were opened previously with any options (as long as the exclusive
status of the file is not violated).
The disadvantage of using the long mapped access option is that it may be
slower than using the short mapped option because of the need to load a
space register to access the long pointer space. Long mapped access can
be as much as four times slower than short mapped access, although the
long mapped option can still operate faster than accessing a file through
the intrinsics for file system data transfer. The performance
degradation can be minimized if you make the references to the long
pointer space in a localized part of your code.
You cannot use either mapped access option to access a loaded program
file or a loaded library file. In addition, you cannot load or execute a
program file that is currently being accessed with either mapped access
option.
If you attempt to use the short mapped option to open a file that has
been previously opened normally or with the long mapped option, you will
receive an error.
Mapped Access Example
The following example illustrates how a file is created and opened with
the short mapped access option. The Pascal XL procedure opens the file,
then writes data to the file via assignments to the array structure. The
procedure then sets the EOF and closes the file. The file is then
reopened with short mapped option and data is retrieved before the file
is closed and purged.
______________________________________________________________________________________
| |
| procedure Mapped_File_Example; {for use within a program} |
| |
| type |
| |
| {** defines an 80 byte record **} |
| record_t = record |
| a_record : packed array [1..80] of char; |
| end; |
| |
| {** define a 4,000,000 byte array **} |
| file_t = array [1..50000] of record_t; |
| |
| var |
| access : integer; |
| domain : integer; |
| dummy : shortint; |
| file_name : packed array [1..20] of char; |
| file_number : integer; |
| file_ptr : ^file_t; {** pointer to the file **}|
| filesize : integer; |
| index : integer; |
| rec : integer; |
| create_domain_perm: integer; |
| domain_OLD : integer; |
| read_write_access: integer; |
| status : record |
| case integer of |
| 0: (all: integer); |
| 1: (info: shortint; |
| subsys: shortint); |
| end; |
| |
| |
| const |
| itemnum_2 = 2; {** file name option **} |
| itemnum_3 = 3; {** domain option **} |
| itemnum_35 = 35; {** filesize option **} |
| itemnum_18 = 18; {** short mapped option**} |
| itemnum_11 = 11 {** access type option **} |
| |
| |
______________________________________________________________________________________
_______________________________________________________________________________________
| |
| begin {** initialize item values for HPFOPEN **} |
| file_name := '%EXAMPLE%'; |
| create_domain_PERM := 4; |
| domain_OLD := 3; |
| filesize := 15265; |
| read_write_access := 4; |
| |
| |
| HPFOPEN ( {** create a short mapped file **} |
| file_number, status, |
| itemnum_2, file_name, |
| itemnum_3, create_domain_PERM, |
| itemnum_35, filesize, |
| itemnum_18, file_ptr, |
| itemnum_11, read_write_access, |
| ); |
| |
| |
| for rec := 1 to 100 do {** put some data into the file **} |
| for index := 1 to 80 do |
| file_ptr^[rec].a_record[index] := Chr (((rec - 1) Mod 26) + 65); |
| |
| |
| FPOINT (file_number, 33); {** set the logical record pointer **} |
| FCONTROL (file_number, 6, dummy); {** set the EOF **} |
| FCLOSE (file_number, 0, 0); {** close the file **}|
| |
| |
| HPFOPEN ( {** re-open the same short mapped file **} |
| file_number, status, |
| itemnum_2, file_name, |
| itemnum_3, domain_OLD, |
| itemnum_18, file_ptr, |
| ); |
| |
| |
| for rec := 1 to 100 do {** retrieve some data you put in file **} |
| begin |
| write ('Record#', rec:4, ' '); |
| for index := 1 to 20 do |
| write (file_ptr^[rec].a_record[index]); |
| writeln; |
| end; |
| |
| FCLOSE (file_number, 4, 0); {** close and purge the file **} |
| end; |
| |
| |
_______________________________________________________________________________________
MPE/iX 5.0 Documentation