| 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: heapalloc.c |
| 8 | ** |
| 9 | ** Purpose: Positive test the HeapAlloc API. |
| 10 | ** Call HeapAlloc by pssing zero as control flag |
| 11 | ** Call HeapAlloc by passing HEAP_ZERO_MEMORY as control flag |
| 12 | ** Call HeapAlloc to allocate one byte heap memory |
| 13 | ** Call HeapAlloc to allocate maximum available heap memory |
| 14 | ** |
| 15 | ** |
| 16 | **============================================================*/ |
| 17 | #include <palsuite.h> |
| 18 | |
| 19 | #define HEAPSIZE 64 |
| 20 | |
| 21 | int __cdecl main(int argc, char *argv[]) |
| 22 | { |
| 23 | int err; |
| 24 | HANDLE ProcessHeapHandle; |
| 25 | LPVOID lpHeap; |
| 26 | |
| 27 | /* Initialize the PAL environment */ |
| 28 | err = PAL_Initialize(argc, argv); |
| 29 | if(0 != 0) |
| 30 | { |
| 31 | ExitProcess(FAIL); |
| 32 | } |
| 33 | |
| 34 | /* Retrieve the calling process heap handle */ |
| 35 | ProcessHeapHandle = GetProcessHeap(); |
| 36 | |
| 37 | if(!ProcessHeapHandle) |
| 38 | { |
| 39 | Fail("\nFailed to call GetProcessHeap API!\n" ); |
| 40 | } |
| 41 | |
| 42 | /* allocate a heap memory in specified size */ |
| 43 | lpHeap = HeapAlloc(ProcessHeapHandle, /* HeapHandle */ |
| 44 | 0, /* control flag */ |
| 45 | HEAPSIZE); /* /specify the heap size */ |
| 46 | if(NULL == lpHeap) |
| 47 | { |
| 48 | Fail("Failed to call HeapAlloc API!\n" ); |
| 49 | } |
| 50 | |
| 51 | /* free the heap memory */ |
| 52 | err = HeapFree(ProcessHeapHandle, |
| 53 | 0, |
| 54 | lpHeap); |
| 55 | if(0 == err) |
| 56 | { |
| 57 | Fail("Failed to call HeapFree API!\n" ); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | /* allocate a heap memory in 1 byte size */ |
| 62 | lpHeap = HeapAlloc(ProcessHeapHandle, /* HeapHandle */ |
| 63 | 0, /* control flag */ |
| 64 | 1); /* specify the heap size*/ |
| 65 | if(NULL == lpHeap) |
| 66 | { |
| 67 | Fail("Failed to call HeapAlloc API to allocate one byte heap memory!\n" ); |
| 68 | } |
| 69 | |
| 70 | /* free the heap memory */ |
| 71 | err = HeapFree(ProcessHeapHandle, |
| 72 | 0, |
| 73 | lpHeap); |
| 74 | if(0 == err) |
| 75 | { |
| 76 | Fail("Failed to call HeapFree API!\n" ); |
| 77 | } |
| 78 | |
| 79 | /* allocate a heap memory and initialize it to zero */ |
| 80 | lpHeap = HeapAlloc(ProcessHeapHandle,/* HeapHandle */ |
| 81 | HEAP_ZERO_MEMORY,/* control flag */ |
| 82 | HEAPSIZE); /* specify the heap size */ |
| 83 | if(NULL == lpHeap) |
| 84 | { |
| 85 | Fail("Failed to call HeapAlloc API with HEAP_ZERO_MEMORY control flag!\n" ); |
| 86 | } |
| 87 | |
| 88 | /* free the heap memory */ |
| 89 | err = HeapFree(ProcessHeapHandle, |
| 90 | 0, |
| 91 | lpHeap); |
| 92 | if(0 == err) |
| 93 | { |
| 94 | Fail("Failed to call HeapFree API!\n" ); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | PAL_Terminate(); |
| 99 | return PASS; |
| 100 | } |
| 101 | |