@@ -219,10 +219,7 @@ public struct OmFileReader2Array<Backend: OmFileReaderBackend, OmType: OmFileArr
219
219
guard error == ERROR_OK else {
220
220
throw OmFileFormatSwiftError . omDecoder ( error: String ( cString: om_error_string ( error) ) )
221
221
}
222
- let chunkBufferSize = om_decoder_read_buffer_size ( & decoder)
223
- let chunkBuffer = UnsafeMutableRawBufferPointer . allocate ( byteCount: Int ( chunkBufferSize) , alignment: 1 )
224
- try fn. decode ( decoder: & decoder, into: into, chunkBuffer: chunkBuffer. baseAddress!)
225
- chunkBuffer. deallocate ( )
222
+ try fn. decode ( decoder: & decoder, into: into)
226
223
} )
227
224
} )
228
225
} )
@@ -267,30 +264,34 @@ public struct OmFileReader2Array<Backend: OmFileReaderBackend, OmType: OmFileArr
267
264
268
265
extension OmFileReaderBackend {
269
266
/// Read and decode
270
- func decode( decoder: UnsafePointer < OmDecoder_t > , into: UnsafeMutableRawPointer , chunkBuffer : UnsafeMutableRawPointer ) throws {
267
+ func decode( decoder: UnsafePointer < OmDecoder_t > , into: UnsafeMutableRawPointer ) throws {
271
268
var indexRead = OmDecoder_indexRead_t ( )
272
269
om_decoder_init_index_read ( decoder, & indexRead)
273
270
274
- /// Loop over index blocks and read index data
275
- while om_decoder_next_index_read ( decoder, & indexRead) {
276
- //print("Read index \(indexRead)")
277
- let indexData = self . getData ( offset: Int ( indexRead. offset) , count: Int ( indexRead. count) )
278
-
279
- var dataRead = OmDecoder_dataRead_t ( )
280
- om_decoder_init_data_read ( & dataRead, & indexRead)
281
-
282
- var error : OmError_t = ERROR_OK
283
- /// Loop over data blocks and read compressed data chunks
284
- while om_decoder_next_data_read ( decoder, & dataRead, indexData, indexRead. count, & error) {
285
- //print("Read data \(dataRead) for chunk index \(dataRead.chunkIndex)")
286
- let dataData = self . getData ( offset: Int ( dataRead. offset) , count: Int ( dataRead. count) )
287
- guard om_decoder_decode_chunks ( decoder, dataRead. chunkIndex, dataData, dataRead. count, into, chunkBuffer, & error) else {
271
+ /// The size to decode a single chunk
272
+ let bufferSize = om_decoder_read_buffer_size ( decoder)
273
+ try withUnsafeTemporaryAllocation ( byteCount: Int ( bufferSize) , alignment: 1 ) { buffer in
274
+ /// Loop over index blocks and read index data
275
+ while om_decoder_next_index_read ( decoder, & indexRead) {
276
+ //print("Read index \(indexRead)")
277
+ let indexData = self . getData ( offset: Int ( indexRead. offset) , count: Int ( indexRead. count) )
278
+
279
+ var dataRead = OmDecoder_dataRead_t ( )
280
+ om_decoder_init_data_read ( & dataRead, & indexRead)
281
+
282
+ var error : OmError_t = ERROR_OK
283
+ /// Loop over data blocks and read compressed data chunks
284
+ while om_decoder_next_data_read ( decoder, & dataRead, indexData, indexRead. count, & error) {
285
+ //print("Read data \(dataRead) for chunk index \(dataRead.chunkIndex)")
286
+ let dataData = self . getData ( offset: Int ( dataRead. offset) , count: Int ( dataRead. count) )
287
+ guard om_decoder_decode_chunks ( decoder, dataRead. chunkIndex, dataData, dataRead. count, into, buffer. baseAddress, & error) else {
288
+ throw OmFileFormatSwiftError . omDecoder ( error: String ( cString: om_error_string ( error) ) )
289
+ }
290
+ }
291
+ guard error == ERROR_OK else {
288
292
throw OmFileFormatSwiftError . omDecoder ( error: String ( cString: om_error_string ( error) ) )
289
293
}
290
294
}
291
- guard error == ERROR_OK else {
292
- throw OmFileFormatSwiftError . omDecoder ( error: String ( cString: om_error_string ( error) ) )
293
- }
294
295
}
295
296
}
296
297
0 commit comments