#include <stdio.h>
#include <string.h>
#include <windows.h>

int mb=1<<20;     // one megabyte
int N=100;        // megabytes allocated
int CYCLES=1200;  // cycles we repeat VirtualFree+VirtualAlloc for all the N mb

#define Check(a) if (!(a))  {printf("Error!");  exit(1);}

#if 0
#define point(a)  {printf a; getchar();}
#else
#define point(a)
#endif

main(int arc,char**argv) {
  void *a[N]; int i;
  point(("before"));
  for ( i=0; i<N; i++) {
    Check( a[i] = VirtualAlloc(0,    mb, MEM_RESERVE, PAGE_READWRITE));
    Check(        VirtualAlloc(a[i], mb, MEM_COMMIT,  PAGE_READWRITE));
  }
  point(("after"));
  for ( int num=0; num<CYCLES; num++) {
#if 0
    for( i=0; i<N; i++) {
      memset(a[i],num+i+1,mb);
    }
    point(("filled %d",num));
#endif
#if 1
    for ( i=0; i<N; i++) {
      Check( VirtualFree( a[i], mb, MEM_DECOMMIT));
      Check( VirtualAlloc(a[i], mb, MEM_COMMIT, PAGE_READWRITE));
    }
    point(("freed",num));
#endif
  }
  exit(0);
}
