93 lines
2.4 KiB
C
93 lines
2.4 KiB
C
|
#include "pch.h"
|
||
|
#include <stdlib.h>
|
||
|
#include <stdio.h>
|
||
|
#include <Windows.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
/**************************************************************************
|
||
|
* Authors: Gary Fleming, Amber McCloughan
|
||
|
* Class: CIS452 - Operating Systems Concepts
|
||
|
* Professor: Dr. Greg Wolffe
|
||
|
* Date: 4/11/2019
|
||
|
*
|
||
|
* A mini programming assignment where we do some memory operations in
|
||
|
* Windows. We get the page size of the system, allocate memory,
|
||
|
* check the state of that memory, deallocate the memory, and then check
|
||
|
* the state of the memory again.
|
||
|
***************************************************************************/
|
||
|
|
||
|
// See function implementation for details.
|
||
|
const char *checkMemState(DWORD);
|
||
|
|
||
|
/**
|
||
|
* Main function for driving the program.
|
||
|
*/
|
||
|
int main()
|
||
|
{
|
||
|
// Buffer for holding system info.
|
||
|
SYSTEM_INFO sysInf;
|
||
|
|
||
|
// Grabbing system info.
|
||
|
GetSystemInfo(&sysInf);
|
||
|
|
||
|
// Figuring out the size of a page and printing it.
|
||
|
printf("The size of a page is: %lu\n", sysInf.dwPageSize);
|
||
|
|
||
|
// Allocating a large sum of memory (1M).
|
||
|
printf("Allocating memory...\n");
|
||
|
int *bigboy = (int *)malloc(1048576);
|
||
|
|
||
|
// Buffer for holding memory information.
|
||
|
MEMORY_BASIC_INFORMATION buf;
|
||
|
|
||
|
// Querying memory information about the memory we allocated.
|
||
|
VirtualQuery(bigboy, &buf, sizeof(MEMORY_BASIC_INFORMATION));
|
||
|
|
||
|
// Printing out the memory's state.
|
||
|
printf("The state of the memory is: %s\n", checkMemState(buf.State));
|
||
|
|
||
|
// Freeing the allocated memory.
|
||
|
printf("Freeing memory...\n");
|
||
|
free(bigboy);
|
||
|
|
||
|
// Querying memory information about the memory location again.
|
||
|
VirtualQuery(bigboy, &buf, sizeof(MEMORY_BASIC_INFORMATION));
|
||
|
|
||
|
// Printing out the memory's state again.
|
||
|
printf("The state of the memory is: %s\n", checkMemState(buf.State));
|
||
|
|
||
|
// Goodbye.
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Function that takes the memory state and
|
||
|
* turns it into a user-readable string.
|
||
|
*
|
||
|
* @param DWORD state, the raw value for the
|
||
|
* memory state.
|
||
|
*/
|
||
|
const char *checkMemState(DWORD state)
|
||
|
{
|
||
|
// Committed state (0x1000)
|
||
|
if (state == 4096)
|
||
|
{
|
||
|
return "COMMITTED";
|
||
|
}
|
||
|
// Free state (0x10000)
|
||
|
else if (state == 65536)
|
||
|
{
|
||
|
return "FREE";
|
||
|
}
|
||
|
// Reserved state (0x2000)
|
||
|
else if (state == 8192)
|
||
|
{
|
||
|
return "RESERVED";
|
||
|
}
|
||
|
// Catch-all for any issues.
|
||
|
else
|
||
|
{
|
||
|
return "UNKNOWN";
|
||
|
}
|
||
|
}
|