home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mmpm21tk.zip / TK / ADMCT / CHECKMEM.C < prev    next >
C/C++ Source or Header  |  1992-10-16  |  6KB  |  102 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. /* MODIFICATION HISTORY:                                                    */
  23. /* DATE      DEVELOPER         CHANGE DESCRIPTION                           */
  24. /* 11/26/90  Marty Paulat      Routine created                              */
  25. /* 02/10/91  Mike Koval        Tested and modified                          */
  26. /* 05/27/91  Marty Paulat      Moved here from SSMSRV.c so all mme can link */
  27. /*                             and use the obj.                             */
  28. /* 08/27/91  MJP               Add pMem+length left if looping required     */
  29. /************************** END OF SPECIFICATIONS ***************************/
  30.  
  31. #define  INCL_NOPMAPI
  32. #define  INCL_ERRORS
  33. #define  INCL_DOSMEMMGR
  34. #include <os2.h>
  35. #include <os2me.h>
  36. #include <checkmem.h>
  37.  
  38. /************************** START OF SPECIFICATIONS *************************/
  39. /*                                                                          */
  40. /* SUBROUTINE NAME: CheckMem                                                */
  41. /*                                                                          */
  42. /* DESCRIPTIVE NAME: Memory Check                                           */
  43. /*                                                                          */
  44. /* FUNCTION: Tests memory at specified address and length to see if it      */
  45. /*           exists for the application and if it has the right access.     */
  46. /*                                                                          */
  47. /* NOTES:    This routine contains OS/2 system specific functions.          */
  48. /*           DosQueryMem                                                    */
  49. /*                                                                          */
  50. /* INPUT:    pmem      - Address of memory to test                          */
  51. /*           ulLength  - Length of memory to test                           */
  52. /*           ulFlags   - Memory flags where:                                */
  53. /*                          PAG_EXECUTE                                     */
  54. /*                          PAG_READ                                        */
  55. /*                          PAG_WRITE                                       */
  56. /*                                                                          */
  57. /* OUTPUT:   rc = error return code.                                        */
  58. /*                                                                          */
  59. /* SIDE EFFECTS:                                                            */
  60. /*                                                                          */
  61. /*************************** END OF SPECIFICATIONS **************************/
  62.  
  63. RC CheckMem ( PVOID pMem,
  64.               ULONG ulLength,
  65.               ULONG ulFlags )
  66. {
  67.  
  68.   RC rc = NO_ERROR;                       // local return code
  69.   ULONG ulLengthLeft;                     // length left to check
  70.   ULONG ulTotLength = 0L;                 // Total length checked
  71.   ULONG ulRetFlags = 0L;                  // Flags returned from Dos call
  72.  
  73.   /**************************************************************************/
  74.   /*                                                                        */
  75.   /*   Set up to check memory.                                              */
  76.   /*                                                                        */
  77.   /**************************************************************************/
  78.  
  79.   ulLengthLeft = ulLength;
  80.   while ((!rc) && (ulTotLength < ulLength))
  81.     {                                             // Call OS to check mem
  82.       if (!(rc = DosQueryMem(pMem, &ulLengthLeft, &ulRetFlags)))
  83.         {                                         // We have the flags
  84.           if ((ulRetFlags & PAG_FREE) ||          // if free then error
  85.               !(ulRetFlags & PAG_COMMIT) ||       // if not committed then error
  86.                                                   // if execute only
  87. //------------((ulRetFlags & PAG_EXECUTE) && !(ulFlags & PAG_EXECUTE)) ||
  88.               ((ulRetFlags & ulFlags) != ulFlags))
  89.             {
  90.               rc = ERROR_INVALID_BLOCK;
  91.             }
  92.           else
  93.             {
  94.               pMem = (PVOID)( (ULONG)pMem + ulLengthLeft );
  95.               ulTotLength += ulLengthLeft;
  96.               ulLengthLeft = ulLength - ulTotLength;
  97.             }
  98.         }
  99.     }
  100.   return(rc);
  101. }
  102.