home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / GIVEMEM.LIB < prev    next >
Text File  |  1994-11-16  |  5KB  |  133 lines

  1. // GiveMem.lib - Creates a single memory block that can temporarily
  2. // ver.2         be given to another process based on its process ID
  3. //               or a window handle.  This allows WinSendMsg() calls
  4. //               (and other calls) to set memory and give it to another
  5. //               function.  The routines in here are smart enough to
  6. //               keep track of the processes that can access this memory.
  7. //
  8. // WARNINGS: Only 1 chunk of memory, almost 4K,  is used here. The assumption
  9. //           is that the same memory block will not be given to another
  10. //           process until this first process is done with it.  Also,
  11. //           the memory block will not be really freed from memory/swap
  12. //           until this process AND whatever processes the memory is
  13. //           given to are freed.
  14. //
  15. //
  16. //**** GiveMemoryToProcess(): get chunk of memory shared with process ID
  17. // SYNTAX: pointer GiveMemoryToProcess(int ProcessID)
  18. // WHERE: ProcessID: a process identifier to share memory with; no checking on this ID
  19. // RETURN: pointer to the shared, allocated memory chunk
  20. //
  21. //
  22. //**** GiveMemoryToWindow(): get chunk of memory shared with process ID owning this window
  23. // SYNTAX: pointer GiveMemoryToWindow(int WindowHandle)
  24. // WHERE: WindowHandle: handle of any window--will give memory to process owning window
  25. //                      no checking is done on this except to not give memory if invalid
  26. // RETURN: pointer to the shared, allocated memory chunk
  27. //
  28. //
  29.  
  30. #define OBJ_GIVEABLE 0x200
  31. #define OBJ_TILE     0x40
  32. #define PAG_COMMIT   0x10
  33. #define PAG_READ     0x1
  34. #define PAG_WRITE    0x2
  35. #define GM_SHARE_SIZE 4080  // almost 4K
  36.  
  37. GiveMemoryToProcess(pPId)
  38. {
  39.    if ( gGMPreviousID != pPId ) {
  40.       gGMPreviousID = pPId;
  41.  
  42.       if ( !defined(gGMSharedMemoryPointer) )
  43.          InitializeGMSharedMemoryPointer();
  44.  
  45.       // Check our array of given processes, and if already gave to that process
  46.       // then do no more, else give to process and add to our given array
  47.       lSharedProcessCount = defined(gGMSharedProcessIDs) ? 1 + GetArraySpan(gGMSharedProcessIDs) : 0 ;
  48.       for ( lIdx = lSharedProcessCount; 0 <= --lIdx; ) {
  49.          if ( pPId == gGMSharedProcessIDs[lIdx] )
  50.             break;
  51.       }
  52.       if ( lIdx < 0 ) {
  53.          // this process isn't in list of processes owning this memory
  54.  
  55.          // take this opportunity to prune out any id's in the gGMSharedProcessIDs[]
  56.          // array to remove processes that no longer exist on the system
  57.          if ( lSharedProcessCount  &&  (lPList = ProcessList()) ) {
  58.             lPListSpan = GetArraySpan(lPList);
  59.             lNewCount = 0;
  60.             for ( lTest = 0; lTest < lSharedProcessCount; lTest++ ) {
  61.                lTestID = gGMSharedProcessIDs[lTest];
  62.                for ( lPid = lPListSpan; lPid <= lPListSpan; lPid++ ) {
  63.                   if ( lPList[lPid] == lTestID ) {
  64.                      lNewArray[lNewCount++] = lTestID;
  65.                      break;
  66.                   }
  67.                }
  68.             }
  69.             if ( lSharedProcessCount = lNewCount ) gGMSharedProcessIDs = lNewArray;
  70.             else undefine(gGMSharedProcessIDs)
  71.          }
  72.  
  73.          #define ORD_DOS32GIVESHAREDMEM   303
  74.          DynamicLink( "DOSCALLS", ORD_DOS32GIVESHAREDMEM, BIT32, CDECL,
  75.                       gGMSharedMemoryPointer, gGMSharedProcessIDs[lSharedProcessCount] = pPId,
  76.                       PAG_READ | PAG_WRITE );
  77.       }
  78.    }
  79.  
  80.    return gGMSharedMemoryPointer;
  81. }
  82.  
  83. GiveMemoryToWindow(pHwnd)
  84. {
  85.    if ( gGMPreviousHwnd != pHwnd ) {
  86.       gGMPreviousHwnd = pHwnd;
  87.  
  88.       // get process ID of this window, and then pass to GiveMemoryToProcess()
  89.       #define ORD_WIN32QUERYWINDOWPROCESS  838
  90.       if ( DynamicLink( "PMWIN", ORD_WIN32QUERYWINDOWPROCESS, BIT32, CDECL,
  91.                         pHwnd, lGMProcessID, lGMThreadID ) )
  92.          GiveMemoryToProcess(lGMProcessID);
  93.    }
  94.    return gGMSharedMemoryPointer;
  95. }
  96.  
  97. ////////////// Routines and globals used by above functions
  98.  
  99. gGMSharedProcessIDs;    // array of processes that have already been given access
  100. gGMSharedMemoryPointer; // the single block of memory that is giveable
  101.  
  102. gGMPreviousID = -1; // avoid checking too often; if just did this then not again
  103. gGMPreviousHwnd = -1;   // avoid checking too often; if just did this then not again
  104. gGMFirstInit = True;
  105.  
  106. InitializeGMSharedMemoryPointer()
  107.    // Initialize pointer to shared memory that will be given to windowed
  108.    // applications to receive their accelerator messages.
  109. {
  110.    #define ORD_DOS32ALLOCSHAREDMEM  300
  111.  
  112.    assert( !DynamicLink( "DOSCALLS", ORD_DOS32ALLOCSHAREDMEM, BIT32, CDECL,
  113.                           gGMSharedMemoryPointer, NULL, GM_SHARE_SIZE,
  114.                           PAG_COMMIT | OBJ_GIVEABLE | PAG_READ | PAG_WRITE | OBJ_TILE ) );
  115.    // Register function to free this memory when the current program exits
  116.    if ( gGMFirstInit ) {
  117.       atexit("TerminateGMSharedMemoryPointer");
  118.       FirstInit = False;
  119.    }
  120. }
  121.  
  122. TerminateGMSharedMemoryPointer() // called at exit to free gGMSharedMemoryPointer
  123. {
  124.    #define ORD_DOS32FREEMEM   304
  125.    if ( defined(gGMSharedMemoryPointer) )
  126.       assert( !DynamicLink( "DOSCALLS", ORD_DOS32FREEMEM, BIT32, CDECL, gGMSharedMemoryPointer ) );
  127.    undefine(gGMSharedMemoryPointer);
  128.    undefine(gGMSharedProcessIDs);
  129.    gGMPreviousID = -1;
  130.    gGMPreviousHwnd = -1;
  131. }
  132.  
  133.