home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / cpluspls / vir_v203.zip / MANUAL.TXT < prev    next >
Text File  |  1992-07-11  |  17KB  |  538 lines

  1.     Introduction to Virtual Arrays in C
  2.  
  3. VIRTUAL AND HUGE ARRAYS Version 2.03
  4. by Graham Robertson
  5.  
  6.  
  7.     The Virtual Array manager in this package is designed 
  8. specifically to allow more memory than is normally available to 
  9. the C programmer by caching memory blocks to DISK or extended 
  10. memory. This is done by the use of a virtual memory manager 
  11. which transparently allows the programmer to access arrays of 
  12. any size. This not only makes programming of C programs with 
  13. large amounts of data easier but makes is portable to other 
  14. architecture's.
  15.  
  16.     Several example programs are included in the package which 
  17. quickly show the easy of use of the array manager. The array 
  18. manager is simplest to use when used with C++.
  19.  
  20.     The example programs are as follows
  21.  
  22. C++ programs
  23. --------------------------------------------------------------
  24.     ARRAYS.EXE         - Example of lots of Virtual 
  25. Arrays
  26.     TARRAYS.EXE        - Same examples but for compilers 
  27. that have templates
  28.  
  29.  
  30. C programs
  31.  
  32. --------------------------------------------------------------
  33.     HUGEARR.EXE        - A huge array
  34.     VIRARRAY.EXE        - Example of lots of Virtual 
  35. Arrays
  36.  
  37.  
  38. --------------------------------------------------------------
  39.  
  40. This software is copyright British Software Licensing 1991-
  41. 1992. The usual disclaimers apply.
  42.  
  43. The number of virtual arrays is restricted to sixteen in the 
  44. unregistered version.
  45.  
  46. If you use this software please send 20 pounds to British 
  47. Software Licensing. If you require a version which allows 
  48. unrestricted virtual arrays please register your copy of this 
  49. software. A copy of the source code can be obtained by sending 
  50. 50 pounds to British Software Licensing.
  51.  
  52. This price is for a single user license. A site license is 
  53. available at a very reasonable price. 
  54.  
  55. If you use this software in any other Shareware or commercial 
  56. application please contact the author to make arrangements.
  57.  
  58.  
  59.  
  60. --------------------------------------------------------------
  61. Order Form - Single User License only
  62. ---------------------------------------------------------------
  63. To    British Software Licensing
  64.     280 (T/L) West Princes Street,
  65.     Woodlands,
  66.     Glasgow
  67.     United Kingdom
  68.     G4 9EU
  69.  
  70. (prices are the same anywhere in the World)
  71. please supply me with:
  72.  
  73. an unrestricted copy of the large array manager  [   ]20 pounds
  74.  
  75. or
  76.  
  77. a copy of the source for the virtual array manger[   ]50 pounds
  78.  
  79.                             -------------
  80. Total payment
  81.                             --------------
  82.  
  83.  
  84. Payment type    ACCESS/VISA/MASTERCARD/CHECK(U.K. pounds)/POSTAL 
  85. ORDER
  86.  
  87. Name on Card                                      
  88.  
  89.  
  90. Card Number                                       
  91.  
  92.  
  93. Expiry date                                       
  94.  
  95.  
  96. Signature of Cardholder                                     
  97.  
  98. Name                                        
  99. Address                                                           
  100.  
  101.                                                                 
  102.  
  103.                                                                 
  104.  
  105.  
  106. --------------------------------------------------------------
  107.  
  108. You may also purchase this software from:
  109. Graham Robertson on
  110.                     041-339-8855x5021
  111. or on an Answering Machine    041-339-7264
  112. or via Email        Graham.J.S.Robertson@glasgow.ac.uk
  113. or by Fax on             041-334-1675
  114. quoting the details required above.
  115.  
  116.  
  117. --------------------------------------------------------------
  118.  
  119.  
  120.  Files in the package.
  121. VIRIMG.LIB        - Software to allow virtual and large arrays 
  122. VIRIMG.H            - Prototypes of procedures for creating 
  123. virtual arrays
  124.  
  125. VIRARRAY.C        - Source code of an example of using virtual 
  126. arrays
  127. VIRARRAY.EXE        - Example of using virtual arrays
  128.                   Compiled VIRARRAY.C linked with VIRIMG.LIB
  129.  
  130. HUGEARR.C        - Source code of an example of using a huge 
  131. array
  132. HUGEARR.EXE        - Example of using a huge array
  133.  
  134. XARRAY.H            - C++ header for virtual arrays
  135.  
  136. ARRAYS.CPP        - Part One of the C++ example program
  137. A2.CPP            - Part Two of the C++ example program
  138.  
  139. TXARRAY.H        - header with templates
  140. TARRAYS.CPP        - part one with templates
  141. TA2.CPP            - part two with templates
  142.  
  143.  
  144. --------------------------------------------------------------
  145. All the code here is compiled using the large data model
  146. and Borland C or Turbo C compiler
  147. --------------------------------------------------------------
  148.  
  149. This software is use by example, so the comments in this 
  150. document mainly explain the procedures that have been developed 
  151. to enable the use of huge and virtual arrays.
  152.  
  153.  
  154.  
  155.  
  156. 1.    Using virtual/extended arrays in C++
  157.  
  158.     This section describes using the Virtual Array manager in 
  159. C++, the next section describes its use with C, however when 
  160. using C much of the functionality and transparency of the C++ 
  161. version is lost.
  162.  
  163.     The functions described can only be used if the file 
  164. xarray.h is included in any files using extended arrays.
  165.  
  166. 1.1.    Declaring arrays
  167.  
  168.     Arrays can be declared and used with any type or with a 
  169. class or structure.
  170.  
  171. 1.1.1.    Simple arrays
  172.  
  173.     In the header file xarray.h various variable types 
  174. have extended definitions. These types are as follows.
  175.  
  176.     float, double, int, char
  177.  
  178.     To create an extended array an 'x' must be prefixed 
  179. to the variable type and the size of the is in parenthesis 
  180. after the variable name. 
  181.  
  182.     i.e.        x<type> <variable>(size of array);
  183.  
  184.     Therefore to declare an array of float called 
  185. 'costing' with 87654 elements the declaration would be as 
  186. follows.
  187.  
  188.         xfloat costing(87654);
  189.  
  190.     This variable can then be used like any other array.
  191.  
  192.     e.g.        costing[5674]=5.334;
  193.  
  194.     To pass this array to a function you must retain the 
  195. prefix in the function deflation.
  196.  
  197.     e.g.        sum (xfloat values)
  198.             {
  199.                 .
  200.                 .
  201.             }
  202.  
  203.             main (..
  204.             .
  205.             .
  206.             sum (costing);
  207.             .
  208.             .
  209.             }
  210.  
  211. 1.1.2.    Two dimensional arrays
  212.  
  213.     Two dimensional arrays are declared in a similar way 
  214. to one dimensional arrays except 'xx' is prefixed to the 
  215. variable type.
  216.  
  217.     i.e.        xx<type> <variable>(number of rows, number 
  218. of columns)
  219.  
  220.     Therefore to declare a 240x200 array of char called 
  221. image the declaration would be as follows.
  222.  
  223.         xxchar image(240,200);
  224.  
  225.     Again you can make assignments as usual
  226.  
  227.     e.g.        image[100][100]=10;
  228.  
  229.     A two dimensional array can be passed to functions as 
  230. for one dimensional arrays.
  231.  
  232.     Indexing a row of a two dimensional array return a 
  233. one dimensional array of the same type.
  234.  
  235.     e.g.        xchar imagerow;
  236.             imagerow = image[10];    /* i.e. image[10] 
  237. returns a variable of type xchar */
  238.  
  239. 1.1.3.    Arrays of undefined types
  240.  
  241.     Arrays of any type can be used but if the type is not 
  242. from the list of defined types you need to insert the 
  243. following after you include the file xarray.h.
  244.  
  245.         XARRAY (<type>)
  246.  
  247.             to declare one dimensional arrays and
  248.  
  249.         XXARRAY (<type>)
  250.  
  251.             to declare two dimensional array.
  252.  
  253.     Therefore to define extended arrays of type 'long' 
  254. the following would be inserted into the code.
  255.  
  256.         XARRAY (long)
  257.         XXARRAY (long)
  258.  
  259.     Note: If two dimensional arrays of a type are 
  260. required then one dimensional arrays of that type need to 
  261. be declared first.
  262.  
  263.     If you want to declare a type with a space in it such 
  264. as 'unsigned char' then a typedef needs to be used first.
  265.  
  266.     e.g.        typedef byte unsigned char
  267.             XARRAY (byte)
  268.             XXARRAY (byte)
  269.  
  270.     These definitions allow the defined types to used as 
  271. described previously (i.e. xlong, xxlong, xbyte, xxbyte)
  272.  
  273.  
  274. 1.1.4.    Arrays of structures or classes
  275.  
  276.     An extended array of a struct or a class can be 
  277. defined in a similar way to other data types. An example 
  278. is given in the program ARRAYS.CPP with a struct called 
  279. 'anything'.
  280.  
  281. First the struct is defined.
  282.  
  283.     struct anything
  284.     {
  285.         .
  286.         .
  287.     }
  288.  
  289.     Then the extended arrays are defined as before
  290.  
  291.     XARRAY (anything);
  292.     XXARRAY (anything);
  293.  
  294.     This definition creates two types - xanything and 
  295. xxanything.
  296.  
  297. 1.2.    Using templates
  298.  
  299. The description of declaring virtual arrays in section 1.1. 
  300. uses the file xarray.h that includes a couple of macros. Some 
  301. C++ compilers have class templates. Enclosed is another header 
  302. file (txarray.h) that uses templates instead of macros.
  303.  
  304. Templates are neated and easier to use. When using the template 
  305. header, to declare a virtual array of any type or class use the 
  306. following format (I have used square brackets here to indicate 
  307. the parameter because templates use the <> symbols):
  308.  
  309. xarray<[type or class]> [variable]([size])
  310.  
  311. to declare a two dimensional array use the following:
  312.  
  313. xxarray<[type or class]> [variable](no. of rows, no. of cols)
  314.  
  315. Use the xarray declaration in your subroutines as well.
  316.  
  317. e.g.
  318.  
  319.     sum (xarray<float> values)
  320.  
  321. 1.3.    Controlling your memory
  322.  
  323.     With the C++ interface the use of extended memory or disk 
  324. caching is controlled in the background without intervention 
  325. from the program.
  326.  
  327.     The virtual array manager splits the virtual memory into a 
  328. number of blocks. A number of these blocks are held in 
  329. conventional memory and the rest is held in extended memory or 
  330. on disk. If a block is accessed which is not currently in 
  331. conventional memory then a block is swapped out and the 
  332. required block is swapped in. The size of a block, the number 
  333. of blocks and the number of blocks that can be held in 
  334. conventional memory all affect the speed and the size of 
  335. conventional memory required by your program.
  336.  
  337. 1.3.1.    Declaring size and number of blocks
  338.  
  339.     When the first Extended array is declared the 
  340. extended memory manger is initialised or a swap file is 
  341. created. All the virtual memory that is needed by your 
  342. program must be defined before the first array is 
  343. declared. The amount of memory allocated is affected by 
  344. the following global variables.
  345.  
  346.     __block_size (default 32768)    -    Size of a block
  347.     __blocks (default 16)    -    Total number of 
  348. blocks declared.
  349.     __mem_buf (default 4)    -    Number of blocks 
  350. held in conventional 
  351. memory.
  352.  
  353.     
  354.  
  355. The amount of conventional memory used is
  356.       block size*  mem buf.
  357.  
  358. The amount of virtual memory required is
  359.       block size*(  blocks-  mem buf)
  360.  
  361.     By increasing blocks and mem_buf and decreasing 
  362. block_size higher performance may be achieved (And more 
  363. memory used).
  364.  
  365.  
  366.  
  367. 1.4.    Other virtual array manager parameters
  368.  
  369. There is a whole host of parameters that can be adjusted. Most 
  370. of them have comments along with their declaration. You do not 
  371. need to adjust them to get the Virtual array manager to work. 
  372. But once you have got your program working you may want to play 
  373. with them. The variables that hold these parameters are found 
  374. in virimg.h. To change the default values of these variables 
  375. they must be set before any virtual arrays are declared. I.e. 
  376. the first thing in 'main()'.
  377.  
  378. ___saftey
  379. If the virtual array manager is using extended memory and 
  380. your program crashes then the array manager with not be 
  381. able to release its access to the extended memory. For 
  382. this reason when the array manager uses extended memory it 
  383. puts the extended memory handle in a file. When it 
  384. finishes with its extended memory it deletes that file. 
  385. However, if the program crashes then the next time you run 
  386. a program with the array manager it will find the file and 
  387. release the memory that had been taken. If you do not want 
  388. to use this safety utility include the following line in 
  389. your program : ___saftey=0;.
  390. *___safe_filename
  391. This is the name of the file where the extended memory 
  392. file handle is kept. The default is "c:\!saftey!.xms". If 
  393. you want to change the name of the file include the 
  394. following line in your program : 
  395. ___safe_filename="<filename>";.
  396.  
  397. ___msgs
  398. By default various messages are output to the screen when 
  399. the array manager is set up and closed down. If you want 
  400. to stop these messages include the following line: 
  401. ___msgs=0;.
  402.  
  403. *___xfilename
  404. When the virtual array manager uses the disk to swap 
  405. memory the virtual arrays are put in the file 
  406. "virarray.swp" which is deleted when the virtual array 
  407. manager is closed down. To change the name of the swap 
  408. files include the following line: 
  409. ___xfilename="<filename>";.
  410. ___xfile_buf
  411. Your machine has a file buffer which is used when writing 
  412. to and reading files. The size of this buffer for the 
  413. array manager is 32768. To change the size of the buffer 
  414. include the following line: ___xfile_buf=<buffer size>;.
  415.  
  416. ___xarrays
  417. By default the array manager first tries to cache to 
  418. extended memory. If there is not enough extended memory 
  419. available it automatically tries to cache to disk. If you 
  420. want to force the array manager to cache to disk include 
  421. the following line: ___xarrays=XDISK;. There is no option 
  422. to cache part of the arrays to disk and the rest to 
  423. extended memory because much higher performance would be 
  424. achieved by using disk caching combined with a file disk 
  425. cache such as comes with windows, msdos5 and drdos. But if 
  426. you do chose this option allocate a lot of memory to the 
  427. file disk cache. 
  428. ___auto_disk
  429. If you do not want the array manager to try and cache to 
  430. disk if it fails to allocated extended memory then include 
  431. the following line: ___auto_disk=0;.
  432.  
  433.  
  434.  
  435. 2.    Using the virtual array manager with C
  436. If you do not have a C++ compiler the Array manager can still 
  437. be used but with slight modification to the code. The 
  438. underlying code of the virtual array manager is a C program 
  439. that creates a large number of virtual arrays of type void and 
  440. of a fixed size. The C++ code simulates the variable types and 
  441. array sizes by extensive use of operator overloading.
  442.  
  443. Because C does not have operator overloading available similar 
  444. routines need to be created by macros. A new macro is needs to 
  445. be created for each extended array declared. Not only this but 
  446. the program must directly interact with the array manager. The 
  447. rest of this section explains the routines available to the 
  448. programmer. Most of the variables described in section 1.3 
  449. still apply.
  450.  
  451. The virtual array routines create a specified number of arrays 
  452. in memory. If more arrays are used than can fit into the memory 
  453. allocated then the array that has not been accessed for the 
  454. longest is cached to disk or extended memory (The rest of this 
  455. section assumes you are caching to disk). All the virtual 
  456. arrays must be of the same size. The array memory must be 
  457. initialised as follows.
  458.  
  459. initialise virtual arrays (number of arrays to be stored in 
  460. conventional memory, size of each array, maximum number of 
  461. virtual arrays to be used, whether to cache to disk or 
  462. extended memory (XXMS or XDISK))
  463.  
  464. This routine initialises a specified number of virtual arrays. 
  465. Each array have identified by a number. The amount of memory 
  466. used depends on the size of the arrays and the number of array 
  467. buffers in conventional memory and virtual memory. The actual 
  468. arrays do not take up disk space or extended memory until they 
  469. are accessed.
  470.  
  471. To access an array the following procedure will return a 
  472. pointer to the array.
  473.  
  474.     void *img(<array number>)
  475.  
  476. (In the restricted version <array number> is between 0 and 15)
  477.  
  478. If the array is not in memory it will be created if it does not 
  479. exist or it will be loaded off disk if it does exist . This 
  480. pointer can be passed to a subroutine just like a normal array.
  481.  
  482.     e.g.   
  483.         process (char picture[50][50])
  484.         {
  485.             .
  486.             .
  487.             Some code processing the array normally
  488.             .
  489.             .
  490.         }
  491.  
  492.         main ()
  493.         {
  494.             .
  495.             .
  496.             process (img(5));
  497.             .
  498.             .
  499.         }
  500.  
  501. When you pass a virtual array to a subroutine it becomes 
  502. active. When the subroutine finishes the array becomes passive. 
  503. More than one array can be passed to a subroutine but there is 
  504. a limit to the number of arrays active arrays allowed at one 
  505. time. This limit is the first parameter in the 
  506. initialise_virtual_arrays routine.
  507.  
  508. Also note that if an array has been cached then when it is 
  509. restored it may be restored to a different memory location. 
  510. I.e. do not hold permanent links to and array after it has been 
  511. made passive.
  512.  
  513. 2.1.    Simulating larger arrays
  514.  
  515.     Another useful procedure is 
  516.  
  517.     void *large array (<array number>,<array index>)
  518.  
  519. This routine returns a pointer to the ith element of the <array 
  520. number>.
  521.  
  522. A macro can then be defined to access an element of a certain 
  523. array where an array number is assigned to an array name.
  524.  
  525.  
  526. #define <array name>(x) *((<data type<)large array(<array 
  527. number>,x))
  528.  
  529. ---------------------------------------------------------------
  530.  
  531. Hugh Arrays
  532.  
  533.     One hugh array can be created from a series of virtual 
  534. images to almost any size. An example is given in the file 
  535. HUGHARR.C
  536.  
  537.  
  538.