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: test3.c
8**
9** Purpose: Allocate some memory. Then reallocate that memory into a
10** bigger space on the heap. Check that the first portion of the data is
11** unchanged. Then set the new portion to a value, to ensure that it is
12** properly writable memory.
13**
14**
15**============================================================*/
16
17#include <palsuite.h>
18
19int __cdecl main(int argc, char *argv[])
20{
21
22 HANDLE TheHeap;
23 char* TheMemory;
24 char* ReAllocMemory;
25 int i;
26
27 if(PAL_Initialize(argc, argv) != 0)
28 {
29 return FAIL;
30 }
31
32 TheHeap = GetProcessHeap();
33
34 if(TheHeap == NULL)
35 {
36 Fail("ERROR: GetProcessHeap() returned NULL when it was called. "
37 "GetLastError() returned %d.",GetLastError());
38 }
39
40 /* Allocate 100 bytes on the heap */
41 if((TheMemory = (char*)HeapAlloc(TheHeap, 0, 100)) == NULL)
42 {
43 Fail("ERROR: HeapAlloc returned NULL when it was called. "
44 "GetLastError() returned %d.",GetLastError());
45 }
46
47 /* Set the first 100 bytes to 'X' */
48 memset(TheMemory, 'X', 100);
49
50 /* Reallocate the memory to 200 bytes */
51 ReAllocMemory = (char*)HeapReAlloc(TheHeap, 0, TheMemory, 200);
52
53 if(ReAllocMemory == NULL)
54 {
55 Fail("ERROR: HeapReAlloc failed to reallocate the 100 bytes of "
56 "heap memory. GetLastError returns %d.",GetLastError());
57 }
58
59 /* Check that each of the first 100 bytes hasn't lost any data. */
60 for(i=0; i<100; ++i)
61 {
62
63 if(ReAllocMemory[i] != 'X')
64 {
65 Fail("ERROR: Byte number %d of the reallocated memory block "
66 "is not set to 'X' as it should be.",i);
67 }
68 }
69
70 /* Beyond the first 100 bytes is valid free memory. We'll set all this
71 memory to a value -- though, even if HeapReAlloc didn't work, it might
72 still be possible to memset this memory without raising an exception.
73 */
74 memset(ReAllocMemory+100, 'Z', 100);
75
76 PAL_Terminate();
77 return PASS;
78}
79