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

  1. /*
  2.  *   GlobalCompact
  3.  *   globcmpc.c, jeffst, v1.00, 22-Sept-1987
  4.  *
  5.  *   This program demonstrates the use of the GlobalCompact function. The
  6.  *   GlobalCompact function compacts the global heap. GlobalCompact was
  7.  *   called from WinMain in this sample application.
  8.  *
  9.  *   Microsoft Product Support Services
  10.  *   Windows Version 2.0 function demonstration application
  11.  *   Copyright (c) Microsoft 1987
  12.  *
  13.  */
  14.  
  15. #include "windows.h"
  16.  
  17. int sprintf();
  18.  
  19. typedef struct {
  20.         int x;
  21.         int y;
  22.            } MYSTRUCT;
  23. typedef MYSTRUCT far *lpMyPtr;
  24.  
  25. typedef struct {
  26.         char smallstruct[2];
  27.            } SMALLSTRUCT;
  28. typedef struct {
  29.         char bigstruct[20000];
  30.            } BIGSTRUCT;
  31.  
  32. typedef BIGSTRUCT far *lpBIGPtr;
  33. typedef SMALLSTRUCT far *lpSMALLPtr;
  34.  
  35.  
  36. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  37. HANDLE hInstance, hPrevInstance;
  38. LPSTR lpszCmdLine;
  39. int cmdShow;
  40. {
  41.     HANDLE      hMemBlock1;      /* Handle to memory block */
  42.     HANDLE      hMemBlock2;      /* Handle to memory block */
  43.     lpMyPtr      ThisPtr;      /* Pointer to myStruct    */
  44.     lpBIGPtr      HugePtr;
  45.     lpSMALLPtr      MiniPtr;
  46.     char szBuff[50];
  47.     DWORD ContigFreeBytes;
  48.  
  49. /* GlobalCompact to ask for FFFF continguous bytes */
  50.     ContigFreeBytes = (DWORD)GlobalCompact((DWORD)-1);
  51.     sprintf(szBuff,"Number of contiguous free bytes before allocation = %lu",
  52.                               ContigFreeBytes);
  53.     MessageBox(NULL,(LPSTR)szBuff,(LPSTR)" ",MB_OK);
  54.  
  55. /* Allocate 2 bytes from global heap */
  56.     hMemBlock1 = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE | GMEM_DISCARDABLE,
  57.             (long)sizeof(SMALLSTRUCT));
  58. /* if block allocated properly */
  59.     if (hMemBlock1 != NULL)
  60.     /* lock block into memory */
  61.     MiniPtr = (lpSMALLPtr)GlobalLock(hMemBlock1);
  62.  
  63. /* ask for FFFF contiguous free bytes */
  64.     ContigFreeBytes = (DWORD)GlobalCompact((DWORD)-1);
  65.     sprintf(szBuff,"Number of contiguous free bytes = %lu",ContigFreeBytes);
  66.     MessageBox(NULL,(LPSTR)szBuff,(LPSTR)"After allocating 2 bytes...",MB_OK);
  67.  
  68. /* Allocate 50000 bytes from global heap */
  69.     hMemBlock2 = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE |GMEM_DISCARDABLE,
  70.             (long)sizeof(BIGSTRUCT));
  71. /* if memory allocated properly */
  72.     if (hMemBlock2 != NULL)
  73.     /* lock block into memory */
  74.     HugePtr = (lpBIGPtr)GlobalLock(hMemBlock2);
  75.  
  76. /* ask for FFFF contiguous free bytes */
  77.     ContigFreeBytes = (DWORD)GlobalCompact((DWORD)-1);
  78.     sprintf(szBuff,"Number of contiguous free bytes = %lu",ContigFreeBytes);
  79.     MessageBox(NULL,(LPSTR)szBuff,(LPSTR)"After allocating 50000 bytes...",MB_OK);
  80.  
  81.     GlobalUnlock(hMemBlock2);      /* unlock the blocks */
  82.     GlobalUnlock(hMemBlock1);      /*               */
  83.  
  84.  
  85. /* ask for FFFF contiguous free bytes */
  86.     ContigFreeBytes = (DWORD)GlobalCompact((DWORD)-1);
  87.     sprintf(szBuff,"Number of contiguous free bytes = %lu",ContigFreeBytes);
  88.     MessageBox(NULL,(LPSTR)szBuff,(LPSTR)"After unlocking 50000 bytes...",MB_OK);
  89.  
  90.  
  91.     return 0;
  92. }
  93.