Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass function pointer instead of &parameter #30

Merged
merged 1 commit into from Apr 30, 2017

Conversation

pwaller
Copy link
Contributor

@pwaller pwaller commented Apr 20, 2017

Before this commit, a pointer to the stack was passed as the function
pointer. If you passed nil as a function pointer, for example, a
pointer to the stack was being passed.

This fix is not thorough and I don't actually want to call a CB, so I
have not checked that it works for that use case. I have not dived in to
discover if there is a better way to make this work, I could use a hand
here improving this fix.

I note that there exists a function pointer type (CB below), which has
a PassRef/PassValue methods. Maybe those should be called?

Before:

func F(cb CB) {
	cb, _ := (*[0]byte)(unsafe.Pointer(&cb)), cgoAllocsUnknown
	C.cfunc(cb)
}

After:

func F(cb CB) {
	cb, _ := *(**[0]byte)(unsafe.Pointer(&cb)), cgoAllocsUnknown
	C.cfunc(cb)
}

Before this commit, a pointer to the stack was passed as the function
pointer. If you passed `nil` as a function pointer, for example, a
pointer to the stack was being passed.

This fix is not thorough and I don't actually want to call a CB, so I
have not checked that it works for that use case. I have not dived in to
discover if there is a better way to make this work, I could use a hand
here improving this fix.

I note that there exists a function pointer type (`CB` below), which has
a `PassRef`/`PassValue` methods. Maybe those should be called?

Before:

```
func F(cb CB) {
	cb, _ := (*[0]byte)(unsafe.Pointer(&cb)), cgoAllocsUnknown
	C.cfunc(cb)
}
```

After:

```
func F(cb CB) {
	cb, _ := *(**[0]byte)(unsafe.Pointer(&cb)), cgoAllocsUnknown
	C.cfunc(cb)
}
```
@pwaller
Copy link
Contributor Author

pwaller commented Apr 20, 2017

It is quite possible this fixes my use case and breaks other use cases, so I'm quite happy if this PR is not merged. I'm not certain if this functionality is in use anywhere - I haven't got time to look very hard at the minute.

@pwaller
Copy link
Contributor Author

pwaller commented Apr 30, 2017

Ping.

@xlab xlab merged commit 7bc5c23 into xlab:master Apr 30, 2017
@xlab
Copy link
Owner

xlab commented Apr 30, 2017

@pwaller
I have no ideas at this moment. But if this fix is useful for your cases, it's a win already.

@pwaller pwaller deleted the fix-function-pointer branch May 1, 2017 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants