-
Notifications
You must be signed in to change notification settings - Fork 323
[WIP/RFC] Pointer indirection #196
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7750e85
Introduce an indirection layer for pointers
carlosmn bde012f
handles: correctly initialize all members
pks-t be3a626
tree: use HandleList for C function callbacks.
pks-t de45a4b
submodule: use HandleList for C function callbacks
pks-t 0a336e4
handles: start slot indices with 1
pks-t 9bbec34
index: use HandleList for C function callbacks.
pks-t e919653
odb: use HandleList for C function callbacks.
pks-t 7ee534d
handles: print pointer handle on panic.
pks-t fe902f5
diff: use HandleList for C function callbacks.
pks-t 83f9e6a
blob: use HandleList for C function callbacks.
pks-t a843b72
packbuilder: use HandleList for C function callbacks.
pks-t d95932c
handles: panic when we cannot retrieve handle data
pks-t 1bd338a
handles: do not store handles by uintptr
pks-t c43afaf
tree: use correct C callback signature
pks-t e8531dd
diff: only untrack notify payload when it is set
pks-t 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,84 @@ | ||
package git | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"unsafe" | ||
) | ||
|
||
type HandleList struct { | ||
sync.RWMutex | ||
// stores the Go pointers | ||
handles []interface{} | ||
// indicates which indices are in use | ||
set map[int]bool | ||
} | ||
|
||
func NewHandleList() *HandleList { | ||
return &HandleList{ | ||
handles: make([]interface{}, 5), | ||
set: make(map[int]bool), | ||
} | ||
} | ||
|
||
// findUnusedSlot finds the smallest-index empty space in our | ||
// list. You must only run this function while holding a write lock. | ||
func (v *HandleList) findUnusedSlot() int { | ||
for i := 1; i < len(v.handles); i++ { | ||
isUsed := v.set[i] | ||
if !isUsed { | ||
return i | ||
} | ||
} | ||
|
||
// reaching here means we've run out of entries so append and | ||
// return the new index, which is equal to the old length. | ||
slot := len(v.handles) | ||
v.handles = append(v.handles, nil) | ||
|
||
return slot | ||
} | ||
|
||
// Track adds the given pointer to the list of pointers to track and | ||
// returns a pointer value which can be passed to C as an opaque | ||
// pointer. | ||
func (v *HandleList) Track(pointer interface{}) unsafe.Pointer { | ||
v.Lock() | ||
|
||
slot := v.findUnusedSlot() | ||
v.handles[slot] = pointer | ||
v.set[slot] = true | ||
|
||
v.Unlock() | ||
|
||
return unsafe.Pointer(&slot) | ||
} | ||
|
||
// Untrack stops tracking the pointer given by the handle | ||
func (v *HandleList) Untrack(handle unsafe.Pointer) { | ||
slot := *(*int)(handle) | ||
|
||
v.Lock() | ||
|
||
v.handles[slot] = nil | ||
delete(v.set, slot) | ||
|
||
v.Unlock() | ||
} | ||
|
||
// Get retrieves the pointer from the given handle | ||
func (v *HandleList) Get(handle unsafe.Pointer) interface{} { | ||
slot := *(*int)(handle) | ||
|
||
v.RLock() | ||
|
||
if _, ok := v.set[slot]; !ok { | ||
panic(fmt.Sprintf("invalid pointer handle: %p", handle)) | ||
} | ||
|
||
ptr := v.handles[slot] | ||
|
||
v.RUnlock() | ||
|
||
return ptr | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
The payload wasn't optional before, it shouldn't be now.
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.
Well, if you take a look at index_test.go:TestIndexAddAllNoCallback it seems as if the callback was optional after all.
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.
It is optional to pass, but we only tell libgit2 to call
indexMatchedPathCallback
if the caller did pass in the callback. This function only gets called if there is a callback.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.
Okay, agreed and fixed.