home *** CD-ROM | disk | FTP | other *** search
/ The Developer Connection…ice Driver Kit for OS/2 3 / DEV3-D1.ISO / source / devnews / vol2 / sample2 / sparse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-15  |  2.4 KB  |  84 lines

  1. /* SPARSE.C.  This program allocates a one MB memory object but commits no
  2. pages.  The program then writes to that memory which is invalid, and this
  3. causes a trap.  The handler commits the invalid page and resumes execution.
  4. Compile and link this program with:  icc /Ss sparse.c */
  5.  
  6. // os2 includes
  7. #define INCL_DOS
  8. #define INCL_ERRORS
  9. #include <os2.h>
  10.  
  11. // c includes
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <assert.h>
  16.  
  17. // exception handler registration record
  18. typedef struct _regrec {
  19.   PVOID pNext;
  20.   PFN   pfnHandler;
  21. } REGREC;
  22. typedef REGREC *PREGREC;
  23.  
  24. // ----------------------------------------------------------------------
  25. ULONG _System Handler( PEXCEPTIONREPORTRECORD p1,
  26.                        PREGREC p2,
  27.                        PCONTEXTRECORD p3,
  28.                        PVOID pv )
  29. {
  30.   // interested in access violation
  31.   if( p1->ExceptionNum == XCPT_ACCESS_VIOLATION  ) {
  32.     assert( p1->ExceptionInfo[0] == XCPT_WRITE_ACCESS );
  33.     // try to commit the referenced page
  34.     if( 0 == DosSetMem( (PVOID)p1->ExceptionInfo[1], 1, PAG_COMMIT|PAG_WRITE )) {
  35.       // successful commit; resume execution
  36.       return XCPT_CONTINUE_EXECUTION;
  37.     }
  38.   }
  39.   // not handled, let other handlers in the chain have the exception
  40.   return XCPT_CONTINUE_SEARCH;
  41. }
  42.  
  43. // ----------------------------------------------------------------------
  44. int main ( void )
  45. {
  46.   APIRET      rc;
  47.   PCHAR       pchar;
  48.   PSZ         psz;
  49.   PVOID       pvBase;
  50.   REGREC      regrec;
  51.  
  52.   // insert exception handler into the chain of handlers for this thread
  53.   regrec.pfnHandler = (PFN)Handler;
  54.   rc = DosSetExceptionHandler( (PEXCEPTIONREGISTRATIONRECORD) ®rec );
  55.   assert( rc == 0 );
  56.  
  57.   // allocate a memory object without committing any of it;
  58.   // note lack of PAG_COMMIT flag
  59.   rc = DosAllocMem(  &pvBase, 1048576, PAG_WRITE );
  60.   assert( rc == 0 );
  61.  
  62.   // this causes an exception since the page is not committed
  63.   pchar = (PCHAR)pvBase;
  64.   *pchar = 'a';
  65.  
  66.   // this string copy causes two more exceptions
  67.   psz = (PSZ)pvBase + (4096 + 4092);
  68.   strcpy( psz, "This string crosses a 4K page boundary." );
  69.  
  70.   // reference the memory
  71.   printf( "%c\n", *pchar );
  72.   printf( "%s\n", psz );
  73.  
  74.   // free memory object
  75.   rc = DosFreeMem( pvBase );
  76.   assert( rc == 0 );
  77.  
  78.   // unlink handler before returning
  79.   rc = DosUnsetExceptionHandler( (PEXCEPTIONREGISTRATIONRECORD) ®rec );
  80.   assert( rc == 0 );
  81.  
  82.   return 0;
  83. }
  84.