From 25c01a9f60ee37aa7fc6e3fd106a732bca4eab29 Mon Sep 17 00:00:00 2001 From: Kyle Lemons Date: Tue, 27 Mar 2012 18:40:32 -0700 Subject: [PATCH] Remove (*Config).Close() --- lsusb/main.go | 5 ----- usb/config.go | 23 ----------------------- usb/descriptor.go | 11 ++--------- usb/usb_test.go | 5 ----- 4 files changed, 2 insertions(+), 42 deletions(-) diff --git a/lsusb/main.go b/lsusb/main.go index 9b0c93c..e4ca387 100644 --- a/lsusb/main.go +++ b/lsusb/main.go @@ -64,11 +64,6 @@ func main() { // be set once the device is opened. All configuration references must be closed, // to free up the memory in libusb. cfgs, err := dev.Configurations() - defer func() { - for _, cfg := range cfgs { - cfg.Close() - } - }() if err != nil { log.Printf(" - configs: %s", err) continue diff --git a/usb/config.go b/usb/config.go index dfaa76a..394df0a 100644 --- a/usb/config.go +++ b/usb/config.go @@ -7,7 +7,6 @@ import "C" import ( "fmt" "reflect" - "runtime" "unsafe" ) @@ -53,8 +52,6 @@ func (i InterfaceInfo) String() string { } type Config struct { - cfg *C.struct_libusb_config_descriptor - Type DescriptorType Config uint8 Attributes uint8 @@ -68,7 +65,6 @@ func (c Config) String() string { func newConfig(cfg *C.struct_libusb_config_descriptor) *Config { c := &Config{ - cfg: cfg, Type: DescriptorType(cfg.bDescriptorType), Config: uint8(cfg.bConfigurationValue), Attributes: uint8(cfg.bmAttributes), @@ -121,24 +117,5 @@ func newConfig(cfg *C.struct_libusb_config_descriptor) *Config { } c.Interfaces = append(c.Interfaces, descs) } - - // *sigh* - runtime.SetFinalizer(c, (*Config).Close) - - //log.Printf("config %p initialized", c.cfg) return c } - -// Close decrements the reference count for the device in the libusb driver -// code. It should be called exactly once! -// -// TODO(kevlar): This information can probably be cached at creation time -// and then immediately closed. -func (c *Config) Close() error { - if c.cfg != nil { - //log.Printf("config %p closed", c.cfg) - C.libusb_free_config_descriptor(c.cfg) - } - c.cfg = nil - return nil -} diff --git a/usb/descriptor.go b/usb/descriptor.go index a145e51..a0954ce 100644 --- a/usb/descriptor.go +++ b/usb/descriptor.go @@ -37,27 +37,20 @@ func newDescriptor(dev *C.libusb_device) (*Descriptor, error) { // Configurations returns a list of configurations configured on this // device. Each config must be Closed. -func (d *DeviceInfo) Configurations() (_c []*Config, _e error) { +func (d *DeviceInfo) Configurations() ([]*Config, error) { var desc C.struct_libusb_device_descriptor if errno := C.libusb_get_device_descriptor(d.dev, &desc); errno < 0 { return nil, usbError(errno) } var cfgs []*Config - defer func() { - if _e != nil { - for _, c := range cfgs { - c.Close() - } - } - }() - for i := 0; i < int(desc.bNumConfigurations); i++ { var cfg *C.struct_libusb_config_descriptor if errno := C.libusb_get_config_descriptor(d.dev, C.uint8_t(i), &cfg); errno < 0 { return nil, usbError(errno) } cfgs = append(cfgs, newConfig(cfg)) + C.libusb_free_config_descriptor(cfg) } return cfgs, nil } diff --git a/usb/usb_test.go b/usb/usb_test.go index e36863b..5ce52e5 100644 --- a/usb/usb_test.go +++ b/usb/usb_test.go @@ -49,11 +49,6 @@ func TestEnum(t *testing.T) { t.Logf("- Protocol: %s", usbid.Classify(desc)) cfgs, err := dev.Configurations() - defer func() { - for _, cfg := range cfgs { - cfg.Close() - } - }() if err != nil { t.Errorf(" - configs: %s", err) continue