[swift-users] Data(bytesNoCopy:count:deallocator)
Andrew Trick
atrick at apple.com
Tue Aug 23 11:24:31 CDT 2016
> On Aug 23, 2016, at 8:40 AM, Stéphane Lizeray via swift-users <swift-users at 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 at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
More information about the swift-users
mailing list