Data(bytesNoCopy:count:deallocator)


(Stéphane Lizeray) #1

Hello,

I create an UsafeMutableRawPointer using the allocate method.

Later on I want to create a Data struct from this pointer using the bytesNoCopy initializer. Which deallocator should I pass?

It looks like this:

let retPointer = UnsafeMutableRawPointer.allocate(bytes: size, alignedTo: MemoryLayout<UInt8>.alignment)
….

let d = Data(bytesNoCopy: retPointer, count: size,deallocator:Deallocator.free)

Thanks,

Stéphane


(Andrew Trick) #2

Hello,

I create an UsafeMutableRawPointer using the allocate method.

Later on I want to create a Data struct from this pointer using the bytesNoCopy initializer. Which deallocator should I pass?

It looks like this:

let retPointer = UnsafeMutableRawPointer.allocate(bytes: size, alignedTo: MemoryLayout<UInt8>.alignment)
….

let d = Data(bytesNoCopy: retPointer, count: size,deallocator:Deallocator.free)

Hi Stéphane,

This should work:

let retPointer = UnsafeMutableRawPointer.allocate(bytes: size, alignedTo: MemoryLayout<UInt8>.alignment)

let d = Data(bytesNoCopy: retPointer, count: size, deallocator: .custom({ (ptr, size) in
      ptr.deallocate(bytes: size, alignedTo: 1)
    }))

-Andy

···

On Aug 23, 2016, at 8:40 AM, Stéphane Lizeray via swift-users <swift-users@swift.org> wrote:

Thanks,

Stéphane

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Tony Parker) #3

We should probably add a predefined enum for a deallocator that matches UnsafeMutableRawPointer.allocate. Stéphane or Andy, would you mind filing a JIRA or radar for that?

- Tony

···

On Aug 23, 2016, at 9:24 AM, Andrew Trick via swift-users <swift-users@swift.org> wrote:

On Aug 23, 2016, at 8:40 AM, Stéphane Lizeray via swift-users <swift-users@swift.org> wrote:

Hello,

I create an UsafeMutableRawPointer using the allocate method.

Later on I want to create a Data struct from this pointer using the bytesNoCopy initializer. Which deallocator should I pass?

It looks like this:

let retPointer = UnsafeMutableRawPointer.allocate(bytes: size, alignedTo: MemoryLayout<UInt8>.alignment)
….

let d = Data(bytesNoCopy: retPointer, count: size,deallocator:Deallocator.free)

Hi Stéphane,

This should work:

let retPointer = UnsafeMutableRawPointer.allocate(bytes: size, alignedTo: MemoryLayout<UInt8>.alignment)

let d = Data(bytesNoCopy: retPointer, count: size, deallocator: .custom({ (ptr, size) in
     ptr.deallocate(bytes: size, alignedTo: 1)
   }))

-Andy

Thanks,

Stéphane

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Stéphane Lizeray) #4

Tony, Andrew,

Here it is.

https://bugs.swift.org/browse/SR-2468

On Radar as well.

Thanks,

Stéphane

···

On 24 Aug 2016, at 00:32, Tony Parker <anthony.parker@apple.com> wrote:

We should probably add a predefined enum for a deallocator that matches UnsafeMutableRawPointer.allocate. Stéphane or Andy, would you mind filing a JIRA or radar for that?

- Tony

On Aug 23, 2016, at 9:24 AM, Andrew Trick via swift-users <swift-users@swift.org> wrote:

On Aug 23, 2016, at 8:40 AM, Stéphane Lizeray via swift-users <swift-users@swift.org> wrote:

Hello,

I create an UsafeMutableRawPointer using the allocate method.

Later on I want to create a Data struct from this pointer using the bytesNoCopy initializer. Which deallocator should I pass?

It looks like this:

let retPointer = UnsafeMutableRawPointer.allocate(bytes: size, alignedTo: MemoryLayout<UInt8>.alignment)
….

let d = Data(bytesNoCopy: retPointer, count: size,deallocator:Deallocator.free)

Hi Stéphane,

This should work:

let retPointer = UnsafeMutableRawPointer.allocate(bytes: size, alignedTo: MemoryLayout<UInt8>.alignment)

let d = Data(bytesNoCopy: retPointer, count: size, deallocator: .custom({ (ptr, size) in
    ptr.deallocate(bytes: size, alignedTo: 1)
  }))

-Andy

Thanks,

Stéphane

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users