-
Notifications
You must be signed in to change notification settings - Fork 646
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
Fix incorrect unsafe usage #220
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f9d3ff6
Fix incorrect unsafe usage
jrick f0005d4
Comment the byte slice conversion
jrick e04f391
go fmt
jrick 81f2578
Shorten max array lengths for the data type
jrick 9034717
Try to use reflect.SliceHeader correctly this time
jrick 044f3bd
Test many DBs used concurrently
jrick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package bbolt | ||
|
||
import "unsafe" | ||
|
||
func unsafeAdd(base unsafe.Pointer, offset uintptr) unsafe.Pointer { | ||
return unsafe.Pointer(uintptr(base) + offset) | ||
} | ||
|
||
func unsafeIndex(base unsafe.Pointer, offset uintptr, elemsz uintptr, n int) unsafe.Pointer { | ||
return unsafe.Pointer(uintptr(base) + offset + uintptr(n)*elemsz) | ||
} | ||
|
||
func unsafeByteSlice(base unsafe.Pointer, offset uintptr, i, j int) []byte { | ||
// See: https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices | ||
// | ||
// This memory is not allocated from C, but it is unmanaged by Go's | ||
// garbage collector and should behave similarly, and the compiler | ||
// should produce similar code. Note that this conversion allows a | ||
// subslice to begin after the base address, with an optional offset, | ||
// while the URL above does not cover this case and only slices from | ||
// index 0. However, the wiki never says that the address must be to | ||
// the beginning of a C allocation (or even that malloc was used at | ||
// all), so this is believed to be correct. | ||
return (*[maxAllocSize]byte)(unsafeAdd(base, offset))[i:j:j] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to other reviewers:
The specific wiki page that permits this is: https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices
These aren't actually C arrays, but are arrays produced by mmap outside the heap so they can be treated as such.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had meant to comment this exact url here and forgot. Will update if desired.
One difference between here and that wiki is that this conversion allows the subslice to begin in the middle of the allocation instead of always at the start, or to subslice from the start of some address after adding an offset. I don't know if either of these is a concern, but I suspect not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome work! I think including the link (and a small description) as a comment would be extremely helpful
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Build is failing on
make fmt
because of the blank comment line from this btw.