home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / MONTE.ZIP / SPARSE / SPARSE.C < prev   
Text File  |  1993-03-05  |  3KB  |  114 lines

  1. /* sparse.c
  2.  
  3. Allocates a 1MB memory object but commits no pages in that memory object.
  4.  
  5. The program then proceeds to write on that memory which is invalid. This
  6. causes an exception, or trap.
  7.  
  8. Traps are handled by an exception handler provided by this program.  At
  9. exception time, the handler will commit the invalid page(s).
  10.  
  11. */
  12.  
  13.  
  14. // os2 includes
  15. #define INCL_DOS
  16. #define INCL_ERRORS
  17. #include <os2.h>
  18.  
  19. // c includes
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <assert.h>
  24.  
  25. // a typedef for a function that handles an exception
  26. typedef ULONG _System FNEH( PEXCEPTIONREPORTRECORD,
  27.                             PEXCEPTIONREGISTRATIONRECORD,
  28.                             PCONTEXTRECORD,
  29.                             PVOID );
  30. typedef FNEH *PFNEH;
  31.  
  32. // a typedef for an exception handler registration record; a link in a list
  33. struct _syseregrec {
  34.   PEXCEPTIONREGISTRATIONRECORD pnext;
  35.   PFNEH                        pfneh;
  36. };
  37. typedef struct _syseregrec SYSEREGREC;
  38.  
  39.  
  40. // ----------------------------------------------------------------------
  41. // the exception handler function
  42.  
  43. ULONG _System Handler( PEXCEPTIONREPORTRECORD p1,
  44.                        PEXCEPTIONREGISTRATIONRECORD p2,
  45.                        PCONTEXTRECORD p3,
  46.                        PVOID pv )
  47. {
  48.    PVOID  pvBase;
  49.  
  50.    // exception number of interest is access violation
  51.    if( p1->ExceptionNum == XCPT_ACCESS_VIOLATION  ) {
  52.  
  53.       // violation occurred on a write access
  54.       if( p1->ExceptionInfo[ 0 ] == XCPT_WRITE_ACCESS ) {
  55.  
  56.          // try to commit the referenced page; first make a pointer to it
  57.          pvBase = (PVOID)(p1->ExceptionInfo[ 1 ] & 0xFFFFF000);
  58.  
  59.          // set the attribute of the page to be commit and writable
  60.          if( NO_ERROR == DosSetMem( pvBase, 1, PAG_COMMIT | PAG_WRITE )) {
  61.            // successful commit; this exception has been handled.
  62.            return XCPT_CONTINUE_EXECUTION;
  63.          }
  64.       }
  65.    }
  66.    // not handled, let other handlers in the chain have the exception
  67.    return XCPT_CONTINUE_SEARCH;
  68. }
  69.  
  70.  
  71. // ----------------------------------------------------------------------
  72. int main ( void )
  73. {
  74.   APIRET      rc;
  75.   PCHAR       pchar;
  76.   PSZ         psz;
  77.   PVOID       pBase;
  78.   SYSEREGREC  regrec;
  79.  
  80.   // insert my exception handler into the chain of handlers for this thread
  81.   regrec.pnext = NULL;
  82.   regrec.pfneh = Handler;
  83.   rc = DosSetExceptionHandler( (PEXCEPTIONREGISTRATIONRECORD) ®rec );
  84.   assert( rc == 0 );
  85.  
  86.   // allocate a 1 megabyte memory object without committing any of it;
  87.   // note no PAG_COMMIT flag
  88.   rc = DosAllocMem(  &pBase, (1024*1024), PAG_WRITE );
  89.   assert( rc == 0 );
  90.  
  91.   // this will cause an exception since the page is not committed
  92.   pchar = (PCHAR)pBase;
  93.   *pchar = 'a';
  94.  
  95.   // this string copy will cause two exceptions
  96.   psz = (PSZ)pBase + 0x06fffa;
  97.   strcpy( psz, "THIS IS A VERY LONG STRING THAT WILL CROSS A 4K BOUND." );
  98.  
  99.   // reference the memory
  100.   printf( "%c\n", *pchar );
  101.   printf( "%s\n", psz );
  102.  
  103.   // free memory object
  104.   rc = DosFreeMem( pBase );
  105.   assert( rc == 0 );
  106.  
  107.   // unlink my handler before exiting;
  108.   // important because the link record is automatic to this function
  109.   rc = DosUnsetExceptionHandler( (PEXCEPTIONREGISTRATIONRECORD) ®rec );
  110.   assert( rc == 0 );
  111.  
  112.   return 0;
  113. }
  114.