unsafe-pointers

Create a copy of CMSampleBuffer in Swift 2.0

☆樱花仙子☆ 提交于 2019-12-06 03:49:26
问题 This has been asked before, but something must have changed in Swift since it was asked. I am trying to store CMSampleBuffer objects returned from an AVCaptureSession to be processed later. After some experimentation I discovered that AVCaptureSession must be reusing its CMSampleBuffer references. When I try to keep more than 15 the session hangs. So I thought I would make copies of the sample buffers. But I can't seem to get it to work. Here is what I have written: var allocator: Unmanaged

Unsafe Pointer iteration and Bitmap - why is UInt64 faster?

心已入冬 提交于 2019-12-06 03:35:05
问题 I have been doing some unsafe bitmap operations and have found out that increasing the pointer less times can lead to some big performance improvements. I am not sure why is that so, even though you do lot more bitwise operations in the loop, it still is better to do less iterations on the pointer. So for example instead of iterating over 32 bit pixels with a UInt32 iterate over two pixels with UInt64 and do twice the operations in one cycle. The following does it by reading two pixels and

A swiftier way to convert String to UnsafePointer<xmlChar> in Swift 3 (libxml2)

早过忘川 提交于 2019-12-06 00:13:51
问题 I'm working on a Swift 3 wrapper for the libxml2 C-library. There are two convenience methods to convert String to UnsafePointer<xmlChar> and vice versa. In libxml2 xmlChar is declared as unsigned char . UnsafePointer<xmlChar> to String is uncomplicated func stringFrom(xmlchar: UnsafePointer<xmlChar>) -> String { let string = xmlchar.withMemoryRebound(to: CChar.self, capacity: 1) { return String(validatingUTF8: $0) } return string ?? "" } For String to UnsafePointer<xmlChar> I tried many

Go: convert unsafe.Pointer to function pointer and vice versa

荒凉一梦 提交于 2019-12-06 00:07:18
In C you can put function pointers into an array of void pointers and convert them back to function pointers of any type: extern int (*fn1)(void); extern void (*fn2)(int); void foo(void) { void *array[2]; int i; /* implicit cast from function pointer to void pointer */ array[0] = fn1; array[1] = fn2; for (i = 0; i < 2; i++) { int (*fp)(int, int, int); /* implicit cast from void pointer to function pointer */ fp = array[i]; /* call function with a different signature */ fp(1, 2, 3); } } I need to do the same in Go, using unsafe.Pointers. The questions are: Can a Go function pointer be converted

Getting pointer for first entry in an array

这一生的挚爱 提交于 2019-12-05 01:44:47
I want to get pointer of first entry in the array. This is how I tried int[] Results = { 1, 2, 3, 4, 5 }; unsafe { int* FirstResult = Results[0]; } Get following compilation error. Any ideas how to fix it? You can only take the address of an unfixed expression inside of a fixed statement initializer The error codes are magic to get the answer - search for error code (CS0212 in your case) and you get explanation with proposed fix in a lot of case. Search: http://www.bing.com/search?q=CS0212+msdn Result: http://msdn.microsoft.com/en-us/library/29ak9b70%28v=vs.90%29.aspx Code from the page:

Unsafe Pointer iteration and Bitmap - why is UInt64 faster?

倖福魔咒の 提交于 2019-12-04 10:11:46
I have been doing some unsafe bitmap operations and have found out that increasing the pointer less times can lead to some big performance improvements. I am not sure why is that so, even though you do lot more bitwise operations in the loop, it still is better to do less iterations on the pointer. So for example instead of iterating over 32 bit pixels with a UInt32 iterate over two pixels with UInt64 and do twice the operations in one cycle. The following does it by reading two pixels and modifying them (of course it will fail with images with odd width, but its just for testing). private

Create a copy of CMSampleBuffer in Swift 2.0

微笑、不失礼 提交于 2019-12-04 08:42:59
This has been asked before, but something must have changed in Swift since it was asked. I am trying to store CMSampleBuffer objects returned from an AVCaptureSession to be processed later. After some experimentation I discovered that AVCaptureSession must be reusing its CMSampleBuffer references. When I try to keep more than 15 the session hangs. So I thought I would make copies of the sample buffers. But I can't seem to get it to work. Here is what I have written: var allocator: Unmanaged<CFAllocator>! = CFAllocatorGetDefault() var bufferCopy: UnsafeMutablePointer<CMSampleBuffer?> let err =

If a function returns an UnsafeMutablePointer is it our responsibility to destroy and dealloc?

耗尽温柔 提交于 2019-12-03 15:41:27
问题 For example if I were to write this code: var t = time_t() time(&t) let x = localtime(&t) // returns UnsafeMutablePointer<tm> println("\(x.memory.tm_hour): \(x.memory.tm_min): \(x.memory.tm_sec)") ...would it also be necessary to also do the following? x.destroy() x.dealloc(1) Or did we not allocate the memory and so therefore don't need to dismiss it? Update #1: If we imagine a function that returns an UnsafeMutablePointer : func point() -> UnsafeMutablePointer<String> { let a =

UnsafePointer<UInt8> initializer in Swift 3

╄→гoц情女王★ 提交于 2019-12-03 05:39:58
I have a receipt validation class that is deprecated since Swift 3 has released. I fixed some issues, but I still have many ... Here is the GitHub source code I used : https://gist.github.com/baileysh9/4386ea92b047d97c7285#file-parsing_productids-swift and https://gist.github.com/baileysh9/eddcba49d544635b3cf5 First Error : var p = UnsafePointer<UInt8>(data.bytes) Compiler throws : Cannot invoke initializer for type UnsafePointer(UInt8) with an argument list of type UnsafeRawPointer Second error while (ptr < end) Binary operators < cannot be applied to two UnsafePointer(UInt8) operands Thank

Swift: gettimeofday and Unsafe Pointers

流过昼夜 提交于 2019-12-02 05:51:43
问题 The code in Swift ... var time:timeval? gettimeofday(UnsafePointer<timeval>, UnsafePointer<()>) // this is the method expansion before filling in any data ... The code in Objective C ... struct timeval time; gettimeofday(&time, NULL); ... I have been trying to find more information on UnsafePointer and alternatives to passing NULL, but I may be barking up the wrong tree. If anyone knows how to get the equivilant code working in Swift, that would be great. If there is a good explanation of