Merge pull request #2890 from sivarv/master
[platform/upstream/coreclr.git] / src / pal / src / memory / local.cpp
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
8
9 Module Name:
10
11     local.c
12
13 Abstract:
14
15     Implementation of local memory management functions.
16
17 Revision History:
18
19
20
21 --*/
22
23 #include "pal/palinternal.h"
24 #include "pal/dbgmsg.h"
25
26
27 SET_DEFAULT_DEBUG_CHANNEL(MEM);
28
29 static
30 int
31 AllocFlagsToHeapAllocFlags (IN  UINT  AllocFlags,
32                        OUT PUINT pHeapallocFlags)
33 {
34     int success = 1;
35     UINT newFlags = 0, flags = AllocFlags;
36     if (flags & LMEM_ZEROINIT) {
37         newFlags |= HEAP_ZERO_MEMORY;
38         flags &= ~LMEM_ZEROINIT;
39     }
40     if (flags != 0) {
41         ASSERT("Invalid parameter AllocFlags=0x%x\n", AllocFlags);
42         SetLastError(ERROR_INVALID_PARAMETER);
43         success = 0;
44     }
45     if (success) {
46         *pHeapallocFlags = newFlags;
47     }
48     return success;
49 }
50         
51     
52
53 /*++
54 Function:
55   LocalAlloc
56
57 See MSDN doc.
58 --*/
59 HLOCAL
60 PALAPI
61 LocalAlloc(
62            IN UINT uFlags,
63            IN SIZE_T uBytes)
64 {
65     LPVOID lpRetVal = NULL;
66     PERF_ENTRY(LocalAlloc);
67     ENTRY("LocalAlloc (uFlags=%#x, uBytes=%u)\n", uFlags, uBytes);
68
69     if (!AllocFlagsToHeapAllocFlags (uFlags, &uFlags)) {
70         goto done;
71     }
72
73     lpRetVal = HeapAlloc( GetProcessHeap(), uFlags, uBytes );
74
75 done:
76     LOGEXIT( "LocalAlloc returning %p.\n", lpRetVal );
77     PERF_EXIT(LocalAlloc);
78     return (HLOCAL) lpRetVal;
79 }
80
81 /*++
82 Function:
83 LocalReAlloc
84
85 See MSDN doc.
86 --*/
87 HLOCAL
88 PALAPI
89 LocalReAlloc(
90        IN HLOCAL hMem,
91        IN SIZE_T uBytes,
92        IN UINT   uFlags)
93 {
94     LPVOID lpRetVal = NULL;
95     PERF_ENTRY(LocalReAlloc);
96     ENTRY("LocalReAlloc (hMem=%p, uBytes=%u, uFlags=%#x)\n", hMem, uBytes, uFlags);
97
98     if (uFlags != LMEM_MOVEABLE) {
99         // Currently valid iff uFlags is LMEM_MOVEABLE
100         ASSERT("Invalid parameter uFlags=0x%x\n", uFlags);
101         SetLastError(ERROR_INVALID_PARAMETER);
102         goto done;
103     }
104     uFlags = 0;
105
106     lpRetVal = HeapReAlloc(GetProcessHeap(), uFlags, hMem, uBytes);
107
108 done:
109     LOGEXIT("LocalReAlloc returning %p.\n", lpRetVal);
110     PERF_EXIT(LocalReAlloc);
111     return (HLOCAL)lpRetVal;
112 }
113
114 /*++
115 Function:
116   LocalFree
117
118 See MSDN doc.
119 --*/
120 HLOCAL
121 PALAPI
122 LocalFree(
123           IN HLOCAL hMem)
124 {
125     BOOL bRetVal = FALSE;
126     PERF_ENTRY(LocalFree);
127     ENTRY("LocalFree (hmem=%p)\n", hMem);
128
129     if ( hMem )
130     {
131         bRetVal = HeapFree( GetProcessHeap(), 0, hMem );
132     }
133     else
134     {
135         bRetVal = TRUE;
136     }
137     
138     LOGEXIT( "LocalFree returning %p.\n", bRetVal == TRUE ? (HLOCAL)NULL : hMem );
139     PERF_EXIT(LocalFree);
140     return bRetVal == TRUE ? (HLOCAL)NULL : hMem;
141 }