1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright 2023 Brian Cook (a.k.a. Coding-Badly)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use PhantomData;
use crateALIGNMENT;
use crateGrowStrategy;
use crateSIZE_OF_WCHAR;
/// Adjustments made by [GrowToNearestNibbleWithExtra] when calculating the next buffer capacity
///
/// [EXTRA][1] is either zero or SIZE_OF_WCHAR. It's SIZE_OF_WCHAR to guarantee space for a `NULL`
/// terminator. Internally, Microsoft has struggled with accommodating `NULL`s and determining
/// buffer capacities. Including space for one extra element protects us from those mistakes.
///
/// [SCALE][2] is either one or two. Some Windows API calls return the amount stored instead of the
/// amount needed. Our only option is to guess what capacity the buffer should be. The strategy is
/// to double the buffer capacity after each attempt.
///
/// [FLOOR][3] is an optional minimum value. If not zero, the buffer capacity is never below this
/// value. A non-zero [FLOOR][3] is appropriate for Windows API calls that have what is essentially
/// a recommended buffer capacity (e.g. `MAX_PATH * SIZE_OF_WCHAR`).
///
/// [1]: NearestNibbleAdjustments::EXTRA
/// [2]: NearestNibbleAdjustments::SCALE
/// [3]: NearestNibbleAdjustments::FLOOR
///
/// This is the core implementation for all things that need a smallish static buffer
///
/// [GrowToNearestNibbleWithExtra] is combined with a [NearestNibbleAdjustments] to form a
/// [GrowStrategy] for a given use-case. A combination is exposed to the world as a use-case
/// (e.g. [GrowForStaticText]).
///
/// A [NearestNibbleAdjustments] that just rounds the `desired_capacity` up to the next higher value
/// evenly divisible by 16.
///
/// [`GrowStrategy`] appropriate for small binary data that is unlikely to change where the call
/// returns the buffer size needed.
///
/// This [`GrowStrategy`] works best when the operating system indicates the buffer size needed
/// (`desired_capacity` is known), that size is unlikely to change, and the buffer size is
/// relatively small.
///
/// This [`GrowStrategy`] rounds the buffer size to the next higher value that's evenly divisible by
/// 16.
///
/// The goals are:
///
/// * Be heap friendly by avoiding many small odd sized heap allocations
/// * For the API call to be successful after at most two attempts
///
/// [`GetLogicalProcessorInformationEx`][1] is a good example for this [`GrowStrategy`].
///
/// Favor the [`GrowForSmallBinary`] alias over using this strategy directly so your code can
/// naturally take advantage of improvements.
///
/// [1]: https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/System/SystemInformation/fn.GetLogicalProcessorInformationEx.html
///
/// Alias for the [`GrowToNearestNibble`] [`GrowStrategy`].
///
/// The [`GrowForSmallBinary`] alias should be favored over using [`GrowToNearestNibble`] directly.
/// Future versions may change the strategy for small static binary data. By using this alias your
/// code will naturally take advantage of improvements.
///
pub type GrowForSmallBinary = GrowToNearestNibble;
/// A [NearestNibbleAdjustments] that rounds the `desired_capacity` up to the next higher value
/// evenly divisible by 16 after adding space for a `NULL` terminator.
///
/// [`GrowStrategy`] appropriate for Windows API calls that return the number of characters that
/// need to be stored for success (the needed buffer size is returned).
///
/// This [`GrowStrategy`] works best when the operating system indicates the buffer size needed
/// (`desired_capacity` is known), that size is unlikely to change, and the buffer size is
/// relatively small.
///
/// This [`GrowStrategy`] rounds the buffer size to the next higher value that's evenly divisible by
/// 16 after adding space for a `NULL` terminator.
///
/// The goals are:
///
/// * Be heap friendly by avoiding many small odd sized heap allocations
/// * Avoid any operating system bugs involving the buffer size requested being incorrect because
/// the `NULL` is not considered
/// * For the API call to be successful after at most two attempts
///
/// [`GetUserNameW`][1] is a good example for this [`GrowStrategy`].
///
/// Favor the [`GrowForStaticText`] alias over using this strategy directly so your code can
/// naturally take advantage of improvements.
///
/// [1]: https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/System/WindowsProgramming/fn.GetUserNameW.html
///
/// Alias for the [`GrowToNearestNibbleWithNull`] [`GrowStrategy`].
///
/// The [`GrowForStaticText`] alias should be favored over using [`GrowToNearestNibbleWithNull`]
/// directly. Future versions may change the strategy for static text data. By using this alias
/// your code will naturally take advantage of improvements.
///
pub type GrowForStaticText = GrowToNearestNibbleWithNull;
/// A [NearestNibbleAdjustments] that rounds the `current_size` up to the next higher value evenly
/// divisible by 16 after adding space for a `NULL` terminator. The target is that value doubled.
///
/// [`GrowStrategy`] appropriate for Windows API calls that return the number of characters stored
/// (the needed buffer space is not available).
///
/// This [`GrowStrategy`] works best when the operating system does not provide the buffer size
/// needed, that size is unlikely to change, and the buffer size is relatively small.
///
/// This [`GrowStrategy`] rounds the buffer size to the next higher value that's evenly divisible by
/// 16 after adding space for a `NULL` terminator then doubles the value.
///
/// The goals are:
///
/// * Be heap friendly by avoiding many small odd sized heap allocations
/// * Avoid any operating system bugs involving the buffer size requested being incorrect because
/// the `NULL` is not considered
/// * Ensure the buffer grows quickly to avoid too many API calls
///
/// [`GetModuleFileNameW`][1] is a good example for this [`GrowStrategy`].
///
/// Favor the [`GrowForStoredIsReturned`] alias over using this strategy directly so your code can
/// naturally take advantage of improvements.
///
/// [1]: https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/System/LibraryLoader/fn.GetModuleFileNameW.html
///
/// Alias for the [`GrowByDoubleWithNull`] [`GrowStrategy`].
///
/// The [`GrowForStoredIsReturned`] alias should be favored over using [`GrowByDoubleWithNull`]
/// directly. Future versions may change the strategy for operating system calls that return the
/// number of elements stored. By using this alias your code will naturally take advantage of
/// improvements.
///
pub type GrowForStoredIsReturned<const FLOOR: u64> = ;
/// [`GrowStrategy`] appropriate for large binary data that may change between calls where the call
/// returns the buffer size needed.
///
/// This [`GrowStrategy`] works best when the operating system indicates the buffer size needed
/// (`desired_capacity` is known), that size may change between calls, and the buffer size is
/// relatively large.
///
/// This [`GrowStrategy`] rounds the buffer size to the next higher value that's evenly divisible by
/// 256 after adding space for alignment.
///
/// The goals are:
///
/// * Be heap friendly by avoiding small odd sized heap allocations
/// * For the API call to be successful after at most two attempts
/// * Ensure there is enough space to provide an aligned buffer
///
/// [`GetAdaptersAddresses`][1] is a good example for this [`GrowStrategy`].
///
/// [1]: https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/NetworkManagement/IpHelper/fn.GetAdaptersAddresses.html
///