diff options
Diffstat (limited to '')
-rw-r--r-- | src/dutil/memutil.cpp | 51 |
1 files changed, 32 insertions, 19 deletions
diff --git a/src/dutil/memutil.cpp b/src/dutil/memutil.cpp index 578c65ee..c805a9c0 100644 --- a/src/dutil/memutil.cpp +++ b/src/dutil/memutil.cpp | |||
@@ -1,10 +1,23 @@ | |||
1 | #pragma once | ||
2 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. | 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
3 | 2 | ||
4 | |||
5 | #include "precomp.h" | 3 | #include "precomp.h" |
6 | 4 | ||
7 | 5 | ||
6 | // Exit macros | ||
7 | #define MemExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_MEMUTIL, x, s, __VA_ARGS__) | ||
8 | #define MemExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_MEMUTIL, x, s, __VA_ARGS__) | ||
9 | #define MemExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_MEMUTIL, x, s, __VA_ARGS__) | ||
10 | #define MemExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_MEMUTIL, x, s, __VA_ARGS__) | ||
11 | #define MemExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_MEMUTIL, x, s, __VA_ARGS__) | ||
12 | #define MemExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_MEMUTIL, x, s, __VA_ARGS__) | ||
13 | #define MemExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_MEMUTIL, p, x, e, s, __VA_ARGS__) | ||
14 | #define MemExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_MEMUTIL, p, x, s, __VA_ARGS__) | ||
15 | #define MemExitOnNullDebugTrace(p, x, e, s, ...) ExitOnNullDebugTraceSource(DUTIL_SOURCE_MEMUTIL, p, x, e, s, __VA_ARGS__) | ||
16 | #define MemExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_MEMUTIL, p, x, s, __VA_ARGS__) | ||
17 | #define MemExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_MEMUTIL, e, x, s, __VA_ARGS__) | ||
18 | #define MemExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_MEMUTIL, g, x, s, __VA_ARGS__) | ||
19 | |||
20 | |||
8 | #if DEBUG | 21 | #if DEBUG |
9 | static BOOL vfMemInitialized = FALSE; | 22 | static BOOL vfMemInitialized = FALSE; |
10 | #endif | 23 | #endif |
@@ -51,7 +64,7 @@ extern "C" HRESULT DAPI MemReAllocSecure( | |||
51 | __in LPVOID pv, | 64 | __in LPVOID pv, |
52 | __in SIZE_T cbSize, | 65 | __in SIZE_T cbSize, |
53 | __in BOOL fZero, | 66 | __in BOOL fZero, |
54 | __out LPVOID* ppvNew | 67 | __deref_out LPVOID* ppvNew |
55 | ) | 68 | ) |
56 | { | 69 | { |
57 | // AssertSz(vfMemInitialized, "MemInitialize() not called, this would normally crash"); | 70 | // AssertSz(vfMemInitialized, "MemInitialize() not called, this would normally crash"); |
@@ -72,14 +85,14 @@ extern "C" HRESULT DAPI MemReAllocSecure( | |||
72 | const SIZE_T cbCurrent = MemSize(pv); | 85 | const SIZE_T cbCurrent = MemSize(pv); |
73 | if (-1 == cbCurrent) | 86 | if (-1 == cbCurrent) |
74 | { | 87 | { |
75 | ExitOnFailure(hr = E_INVALIDARG, "Failed to get memory size"); | 88 | MemExitOnRootFailure(hr = E_INVALIDARG, "Failed to get memory size"); |
76 | } | 89 | } |
77 | 90 | ||
78 | // HeapReAlloc may allocate more memory than requested. | 91 | // HeapReAlloc may allocate more memory than requested. |
79 | const SIZE_T cbNew = MemSize(pvNew); | 92 | const SIZE_T cbNew = MemSize(pvNew); |
80 | if (-1 == cbNew) | 93 | if (-1 == cbNew) |
81 | { | 94 | { |
82 | ExitOnFailure(hr = E_INVALIDARG, "Failed to get memory size"); | 95 | MemExitOnRootFailure(hr = E_INVALIDARG, "Failed to get memory size"); |
83 | } | 96 | } |
84 | 97 | ||
85 | cbSize = cbNew; | 98 | cbSize = cbNew; |
@@ -94,7 +107,7 @@ extern "C" HRESULT DAPI MemReAllocSecure( | |||
94 | MemFree(pv); | 107 | MemFree(pv); |
95 | } | 108 | } |
96 | } | 109 | } |
97 | ExitOnNull(pvNew, hr, E_OUTOFMEMORY, "Failed to reallocate memory"); | 110 | MemExitOnNull(pvNew, hr, E_OUTOFMEMORY, "Failed to reallocate memory"); |
98 | 111 | ||
99 | *ppvNew = pvNew; | 112 | *ppvNew = pvNew; |
100 | pvNew = NULL; | 113 | pvNew = NULL; |
@@ -129,10 +142,10 @@ extern "C" HRESULT DAPI MemReAllocArray( | |||
129 | SIZE_T cbNew = 0; | 142 | SIZE_T cbNew = 0; |
130 | 143 | ||
131 | hr = ::DWordAdd(cArray, dwNewItemCount, &cNew); | 144 | hr = ::DWordAdd(cArray, dwNewItemCount, &cNew); |
132 | ExitOnFailure(hr, "Integer overflow when calculating new element count."); | 145 | MemExitOnFailure(hr, "Integer overflow when calculating new element count."); |
133 | 146 | ||
134 | hr = ::SIZETMult(cNew, cbArrayType, &cbNew); | 147 | hr = ::SIZETMult(cNew, cbArrayType, &cbNew); |
135 | ExitOnFailure(hr, "Integer overflow when calculating new block size."); | 148 | MemExitOnFailure(hr, "Integer overflow when calculating new block size."); |
136 | 149 | ||
137 | if (*ppvArray) | 150 | if (*ppvArray) |
138 | { | 151 | { |
@@ -140,7 +153,7 @@ extern "C" HRESULT DAPI MemReAllocArray( | |||
140 | if (cbCurrent < cbNew) | 153 | if (cbCurrent < cbNew) |
141 | { | 154 | { |
142 | pvNew = MemReAlloc(*ppvArray, cbNew, TRUE); | 155 | pvNew = MemReAlloc(*ppvArray, cbNew, TRUE); |
143 | ExitOnNull(pvNew, hr, E_OUTOFMEMORY, "Failed to allocate larger array."); | 156 | MemExitOnNull(pvNew, hr, E_OUTOFMEMORY, "Failed to allocate larger array."); |
144 | 157 | ||
145 | *ppvArray = pvNew; | 158 | *ppvArray = pvNew; |
146 | } | 159 | } |
@@ -148,7 +161,7 @@ extern "C" HRESULT DAPI MemReAllocArray( | |||
148 | else | 161 | else |
149 | { | 162 | { |
150 | pvNew = MemAlloc(cbNew, TRUE); | 163 | pvNew = MemAlloc(cbNew, TRUE); |
151 | ExitOnNull(pvNew, hr, E_OUTOFMEMORY, "Failed to allocate new array."); | 164 | MemExitOnNull(pvNew, hr, E_OUTOFMEMORY, "Failed to allocate new array."); |
152 | 165 | ||
153 | *ppvArray = pvNew; | 166 | *ppvArray = pvNew; |
154 | } | 167 | } |
@@ -159,7 +172,7 @@ LExit: | |||
159 | 172 | ||
160 | 173 | ||
161 | extern "C" HRESULT DAPI MemEnsureArraySize( | 174 | extern "C" HRESULT DAPI MemEnsureArraySize( |
162 | __deref_out_bcount(cArray * cbArrayType) LPVOID* ppvArray, | 175 | __deref_inout_bcount(cArray * cbArrayType) LPVOID* ppvArray, |
163 | __in DWORD cArray, | 176 | __in DWORD cArray, |
164 | __in SIZE_T cbArrayType, | 177 | __in SIZE_T cbArrayType, |
165 | __in DWORD dwGrowthCount | 178 | __in DWORD dwGrowthCount |
@@ -171,10 +184,10 @@ extern "C" HRESULT DAPI MemEnsureArraySize( | |||
171 | SIZE_T cbNew = 0; | 184 | SIZE_T cbNew = 0; |
172 | 185 | ||
173 | hr = ::DWordAdd(cArray, dwGrowthCount, &cNew); | 186 | hr = ::DWordAdd(cArray, dwGrowthCount, &cNew); |
174 | ExitOnFailure(hr, "Integer overflow when calculating new element count."); | 187 | MemExitOnFailure(hr, "Integer overflow when calculating new element count."); |
175 | 188 | ||
176 | hr = ::SIZETMult(cNew, cbArrayType, &cbNew); | 189 | hr = ::SIZETMult(cNew, cbArrayType, &cbNew); |
177 | ExitOnFailure(hr, "Integer overflow when calculating new block size."); | 190 | MemExitOnFailure(hr, "Integer overflow when calculating new block size."); |
178 | 191 | ||
179 | if (*ppvArray) | 192 | if (*ppvArray) |
180 | { | 193 | { |
@@ -183,7 +196,7 @@ extern "C" HRESULT DAPI MemEnsureArraySize( | |||
183 | if (cbCurrent < cbUsed) | 196 | if (cbCurrent < cbUsed) |
184 | { | 197 | { |
185 | pvNew = MemReAlloc(*ppvArray, cbNew, TRUE); | 198 | pvNew = MemReAlloc(*ppvArray, cbNew, TRUE); |
186 | ExitOnNull(pvNew, hr, E_OUTOFMEMORY, "Failed to allocate array larger."); | 199 | MemExitOnNull(pvNew, hr, E_OUTOFMEMORY, "Failed to allocate array larger."); |
187 | 200 | ||
188 | *ppvArray = pvNew; | 201 | *ppvArray = pvNew; |
189 | } | 202 | } |
@@ -191,7 +204,7 @@ extern "C" HRESULT DAPI MemEnsureArraySize( | |||
191 | else | 204 | else |
192 | { | 205 | { |
193 | pvNew = MemAlloc(cbNew, TRUE); | 206 | pvNew = MemAlloc(cbNew, TRUE); |
194 | ExitOnNull(pvNew, hr, E_OUTOFMEMORY, "Failed to allocate new array."); | 207 | MemExitOnNull(pvNew, hr, E_OUTOFMEMORY, "Failed to allocate new array."); |
195 | 208 | ||
196 | *ppvArray = pvNew; | 209 | *ppvArray = pvNew; |
197 | } | 210 | } |
@@ -202,7 +215,7 @@ LExit: | |||
202 | 215 | ||
203 | 216 | ||
204 | extern "C" HRESULT DAPI MemInsertIntoArray( | 217 | extern "C" HRESULT DAPI MemInsertIntoArray( |
205 | __deref_out_bcount((cExistingArray + cInsertItems) * cbArrayType) LPVOID* ppvArray, | 218 | __deref_inout_bcount((cExistingArray + cInsertItems) * cbArrayType) LPVOID* ppvArray, |
206 | __in DWORD dwInsertIndex, | 219 | __in DWORD dwInsertIndex, |
207 | __in DWORD cInsertItems, | 220 | __in DWORD cInsertItems, |
208 | __in DWORD cExistingArray, | 221 | __in DWORD cExistingArray, |
@@ -220,7 +233,7 @@ extern "C" HRESULT DAPI MemInsertIntoArray( | |||
220 | } | 233 | } |
221 | 234 | ||
222 | hr = MemEnsureArraySize(ppvArray, cExistingArray + cInsertItems, cbArrayType, dwGrowthCount); | 235 | hr = MemEnsureArraySize(ppvArray, cExistingArray + cInsertItems, cbArrayType, dwGrowthCount); |
223 | ExitOnFailure(hr, "Failed to resize array while inserting items"); | 236 | MemExitOnFailure(hr, "Failed to resize array while inserting items"); |
224 | 237 | ||
225 | pbArray = reinterpret_cast<BYTE *>(*ppvArray); | 238 | pbArray = reinterpret_cast<BYTE *>(*ppvArray); |
226 | for (i = cExistingArray + cInsertItems - 1; i > dwInsertIndex; --i) | 239 | for (i = cExistingArray + cInsertItems - 1; i > dwInsertIndex; --i) |
@@ -236,7 +249,7 @@ LExit: | |||
236 | } | 249 | } |
237 | 250 | ||
238 | extern "C" void DAPI MemRemoveFromArray( | 251 | extern "C" void DAPI MemRemoveFromArray( |
239 | __inout_bcount((cExistingArray + cInsertItems) * cbArrayType) LPVOID pvArray, | 252 | __inout_bcount((cExistingArray) * cbArrayType) LPVOID pvArray, |
240 | __in DWORD dwRemoveIndex, | 253 | __in DWORD dwRemoveIndex, |
241 | __in DWORD cRemoveItems, | 254 | __in DWORD cRemoveItems, |
242 | __in DWORD cExistingArray, | 255 | __in DWORD cExistingArray, |
@@ -261,7 +274,7 @@ extern "C" void DAPI MemRemoveFromArray( | |||
261 | } | 274 | } |
262 | 275 | ||
263 | extern "C" void DAPI MemArraySwapItems( | 276 | extern "C" void DAPI MemArraySwapItems( |
264 | __inout_bcount((cExistingArray) * cbArrayType) LPVOID pvArray, | 277 | __inout_bcount(cbArrayType) LPVOID pvArray, |
265 | __in DWORD dwIndex1, | 278 | __in DWORD dwIndex1, |
266 | __in DWORD dwIndex2, | 279 | __in DWORD dwIndex2, |
267 | __in SIZE_T cbArrayType | 280 | __in SIZE_T cbArrayType |