1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5/*=============================================================
6**
7** Source: virtualalloc.c
8**
9** Purpose: Positive test the VirtualAlloc API.
10** Ensure that memory committed through VirtualAlloc,
11** then freed, then committed again is zeroed.
12**
13**
14**============================================================*/
15#include <palsuite.h>
16
17int __cdecl main(int argc, char *argv[])
18{
19 int err;
20 int *ptr;
21
22 //Initialize the PAL environment
23 err = PAL_Initialize(argc, argv);
24 if(0 != err)
25 {
26 ExitProcess(FAIL);
27 }
28
29 ptr = (int *) VirtualAlloc(NULL, 4096, MEM_COMMIT | MEM_RESERVE,
30 PAGE_READWRITE);
31 if (ptr == NULL)
32 {
33 Fail("First VirtualAlloc failed!\n");
34 }
35 *ptr = 123;
36
37 if (!VirtualFree(ptr, 4096, MEM_DECOMMIT))
38 {
39 Fail("VirtualFree failed!\n");
40 }
41
42 ptr = (int *) VirtualAlloc(ptr, 4096, MEM_COMMIT, PAGE_READWRITE);
43 if (ptr == NULL)
44 {
45 Fail("Second VirtualAlloc failed!\n");
46 }
47 if (*ptr != 0)
48 {
49 Fail("VirtualAlloc failed to zero its memory!\n");
50 }
51
52 PAL_Terminate();
53 return PASS;
54}
55