home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / mm / admct / checkmem.c < prev    next >
C/C++ Source or Header  |  1999-05-11  |  6KB  |  95 lines

  1. /*static char *SCCSID = "@(#)checkmem.c 13.1 91/09/04";*/
  2. /**************************START OF SPECIFICATIONS **************************/
  3. /*                                                                          */
  4. /* SOURCE FILE NAME:  CHECKMEM.C                                            */
  5. /*                                                                          */
  6. /* DISCRIPTIVE NAME: Check memory address and length                        */
  7. /*                                                                          */
  8. /* COPYRIGHT:                                                               */
  9. /*              Copyright (c) IBM Corporation  1991                         */
  10. /*                        All Rights Reserved                               */
  11. /*                                                                          */
  12. /* STATUS: OS/2 Release 2.0                                                 */
  13. /*                                                                          */
  14. /* FUNCTION: Checks memory address passed to make sure it is valid memory   */
  15. /*                                                                          */
  16. /* NOTES:                                                                   */
  17. /*    DEPENDENCIES: NONE                                                    */
  18. /*    RESTRICTIONS: NONE                                                    */
  19. /*                                                                          */
  20. /* ENTRY POINTS:                                                            */
  21. /*                                                                          */
  22. /************************** END OF SPECIFICATIONS ***************************/
  23.  
  24. #define  INCL_NOPMAPI
  25. #define  INCL_ERRORS
  26. #define  INCL_DOSMEMMGR
  27. #include <os2.h>
  28. #include <os2me.h>
  29. #include <checkmem.h>
  30.  
  31. /************************** START OF SPECIFICATIONS *************************/
  32. /*                                                                          */
  33. /* SUBROUTINE NAME: CheckMem                                                */
  34. /*                                                                          */
  35. /* DESCRIPTIVE NAME: Memory Check                                           */
  36. /*                                                                          */
  37. /* FUNCTION: Tests memory at specified address and length to see if it      */
  38. /*           exists for the application and if it has the right access.     */
  39. /*                                                                          */
  40. /* NOTES:    This routine contains OS/2 system specific functions.          */
  41. /*           DosQueryMem                                                    */
  42. /*                                                                          */
  43. /* INPUT:    pmem      - Address of memory to test                          */
  44. /*           ulLength  - Length of memory to test                           */
  45. /*           ulFlags   - Memory flags where:                                */
  46. /*                          PAG_EXECUTE                                     */
  47. /*                          PAG_READ                                        */
  48. /*                          PAG_WRITE                                       */
  49. /*                                                                          */
  50. /* OUTPUT:   rc = error return code.                                        */
  51. /*                                                                          */
  52. /* SIDE EFFECTS:                                                            */
  53. /*                                                                          */
  54. /*************************** END OF SPECIFICATIONS **************************/
  55.  
  56. RC CheckMem ( PVOID pMem,
  57.               ULONG ulLength,
  58.               ULONG ulFlags )
  59. {
  60.  
  61.   RC rc = NO_ERROR;                       // local return code
  62.   ULONG ulLengthLeft;                     // length left to check
  63.   ULONG ulTotLength = 0L;                 // Total length checked
  64.   ULONG ulRetFlags = 0L;                  // Flags returned from Dos call
  65.  
  66.   /**************************************************************************/
  67.   /*                                                                        */
  68.   /*   Set up to check memory.                                              */
  69.   /*                                                                        */
  70.   /**************************************************************************/
  71.  
  72.   ulLengthLeft = ulLength;
  73.   while ((!rc) && (ulTotLength < ulLength))
  74.     {                                             // Call OS to check mem
  75.       if (!(rc = DosQueryMem(pMem, &ulLengthLeft, &ulRetFlags)))
  76.         {                                         // We have the flags
  77.           if ((ulRetFlags & PAG_FREE) ||          // if free then error
  78.               !(ulRetFlags & PAG_COMMIT) ||       // if not committed then error
  79.                                                   // if execute only
  80. //------------((ulRetFlags & PAG_EXECUTE) && !(ulFlags & PAG_EXECUTE)) ||
  81.               ((ulRetFlags & ulFlags) != ulFlags))
  82.             {
  83.               rc = ERROR_INVALID_BLOCK;
  84.             }
  85.           else
  86.             {
  87.               pMem = (PVOID)( (ULONG)pMem + ulLengthLeft );
  88.               ulTotLength += ulLengthLeft;
  89.               ulLengthLeft = ulLength - ulTotLength;
  90.             }
  91.         }
  92.     }
  93.   return(rc);
  94. }
  95.