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

Fix/safer c pointer store #178

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 44 additions & 9 deletions longtaillib/longtaillib.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"fmt"
"os"
"reflect"
"sync/atomic"
"sync"
"unsafe"

"github.com/pkg/errors"
Expand Down Expand Up @@ -362,22 +362,28 @@ var pointerIndex uint32
var pointerStore [1024]interface{}
var pointerIndexer = (*[1 << 30]C.uint32_t)(C.malloc(4 * 1024))

var refPointerSync sync.Mutex

func SavePointer(v interface{}) unsafe.Pointer {
if v == nil {
return nil
}
refPointerSync.Lock()
defer refPointerSync.Unlock()

newPointerIndex := (atomic.AddUint32(&pointerIndex, 1)) % 1024
startPointerIndex := newPointerIndex
for pointerStore[newPointerIndex] != nil {
newPointerIndex = (atomic.AddUint32(&pointerIndex, 1)) % 1024
if newPointerIndex == startPointerIndex {
startPointerIndex := pointerIndex
for pointerStore[pointerIndex] != nil {
pointerIndex++
if pointerIndex == 1024 {
pointerIndex = 0
}
if pointerIndex == startPointerIndex {
return nil
}
}
pointerIndexer[newPointerIndex] = C.uint32_t(newPointerIndex)
pointerStore[newPointerIndex] = v
return unsafe.Pointer(&pointerIndexer[newPointerIndex])
pointerIndexer[pointerIndex] = C.uint32_t(pointerIndex)
pointerStore[pointerIndex] = v
return unsafe.Pointer(&pointerIndexer[pointerIndex])
}

func RestorePointer(ptr unsafe.Pointer) (v interface{}) {
Expand All @@ -395,6 +401,8 @@ func UnrefPointer(ptr unsafe.Pointer) {
if ptr == nil {
return
}
refPointerSync.Lock()
defer refPointerSync.Unlock()

p := (*C.uint32_t)(ptr)
index := uint32(*p)
Expand Down Expand Up @@ -1785,6 +1793,9 @@ func ReadStoreIndexFromBuffer(buffer []byte) (Longtail_StoreIndex, error) {
// CreateProgressAPI ...
func CreateProgressAPI(progress ProgressAPI) Longtail_ProgressAPI {
cContext := SavePointer(progress)
if cContext == nil {
return Longtail_ProgressAPI{}
}
progressAPIProxy := C.CreateProgressProxyAPI(cContext)
return Longtail_ProgressAPI{cProgressAPI: progressAPIProxy}
}
Expand All @@ -1809,6 +1820,9 @@ func ProgressAPIProxy_Dispose(api *C.struct_Longtail_API) {
// CreatePathFilterAPI ...
func CreatePathFilterAPI(pathFilter PathFilterAPI) Longtail_PathFilterAPI {
cContext := SavePointer(pathFilter)
if cContext == nil {
return Longtail_PathFilterAPI{}
}
pathFilterAPIProxy := C.CreatePathFilterProxyAPI(cContext)
return Longtail_PathFilterAPI{cPathFilterAPI: pathFilterAPIProxy}
}
Expand Down Expand Up @@ -1839,6 +1853,9 @@ func PathFilterAPIProxy_Dispose(api *C.struct_Longtail_API) {
// CreateAsyncPutStoredBlockAPI ...
func CreateAsyncPutStoredBlockAPI(asyncComplete AsyncPutStoredBlockAPI) Longtail_AsyncPutStoredBlockAPI {
cContext := SavePointer(asyncComplete)
if cContext == nil {
return Longtail_AsyncPutStoredBlockAPI{}
}
asyncCompleteAPIProxy := C.CreateAsyncPutStoredBlockAPI(cContext)
return Longtail_AsyncPutStoredBlockAPI{cAsyncCompleteAPI: asyncCompleteAPIProxy}
}
Expand All @@ -1865,6 +1882,9 @@ func AsyncPutStoredBlockAPIProxy_Dispose(api *C.struct_Longtail_API) {
// CreateAsyncGetStoredBlockAPI ...
func CreateAsyncGetStoredBlockAPI(asyncComplete AsyncGetStoredBlockAPI) Longtail_AsyncGetStoredBlockAPI {
cContext := SavePointer(asyncComplete)
if cContext == nil {
return Longtail_AsyncGetStoredBlockAPI{}
}
asyncCompleteAPIProxy := C.CreateAsyncGetStoredBlockAPI(cContext)
return Longtail_AsyncGetStoredBlockAPI{cAsyncCompleteAPI: asyncCompleteAPIProxy}
}
Expand All @@ -1891,6 +1911,9 @@ func AsyncGetStoredBlockAPIProxy_Dispose(api *C.struct_Longtail_API) {
// CreateAsyncGetExistingContentAPI ...
func CreateAsyncGetExistingContentAPI(asyncComplete AsyncGetExistingContentAPI) Longtail_AsyncGetExistingContentAPI {
cContext := SavePointer(asyncComplete)
if cContext == nil {
return Longtail_AsyncGetExistingContentAPI{}
}
asyncCompleteAPIProxy := C.CreateAsyncGetExistingContentAPI(cContext)
return Longtail_AsyncGetExistingContentAPI{cAsyncCompleteAPI: asyncCompleteAPIProxy}
}
Expand All @@ -1917,6 +1940,9 @@ func AsyncGetExistingContentAPIProxy_Dispose(api *C.struct_Longtail_API) {
// CreateAsyncPruneBlocksAPI ...
func CreateAsyncPruneBlocksAPI(asyncComplete AsyncPruneBlocksAPI) Longtail_AsyncPruneBlocksAPI {
cContext := SavePointer(asyncComplete)
if cContext == nil {
return Longtail_AsyncPruneBlocksAPI{}
}
asyncCompleteAPIProxy := C.CreateAsyncPruneBlocksAPI(cContext)
return Longtail_AsyncPruneBlocksAPI{cAsyncCompleteAPI: asyncCompleteAPIProxy}
}
Expand All @@ -1943,6 +1969,9 @@ func AsyncPruneBlocksAPIProxy_Dispose(api *C.struct_Longtail_API) {
// CreateAsyncFlushAPI ...
func CreateAsyncFlushAPI(asyncComplete AsyncFlushAPI) Longtail_AsyncFlushAPI {
cContext := SavePointer(asyncComplete)
if cContext == nil {
return Longtail_AsyncFlushAPI{}
}
asyncCompleteAPIProxy := C.CreateAsyncFlushAPI(cContext)
return Longtail_AsyncFlushAPI{cAsyncCompleteAPI: asyncCompleteAPIProxy}
}
Expand Down Expand Up @@ -2267,6 +2296,9 @@ func BlockStoreAPIProxy_Flush(api *C.struct_Longtail_BlockStoreAPI, async_comple

func CreateBlockStoreAPI(blockStore BlockStoreAPI) Longtail_BlockStoreAPI {
cContext := SavePointer(blockStore)
if cContext == nil {
return Longtail_BlockStoreAPI{}
}
blockStoreAPIProxy := C.CreateBlockStoreProxyAPI(cContext)
return Longtail_BlockStoreAPI{cBlockStoreAPI: blockStoreAPIProxy}
}
Expand All @@ -2281,6 +2313,9 @@ func getLoggerFunc(logger Logger) C.Longtail_Log {
//SetLogger ...
func SetLogger(logger Logger) {
cLoggerContext := SavePointer(logger)
if cLoggerContext == nil {
return
}
C.Longtail_SetLog(getLoggerFunc(logger), cLoggerContext)
}

Expand Down