Merge pull request #662 from richlander/rich-wiki
[platform/upstream/coreclr.git] / src / pal / src / memory / local.cpp
1 //
2 // Copyright (c) Microsoft. All rights reserved.
3 // Licensed under the MIT license. See LICENSE file in the project root for full license information. 
4 //
5
6 /*++
7
8
9
10 Module Name:
11
12     local.c
13
14 Abstract:
15
16     Implementation of local memory management functions.
17
18 Revision History:
19
20
21
22 --*/
23
24 #include "pal/palinternal.h"
25 #include "pal/dbgmsg.h"
26
27
28 SET_DEFAULT_DEBUG_CHANNEL(MEM);
29
30 static
31 int
32 AllocFlagsToHeapAllocFlags (IN  UINT  AllocFlags,
33                        OUT PUINT pHeapallocFlags)
34 {
35     int success = 1;
36     UINT newFlags = 0, flags = AllocFlags;
37     if (flags & LMEM_ZEROINIT) {
38         newFlags |= HEAP_ZERO_MEMORY;
39         flags &= ~LMEM_ZEROINIT;
40     }
41     if (flags != 0) {
42         ASSERT("Invalid parameter AllocFlags=0x%x\n", AllocFlags);
43         SetLastError(ERROR_INVALID_PARAMETER);
44         success = 0;
45     }
46     if (success) {
47         *pHeapallocFlags = newFlags;
48     }
49     return success;
50 }
51         
52     
53
54 /*++
55 Function:
56   LocalAlloc
57
58 See MSDN doc.
59 --*/
60 HLOCAL
61 PALAPI
62 LocalAlloc(
63            IN UINT uFlags,
64            IN SIZE_T uBytes)
65 {
66     LPVOID lpRetVal = NULL;
67     PERF_ENTRY(LocalAlloc);
68     ENTRY("LocalAlloc (uFlags=%#x, uBytes=%u)\n", uFlags, uBytes);
69
70     if (!AllocFlagsToHeapAllocFlags (uFlags, &uFlags)) {
71         goto done;
72     }
73
74     lpRetVal = HeapAlloc( GetProcessHeap(), uFlags, uBytes );
75
76 done:
77     LOGEXIT( "LocalAlloc returning %p.\n", lpRetVal );
78     PERF_EXIT(LocalAlloc);
79     return (HLOCAL) lpRetVal;
80 }
81
82
83 /*++
84 Function:
85   LocalFree
86
87 See MSDN doc.
88 --*/
89 HLOCAL
90 PALAPI
91 LocalFree(
92           IN HLOCAL hMem)
93 {
94     BOOL bRetVal = FALSE;
95     PERF_ENTRY(LocalFree);
96     ENTRY("LocalFree (hmem=%p)\n", hMem);
97
98     if ( hMem )
99     {
100         bRetVal = HeapFree( GetProcessHeap(), 0, hMem );
101     }
102     else
103     {
104         bRetVal = TRUE;
105     }
106     
107     LOGEXIT( "LocalFree returning %p.\n", bRetVal == TRUE ? (HLOCAL)NULL : hMem );
108     PERF_EXIT(LocalFree);
109     return bRetVal == TRUE ? (HLOCAL)NULL : hMem;
110 }