From b7842d55a89c8e278589dd827256aaf02686c0d9 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Thu, 5 Mar 2015 09:33:51 +1100 Subject: [PATCH] Use alloc helpers in usbutils --- usbutils.c | 69 ++++++++++++++---------------------------------------- 1 file changed, 17 insertions(+), 52 deletions(-) diff --git a/usbutils.c b/usbutils.c index 5786d7fce7..1dc0493da7 100644 --- a/usbutils.c +++ b/usbutils.c @@ -1,6 +1,6 @@ /* * Copyright 2012-2013 Andrew Smith - * Copyright 2013-2014 Con Kolivas + * Copyright 2013-2015 Con Kolivas * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free @@ -1122,9 +1122,7 @@ static void append(char **buf, char *append, size_t *off, size_t *len) if ((new + *off) >= *len) { *len *= 2; - *buf = realloc(*buf, *len); - if (unlikely(!*buf)) - quit(1, "USB failed to realloc append"); + *buf = cgrealloc(*buf, *len); } strcpy(*buf + *off, append); @@ -1662,9 +1660,7 @@ static void add_in_use(uint8_t bus_number, uint8_t device_address, bool blacklis else head = &in_use_head; - in_use_tmp = calloc(1, sizeof(*in_use_tmp)); - if (unlikely(!in_use_tmp)) - quit(1, "USB failed to calloc in_use_tmp"); + in_use_tmp = cgcalloc(1, sizeof(*in_use_tmp)); in_use_tmp->in_use.bus_number = (int)bus_number; in_use_tmp->in_use.device_address = (int)device_address; in_use_tmp->next = in_use_head; @@ -1732,9 +1728,7 @@ static bool cgminer_usb_lock_bd(struct device_drv *drv, uint8_t bus_number, uint applog(LOG_DEBUG, "USB lock %s %d-%d", drv->dname, (int)bus_number, (int)device_address); - res_work = calloc(1, sizeof(*res_work)); - if (unlikely(!res_work)) - quit(1, "USB failed to calloc lock res_work"); + res_work = cgcalloc(1, sizeof(*res_work)); res_work->lock = true; res_work->dname = (const char *)(drv->dname); res_work->bus_number = bus_number; @@ -1791,9 +1785,7 @@ static void cgminer_usb_unlock_bd(struct device_drv *drv, uint8_t bus_number, ui applog(LOG_DEBUG, "USB unlock %s %d-%d", drv->dname, (int)bus_number, (int)device_address); - res_work = calloc(1, sizeof(*res_work)); - if (unlikely(!res_work)) - quit(1, "USB failed to calloc unlock res_work"); + res_work = cgcalloc(1, sizeof(*res_work)); res_work->lock = false; res_work->dname = (const char *)(drv->dname); res_work->bus_number = bus_number; @@ -1976,9 +1968,7 @@ struct cgpu_info *usb_copy_cgpu(struct cgpu_info *orig) DEVWLOCK(orig, pstate); - copy = calloc(1, sizeof(*copy)); - if (unlikely(!copy)) - quit(1, "Failed to calloc cgpu for %s in usb_copy_cgpu", orig->drv->dname); + copy = cgcalloc(1, sizeof(*copy)); copy->name = orig->name; copy->drv = copy_drv(orig->drv); @@ -1998,10 +1988,7 @@ struct cgpu_info *usb_copy_cgpu(struct cgpu_info *orig) struct cgpu_info *usb_alloc_cgpu(struct device_drv *drv, int threads) { - struct cgpu_info *cgpu = calloc(1, sizeof(*cgpu)); - - if (unlikely(!cgpu)) - quit(1, "Failed to calloc cgpu for %s in usb_alloc_cgpu", drv->dname); + struct cgpu_info *cgpu = cgcalloc(1, sizeof(*cgpu)); cgpu->drv = drv; cgpu->deven = DEV_ENABLED; @@ -2064,9 +2051,7 @@ static int _usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct u snprintf(devstr, sizeof(devstr), "- %s device %s", found->name, devpath); - cgusb = calloc(1, sizeof(*cgusb)); - if (unlikely(!cgusb)) - quit(1, "USB failed to calloc _usb_init cgusb"); + cgusb = cgcalloc(1, sizeof(*cgusb)); cgusb->found = found; if (found->idVendor == IDVENDOR_FTDI) @@ -2074,9 +2059,7 @@ static int _usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct u cgusb->ident = found->ident; - cgusb->descriptor = calloc(1, sizeof(*(cgusb->descriptor))); - if (unlikely(!cgusb->descriptor)) - quit(1, "USB failed to calloc _usb_init cgusb descriptor"); + cgusb->descriptor = cgcalloc(1, sizeof(*(cgusb->descriptor))); err = libusb_get_device_descriptor(dev, cgusb->descriptor); if (err) { @@ -2410,9 +2393,7 @@ bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find if (find_dev[i].drv == found_match->drv && find_dev[i].idVendor == found_match->idVendor && find_dev[i].idProduct == found_match->idProduct) { - found_use = malloc(sizeof(*found_use)); - if (unlikely(!found_use)) - quit(1, "USB failed to malloc found_use"); + found_use = cgmalloc(sizeof(*found_use)); cg_memcpy(found_use, &(find_dev[i]), sizeof(*found_use)); ret = _usb_init(cgpu, dev, found_use); @@ -2486,9 +2467,7 @@ static struct usb_find_devices *usb_check_each(int drvnum, struct device_drv *dr for (i = 0; find_dev[i].drv != DRIVER_MAX; i++) if (find_dev[i].drv == drvnum) { if (usb_check_device(drv, dev, &(find_dev[i]))) { - found = malloc(sizeof(*found)); - if (unlikely(!found)) - quit(1, "USB failed to malloc found"); + found = cgmalloc(sizeof(*found)); cg_memcpy(found, &(find_dev[i]), sizeof(*found)); return found; } @@ -2717,15 +2696,10 @@ static void newstats(struct cgpu_info *cgpu) cgpu->usbinfo.usbstat = next_stat + 1; - usb_stats = realloc(usb_stats, sizeof(*usb_stats) * (next_stat+1)); - if (unlikely(!usb_stats)) - quit(1, "USB failed to realloc usb_stats %d", next_stat+1); - + usb_stats = cgrealloc(usb_stats, sizeof(*usb_stats) * (next_stat+1)); usb_stats[next_stat].name = cgpu->drv->name; usb_stats[next_stat].device_id = -1; - usb_stats[next_stat].details = calloc(2, sizeof(struct cg_usb_stats_details) * (C_MAX + 1)); - if (unlikely(!usb_stats[next_stat].details)) - quit(1, "USB failed to calloc details for %d", next_stat+1); + usb_stats[next_stat].details = cgcalloc(2, sizeof(struct cg_usb_stats_details) * (C_MAX + 1)); for (i = 1; i < C_MAX * 2; i += 2) usb_stats[next_stat].details[i].seq = 1; @@ -3804,10 +3778,7 @@ void usb_initialise(void) quit(1, "Invalid --usb bus:dev - dev must be > 0 or '*'"); } - busdev = realloc(busdev, sizeof(*busdev) * (++busdev_count)); - if (unlikely(!busdev)) - quit(1, "USB failed to realloc busdev"); - + busdev = cgrealloc(busdev, sizeof(*busdev) * (++busdev_count)); busdev[busdev_count-1].bus_number = bus; busdev[busdev_count-1].device_address = dev; @@ -3881,9 +3852,7 @@ static LPSECURITY_ATTRIBUTES mksec(const char *dname, uint8_t bus_number, uint8_ LPSECURITY_ATTRIBUTES sec_att = NULL; PSECURITY_DESCRIPTOR sec_des = NULL; - sec_des = malloc(sizeof(*sec_des)); - if (unlikely(!sec_des)) - quit(1, "MTX: Failed to malloc LPSECURITY_DESCRIPTOR"); + sec_des = cgmalloc(sizeof(*sec_des)); if (!InitializeSecurityDescriptor(sec_des, SECURITY_DESCRIPTOR_REVISION)) { applog(LOG_ERR, @@ -3922,9 +3891,7 @@ static LPSECURITY_ATTRIBUTES mksec(const char *dname, uint8_t bus_number, uint8_ return NULL; } - sec_att = malloc(sizeof(*sec_att)); - if (unlikely(!sec_att)) - quit(1, "MTX: Failed to malloc LPSECURITY_ATTRIBUTES"); + sec_att = cgmalloc(sizeof(*sec_att)); sec_att->nLength = sizeof(*sec_att); sec_att->lpSecurityDescriptor = sec_des; @@ -4112,9 +4079,7 @@ static void resource_process() (int)res_work_head->device_address, ok); - res_reply = calloc(1, sizeof(*res_reply)); - if (unlikely(!res_reply)) - quit(1, "USB failed to calloc res_reply"); + res_reply = cgcalloc(1, sizeof(*res_reply)); res_reply->bus_number = res_work_head->bus_number; res_reply->device_address = res_work_head->device_address;