@@ -119,8 +119,6 @@ internal func _isStackAllocationSafe(byteCount: Int, alignment: Int) -> Bool {
119
119
///
120
120
/// - Returns: Whatever is returned by `body`.
121
121
///
122
- /// - Throws: Whatever is thrown by `body`.
123
- ///
124
122
/// This function encapsulates the various calls to builtins required by
125
123
/// `withUnsafeTemporaryAllocation()`.
126
124
@_alwaysEmitIntoClient @_transparent
@@ -130,13 +128,13 @@ internal func _withUnsafeTemporaryAllocation<
130
128
of type: T . Type ,
131
129
capacity: Int ,
132
130
alignment: Int ,
133
- _ body: ( Builtin . RawPointer ) throws -> R
134
- ) rethrows -> R {
131
+ _ body: ( Builtin . RawPointer ) -> R
132
+ ) -> R {
135
133
// How many bytes do we need to allocate?
136
134
let byteCount = _byteCountForTemporaryAllocation ( of: type, capacity: capacity)
137
135
138
136
guard _isStackAllocationSafe ( byteCount: byteCount, alignment: alignment) else {
139
- return try _fallBackToHeapAllocation ( byteCount: byteCount, alignment: alignment, body)
137
+ return _fallBackToHeapAllocation ( byteCount: byteCount, alignment: alignment, body)
140
138
}
141
139
142
140
// This declaration must come BEFORE Builtin.stackAlloc() or
@@ -154,15 +152,9 @@ internal func _withUnsafeTemporaryAllocation<
154
152
// The multiple calls to Builtin.stackDealloc() are because defer { } produces
155
153
// a child function at the SIL layer and that conflicts with the verifier's
156
154
// idea of a stack allocation's lifetime.
157
- do {
158
- result = try body ( stackAddress)
159
- Builtin . stackDealloc ( stackAddress)
160
- return result
161
-
162
- } catch {
163
- Builtin . stackDealloc ( stackAddress)
164
- throw error
165
- }
155
+ result = body ( stackAddress)
156
+ Builtin . stackDealloc ( stackAddress)
157
+ return result
166
158
#else
167
159
fatalError ( " unsupported compiler " )
168
160
#endif
@@ -175,13 +167,13 @@ internal func _withUnprotectedUnsafeTemporaryAllocation<
175
167
of type: T . Type ,
176
168
capacity: Int ,
177
169
alignment: Int ,
178
- _ body: ( Builtin . RawPointer ) throws -> R
179
- ) rethrows -> R {
170
+ _ body: ( Builtin . RawPointer ) -> R
171
+ ) -> R {
180
172
// How many bytes do we need to allocate?
181
173
let byteCount = _byteCountForTemporaryAllocation ( of: type, capacity: capacity)
182
174
183
175
guard _isStackAllocationSafe ( byteCount: byteCount, alignment: alignment) else {
184
- return try _fallBackToHeapAllocation ( byteCount: byteCount, alignment: alignment, body)
176
+ return _fallBackToHeapAllocation ( byteCount: byteCount, alignment: alignment, body)
185
177
}
186
178
187
179
// This declaration must come BEFORE Builtin.unprotectedStackAlloc() or
@@ -198,23 +190,17 @@ internal func _withUnprotectedUnsafeTemporaryAllocation<
198
190
// The multiple calls to Builtin.stackDealloc() are because defer { } produces
199
191
// a child function at the SIL layer and that conflicts with the verifier's
200
192
// idea of a stack allocation's lifetime.
201
- do {
202
- result = try body ( stackAddress)
203
- Builtin . stackDealloc ( stackAddress)
204
- return result
205
-
206
- } catch {
207
- Builtin . stackDealloc ( stackAddress)
208
- throw error
209
- }
193
+ result = body ( stackAddress)
194
+ Builtin . stackDealloc ( stackAddress)
195
+ return result
210
196
}
211
197
212
198
@_alwaysEmitIntoClient @_transparent
213
- internal func _fallBackToHeapAllocation< R: ~ Copyable> (
199
+ internal func _fallBackToHeapAllocation< R: ~ Copyable, E : Error > (
214
200
byteCount: Int ,
215
201
alignment: Int ,
216
- _ body: ( Builtin . RawPointer ) throws -> R
217
- ) rethrows -> R {
202
+ _ body: ( Builtin . RawPointer ) throws ( E ) -> R
203
+ ) throws ( E ) -> R {
218
204
let buffer = UnsafeMutableRawPointer . allocate (
219
205
byteCount: byteCount,
220
206
alignment: alignment
@@ -259,22 +245,28 @@ internal func _fallBackToHeapAllocation<R: ~Copyable>(
259
245
/// the buffer) must not escape. It will be deallocated when `body` returns and
260
246
/// cannot be used afterward.
261
247
@_alwaysEmitIntoClient @_transparent
262
- public func withUnsafeTemporaryAllocation< R: ~ Copyable> (
248
+ public func withUnsafeTemporaryAllocation< R: ~ Copyable, E : Error > (
263
249
byteCount: Int ,
264
250
alignment: Int ,
265
- _ body: ( UnsafeMutableRawBufferPointer ) throws -> R
266
- ) rethrows -> R {
267
- return try _withUnsafeTemporaryAllocation (
251
+ _ body: ( UnsafeMutableRawBufferPointer ) throws ( E ) -> R
252
+ ) throws ( E ) -> R {
253
+ let result : Result < R , E > = _withUnsafeTemporaryAllocation (
268
254
of: Int8 . self,
269
255
capacity: byteCount,
270
256
alignment: alignment
271
257
) { pointer in
272
- let buffer = unsafe UnsafeMutableRawBufferPointer(
273
- start: . init( pointer) ,
274
- count: byteCount
275
- )
276
- return try unsafe body( buffer)
258
+ do throws ( E) {
259
+ let buffer = unsafe UnsafeMutableRawBufferPointer(
260
+ start: . init( pointer) ,
261
+ count: byteCount
262
+ )
263
+ return . success( try unsafe body( buffer) )
264
+ } catch {
265
+ return . failure( error)
266
+ }
277
267
}
268
+
269
+ return try result. get ( )
278
270
}
279
271
280
272
/// Provides scoped access to a raw buffer pointer with the specified byte count
@@ -283,22 +275,28 @@ public func withUnsafeTemporaryAllocation<R: ~Copyable>(
283
275
/// This function is similar to `withUnsafeTemporaryAllocation`, except that it
284
276
/// doesn't trigger stack protection for the stack allocated memory.
285
277
@_alwaysEmitIntoClient @_transparent
286
- public func _withUnprotectedUnsafeTemporaryAllocation< R: ~ Copyable> (
278
+ public func _withUnprotectedUnsafeTemporaryAllocation< R: ~ Copyable, E : Error > (
287
279
byteCount: Int ,
288
280
alignment: Int ,
289
- _ body: ( UnsafeMutableRawBufferPointer ) throws -> R
290
- ) rethrows -> R {
291
- return try _withUnprotectedUnsafeTemporaryAllocation (
281
+ _ body: ( UnsafeMutableRawBufferPointer ) throws ( E ) -> R
282
+ ) throws ( E ) -> R {
283
+ let result : Result < R , E > = _withUnprotectedUnsafeTemporaryAllocation (
292
284
of: Int8 . self,
293
285
capacity: byteCount,
294
286
alignment: alignment
295
287
) { pointer in
296
- let buffer = unsafe UnsafeMutableRawBufferPointer(
297
- start: . init( pointer) ,
298
- count: byteCount
299
- )
300
- return try unsafe body( buffer)
288
+ do throws ( E) {
289
+ let buffer = unsafe UnsafeMutableRawBufferPointer(
290
+ start: . init( pointer) ,
291
+ count: byteCount
292
+ )
293
+ return try unsafe . success ( body ( buffer) )
294
+ } catch {
295
+ return . failure( error)
296
+ }
301
297
}
298
+
299
+ return try result. get ( )
302
300
}
303
301
304
302
/// Provides scoped access to a buffer pointer to memory of the specified type
@@ -334,24 +332,31 @@ public func _withUnprotectedUnsafeTemporaryAllocation<R: ~Copyable>(
334
332
/// cannot be used afterward.
335
333
@_alwaysEmitIntoClient @_transparent
336
334
public func withUnsafeTemporaryAllocation<
337
- T: ~ Copyable, R: ~ Copyable
335
+ T: ~ Copyable, R: ~ Copyable,
336
+ E: Error
338
337
> (
339
338
of type: T . Type ,
340
339
capacity: Int ,
341
- _ body: ( UnsafeMutableBufferPointer < T > ) throws -> R
342
- ) rethrows -> R {
343
- return try _withUnsafeTemporaryAllocation (
340
+ _ body: ( UnsafeMutableBufferPointer < T > ) throws ( E ) -> R
341
+ ) throws ( E ) -> R {
342
+ let result : Result < R , E > = _withUnsafeTemporaryAllocation (
344
343
of: type,
345
344
capacity: capacity,
346
345
alignment: MemoryLayout< T> . alignment
347
346
) { pointer in
348
- Builtin . bindMemory ( pointer, capacity. _builtinWordValue, type)
349
- let buffer = unsafe UnsafeMutableBufferPointer< T > (
350
- start: . init( pointer) ,
351
- count: capacity
352
- )
353
- return try unsafe body( buffer)
347
+ do throws ( E) {
348
+ Builtin . bindMemory ( pointer, capacity. _builtinWordValue, type)
349
+ let buffer = unsafe UnsafeMutableBufferPointer< T > (
350
+ start: . init( pointer) ,
351
+ count: capacity
352
+ )
353
+ return try unsafe . success ( body ( buffer) )
354
+ } catch {
355
+ return . failure( error)
356
+ }
354
357
}
358
+
359
+ return try result. get ( )
355
360
}
356
361
357
362
/// Provides scoped access to a buffer pointer to memory of the specified type
@@ -361,22 +366,29 @@ public func withUnsafeTemporaryAllocation<
361
366
/// doesn't trigger stack protection for the stack allocated memory.
362
367
@_alwaysEmitIntoClient @_transparent
363
368
public func _withUnprotectedUnsafeTemporaryAllocation<
364
- T: ~ Copyable, R: ~ Copyable
369
+ T: ~ Copyable, R: ~ Copyable,
370
+ E: Error
365
371
> (
366
372
of type: T . Type ,
367
373
capacity: Int ,
368
- _ body: ( UnsafeMutableBufferPointer < T > ) throws -> R
369
- ) rethrows -> R {
370
- return try _withUnprotectedUnsafeTemporaryAllocation (
374
+ _ body: ( UnsafeMutableBufferPointer < T > ) throws ( E ) -> R
375
+ ) throws ( E ) -> R {
376
+ let result : Result < R , E > = _withUnprotectedUnsafeTemporaryAllocation (
371
377
of: type,
372
378
capacity: capacity,
373
379
alignment: MemoryLayout< T> . alignment
374
380
) { pointer in
375
- Builtin . bindMemory ( pointer, capacity. _builtinWordValue, type)
376
- let buffer = unsafe UnsafeMutableBufferPointer< T > (
377
- start: . init( pointer) ,
378
- count: capacity
379
- )
380
- return try unsafe body( buffer)
381
+ do throws ( E) {
382
+ Builtin . bindMemory ( pointer, capacity. _builtinWordValue, type)
383
+ let buffer = unsafe UnsafeMutableBufferPointer< T > (
384
+ start: . init( pointer) ,
385
+ count: capacity
386
+ )
387
+ return try unsafe . success ( body ( buffer) )
388
+ } catch {
389
+ return . failure( error)
390
+ }
381
391
}
392
+
393
+ return try result. get ( )
382
394
}
0 commit comments