# Help with some pointer code

**URL:** <https://forums.swift.org/t/help-with-some-pointer-code/35061>\
**Category:** Using Swift\
**Tags:** pointers\
**Created:** [April 3, 2020, 3:12pm UTC](https://forums.swift.org/t/help-with-some-pointer-code/35061 "2020-04-03T15:12:26Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![audulus](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/audulus/32/40637_2.png) [@audulus](https://forums.swift.org/u/audulus)\
**Post date:** [April 3, 2020, 3:12pm UTC](https://forums.swift.org/t/help-with-some-pointer-code/35061/1 "2020-04-03T15:12:26Z")

</div>

I'm trying to write a decompression function. Encountering the struggle.

Error is: `Overlapping accesses to 'block', but modification requires exclusive access; consider copying to a local variable`

```swift
func Stream(blocks: Data, callback: (FineBlockLinear) -> Void) -> Bool {
    
    return blocks.withUnsafeBytes { ptr in
        
        var block = FineBlockLinear()
        
        return withUnsafeMutableBytes(of: &block) { blockPtr in
            
            var stream = compression_stream(dst_ptr: blockPtr.bindMemory(to: UInt8.self).baseAddress!,
                                            dst_size: MemoryLayout<FineBlockLinear>.size,
                                            src_ptr: ptr.bindMemory(to: UInt8.self).baseAddress!,
                                            src_size: blocks.count, state: nil)
            
            if compression_stream_init(&stream, COMPRESSION_STREAM_DECODE, COMPRESSION_LZ4) == COMPRESSION_STATUS_ERROR
            {
                return false
            }
            
            defer {
                compression_stream_destroy(&stream)
            }
            
            var done = false
            while !done {
                
                while stream.dst_size > 0 {
                    
                    if stream.src_size == 0 {
                        return false
                    }
                    
                    let status = compression_stream_process(&stream, Int32(COMPRESSION_STREAM_FINALIZE.rawValue))
                    
                    if status == COMPRESSION_STATUS_END {
                        done = true
                    }
                    
                    if status == COMPRESSION_STATUS_ERROR {
                        return false
                    }
                }
                
                callback(block)
                
            }
            
            return true
            
        }
        
    }
}

```

If I could make the `withUnsafeMutableBytes` smaller, I could avoid having the callback inside it. However I need to initialize `stream` somehow. I suppose I could create a dummy `FineBlockLinear` just to initialize `stream` and then use yet another level of `withUnsafeMutableBytes`.

Can I define a default constructor for `compression_stream`? It seems I need non-null pointers passed in. I suppose I could allocate a `compression_stream` uninitialized?

Maybe there's a totally better way of doing streaming decompression in Swift?

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [April 3, 2020, 3:48pm UTC](https://forums.swift.org/t/help-with-some-pointer-code/35061/2 "2020-04-03T15:48:27Z")

</div>

Is the validity of `stream` tied to `blockPtr`? If not, the inner block can pretty much be used to create `stream`.

```swift
var block = FineBlockLinear()
var stream = withUnsafeMutableByte(of: &block) { blockPtr in
    compression_stream(...)
}

guard compression_stream_init(&stream, COMPRESSION_STREAM_DECODE, COMPRESSION_LZ4) != COMPRESSION_STATUS_ERROR else {
    return false
}

defer { compression_stream_destroy(&stream) }

...

```

To answer the original question, the inner `unsafe` block is writing to `block`, but the closure used is also reading `block` hence the exclusivity violation.

PS

I'm not sure what `FineBlockLinear` looks like, but chances are, you don't want to use `bindMemory`, or at least, you'd want to bind it back to `FineBlockLinear`.

---

<div class="post-metadata">

**Author:** ![audulus](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/audulus/32/40637_2.png) [@audulus](https://forums.swift.org/u/audulus)\
**Post date:** [April 3, 2020, 3:58pm UTC](https://forums.swift.org/t/help-with-some-pointer-code/35061/3 "2020-04-03T15:58:30Z")

</div>

Oof this gets hairy. Gotta change the logic around to accommodate the second `withUnsafeMutableBytes`, since I can't simply return from the whole function:

```swift
func Stream(blocks: Data, callback: (FineBlockLinear) -> Void) -> Bool {
    
    return blocks.withUnsafeBytes { ptr in
        
        var block = FineBlockLinear()
        
        var stream = withUnsafeMutableBytes(of: &block) { blockPtr in
            return compression_stream(dst_ptr: blockPtr.bindMemory(to: UInt8.self).baseAddress!,
            dst_size: MemoryLayout<FineBlockLinear>.size,
            src_ptr: ptr.bindMemory(to: UInt8.self).baseAddress!,
            src_size: blocks.count, state: nil)
        }
        
        if compression_stream_init(&stream, COMPRESSION_STREAM_DECODE, COMPRESSION_LZ4) == COMPRESSION_STATUS_ERROR
        {
            return false
        }
        
        defer {
            compression_stream_destroy(&stream)
        }
        
        var done = false
        while !done {
            
            withUnsafeMutableBytes(of: &block) { blockPtr in
                
                stream.dst_ptr = blockPtr.bindMemory(to: UInt8.self).baseAddress!
                stream.dst_size = MemoryLayout<FineBlockLinear>.size
            
                /*
                while stream.dst_size > 0 {
                    
                    if stream.src_size == 0 {
                        return false
                    }
                    
                    let status = compression_stream_process(&stream, Int32(COMPRESSION_STREAM_FINALIZE.rawValue))
                    
                    if status == COMPRESSION_STATUS_END {
                        done = true
                    }
                    
                    if status == COMPRESSION_STATUS_ERROR {
                        return false
                    }
                }
                */
                
            }
            
            callback(block)
            
        }
        
        return true
        
    }
}

```

Also realized I've gotta reset `dst_ptr` and `dst_size` at the begging of the outer `while`.

FWIW here's the original C function I'm translating, because evidently I'm a masochist.

```swift
bool StreamBlocks(NSData* data, void (^callback)(const FineBlockLinear* block)) {
        
    compression_stream stream;
    if(compression_stream_init(&stream, COMPRESSION_STREAM_DECODE, COMPRESSION_LZ4) == COMPRESSION_STATUS_ERROR) {
        return false;
    }
    
    stream.src_ptr = (uint8_t*) data.bytes;
    stream.src_size = data.length;
    bool done = false;
    
    while(!done) {
        
        FineBlockLinear block;
        
        compression_status status = COMPRESSION_STATUS_END;
        stream.dst_ptr = (uint8_t*) &block;
        stream.dst_size = sizeof(FineBlockLinear);
        
        // We need to fill the buffer.
        while(stream.dst_size > 0) {
            
            // If we've used up all the input, get some more.
            if(stream.src_size == 0) {
                return false;
            }
            
            status = compression_stream_process(&stream, COMPRESSION_STREAM_FINALIZE);
            
            if(status == COMPRESSION_STATUS_END) {
                done = YES;
            }
            
            if(status == COMPRESSION_STATUS_ERROR) {
                return false;
            }
            
        }
        
        // Load the texture block.
        callback(&block);
        
    }
    
    compression_stream_destroy(&stream);
    
    return true;
    
}

```

---

<div class="post-metadata">

**Author:** ![audulus](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/audulus/32/40637_2.png) [@audulus](https://forums.swift.org/u/audulus)\
**Post date:** [April 3, 2020, 4:01pm UTC](https://forums.swift.org/t/help-with-some-pointer-code/35061/4 "2020-04-03T16:01:14Z")

</div>

> [@Lantua](#):
>
> I'm not sure what `FineBlockLinear` looks like, but chances are, you don't want to use `bindMemory` , or at least, you'd want to bind it back to `FineBlockLinear` .

Well I'm just messing around until I get the right type to pass to the C function. What should I do instead of `blockPtr.bindMemory(to: UInt8.self).baseAddress!`?

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [April 3, 2020, 4:06pm UTC](https://forums.swift.org/t/help-with-some-pointer-code/35061/5 "2020-04-03T16:06:06Z")

</div>

Before we dive in deeper, could you elaborate a bit:

- what's the relationship between `compression_stream` return value and `dst_ptr`. Are they the same? Or is `stream` a brand new value that you need to deallocate (and rightfully so at `defer`). Do they point to one another?
- does `compression_stream_process` alter values pointed by `dst_ptr`, which you want to be reflected in `block`.

---

<div class="post-metadata">

**Author:** ![audulus](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/audulus/32/40637_2.png) [@audulus](https://forums.swift.org/u/audulus)\
**Post date:** [April 3, 2020, 4:07pm UTC](https://forums.swift.org/t/help-with-some-pointer-code/35061/6 "2020-04-03T16:07:50Z")

</div>

So `compression_stream` is a C struct. Does that answer your questions?

Yes, `compression_stream_process` writes data out to `dst_ptr`. It also advances `dst_ptr` and reduces `dst_size`.

Thanks for your help, by the way 🙂

---

<div class="post-metadata">

**Author:** ![audulus](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/audulus/32/40637_2.png) [@audulus](https://forums.swift.org/u/audulus)\
**Post date:** [April 3, 2020, 4:24pm UTC](https://forums.swift.org/t/help-with-some-pointer-code/35061/7 "2020-04-03T16:24:53Z")

</div>

Well, throwing exceptions makes the error handling easier:

```swift
enum MyError: Error {
    case compressionError
}

func Stream(blocks: Data, callback: (FineBlockLinear) -> Void) throws {
    
    return try blocks.withUnsafeBytes { ptr in
        
        var block = FineBlockLinear()
        
        var stream = withUnsafeMutableBytes(of: &block) { blockPtr in
            return compression_stream(dst_ptr: blockPtr.bindMemory(to: UInt8.self).baseAddress!,
            dst_size: MemoryLayout<FineBlockLinear>.size,
            src_ptr: ptr.bindMemory(to: UInt8.self).baseAddress!,
            src_size: blocks.count, state: nil)
        }
        
        if compression_stream_init(&stream, COMPRESSION_STREAM_DECODE, COMPRESSION_LZ4) == COMPRESSION_STATUS_ERROR
        {
            throw MyError.compressionError
        }
        
        defer {
            compression_stream_destroy(&stream)
        }
        
        var done = false
        while !done {
            
            try withUnsafeMutableBytes(of: &block) { blockPtr in
                
                stream.dst_ptr = blockPtr.bindMemory(to: UInt8.self).baseAddress!
                stream.dst_size = MemoryLayout<FineBlockLinear>.size
            
                while stream.dst_size > 0 {
                    
                    if stream.src_size == 0 {
                        throw MyError.compressionError
                    }
                    
                    let status = compression_stream_process(&stream, Int32(COMPRESSION_STREAM_FINALIZE.rawValue))
                    
                    if status == COMPRESSION_STATUS_END {
                        done = true
                    }
                    
                    if status == COMPRESSION_STATUS_ERROR {
                        throw MyError.compressionError
                    }
                }
                
            }
            
            callback(block)
            
        }
        
    }
}

```

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [April 3, 2020, 4:58pm UTC](https://forums.swift.org/t/help-with-some-pointer-code/35061/8 "2020-04-03T16:58:55Z")

</div>

It might cause a problem since `stream`, and by extension, `dst_ptr`, is outliving the unsafe block. The first `blockPtr` are not necessarily the same as the second `blockPtr`. I think the cleanest way is to create `block` manually, and pass them in

```swift
func Stream(blocks: Data, callback: (FineBlockLinear) -> Void) -> Bool {
  let block = UnsafeMutableRawPointer.allocate(byteCount: MemoryLayout<FineBlockLinear>.size, alignment: MemoryLayout<FineBlockLinear>.alignment)
  defer { block.deallocate() }

  return blocks.withUnsafeBytes { ptr in
    var stream = compression_stream(dst_ptr: block.assumingMemoryBound(to: UInt8.self),
                                    dst_size: MemoryLayout<FineBlockLinear>.size,
                                    src_ptr: ptr.assumingMemoryBount(to: UInt8.self).baseAddress!,
                                    src_size: blocks.count, state: nil)

    ...
    while ... {
      callback(block.load(as: FineBlockLinear.self))
    }
}

```

Though I'm not sure if this is the right way to have both `UnsafeMutablePointer<UInt8>` in a C struct, and to load `FineBlockLinear` at the same time. I'm also not sure if `assumingMemoryBound` would work here since `UInt8` and `FineBlockLinear` doesn't seem to be layout compatible, though I believe it's more fitting here than `bindMemory`. @Andrew_Trick Is this the right usage surrounding binding?

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [April 3, 2020, 8:37pm UTC](https://forums.swift.org/t/help-with-some-pointer-code/35061/9 "2020-04-03T20:37:19Z")

</div>

@audulus After inspecting your original code more closely, I've got a question.

It seems your while loop is made so that `callback` is called only once, and only when the compression succeed. Is that the case? If so, we might be able to make the unsafe block much cleaner.

---

<div class="post-metadata">

**Author:** ![audulus](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/audulus/32/40637_2.png) [@audulus](https://forums.swift.org/u/audulus)\
**Post date:** [April 4, 2020, 12:03am UTC](https://forums.swift.org/t/help-with-some-pointer-code/35061/10 "2020-04-04T00:03:20Z")

</div>

No, the callback is called many times. Every time `dst_size` gets down to zero (so the block data is filled), the callback is called.

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [April 4, 2020, 5:19am UTC](https://forums.swift.org/t/help-with-some-pointer-code/35061/11 "2020-04-04T05:19:39Z")

</div>

Ok, I see the reset at the beginning of the first while loop in the original c code.

I think it’d be easier to make `block` an array of `UInt8`, then load them as `FineBlockLinear` as needed

```swift
func Stream(blocks: Data, callback: (FineBlockLinear) -> Void) throws {
  let block = UnsafeMutableRawPointer.allocate(byteCount: size, alignment: MemoryLayout<FineBlockLinear>.alignment)
  defer { block.deallocate() }

  let dst_ptr = block.bindMemory(to: UInt8.self)
  let dst_size = MemoryLayout<FineBlockLinear>.size

  let stream = compression_stream(dst_ptr: nil,
                                  dst_size: 0,
                                  src_ptr: nil,
                                  src_size: blocks.count, state: nil)

  guard compression_stream_init(&stream, COMPRESSION_STREAM_DECODE, COMPRESSION_LZ4) != COMPRESSION_STATUS_ERROR else {
    throw MyError.compressionError
  }

  defer { compression_stream_destroy(&stream) }

  try blocks.withUnsafeBytes { ptr in
    stream.src_ptr = ptr.assumingMemoryBound(to: UInt8.self).baseAddress

    var done = false
    while !done {
      stream.dst_ptr = dst_ptr
      stream.dst_size = size

      repeat {
        guard stream.src_size > 0 else {
          throw MyError.compressionError
        }

        let status = compression_stream_process(&stream, COMPRESSION_STREAM_FINALIZE)

        if status == COMPRESSION_STATUS_END {
          done = true
        } else if status == COMPRESSION_STATUS_ERROR {
          throw MyError.compressionError
        }
      } while stream.dst_size > 0

      callback(block.load(as: FineBlockLinear.self))
    }
  }
}

```
