[swift-users] How to get UnsafePointer or UnsafeMutablePointer from ContiguousArray?
Sergey Kuratov
ksubox.swift at gmail.com
Thu Feb 11 03:10:19 CST 2016
I try to find _fixLifetime in standard library and finally can't :( But
function exists in reality and I can call it.
I also found some discussion about
https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151228/005017.html
So as I can understand it's not fixed API to manage scoped life and
probably will be changed in the future.
On Thu, Feb 11, 2016 at 5:38 PM, Dmitri Gribenko <gribozavr at gmail.com>
wrote:
> On Thu, Feb 11, 2016 at 12:19 AM, Dmitri Gribenko <gribozavr at gmail.com>
> wrote:
> > On Thu, Feb 11, 2016 at 12:13 AM, Sergey Kuratov <ksubox.swift at gmail.com>
> wrote:
> >>>> Can I define similar for my Swift classes ?
> >>
> >>>No. What would be your use case?
> >>
> >> I also could have my Swift object with several members, but when send to
> >> native part would like to send address exactly one of members.
> >> Something like this:
> >>
> >> // Swift part
> >>
> >> class ABlock {
> >>
> >> var channels = 2
> >>
> >> var samplerate = 44100
> >>
> >> var buffer : UnsafeMutablePointer<Int16>
> >>
> >> init() {
> >>
> >> buffer = UnsafeMutablePointer<Int16>.alloc(2048)
> >>
> >> }
> >>
> >> deinit {
> >>
> >> buffer.dealloc(2048)
> >>
> >> }
> >>
> >> }
> >>
> >>
> >> var block = ABlock
> >> generate(block) <- here I would like to send address of ABlock.buffer to
> >> native part
> >>
> >> // C part
> >> void generate(int16_t *buf) {
> >> ...
> >> }
> >
> > I'd recommend to write a Swift wrapper for your C code that accepts
> > the class instance, and calls the generate() function with appropriate
> > arguments.
>
> Oh, and since your class stores an unsafe pointer (and not an array),
> you'd need to call _fixLifetime() on it after the your C function
> returns, to ensure that the class instance can't be deallocated before
> then. Unless you have other requirements though, I'd recommend just
> storing an array though. Then you wouldn't need to care about
> _fixLifetime().
>
> Dmitri
>
> --
> main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
> (j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160211/bcdc230a/attachment.html>
More information about the swift-users
mailing list