/* This is a small tool to flush the memory of a Windows XP notebook before hibernating. 
 * Windows XP (up to and including SP2) fails to hibernate if you have more than 512MB commit 
 * charge (see Windows Task Manger, the Performance tab). 
 * This tools helps somewhat by flushing the memory of the system, but the real fix is to get 
 * the Windows XP hotfix that solves the problem: 
 * http://support.microsoft.com/?kbid=909095
 * 
 * compile with cc -o flushmem flushmem.c
 * do not use any optimization flags. 
 * - John Markus Bjørndalen
 * 
 */
#include <stdlib.h>
#include <stdio.h>

#define MAX 2048
#define NMEGS 1550

int main()
{
  char *bufs[MAX];
  int i, count; 

  printf("Allocating\n");
  for (i = 0; i < NMEGS; i++) {
    bufs[i] = (char*) malloc(1024  * 1024); 
    if (bufs[i] == NULL) {
      printf("failed to get more ram at %d\n", i);
      break; 
    }
  }
  count = i; 
  printf("Now, stepping on memory - got %d megs\n", count);
  
  for (i = 0; i < count-1; i++) {
    if (i % 100 == 0) {
      printf(" %d\n", i); fflush(stdout); 
    }
    memset(bufs[i], 'a', 1024 * 1024); 
  }
  return 0; 
}
