From d35b0f5b200dc75b256e30b1de4f281daefde7dd Mon Sep 17 00:00:00 2001 From: Matthew Brush Date: Mon, 4 Mar 2013 17:57:39 -0800 Subject: [PATCH] Remove a bunch of superfluous casts They just ugly up the code and could potentially hide errors. The only problem it might cause is if using C++ compiler and, well, don't do that. This is C code, use the right compiler :) --- src/cfuconf.c | 23 +++++++++++------------ src/cfuhash.c | 14 +++++++------- src/cfulist.c | 6 +++--- src/cfuopt.c | 2 +- src/cfustring.c | 36 ++++++++++++++++++------------------ src/cfuthread_queue.c | 5 ++--- src/cfutime.c | 2 +- 7 files changed, 43 insertions(+), 45 deletions(-) diff --git a/src/cfuconf.c b/src/cfuconf.c index 922ffda..fe05b20 100644 --- a/src/cfuconf.c +++ b/src/cfuconf.c @@ -79,7 +79,7 @@ typedef struct cfuconf_stack_entry { static cfuconf_t * cfuconf_new() { - cfuconf_t *conf = (cfuconf_t *)calloc(1, sizeof(cfuconf_t)); + cfuconf_t *conf = calloc(1, sizeof(cfuconf_t)); conf->type = libcfu_t_conf; conf->containers = cfuhash_new_with_flags(CFUHASH_IGNORE_CASE); conf->directives = cfuhash_new_with_flags(CFUHASH_IGNORE_CASE); @@ -244,8 +244,7 @@ cfuconf_get_directive_n_args(cfuconf_t *conf, char *directive, size_t n, ...) { /* static cfuconf_stack_entry_t * new_stack_entry(cfuhash_table_t *ht, char *container_type, char *container_name) { - cfuconf_stack_entry_t *entry = - (cfuconf_stack_entry_t *)calloc(1, sizeof(cfuconf_stack_entry_t)); + cfuconf_stack_entry_t *entry = calloc(1, sizeof(cfuconf_stack_entry_t)); entry->ht = ht; entry->container_type = container_type; entry->container_name = container_name; @@ -350,8 +349,8 @@ _dup_c_str_n_drop_escape(const char *str, size_t n, char escape) { if (n == 0) return NULL; - ptr = (char *)str; - ns_ptr = ns = (char *)calloc(len + 1, 1); + ptr = (char *)str; /* ? */ + ns_ptr = ns = calloc(len + 1, 1); last_char = *ptr; for (ptr = (char *)str; ptr < end; ptr++) { @@ -749,11 +748,11 @@ print_sub_container_foreach_fn(void *name, size_t key_size, void *data, size_t d static int print_container_foreach_fn(void *name, size_t key_size, void *data, size_t data_size, void *arg) { - directive_foreach_ds *ds = (directive_foreach_ds *)arg; - directive_foreach_ds *new_ds = (directive_foreach_ds *)calloc(1, sizeof(directive_foreach_ds)); + directive_foreach_ds *ds = arg; + directive_foreach_ds *new_ds = calloc(1, sizeof(directive_foreach_ds)); memcpy(new_ds, ds, sizeof(directive_foreach_ds)); - new_ds->name = (char *)name; + new_ds->name = name; key_size = key_size; data_size = data_size; @@ -782,9 +781,9 @@ print_directive_list_foreach_fn(void *data, size_t data_size, void *arg) { static int print_conf_foreach_directive(void *name, size_t key_size, void *data, size_t data_size, void *arg) { - directive_foreach_ds *ds = (directive_foreach_ds *)arg; - cfulist_t *this_directive_list = (cfulist_t *)data; - directive_foreach_ds *new_ds = (directive_foreach_ds *)calloc(1, sizeof(directive_foreach_ds)); + directive_foreach_ds *ds = arg; + cfulist_t *this_directive_list = data; + directive_foreach_ds *new_ds = calloc(1, sizeof(directive_foreach_ds)); key_size = key_size; data_size = data_size; @@ -802,7 +801,7 @@ print_conf_foreach_directive(void *name, size_t key_size, void *data, size_t dat static void print_conf(cfuconf_t *conf, size_t depth, FILE *fp) { - directive_foreach_ds *ds = (directive_foreach_ds *)calloc(1, sizeof(directive_foreach_ds)); + directive_foreach_ds *ds = calloc(1, sizeof(directive_foreach_ds)); ds->depth = depth; ds->fp = fp; diff --git a/src/cfuhash.c b/src/cfuhash.c index 1934ec5..c810c2d 100644 --- a/src/cfuhash.c +++ b/src/cfuhash.c @@ -181,14 +181,14 @@ _cfuhash_new(size_t size, u_int32_t flags) { cfuhash_table_t *ht; size = hash_size(size); - ht = (cfuhash_table_t *)malloc(sizeof(cfuhash_table_t)); + ht = malloc(sizeof(cfuhash_table_t)); memset(ht, '\000', sizeof(cfuhash_table_t)); ht->type = libcfu_t_hash_table; ht->num_buckets = size; ht->entries = 0; ht->flags = flags; - ht->buckets = (cfuhash_entry **)calloc(size, sizeof(cfuhash_entry *)); + ht->buckets = calloc(size, sizeof(cfuhash_entry *)); #ifdef HAVE_PTHREAD_H pthread_mutex_init(&ht->mutex, NULL); @@ -359,7 +359,7 @@ hash_cmp(const void *key, size_t key_size, cfuhash_entry *he, unsigned int case_ static CFU_INLINE cfuhash_entry * hash_add_entry(cfuhash_table_t *ht, unsigned int hv, const void *key, size_t key_size, void *data, size_t data_size) { - cfuhash_entry *he = (cfuhash_entry *)calloc(1, sizeof(cfuhash_entry)); + cfuhash_entry *he = calloc(1, sizeof(cfuhash_entry)); assert(hv < ht->num_buckets); @@ -601,8 +601,8 @@ cfuhash_keys_data(cfuhash_table_t *ht, size_t *num_keys, size_t **key_sizes, int if (! (ht->flags & CFUHASH_NO_LOCKING) ) lock_hash(ht); - if (key_sizes) key_lengths = (size_t *)calloc(ht->entries, sizeof(size_t)); - keys = (void **)calloc(ht->entries, sizeof(void *)); + if (key_sizes) key_lengths = calloc(ht->entries, sizeof(size_t)); + keys = calloc(ht->entries, sizeof(void *)); for (bucket = 0; bucket < ht->num_buckets; bucket++) { if ( (he = ht->buckets[bucket]) ) { @@ -612,7 +612,7 @@ cfuhash_keys_data(cfuhash_table_t *ht, size_t *num_keys, size_t **key_sizes, int if (fast) { keys[entry_index] = he->key; } else { - keys[entry_index] = (void *)calloc(he->key_size, 1); + keys[entry_index] = calloc(he->key_size, 1); memcpy(keys[entry_index], he->key, he->key_size); } key_count++; @@ -847,7 +847,7 @@ cfuhash_rehash(cfuhash_table_t *ht) { unlock_hash(ht); return 0; } - new_buckets = (cfuhash_entry **)calloc(new_size, sizeof(cfuhash_entry *)); + new_buckets = calloc(new_size, sizeof(cfuhash_entry *)); for (i = 0; i < ht->num_buckets; i++) { cfuhash_entry *he = ht->buckets[i]; diff --git a/src/cfulist.c b/src/cfulist.c index a4455e2..a0d2aaf 100644 --- a/src/cfulist.c +++ b/src/cfulist.c @@ -73,7 +73,7 @@ struct cfulist { extern cfulist_t * cfulist_new() { - cfulist_t *list = (cfulist_t *)calloc(1, sizeof(cfulist_t)); + cfulist_t *list = calloc(1, sizeof(cfulist_t)); #ifdef HAVE_PTHREAD_H pthread_mutex_init(&list->mutex, NULL); #endif @@ -109,7 +109,7 @@ unlock_list(cfulist_t *list) { static CFU_INLINE cfulist_entry * new_list_entry() { - return (cfulist_entry *)calloc(1, sizeof(cfulist_entry)); + return calloc(1, sizeof(cfulist_entry)); } extern int @@ -495,7 +495,7 @@ _join_foreach_fn(void *data, size_t data_size, void *arg) { extern char * cfulist_join(cfulist_t *list, const char *delimiter) { - _join_foreach_struct *arg = (_join_foreach_struct *)calloc(1, sizeof(_join_foreach_struct)); + _join_foreach_struct *arg = calloc(1, sizeof(_join_foreach_struct)); char *str = NULL; arg->delimiter = delimiter; diff --git a/src/cfuopt.c b/src/cfuopt.c index 0e3aa2f..beac7d4 100644 --- a/src/cfuopt.c +++ b/src/cfuopt.c @@ -73,7 +73,7 @@ typedef struct cfuopt_list_entry { extern cfuopt_t * cfuopt_new() { - cfuopt_t *context = (cfuopt_t *)calloc(1, sizeof(cfuopt_t)); + cfuopt_t *context = calloc(1, sizeof(cfuopt_t)); context->option_list = cfulist_new(); context->option_map = cfuhash_new(); context->extra = cfulist_new(); diff --git a/src/cfustring.c b/src/cfustring.c index 4806b2a..edf1c45 100644 --- a/src/cfustring.c +++ b/src/cfustring.c @@ -68,10 +68,10 @@ cfustring_new() { extern cfustring_t * cfustring_new_with_initial_size(size_t initial_size) { - cfustring_t *cfu_str = (cfustring_t *)calloc(1, sizeof(cfustring_t)); + cfustring_t *cfu_str = calloc(1, sizeof(cfustring_t)); cfu_str->type = libcfu_t_string; if (initial_size > 0) { - cfu_str->str = (char *)calloc(initial_size, 1); + cfu_str->str = calloc(initial_size, 1); cfu_str->max_size = initial_size; cfu_str->used_size = 1; } @@ -129,7 +129,7 @@ cfustring_append_n(cfustring_t *cfu_str, const char *string, size_t n) { } if (!cfu_str->str) { - cfu_str->str = (char *)malloc(str_len + 1); + cfu_str->str = malloc(str_len + 1); cfu_str->max_size = str_len + 1; cfu_str->used_size = 1; cfu_str->str[0] = '\000'; @@ -144,7 +144,7 @@ cfustring_append_n(cfustring_t *cfu_str, const char *string, size_t n) { } else { cfu_str->max_size = cfu_str->used_size + str_len + 1; } - tmp = (char *)malloc(cfu_str->max_size); + tmp = malloc(cfu_str->max_size); memcpy(tmp, cfu_str->str, cfu_str->used_size); free(cfu_str->str); cfu_str->str = tmp; @@ -174,7 +174,7 @@ cfustring_get_buffer_copy(cfustring_t *cfu_str) { char *buffer = NULL; if (!cfu_str->str) return NULL; - buffer = (char *)calloc(cfu_str->used_size, 1); + buffer = calloc(cfu_str->used_size, 1); memcpy(buffer, cfu_str->str, cfu_str->used_size); return buffer; } @@ -182,7 +182,7 @@ cfustring_get_buffer_copy(cfustring_t *cfu_str) { static char * _dup_str(const char *str) { size_t len = strlen(str) + 1; - char *ns = (char *)calloc(len, 1); + char *ns = calloc(len, 1); memcpy(ns, str, len); return ns; } @@ -194,7 +194,7 @@ _dup_str_n(const char *str, size_t n) { if (n == 0) return NULL; - ns = (char *)calloc(len + 1, 1); + ns = calloc(len + 1, 1); memcpy(ns, str, len); ns[len] = '\000'; return ns; @@ -260,9 +260,9 @@ __cfustring_split_to_raw(cfustring_t *cfu_str, size_t *num_strings, size_t num_s va_list ap) { char *sep = NULL; size_t i = 0; - char **sep_array = (char **)calloc(num_seps, sizeof(char *)); - char **sep_chk_ptrs = (char **)calloc(num_seps, sizeof(char *)); - char **ret_strings = (char **)calloc(2, sizeof(char *)); + char **sep_array = calloc(num_seps, sizeof(char *)); + char **sep_chk_ptrs = calloc(num_seps, sizeof(char *)); + char **ret_strings = calloc(2, sizeof(char *)); unsigned int max_ret_strings = 2; unsigned int used_ret_strings = 0; char *end = NULL; @@ -297,7 +297,7 @@ __cfustring_split_to_raw(cfustring_t *cfu_str, size_t *num_strings, size_t num_s if (used_ret_strings == max_ret_strings) { /* allocate more space */ size_t new_size = max_ret_strings << 1; - char **tmp = (char **)calloc(new_size, sizeof(char *)); + char **tmp = calloc(new_size, sizeof(char *)); for (i = 0; i < used_ret_strings; i++) tmp[i] = ret_strings[i]; free(ret_strings); ret_strings = tmp; @@ -343,7 +343,7 @@ cfustring_split(cfustring_t *cfu_str, size_t *num_strings, size_t limit, ...) { va_end(ap); if (!*num_strings) return NULL; - rv = (cfustring_t **)malloc(*num_strings * sizeof(cfustring_t *)); + rv = malloc(*num_strings * sizeof(cfustring_t *)); for (i = 0; i < *num_strings; i++) { rv[i] = cfustring_new_from_string(strings[i]); free(strings[i]); @@ -411,7 +411,7 @@ _safe_sprintf(char **buf, size_t *buf_size, const char *fmt, ...) { if (!(*buf) || *buf_size == 0) { *buf_size = 128; if (*buf) free(*buf); - *buf = (char *)malloc(*buf_size); + *buf = malloc(*buf_size); } while (!done) { @@ -421,7 +421,7 @@ _safe_sprintf(char **buf, size_t *buf_size, const char *fmt, ...) { if (rv >= (int)(*buf_size) - 1) { *buf_size *= 2; free(*buf); - *buf = (char *)malloc(*buf_size); + *buf = malloc(*buf_size); } else { done = 1; @@ -438,14 +438,14 @@ _safe_strncpy(char **buf, size_t *buf_size, const char *src, size_t size) { if (!(*buf) || *buf_size == 0) { *buf_size = size + 1; if (*buf) free(*buf); - *buf = (char *)malloc(*buf_size); + *buf = malloc(*buf_size); (*buf)[0] = '\000'; } if (size > *buf_size - 1) { *buf_size = size + 1; if (*buf) free(*buf); - *buf = (char *)malloc(*buf_size); + *buf = malloc(*buf_size); (*buf)[0] = '\000'; } @@ -466,8 +466,8 @@ cfustring_vsprintf(cfustring_t *cfu_str, const char *fmt_in, va_list ap) { char *buf2 = NULL; const char *fmt = NULL; - buf = (char *)malloc(buf_size); - buf2 = (char *)malloc(buf2_size); + buf = malloc(buf_size); + buf2 = malloc(buf2_size); cfustring_clear(cfu_str); diff --git a/src/cfuthread_queue.c b/src/cfuthread_queue.c index 02b518f..9845516 100644 --- a/src/cfuthread_queue.c +++ b/src/cfuthread_queue.c @@ -64,8 +64,7 @@ typedef struct cfuthread_queue_entry { static cfuthread_queue_entry * _new_cfuthread_entry(void *data) { - cfuthread_queue_entry *entry = - (cfuthread_queue_entry *)calloc(1, sizeof(cfuthread_queue_entry)); + cfuthread_queue_entry *entry = calloc(1, sizeof(cfuthread_queue_entry)); pthread_mutex_init(&entry->mutex, NULL); pthread_cond_init(&entry->cv, NULL); entry->data_in = data; @@ -115,7 +114,7 @@ extern cfuthread_queue_t * cfuthread_queue_new_with_cleanup(cfuthread_queue_fn_t fn, cfuthread_queue_init_t init_fn, void *init_arg, cfuthread_queue_cleanup_t cleanup_fn, void *cleanup_arg) { - cfuthread_queue_t *tq = (cfuthread_queue_t *)calloc(1, sizeof(cfuthread_queue_t)); + cfuthread_queue_t *tq = calloc(1, sizeof(cfuthread_queue_t)); pthread_mutex_init(&tq->mutex, NULL); pthread_cond_init(&tq->cv, NULL); tq->fn = fn; diff --git a/src/cfutime.c b/src/cfutime.c index bace1a9..d501b18 100644 --- a/src/cfutime.c +++ b/src/cfutime.c @@ -52,7 +52,7 @@ typedef struct cfutime { extern cfutime_t * cfutime_new() { - cfutime_t *time = (cfutime_t *)calloc(1, sizeof(cfutime)); + cfutime_t *time = calloc(1, sizeof(cfutime)); time->type = libcfu_t_time; return time; }