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

Add Strs helper for Go 1.6 cgo pointer passing rules. #69

Merged
merged 2 commits into from
Feb 20, 2016
Merged

Conversation

dmitshur
Copy link
Member

This new helper creates C strings in the C heap using malloc, unlike
the normal Str helper that uses Go memory.

It's possible to pass it to cgo, for example, for gl.ShaderSource. This
is intended usage:

csources, free := gl.Strs(source) // source string ends with "\x00"
gl.ShaderSource(shader, 1, csources, nil)
free()

Or for multiple parameters:

csources, free := gl.Strs(sources...) // all strings end with "\x00"
gl.ShaderSource(shader, len(sources), csources, nil)
free()

It returns a free func that must be called once the string is no longer
needed to free memory.

Pair programmed implementation of Strs with @slimsag.

Fixes #66. Needed for go-gl/gl#31.

Change Str helper to panic on invalid input rather than os.Exit. A
panic will help identify where the problem is during development, and
in general, libraries should avoid os.Exit in favor of panic.

Use plain panic, since log.Panicln results in panic text printed twice,
which is unhelpful. It's also less imports.

Remove //glow:rmspace to preserve whitespace. It's not needed in this
small hand-written .go file.
This new helper creates C strings in the C heap using malloc, unlike
the normal Str helper that uses Go memory.

It's possible to pass it to cgo, for example, for gl.ShaderSource. This
is intended usage:

	csources, free := gl.Strs(source) // source string ends with "\x00"
	gl.ShaderSource(shader, 1, csources, nil)
	free()

Or for multiple parameters:

	csources, free := gl.Strs(sources...) // all strings end with "\x00"
	gl.ShaderSource(shader, len(sources), csources, nil)
	free()

It returns a free func that must be called once the string is no longer
needed to free memory.

Pair programmed implementation of Strs with @slimsag.

Fixes #66. Needed for go-gl/gl#31.
@emidoots
Copy link
Member

LGTM

dmitshur added a commit to go-gl/gl that referenced this pull request Feb 20, 2016
This new helper creates C strings in the C heap using malloc, unlike
the normal Str helper that uses Go memory.

It's possible to pass it to cgo, for example, for gl.ShaderSource. This
is intended usage:

	csources, free := gl.Strs(source) // source string ends with "\x00"
	gl.ShaderSource(shader, 1, csources, nil)
	free()

Or for multiple parameters:

	csources, free := gl.Strs(sources...) // all strings end with "\x00"
	gl.ShaderSource(shader, len(sources), csources, nil)
	free()

It returns a free func that must be called once the string is no longer
needed to free memory.

Pair programmed implementation of Strs with @slimsag.

Fixes #31.

Generated using changes in go-gl/glow#69.
@dmitshur dmitshur merged commit a2dd670 into master Feb 20, 2016
@dmitshur dmitshur deleted the Strs-go1.6 branch February 20, 2016 07:53
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.

2 participants