1
0
mirror of https://github.com/libp2p/go-openssl.git synced 2025-04-25 17:50:23 +08:00

fixes for go1.6

Change-Id: I35a41afbdfe2016f7b0967087deb7b418dca2bb2
This commit is contained in:
Jeff Wendling 2016-01-28 13:41:57 -07:00
parent 71f9da2a48
commit fb0c387e74
3 changed files with 76 additions and 9 deletions

16
bio.go
View File

@ -99,6 +99,8 @@ func cbioNew(b *C.BIO) C.int {
return 1
}
var writeBioMapping = newMapping()
type writeBio struct {
data_mtx sync.Mutex
op_mtx sync.Mutex
@ -107,7 +109,7 @@ type writeBio struct {
}
func loadWritePtr(b *C.BIO) *writeBio {
return (*writeBio)(unsafe.Pointer(b.ptr))
return (*writeBio)(writeBioMapping.Get(token(b.ptr)))
}
func bioClearRetryFlags(b *C.BIO) {
@ -195,16 +197,20 @@ func (b *writeBio) WriteTo(w io.Writer) (rv int64, err error) {
func (self *writeBio) Disconnect(b *C.BIO) {
if loadWritePtr(b) == self {
writeBioMapping.Del(token(b.ptr))
b.ptr = nil
}
}
func (b *writeBio) MakeCBIO() *C.BIO {
rv := C.BIO_new(C.BIO_s_writeBio())
rv.ptr = unsafe.Pointer(b)
token := writeBioMapping.Add(unsafe.Pointer(b))
rv.ptr = unsafe.Pointer(token)
return rv
}
var readBioMapping = newMapping()
type readBio struct {
data_mtx sync.Mutex
op_mtx sync.Mutex
@ -214,7 +220,7 @@ type readBio struct {
}
func loadReadPtr(b *C.BIO) *readBio {
return (*readBio)(unsafe.Pointer(b.ptr))
return (*readBio)(readBioMapping.Get(token(b.ptr)))
}
//export readBioRead
@ -311,12 +317,14 @@ func (b *readBio) ReadFromOnce(r io.Reader) (n int, err error) {
func (b *readBio) MakeCBIO() *C.BIO {
rv := C.BIO_new(C.BIO_s_readBio())
rv.ptr = unsafe.Pointer(b)
token := readBioMapping.Add(unsafe.Pointer(b))
rv.ptr = unsafe.Pointer(token)
return rv
}
func (self *readBio) Disconnect(b *C.BIO) {
if loadReadPtr(b) == self {
readBioMapping.Del(token(b.ptr))
b.ptr = nil
}
}

View File

@ -118,11 +118,6 @@ import (
"errors"
"fmt"
"strings"
"sync"
)
var (
sslMutexes []sync.Mutex
)
func init() {

64
mapping.go Normal file
View File

@ -0,0 +1,64 @@
// Copyright (C) 2014 Space Monkey, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build cgo
package openssl
import (
"sync"
"unsafe"
)
// #include <stdlib.h>
import "C"
type mapping struct {
lock sync.Mutex
values map[token]unsafe.Pointer
}
func newMapping() *mapping {
return &mapping{
values: make(map[token]unsafe.Pointer),
}
}
type token unsafe.Pointer
func (m *mapping) Add(x unsafe.Pointer) token {
res := token(C.malloc(1))
m.lock.Lock()
m.values[res] = x
m.lock.Unlock()
return res
}
func (m *mapping) Get(x token) unsafe.Pointer {
m.lock.Lock()
res := m.values[x]
m.lock.Unlock()
return res
}
func (m *mapping) Del(x token) {
m.lock.Lock()
delete(m.values, x)
m.lock.Unlock()
C.free(unsafe.Pointer(x))
}