From 24d595aa71e7fd0f14d40e933a33f852f7269c8b Mon Sep 17 00:00:00 2001 From: Pete Batard Date: Tue, 10 Apr 2012 13:05:37 +0100 Subject: [PATCH] Windows: misc improvements * prefer calloc over malloc * silence VS2010 intellisense warnings on mem allocation * other minor fixes and formatting improvements to align with -pbatard --- libusb/os/poll_windows.c | 8 ++++---- libusb/os/poll_windows.h | 2 +- libusb/os/threads_windows.c | 2 +- libusb/os/windows_usb.c | 33 +++++++++++++++++---------------- libusb/os/windows_usb.h | 22 +++++++++++----------- 5 files changed, 34 insertions(+), 33 deletions(-) diff --git a/libusb/os/poll_windows.c b/libusb/os/poll_windows.c index 5204bcb..2201ffa 100644 --- a/libusb/os/poll_windows.c +++ b/libusb/os/poll_windows.c @@ -182,7 +182,7 @@ int _fd_to_index_and_lock(int fd) OVERLAPPED *create_overlapped(void) { - OVERLAPPED *overlapped = calloc(1, sizeof(OVERLAPPED)); + OVERLAPPED *overlapped = (OVERLAPPED*) calloc(1, sizeof(OVERLAPPED)); if (overlapped == NULL) { return NULL; } @@ -274,7 +274,7 @@ int usbi_pipe(int filedes[2]) CHECK_INIT_POLLING; - overlapped = calloc(1, sizeof(OVERLAPPED)); + overlapped = (OVERLAPPED*) calloc(1, sizeof(OVERLAPPED)); if (overlapped == NULL) { return -1; } @@ -572,8 +572,8 @@ int usbi_poll(struct pollfd *fds, unsigned int nfds, int timeout) CHECK_INIT_POLLING; triggered = 0; - handles_to_wait_on = malloc((nfds+1)*sizeof(HANDLE)); // +1 for fd_update - handle_to_index = malloc(nfds*sizeof(int)); + handles_to_wait_on = (HANDLE*) calloc(nfds+1, sizeof(HANDLE)); // +1 for fd_update + handle_to_index = (int*) calloc(nfds, sizeof(int)); if ((handles_to_wait_on == NULL) || (handle_to_index == NULL)) { errno = ENOMEM; triggered = -1; diff --git a/libusb/os/poll_windows.h b/libusb/os/poll_windows.h index 2aee946..1fe3669 100644 --- a/libusb/os/poll_windows.h +++ b/libusb/os/poll_windows.h @@ -38,7 +38,7 @@ #define STATUS_COMPLETED_SYNCHRONOUSLY STATUS_REPARSE #define HasOverlappedIoCompletedSync(lpOverlapped) (((DWORD)(lpOverlapped)->Internal) == STATUS_COMPLETED_SYNCHRONOUSLY) -#define DUMMY_HANDLE ((HANDLE)(LONG)-2) +#define DUMMY_HANDLE ((HANDLE)(LONG_PTR)-2) enum windows_version { WINDOWS_UNSUPPORTED, diff --git a/libusb/os/threads_windows.c b/libusb/os/threads_windows.c index c4fa489..bb5c4c8 100644 --- a/libusb/os/threads_windows.c +++ b/libusb/os/threads_windows.c @@ -142,7 +142,7 @@ static int __inline usbi_cond_intwait(usbi_cond_t *cond, } } if(!found) { - pos = malloc(sizeof(struct usbi_cond_perthread)); + pos = (struct usbi_cond_perthread*) calloc(1, sizeof(struct usbi_cond_perthread)); if(!pos) return ((errno=ENOMEM)); // This errno is not POSIX-allowed. pos->tid = tid; pos->event = CreateEvent(NULL, FALSE, FALSE, NULL); // auto-reset. diff --git a/libusb/os/windows_usb.c b/libusb/os/windows_usb.c index 631cdc7..655edf3 100644 --- a/libusb/os/windows_usb.c +++ b/libusb/os/windows_usb.c @@ -122,7 +122,7 @@ static inline BOOLEAN guid_eq(const GUID *guid1, const GUID *guid2) { return false; } -#if defined(ENABLE_DEBUG_LOGGING) || (defined(_MSC_VER) && _MSC_VER <= 1200) +#if defined(ENABLE_DEBUG_LOGGING) || (defined(_MSC_VER) && _MSC_VER < 1400) static char* guid_to_string(const GUID* guid) { static char guid_string[MAX_GUID_STRING_LENGTH]; @@ -197,7 +197,7 @@ static char* sanitize_path(const char* path) size += add_root; } - if ((ret_path = (char*)calloc(size, 1)) == NULL) + if ((ret_path = (char*) calloc(size, 1)) == NULL) return NULL; safe_strcpy(&ret_path[add_root], size-add_root, path); @@ -336,7 +336,7 @@ static SP_DEVICE_INTERFACE_DETAIL_DATA_A *get_interface_details(struct libusb_co goto err_exit; } - if ((dev_interface_details = malloc(size)) == NULL) { + if ((dev_interface_details = (SP_DEVICE_INTERFACE_DETAIL_DATA_A*) calloc(size, 1)) == NULL) { usbi_err(ctx, "could not allocate interface data for index %u.", _index); goto err_exit; } @@ -405,7 +405,7 @@ static int htab_create(struct libusb_context *ctx, unsigned long nel) htab_filled = 0; // allocate memory and zero out. - htab_table = (htab_entry*)calloc(htab_size + 1, sizeof(htab_entry)); + htab_table = (htab_entry*) calloc(htab_size + 1, sizeof(htab_entry)); if (htab_table == NULL) { usbi_err(ctx, "could not allocate space for hash table"); return 0; @@ -510,7 +510,7 @@ static unsigned long htab_hash(char* str) // string (same hash, different string) at the same time is extremely low safe_free(htab_table[idx].str); htab_table[idx].used = hval; - htab_table[idx].str = malloc(safe_strlen(str)+1); + htab_table[idx].str = (char*) malloc(safe_strlen(str)+1); if (htab_table[idx].str == NULL) { usbi_err(NULL, "could not duplicate string for hash table"); usbi_mutex_unlock(&htab_write_mutex); @@ -580,7 +580,7 @@ static int windows_assign_endpoints(struct libusb_device_handle *dev_handle, int return LIBUSB_SUCCESS; } - priv->usb_interface[iface].endpoint = malloc(if_desc->bNumEndpoints); + priv->usb_interface[iface].endpoint = (uint8_t*) malloc(if_desc->bNumEndpoints); if (priv->usb_interface[iface].endpoint == NULL) { return LIBUSB_ERROR_NO_MEM; } @@ -609,7 +609,7 @@ static bool is_api_driver(char* driver, uint8_t api) size_t len = safe_strlen(driver); if (len == 0) return false; - tmp_str = calloc(len+1, 1); + tmp_str = (char*) calloc(len+1, 1); if (tmp_str == NULL) return false; memcpy(tmp_str, driver, len+1); tok = strtok(tmp_str, sep_str); @@ -688,8 +688,8 @@ static void auto_release(struct usbi_transfer *itransfer) if (r == LIBUSB_SUCCESS) { usbi_dbg("auto-released interface %d", transfer_priv->interface_number); } else { - usbi_dbg("failed to auto-release interface %d (error=%d)", - transfer_priv->interface_number, r); + usbi_dbg("failed to auto-release interface %d (%s)", + transfer_priv->interface_number, libusb_error_name((enum libusb_error)r)); } } } @@ -891,7 +891,7 @@ static int cache_config_descriptors(struct libusb_device *dev, HANDLE hub_handle if (dev->num_configurations == 0) return LIBUSB_ERROR_INVALID_PARAM; - priv->config_descriptor = malloc(dev->num_configurations * sizeof(PUSB_CONFIGURATION_DESCRIPTOR)); + priv->config_descriptor = (unsigned char**) calloc(dev->num_configurations, sizeof(PUSB_CONFIGURATION_DESCRIPTOR)); if (priv->config_descriptor == NULL) return LIBUSB_ERROR_NO_MEM; for (i=0; inum_configurations; i++) @@ -929,7 +929,7 @@ static int cache_config_descriptors(struct libusb_device *dev, HANDLE hub_handle } size = sizeof(USB_DESCRIPTOR_REQUEST) + cd_buf_short.data.wTotalLength; - if ((cd_buf_actual = (PUSB_DESCRIPTOR_REQUEST)malloc(size)) == NULL) { + if ((cd_buf_actual = (PUSB_DESCRIPTOR_REQUEST) calloc(1, size)) == NULL) { usbi_err(ctx, "could not allocate configuration descriptor buffer for '%s'.", device_id); LOOP_BREAK(LIBUSB_ERROR_NO_MEM); } @@ -965,10 +965,9 @@ static int cache_config_descriptors(struct libusb_device *dev, HANDLE hub_handle i, cd_data->bConfigurationValue, cd_data->wTotalLength); // Cache the descriptor - priv->config_descriptor[i] = malloc(cd_data->wTotalLength); + priv->config_descriptor[i] = (unsigned char*) malloc(cd_data->wTotalLength); if (priv->config_descriptor[i] == NULL) return LIBUSB_ERROR_NO_MEM; - memcpy(priv->config_descriptor[i], cd_data, cd_data->wTotalLength); } return LIBUSB_SUCCESS; @@ -1230,7 +1229,7 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered guid[DEV_PASS] = &GUID_DEVINTERFACE_USB_DEVICE; nb_guids = DEV_PASS+1; - unref_list = malloc(unref_size*sizeof(libusb_device*)); + unref_list = (libusb_device**) calloc(unref_size, sizeof(libusb_device*)); if (unref_list == NULL) { return LIBUSB_ERROR_NO_MEM; } @@ -1351,7 +1350,7 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered usbi_err(ctx, "program assertion failed: too many GUIDs"); LOOP_BREAK(LIBUSB_ERROR_OVERFLOW); } - if_guid = calloc(1, sizeof(GUID)); + if_guid = (GUID*) calloc(1, sizeof(GUID)); pCLSIDFromString(guid_string_w, if_guid); guid[nb_guids++] = if_guid; usbi_dbg("extra GUID: %s", guid_to_string(if_guid)); @@ -1460,9 +1459,11 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered break; default: // For other devices, the first interface is the same as the device - priv->usb_interface[0].path = malloc(safe_strlen(priv->path)+1); + priv->usb_interface[0].path = (char*) calloc(safe_strlen(priv->path)+1, 1); if (priv->usb_interface[0].path != NULL) { safe_strcpy(priv->usb_interface[0].path, safe_strlen(priv->path)+1, priv->path); + } else { + usbi_warn(ctx, "could not duplicate interface path '%s'", priv->path); } // The following is needed if we want API calls to work for both simple // and composite devices. diff --git a/libusb/os/windows_usb.h b/libusb/os/windows_usb.h index d3fc59f..0c71110 100644 --- a/libusb/os/windows_usb.h +++ b/libusb/os/windows_usb.h @@ -245,26 +245,26 @@ struct driver_lookup { * API macros - from libusb-win32 1.x */ #define DLL_DECLARE_PREFIXNAME(api, ret, prefixname, name, args) \ - typedef ret (api * __dll_##name##_t)args; \ + typedef ret (api * __dll_##name##_t)args; \ static __dll_##name##_t prefixname = NULL #define DLL_LOAD_PREFIXNAME(dll, prefixname, name, ret_on_failure) \ - do { \ - HMODULE h = GetModuleHandleA(#dll); \ - if (!h) \ - h = LoadLibraryA(#dll); \ - if (!h) { \ - if (ret_on_failure) { return LIBUSB_ERROR_NOT_FOUND; }\ - else { break; } \ - } \ + do { \ + HMODULE h = GetModuleHandleA(#dll); \ + if (!h) \ + h = LoadLibraryA(#dll); \ + if (!h) { \ + if (ret_on_failure) { return LIBUSB_ERROR_NOT_FOUND; } \ + else { break; } \ + } \ prefixname = (__dll_##name##_t)GetProcAddress(h, #name); \ if (prefixname) break; \ prefixname = (__dll_##name##_t)GetProcAddress(h, #name "A"); \ if (prefixname) break; \ prefixname = (__dll_##name##_t)GetProcAddress(h, #name "W"); \ if (prefixname) break; \ - if(ret_on_failure) \ - return LIBUSB_ERROR_NOT_FOUND; \ + if(ret_on_failure) \ + return LIBUSB_ERROR_NOT_FOUND; \ } while(0) #define DLL_DECLARE(api, ret, name, args) DLL_DECLARE_PREFIXNAME(api, ret, name, name, args) -- 2.7.4