Merge pull request #1452 from akoeplinger/dictionary-duplicatekey
[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 Function:
84 LocalReAlloc
85
86 See MSDN doc.
87 --*/
88 HLOCAL
89 PALAPI
90 LocalReAlloc(
91        IN HLOCAL hMem,
92        IN SIZE_T uBytes,
93        IN UINT   uFlags)
94 {
95     LPVOID lpRetVal = NULL;
96     PERF_ENTRY(LocalReAlloc);
97     ENTRY("LocalReAlloc (hMem=%p, uBytes=%u, uFlags=%#x)\n", hMem, uBytes, uFlags);
98
99     if (uFlags != LMEM_MOVEABLE) {
100         // Currently valid iff uFlags is LMEM_MOVEABLE
101         ASSERT("Invalid parameter uFlags=0x%x\n", uFlags);
102         SetLastError(ERROR_INVALID_PARAMETER);
103         goto done;
104     }
105     uFlags = 0;
106
107     lpRetVal = HeapReAlloc(GetProcessHeap(), uFlags, hMem, uBytes);
108
109 done:
110     LOGEXIT("LocalReAlloc returning %p.\n", lpRetVal);
111     PERF_EXIT(LocalReAlloc);
112     return (HLOCAL)lpRetVal;
113 }
114
115 /*++
116 Function:
117   LocalFree
118
119 See MSDN doc.
120 --*/
121 HLOCAL
122 PALAPI
123 LocalFree(
124           IN HLOCAL hMem)
125 {
126     BOOL bRetVal = FALSE;
127     PERF_ENTRY(LocalFree);
128     ENTRY("LocalFree (hmem=%p)\n", hMem);
129
130     if ( hMem )
131     {
132         bRetVal = HeapFree( GetProcessHeap(), 0, hMem );
133     }
134     else
135     {
136         bRetVal = TRUE;
137     }
138     
139     LOGEXIT( "LocalFree returning %p.\n", bRetVal == TRUE ? (HLOCAL)NULL : hMem );
140     PERF_EXIT(LocalFree);
141     return bRetVal == TRUE ? (HLOCAL)NULL : hMem;
142 }