aboutsummaryrefslogtreecommitdiff
path: root/src/libs
diff options
context:
space:
mode:
Diffstat (limited to 'src/libs')
-rw-r--r--src/libs/dutil/WixToolset.DUtil/dictutil.cpp128
1 files changed, 58 insertions, 70 deletions
diff --git a/src/libs/dutil/WixToolset.DUtil/dictutil.cpp b/src/libs/dutil/WixToolset.DUtil/dictutil.cpp
index 0d0743eb..09b150df 100644
--- a/src/libs/dutil/WixToolset.DUtil/dictutil.cpp
+++ b/src/libs/dutil/WixToolset.DUtil/dictutil.cpp
@@ -72,6 +72,14 @@ struct STRINGDICT_STRUCT
72 72
73const int STRINGDICT_HANDLE_BYTES = sizeof(STRINGDICT_STRUCT); 73const int STRINGDICT_HANDLE_BYTES = sizeof(STRINGDICT_STRUCT);
74 74
75static HRESULT CreateDict(
76 __out_bcount(STRINGDICT_HANDLE_BYTES) STRINGDICT_HANDLE* psdHandle,
77 __in DICT_TYPE dtType,
78 __in DWORD dwNumExpectedItems,
79 __in_opt void** ppvArray,
80 __in size_t cByteOffset,
81 __in DICT_FLAG dfFlags
82 );
75static HRESULT StringHash( 83static HRESULT StringHash(
76 __in const STRINGDICT_STRUCT *psd, 84 __in const STRINGDICT_STRUCT *psd,
77 __in DWORD dwNumBuckets, 85 __in DWORD dwNumBuckets,
@@ -135,41 +143,7 @@ extern "C" HRESULT DAPI DictCreateWithEmbeddedKey(
135 __in DICT_FLAG dfFlags 143 __in DICT_FLAG dfFlags
136 ) 144 )
137{ 145{
138 HRESULT hr = S_OK; 146 return CreateDict(psdHandle, DICT_EMBEDDED_KEY, dwNumExpectedItems, ppvArray, cByteOffset, dfFlags);
139
140 DictExitOnNull(psdHandle, hr, E_INVALIDARG, "Handle not specified while creating dict");
141
142 // Allocate the handle
143 *psdHandle = static_cast<STRINGDICT_HANDLE>(MemAlloc(sizeof(STRINGDICT_STRUCT), FALSE));
144 DictExitOnNull(*psdHandle, hr, E_OUTOFMEMORY, "Failed to allocate dictionary object");
145
146 STRINGDICT_STRUCT *psd = static_cast<STRINGDICT_STRUCT *>(*psdHandle);
147
148 // Fill out the new handle's values
149 psd->dtType = DICT_EMBEDDED_KEY;
150 psd->dfFlags = dfFlags;
151 psd->cByteOffset = cByteOffset;
152 psd->dwBucketSizeIndex = 0;
153 psd->dwNumItems = 0;
154 psd->ppvItemList = NULL;
155 psd->ppvValueArray = ppvArray;
156
157 // Make psd->dwBucketSizeIndex point to the appropriate spot in the prime
158 // array based on expected number of items and items to buckets ratio
159 // Careful: the "-1" in "countof(MAX_BUCKET_SIZES)-1" ensures we don't end
160 // this loop past the end of the array!
161 while (psd->dwBucketSizeIndex < (countof(MAX_BUCKET_SIZES)-1) &&
162 MAX_BUCKET_SIZES[psd->dwBucketSizeIndex] < dwNumExpectedItems * MAX_BUCKETS_TO_ITEMS_RATIO)
163 {
164 ++psd->dwBucketSizeIndex;
165 }
166
167 // Finally, allocate our initial buckets
168 psd->ppvBuckets = static_cast<void**>(MemAlloc(sizeof(void *) * MAX_BUCKET_SIZES[psd->dwBucketSizeIndex], TRUE));
169 DictExitOnNull(psd->ppvBuckets, hr, E_OUTOFMEMORY, "Failed to allocate buckets for dictionary");
170
171LExit:
172 return hr;
173} 147}
174 148
175// The dict will store a set of keys, with no values associated with them. Use DictAddKey() and DictKeyExists() with this dictionary type. 149// The dict will store a set of keys, with no values associated with them. Use DictAddKey() and DictKeyExists() with this dictionary type.
@@ -179,41 +153,7 @@ extern "C" HRESULT DAPI DictCreateStringList(
179 __in DICT_FLAG dfFlags 153 __in DICT_FLAG dfFlags
180 ) 154 )
181{ 155{
182 HRESULT hr = S_OK; 156 return CreateDict(psdHandle, DICT_STRING_LIST, dwNumExpectedItems, NULL, 0, dfFlags);
183
184 DictExitOnNull(psdHandle, hr, E_INVALIDARG, "Handle not specified while creating dict");
185
186 // Allocate the handle
187 *psdHandle = static_cast<STRINGDICT_HANDLE>(MemAlloc(sizeof(STRINGDICT_STRUCT), FALSE));
188 DictExitOnNull(*psdHandle, hr, E_OUTOFMEMORY, "Failed to allocate dictionary object");
189
190 STRINGDICT_STRUCT *psd = static_cast<STRINGDICT_STRUCT *>(*psdHandle);
191
192 // Fill out the new handle's values
193 psd->dtType = DICT_STRING_LIST;
194 psd->dfFlags = dfFlags;
195 psd->cByteOffset = 0;
196 psd->dwBucketSizeIndex = 0;
197 psd->dwNumItems = 0;
198 psd->ppvItemList = NULL;
199 psd->ppvValueArray = NULL;
200
201 // Make psd->dwBucketSizeIndex point to the appropriate spot in the prime
202 // array based on expected number of items and items to buckets ratio
203 // Careful: the "-1" in "countof(MAX_BUCKET_SIZES)-1" ensures we don't end
204 // this loop past the end of the array!
205 while (psd->dwBucketSizeIndex < (countof(MAX_BUCKET_SIZES)-1) &&
206 MAX_BUCKET_SIZES[psd->dwBucketSizeIndex] < dwNumExpectedItems * MAX_BUCKETS_TO_ITEMS_RATIO)
207 {
208 ++psd->dwBucketSizeIndex;
209 }
210
211 // Finally, allocate our initial buckets
212 psd->ppvBuckets = static_cast<void**>(MemAlloc(sizeof(void *) * MAX_BUCKET_SIZES[psd->dwBucketSizeIndex], TRUE));
213 DictExitOnNull(psd->ppvBuckets, hr, E_OUTOFMEMORY, "Failed to allocate buckets for dictionary");
214
215LExit:
216 return hr;
217} 157}
218 158
219extern "C" HRESULT DAPI DictCreateStringListFromArray( 159extern "C" HRESULT DAPI DictCreateStringListFromArray(
@@ -467,6 +407,54 @@ extern "C" void DAPI DictDestroy(
467 ReleaseMem(psd); 407 ReleaseMem(psd);
468} 408}
469 409
410static HRESULT CreateDict(
411 __out_bcount(STRINGDICT_HANDLE_BYTES) STRINGDICT_HANDLE* psdHandle,
412 __in DICT_TYPE dtType,
413 __in DWORD dwNumExpectedItems,
414 __in_opt void** ppvArray,
415 __in size_t cByteOffset,
416 __in DICT_FLAG dfFlags
417 )
418{
419 HRESULT hr = S_OK;
420
421 DictExitOnNull(psdHandle, hr, E_INVALIDARG, "Handle not specified while creating dict.");
422
423 // Allocate the handle
424 *psdHandle = static_cast<STRINGDICT_HANDLE>(MemAlloc(sizeof(STRINGDICT_STRUCT), TRUE));
425 DictExitOnNull(*psdHandle, hr, E_OUTOFMEMORY, "Failed to allocate dictionary object.");
426
427 STRINGDICT_STRUCT* psd = static_cast<STRINGDICT_STRUCT*>(*psdHandle);
428
429 // Fill out the new handle's values
430 psd->dtType = dtType;
431 psd->dfFlags = dfFlags;
432 psd->cByteOffset = cByteOffset;
433 psd->ppvValueArray = ppvArray;
434
435 // Make psd->dwBucketSizeIndex point to the appropriate spot in the prime
436 // array based on expected number of items and items to buckets ratio
437 // Careful: the "-1" in "countof(MAX_BUCKET_SIZES)-1" ensures we don't end
438 // this loop past the end of the array!
439 while (psd->dwBucketSizeIndex < (countof(MAX_BUCKET_SIZES) - 1) &&
440 MAX_BUCKET_SIZES[psd->dwBucketSizeIndex] < dwNumExpectedItems * MAX_BUCKETS_TO_ITEMS_RATIO)
441 {
442 ++psd->dwBucketSizeIndex;
443 }
444
445 hr = MemAllocArray(reinterpret_cast<LPVOID*>(&psd->ppvBuckets), sizeof(void*), MAX_BUCKET_SIZES[psd->dwBucketSizeIndex]);
446 DictExitOnFailure(hr, "Failed to allocate buckets for dictionary.");
447
448 if (dwNumExpectedItems)
449 {
450 hr = MemAllocArray(reinterpret_cast<LPVOID*>(&psd->ppvItemList), sizeof(void*), dwNumExpectedItems);
451 DictExitOnFailure(hr, "Failed to pre-allocate item list for dictionary.");
452 }
453
454LExit:
455 return hr;
456}
457
470static HRESULT StringHash( 458static HRESULT StringHash(
471 __in const STRINGDICT_STRUCT *psd, 459 __in const STRINGDICT_STRUCT *psd,
472 __in DWORD dwNumBuckets, 460 __in DWORD dwNumBuckets,