@@ -36,16 +36,10 @@ public struct ByteBuffer {
36
36
case byteBuffer( _InternalByteBuffer )
37
37
case array( [ UInt8 ] )
38
38
case pointer( UnsafeMutableRawPointer )
39
- case writePointer( UnsafeMutableRawPointer )
40
-
41
- var isOwned : Bool {
42
- switch self {
43
- case . writePointer: true
44
- default : false
45
- }
46
- }
47
39
}
48
40
41
+ /// This storage doesn't own the memory, therefore, we won't deallocate on deinit.
42
+ private let isOwned : Bool
49
43
/// Retained blob of data that requires the storage to retain a pointer to.
50
44
@usableFromInline
51
45
var retainedBlob : Blob
@@ -58,18 +52,21 @@ public struct ByteBuffer {
58
52
byteCount: count,
59
53
alignment: MemoryLayout< UInt8> . alignment)
60
54
capacity = count
61
- retainedBlob = . writePointer( memory)
55
+ retainedBlob = . pointer( memory)
56
+ isOwned = true
62
57
}
63
58
64
59
@usableFromInline
65
60
init ( blob: Blob , capacity count: Int ) {
66
61
capacity = count
67
62
retainedBlob = blob
63
+ isOwned = false
68
64
}
69
65
70
66
deinit {
67
+ guard isOwned else { return }
71
68
switch retainedBlob {
72
- case . writePointer ( let unsafeMutableRawPointer) :
69
+ case . pointer ( let unsafeMutableRawPointer) :
73
70
unsafeMutableRawPointer. deallocate ( )
74
71
default : break
75
72
}
@@ -78,7 +75,7 @@ public struct ByteBuffer {
78
75
@usableFromInline
79
76
func copy( from ptr: UnsafeRawPointer , count: Int ) {
80
77
assert (
81
- retainedBlob . isOwned,
78
+ isOwned,
82
79
" copy should NOT be called on a buffer that is built by assumingMemoryBound " )
83
80
withUnsafeRawPointer {
84
81
$0. copyMemory ( from: ptr, byteCount: count)
@@ -88,88 +85,91 @@ public struct ByteBuffer {
88
85
@usableFromInline
89
86
func initialize( for size: Int ) {
90
87
assert (
91
- retainedBlob . isOwned,
88
+ isOwned,
92
89
" initalize should NOT be called on a buffer that is built by assumingMemoryBound " )
93
90
withUnsafeRawPointer {
94
91
memset ( $0, 0 , size)
95
92
}
96
93
}
97
94
95
+ @discardableResult
98
96
@inline ( __always)
99
97
func withUnsafeBytes< T> (
100
98
_ body: ( UnsafeRawBufferPointer ) throws
101
99
-> T ) rethrows -> T
102
100
{
103
101
switch retainedBlob {
104
102
case . byteBuffer( let byteBuffer) :
105
- try byteBuffer. withUnsafeBytes ( body)
103
+ return try byteBuffer. withUnsafeBytes ( body)
104
+ #if !os(WASI)
106
105
case . data( let data) :
107
- try data. withUnsafeBytes ( body)
106
+ return try data. withUnsafeBytes ( body)
108
107
case . bytes( let contiguousBytes) :
109
- try contiguousBytes. withUnsafeBytes ( body)
108
+ return try contiguousBytes. withUnsafeBytes ( body)
109
+ #endif
110
110
case . array( let array) :
111
- try array. withUnsafeBytes ( body)
111
+ return try array. withUnsafeBytes ( body)
112
112
case . pointer( let ptr) :
113
- try body ( UnsafeRawBufferPointer ( start: ptr, count: capacity) )
114
- case . writePointer( let ptr) :
115
- try body ( UnsafeRawBufferPointer ( start: ptr, count: capacity) )
113
+ return try body ( UnsafeRawBufferPointer ( start: ptr, count: capacity) )
116
114
}
117
115
}
118
116
117
+ @discardableResult
119
118
@inline ( __always)
120
119
func withUnsafeRawPointer< T> (
121
120
_ body: ( UnsafeMutableRawPointer ) throws
122
121
-> T ) rethrows -> T
123
122
{
124
123
switch retainedBlob {
125
124
case . byteBuffer( let byteBuffer) :
126
- try byteBuffer. withUnsafeRawPointer ( body)
125
+ return try byteBuffer. withUnsafeRawPointer ( body)
126
+ #if !os(WASI)
127
127
case . data( let data) :
128
- try data
128
+ return try data
129
129
. withUnsafeBytes {
130
130
try body ( UnsafeMutableRawPointer ( mutating: $0. baseAddress!) )
131
131
}
132
132
case . bytes( let contiguousBytes) :
133
- try contiguousBytes
133
+ return try contiguousBytes
134
134
. withUnsafeBytes {
135
135
try body ( UnsafeMutableRawPointer ( mutating: $0. baseAddress!) )
136
136
}
137
+ #endif
137
138
case . array( let array) :
138
- try array
139
+ return try array
139
140
. withUnsafeBytes {
140
141
try body ( UnsafeMutableRawPointer ( mutating: $0. baseAddress!) )
141
142
}
142
143
case . pointer( let ptr) :
143
- try body ( ptr)
144
- case . writePointer( let ptr) :
145
- try body ( ptr)
144
+ return try body ( ptr)
146
145
}
147
146
}
148
147
148
+ @discardableResult
149
149
@inline ( __always)
150
150
func readWithUnsafeRawPointer< T> (
151
151
position: Int ,
152
152
_ body: ( UnsafeRawPointer ) throws -> T ) rethrows -> T
153
153
{
154
154
switch retainedBlob {
155
155
case . byteBuffer( let byteBuffer) :
156
- try byteBuffer. readWithUnsafeRawPointer ( position: position, body)
156
+ return try byteBuffer. readWithUnsafeRawPointer ( position: position, body)
157
+ #if !os(WASI)
157
158
case . data( let data) :
158
- try data. withUnsafeBytes {
159
+ return try data. withUnsafeBytes {
159
160
try body ( $0. baseAddress!. advanced ( by: position) )
160
161
}
161
162
case . bytes( let contiguousBytes) :
162
- try contiguousBytes. withUnsafeBytes {
163
+ return try contiguousBytes. withUnsafeBytes {
163
164
try body ( $0. baseAddress!. advanced ( by: position) )
164
165
}
166
+ #endif
165
167
case . array( let array) :
166
- try array. withUnsafeBytes {
168
+ return try array. withUnsafeBytes {
167
169
try body ( $0. baseAddress!. advanced ( by: position) )
168
170
}
169
171
case . pointer( let ptr) :
170
- try body ( ptr. advanced ( by: position) )
171
- case . writePointer( let ptr) :
172
- try body ( ptr. advanced ( by: position) )
172
+ return try body ( ptr. advanced ( by: position) )
173
173
}
174
174
}
175
175
}
@@ -178,10 +178,8 @@ public struct ByteBuffer {
178
178
179
179
/// The size of the elements written to the buffer + their paddings
180
180
private var _readerIndex : Int = 0
181
- /// Current Index which is being used to write to the buffer, it is written from the end to the start of the buffer
182
- var writerIndex : Int { _storage. capacity &- _readerIndex }
183
181
/// Reader is the position of the current Writer Index (capacity - size)
184
- public var reader : Int { writerIndex }
182
+ public var reader : Int { _storage . capacity &- _readerIndex }
185
183
/// Current size of the buffer
186
184
public var size : UOffset { UOffset ( _readerIndex) }
187
185
/// Current capacity for the buffer
@@ -192,7 +190,9 @@ public struct ByteBuffer {
192
190
/// - bytes: Array of UInt8
193
191
@inline ( __always)
194
192
init ( byteBuffer: _InternalByteBuffer ) {
195
- _storage = Storage ( blob: . byteBuffer( byteBuffer) , capacity: byteBuffer. capacity)
193
+ _storage = Storage (
194
+ blob: . byteBuffer( byteBuffer) ,
195
+ capacity: byteBuffer. capacity)
196
196
_readerIndex = Int ( byteBuffer. size)
197
197
}
198
198
@@ -293,7 +293,7 @@ public struct ByteBuffer {
293
293
}
294
294
assert ( index < _storage. capacity, " Write index is out of writing bound " )
295
295
assert ( index >= 0 , " Writer index should be above zero " )
296
- withUnsafePointer ( to: value) { ptr in
296
+ _ = withUnsafePointer ( to: value) { ptr in
297
297
_storage. withUnsafeRawPointer {
298
298
memcpy (
299
299
$0. advanced ( by: index) ,
@@ -392,7 +392,7 @@ public struct ByteBuffer {
392
392
assert (
393
393
index + count <= _storage. capacity,
394
394
" Reading out of bounds is illegal " )
395
- return _storage. retainedBlob . readWithUnsafeRawPointer ( position: index) {
395
+ return _storage. readWithUnsafeRawPointer ( position: index) {
396
396
String ( cString: $0. bindMemory ( to: UInt8 . self, capacity: count) )
397
397
}
398
398
}
@@ -433,14 +433,22 @@ public struct ByteBuffer {
433
433
}
434
434
435
435
@discardableResult
436
- @usableFromInline
437
436
@inline ( __always)
438
437
func withUnsafeMutableRawPointer< T> (
439
438
body: ( UnsafeMutableRawPointer ) throws
440
439
-> T ) rethrows -> T
441
440
{
442
441
try _storage. withUnsafeRawPointer ( body)
443
442
}
443
+
444
+ @discardableResult
445
+ @inline ( __always)
446
+ func readWithUnsafeRawPointer< T> (
447
+ position: Int ,
448
+ _ body: ( UnsafeRawPointer ) throws -> T ) rethrows -> T
449
+ {
450
+ try _storage. readWithUnsafeRawPointer ( position: position, body)
451
+ }
444
452
}
445
453
446
454
extension ByteBuffer : CustomDebugStringConvertible {
@@ -449,7 +457,8 @@ extension ByteBuffer: CustomDebugStringConvertible {
449
457
"""
450
458
buffer located at: \( _storage. retainedBlob) ,
451
459
with capacity of \( _storage. capacity) ,
452
- { writerSize: \( _readerIndex) , readerSize: \( reader) , writerIndex: \( writerIndex) }
460
+ { writtenSize: \( _readerIndex) , readerSize: \( reader) ,
461
+ size: \( size) }
453
462
"""
454
463
}
455
464
}
0 commit comments