Skip to content
Merged
Show file tree
Hide file tree
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
43 changes: 36 additions & 7 deletions fakelibusb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,35 @@ func (f *fakeLibusb) getDevice(handle *libusbDevHandle) *libusbDevice {
return f.handles[handle]
}

func (f *fakeLibusb) exit(*libusbContext) error {
func (f *fakeLibusb) exit(ctx *libusbContext) error {
close(f.submitted)
if got := len(f.ts); got > 0 {
for t := range f.ts {
f.free(t)
}
f.mu.Lock()
for d := range f.devices {
freeDevicePointer(d)
delete(f.devices, d)
}
var remainingHandles []*libusbDevHandle
for h := range f.handles {
Comment thread
BryanDClark marked this conversation as resolved.
Resolved
remainingHandles = append(remainingHandles, h)
}
var remainingTransfers []*libusbTransfer
for t := range f.ts {
remainingTransfers = append(remainingTransfers, t)
}
f.mu.Unlock()

freeContextPointer(ctx)

for _, h := range remainingHandles {
f.close(h)
}
for _, t := range remainingTransfers {
f.free(t)
}
if got := len(remainingHandles); got > 0 {
return fmt.Errorf("fakeLibusb has %d remaining handles that should have been closed", got)
}
if got := len(remainingTransfers); got > 0 {
return fmt.Errorf("fakeLibusb has %d remaining transfers that should have been freed", got)
}
return nil
Expand All @@ -156,7 +179,10 @@ func (f *fakeLibusb) open(d *libusbDevice) (*libusbDevHandle, error) {
func (f *fakeLibusb) close(h *libusbDevHandle) {
f.mu.Lock()
defer f.mu.Unlock()
delete(f.handles, h)
if _, ok := f.handles[h]; ok {
delete(f.handles, h)
freeDevHandlePointer(h)
}
}
func (f *fakeLibusb) reset(*libusbDevHandle) error { return nil }
func (f *fakeLibusb) control(*libusbDevHandle, time.Duration, uint8, uint8, uint16, uint16, []byte) (int, error) {
Expand Down Expand Up @@ -274,7 +300,10 @@ func (f *fakeLibusb) data(t *libusbTransfer) (int, TransferStatus) {
func (f *fakeLibusb) free(t *libusbTransfer) {
f.mu.Lock()
defer f.mu.Unlock()
delete(f.ts, t)
if _, ok := f.ts[t]; ok {
delete(f.ts, t)
freeFakeTransferPointer(t)
}
}
func (f *fakeLibusb) setIsoPacketLengths(t *libusbTransfer, length uint32) {
f.mu.Lock()
Expand Down
24 changes: 23 additions & 1 deletion libusb.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

/*
#cgo pkg-config: libusb-1.0
#include <stdlib.h>
#include <libusb.h>

int gousb_compact_iso_data(struct libusb_transfer *xfer, unsigned char *status);
Expand Down Expand Up @@ -550,7 +551,10 @@ func libusbSetDebug(c *libusbContext, lvl int) {
C.gousb_set_debug((*C.libusb_context)(c), C.int(lvl))
}

// for obtaining unique CGo pointers.
// In tests, the code allocates some C pointers directly to obtain unique CGo
// pointers, and it needs to be able to free them directly too. These helpers
// are defined here because Go does not support cgo (import "C") in *_test.go
// files.
func newDevicePointer() *libusbDevice {
return (*libusbDevice)(unsafe.Pointer(C.malloc(1)))
}
Expand All @@ -566,3 +570,21 @@ func newContextPointer() *libusbContext {
func newDevHandlePointer() *libusbDevHandle {
return (*libusbDevHandle)(unsafe.Pointer(C.malloc(1)))
}

// In tests, the code allocates some C pointers directly, so it needs to be
// able to free them directly too.
func freeDevicePointer(d *libusbDevice) {
Comment thread
BryanDClark marked this conversation as resolved.
Resolved
C.free(unsafe.Pointer(d))
}

func freeFakeTransferPointer(t *libusbTransfer) {
C.free(unsafe.Pointer(t))
}

func freeContextPointer(c *libusbContext) {
C.free(unsafe.Pointer(c))
}

func freeDevHandlePointer(h *libusbDevHandle) {
C.free(unsafe.Pointer(h))
}
Loading