home *** CD-ROM | disk | FTP | other *** search
/ Point Programming 1 / PPROG1.ISO / c / sclib31 / examples / xms.c < prev   
Encoding:
C/C++ Source or Header  |  1991-12-31  |  8.8 KB  |  333 lines

  1. #include <scl1xms.h>
  2. #include <memory.h>
  3. #include <dos.h>
  4.  
  5.     /************************************
  6.         eXtended (XMS) Memory functions */
  7.  
  8. void Pause(void);
  9. void PrintA20Status(void);
  10. void PrintError(unsigned int Error);
  11. void PrintHandleInfo(int handle);
  12.  
  13. typedef struct{
  14.     unsigned int ErrorCode;
  15.     char *ErrorMess;
  16.     }XMSERRORS;
  17.  
  18. XMSERRORS xmse[]=
  19.     {
  20.     XMS_NOT_IMPLEMENTED         ,"XMS function not available",
  21.     XMS_VDISK_PRESENT           ,"Virtual Disk present",
  22.     XMS_A20_ERROR               ,"A20 line error",
  23.     XMS_GENERAL_DRIVER_ERROR    ,"General XMS driver error",
  24.     XMS_UNRECOVERABLE_ERROR     ,"Unrecoverable error",
  25.     XMS_HMA_NOT_EXIST           ,"HMA does not exist",
  26.     XMS_HMA_IN_USE              ,"HMA already in use",
  27.     XMS_HMA_NOT_ALLOCATED       ,"HMA is not allocated",
  28.     XMS_A20_ALREADY_ENABLED     ,"A20 is already enabled",
  29.     XMS_ALL_EXTENDED_ALLOCATED  ,"All XMS memory allocated",
  30.     XMS_NO_MORE_HANDLES         ,"No more handles available",
  31.     XMS_INVALID_HANDLE          ,"Invalid handle number",
  32.     XMS_INVALID_SOURCE_HANDLE   ,"Invalid source handle",
  33.     XMS_INVALID_SOURCE_OFFSET   ,"Invalid source offset",
  34.     XMS_INVALID_DEST_HANDLE     ,"Invalid destination handle",
  35.     XMS_INVALID_DEST_OFFSET     ,"Invalid destination offset",
  36.     XMS_INVALID_LENGTH          ,"Invalid length",
  37.     XMS_INVALID_OVERLAP         ,"Invalid overlap",
  38.     XMS_PARITY_ERROR            ,"Parity error",
  39.     XMS_BLOCK_NOT_LOCKED        ,"Block not locked",
  40.     XMS_BLOCK_LOCKED            ,"Block locked",
  41.     XMS_LOCK_COUNT_OVERFLOW     ,"Lock count overflow",
  42.     XMS_LOCK_FAIL               ,"Lock fail",
  43.     XMS_UMB_SMALL_AVAILABLE     ,"Smaller UMB available",
  44.     XMS_UMB_NOT_AVAILABLE       ,"UMB not available",
  45.     XMS_UMB_INVALID_SEGMENT     ,"Invalid segment",
  46.     };
  47.  
  48. char CompOkStr[]="\tCompare OK\n";
  49. char CompErrStr[]="\tCompare ERROR\n";
  50.  
  51. /* struct used to move or copy data from extended memory */
  52.  
  53. XMSMove xmove;
  54.  
  55. char TestData[80]="This string will be used to test XMS functions";
  56.  
  57. main()
  58. {
  59. unsigned int available,total,i,handle;
  60. char buffer[sizeof(TestData)];
  61. void far *p;
  62.  
  63. /***********************************************
  64.     initialize, check if XMS driver is present */
  65.  
  66. if(XMS_Init() != XMS_OK)
  67.     {
  68.     printf("No XMS available\n");
  69.     exit(-1);
  70.     }
  71. else
  72.     printf("\n\nSCL1 Version 3.0 eXtended (XMS) Memory Test\n");
  73.  
  74. XMS_GetVersion(&i);           /* get version number */
  75.  
  76.     /* add 0x30 to returned value to get ASCII code */
  77.  
  78. memset(buffer,0,sizeof(buffer));
  79. buffer[0]=XMS_MAJOR_VER(i) + 0x30;
  80. buffer[1]='.';
  81. buffer[2]=XMS_MINOR_VER(i) + 0x30;
  82. printf("XMS Version %s\n\n",buffer);
  83.  
  84.     /***********************
  85.      High Memory Area Test */
  86.  
  87. printf("High Memory Area (HMA) Test\n");
  88.  
  89. if((i=XMS_RequestHMA())==XMS_OK)
  90.     {
  91.  
  92.     /* allocated OK */
  93.  
  94.     printf("\tHMA allocated...\n");
  95.  
  96.     /* enable the A20 line so that we can access HMA */
  97.  
  98.     if((i=XMS_GlobalEnableA20())==XMS_OK)
  99.         {
  100.  
  101.         _fmemcpy(XMS_HMA,(void far *)TestData,sizeof(TestData));
  102.         printf("\tData copied to HMA...\n");
  103.  
  104.         /* copy HMA to local buffer */
  105.  
  106.         memset(buffer,0,sizeof(buffer));
  107.         _fmemcpy((void far *)buffer,XMS_HMA,sizeof(buffer));
  108.  
  109.         if(memcmp(buffer,TestData,sizeof(TestData))==0)
  110.             printf(CompOkStr);
  111.         else
  112.             printf(CompErrStr);
  113.         }
  114.     else
  115.  
  116.         printf("\tError enabling A20 line, cannot access HMA\n");
  117.  
  118.      /* free HMA */
  119.  
  120.     XMS_ReleaseHMA();
  121.  
  122.     printf("\tReleasing HMA...\n");
  123.     }
  124. else
  125.  
  126.     PrintError(i);
  127.  
  128. Pause();
  129.  
  130.     /*************************
  131.         Extended Memory Test */
  132.  
  133. printf("Extended Memory Test\n");
  134.  
  135. if((i=XMS_QueryFreeExtended(&available,&total))==XMS_OK && available > 0)
  136.     {
  137.     printf("\tLargest available block: %u KB\n",available);
  138.     printf("\tTotal available memory: %u KB\n",total);
  139.  
  140.      /* allocate all memory */
  141.  
  142.     if((i=XMS_AllocExtended(&handle,available))==XMS_OK)
  143.         {
  144.         printf("\tSuccesful allocation of %u KB...\n",available);
  145.         printf("\tCopying data to allocated memory...\n");
  146.  
  147.         /* copy TestData buffer to extended memory */
  148.  
  149.         xmove.bytes=(unsigned long)sizeof(TestData);
  150.         xmove.shandle=0;
  151.         xmove.source=(void far *)TestData;
  152.         xmove.dhandle=handle;
  153.         xmove.destination=0;
  154.         if((i=XMS_MoveExtended(&xmove))==XMS_OK)
  155.             {
  156.  
  157.             /* copy extended memory to local buffer */
  158.  
  159.             memset(buffer,0,sizeof(buffer));
  160.             xmove.bytes=sizeof(TestData);
  161.             xmove.shandle=handle;
  162.             xmove.source=0;
  163.             xmove.dhandle=0;
  164.             xmove.destination=(void far *)buffer;
  165.             if((i=XMS_MoveExtended(&xmove))==XMS_OK)
  166.                 {
  167.                 if(memcmp(TestData,buffer,sizeof(TestData))==0)
  168.                     printf(CompOkStr);
  169.                 else
  170.                     printf(CompErrStr);
  171.                 }
  172.             else
  173.                 PrintError(i);
  174.             }
  175.         else
  176.             PrintError(i);
  177.  
  178.         /* get handle information */
  179.  
  180.         PrintHandleInfo(handle);
  181.         Pause();
  182.  
  183.         /*******************
  184.           Lock/Unlock Test */
  185.  
  186.         printf("Lock/Unlock Test\n\tLocking allocated memory...\n");
  187.         if((i=XMS_LockExtended(handle,&p))==XMS_OK)
  188.             {
  189.             printf("\tLocked block 32-bit linear address: %lX\n",p);
  190.             PrintHandleInfo(handle);
  191.             printf("\tUnlocking allocated memory...\n");
  192.             XMS_UnlockExtended(handle);
  193.             PrintHandleInfo(handle);
  194.             }
  195.         else
  196.             PrintError(i);
  197.  
  198.         /*********************
  199.           Reallocating Test  */
  200.  
  201.         Pause();
  202.         printf("Reallocating Test\n");
  203.  
  204.         /* realllocate buffer to 1kb */
  205.  
  206.         if((i=XMS_ReallocExtended(handle,1))==XMS_OK)
  207.             {
  208.             printf("\tBlock reallocated to 1 KB\n");
  209.  
  210.             /* get handle info */
  211.  
  212.             PrintHandleInfo(handle);
  213.             }
  214.         else
  215.             printf("\tError reallocating\n");
  216.  
  217.           /* free memory */
  218.  
  219.           XMS_FreeExtended(handle);
  220.           printf("\tReleasing Extended memory...\n");
  221.           }
  222.      else
  223.           printf("\tError Allocating\n");
  224.      }
  225.  
  226. Pause();
  227.  
  228.     /****************
  229.       A20 Line Test */
  230.  
  231. printf("A20 Line Test\n");
  232. PrintA20Status();
  233.  
  234. if((i=XMS_LocalEnableA20())==XMS_OK)
  235.     {
  236.     printf("\tLocally enabling A20 line...\n");
  237.     XMS_LocalEnableA20();
  238.     PrintA20Status();
  239.  
  240.     printf("\tLocally disabling A20 line...\n");
  241.     XMS_LocalDisableA20();
  242.     PrintA20Status();
  243.     }
  244. else
  245.     PrintError(i);
  246.  
  247. Pause();
  248.  
  249.     /****************************
  250.       Upper Memory Blocks Test  */
  251.  
  252. printf("Upper Memory Block (UMB) Test\n");
  253.  
  254.   /* By requesting 0xffff paragraphs, the largest available block size will be 
  255.      reported if UMB blocks are available */
  256.  
  257. if((i=XMS_RequestUMB(0xffff,&p,&available))==XMS_OK || i==XMS_UMB_SMALL_AVAILABLE)
  258.      {
  259.      printf("\tUMB available, allocating largest block available...\n");
  260.      if(i==XMS_UMB_SMALL_AVAILABLE)
  261.         {
  262.         if((i=XMS_RequestUMB(available,&p,&available))!=XMS_OK)
  263.             PrintError(i);
  264.         else
  265.             {
  266.             printf("\tUMB Size: %li bytes\n\tUMB Address: %X\n",(long) available * 16L,FP_SEG(p));
  267.             printf("\tCopying data to allocated memory...\n");
  268.             _fmemcpy(p,(void far *)TestData,sizeof(TestData));
  269.             memset(buffer,0,sizeof(buffer));
  270.             _fmemcpy((void far *)buffer,p,sizeof(TestData));
  271.             if(memcmp(buffer,TestData,sizeof(TestData))==0)
  272.                 printf(CompOkStr);
  273.             else
  274.                 printf(CompErrStr);
  275.             printf("\tReleasing UMB...\n");
  276.             XMS_ReleaseUMB(p);
  277.             }
  278.         }
  279.      }
  280. else
  281.     PrintError(i);
  282. }
  283.  
  284.  
  285. void PrintError(unsigned int Error)
  286. {
  287. int i;
  288.  
  289. for(i=0;i < sizeof(xmse) /sizeof(XMSERRORS);++i)
  290.     {
  291.     if(xmse[i].ErrorCode==Error)
  292.         {
  293.         printf("\t%s\n",xmse[i].ErrorMess);
  294.         return;
  295.         }
  296.     }
  297. printf("\tUnknown error #%X\n",Error);
  298. }
  299.  
  300. void Pause(void)
  301. {
  302. printf("-- more --");
  303. GetKey();
  304. printf("\r          \n");
  305. }
  306.  
  307. void PrintHandleInfo(int handle)
  308. {
  309. int lcount,fhandle,bsize;
  310.  
  311. XMS_GetEMBHandleInfo(handle,&lcount,&fhandle,&bsize);
  312. printf("\tHandle Report:\n");
  313. printf("\t\tFree Handles: %i\n",fhandle);
  314. printf("\t\tAllocated Block Lock Count: %i\n",lcount);
  315. printf("\t\tAllocated Block Size: %i KB\n",bsize);
  316. }
  317.  
  318. void PrintA20Status(void)
  319. {
  320. unsigned int i;
  321. int j;
  322.  
  323. if((j=XMS_QueryA20(&i))==XMS_OK)
  324.     {
  325.     if(i==XMS_A20_ENABLED)
  326.         printf("\tA20 enabled\n");
  327.     else
  328.         printf("\tA20 NOT enabled\n");
  329.     }
  330. else
  331.     PrintError(j);
  332. }
  333.