home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / memory / local / loclcmpc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  2.3 KB  |  70 lines

  1. /*
  2.  *   LocalCompact
  3.  *
  4.  *   This program demonstrates the use of the LocalCompact function. The
  5.  *   LocalCompact function compacts the global heap. LocalCompact was
  6.  *   called from WinMain in this sample application.
  7.  */
  8.  
  9. #include "windows.h"
  10.  
  11. int sprintf();
  12.  
  13. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  14. HANDLE hInstance, hPrevInstance;
  15. LPSTR lpszCmdLine;
  16. int cmdShow;
  17. {
  18.     HANDLE      hMemBlock1;      /* Handle to memory block */
  19.     HANDLE      hMemBlock2;      /* Handle to memory block */
  20.     char szBuff[90];
  21.     WORD ContigFreeBytes;
  22.     char NEAR *   Rasta1, Rasta2;
  23.  
  24. /* LocalCompact to ask for 4096 continguous bytes */
  25.     ContigFreeBytes = (WORD)LocalCompact((WORD)4096);
  26.     sprintf(szBuff,"Number of contiguous free bytes before allocation = %u",
  27.                               ContigFreeBytes);
  28.     MessageBox(NULL,(LPSTR)szBuff,(LPSTR)" ",MB_OK);
  29.  
  30. /* Allocate 2 bytes from global heap */
  31.     hMemBlock1 = LocalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE | GMEM_DISCARDABLE,
  32.             (long)2);
  33. /* if block allocated properly */
  34.     if (hMemBlock1 != NULL)
  35.     /* lock block into memory */
  36.     Rasta1 = (char NEAR *)LocalLock(hMemBlock1);
  37.  
  38. /* ask for 4096 contiguous free bytes */
  39.     ContigFreeBytes = (WORD)LocalCompact((WORD)4096);
  40.     sprintf(szBuff,"Number of contiguous free bytes = %u",ContigFreeBytes);
  41.     MessageBox(NULL,(LPSTR)szBuff,(LPSTR)"After allocating 2 bytes...",MB_OK);
  42.  
  43. /* Allocate 200 bytes from local heap */
  44.     hMemBlock2 = LocalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE |GMEM_DISCARDABLE,
  45.             (long)200);
  46. /* if memory allocated properly */
  47.     if (hMemBlock2 != NULL)
  48.     /* lock block into memory */
  49.     Rasta2 = (char NEAR *)LocalLock(hMemBlock2);
  50.  
  51. /* ask for 4096 contiguous free bytes */
  52.     ContigFreeBytes = (WORD)LocalCompact((WORD)4096);
  53.     sprintf(szBuff,"Number of contiguous free bytes = %u",ContigFreeBytes);
  54.     MessageBox(NULL,(LPSTR)szBuff,(LPSTR)"After allocating 200 bytes...",MB_OK);
  55.  
  56.     LocalUnlock(hMemBlock1);
  57.     LocalUnlock(hMemBlock2);
  58.  
  59.     LocalFree(hMemBlock1);
  60.     LocalFree(hMemBlock2);
  61.  
  62. /* ask for FFFF contiguous free bytes */
  63.     ContigFreeBytes = (WORD)LocalCompact((WORD)4096);
  64.  
  65.     sprintf(szBuff,"Number of contiguous free bytes = %u",ContigFreeBytes);
  66.     MessageBox(NULL,(LPSTR)szBuff,(LPSTR)"After unlocking 200 bytes...",MB_OK);
  67.  
  68.     return 0;
  69. }
  70.