Make valid C99 and cleanup warnings using stricter flags

Flags used for these changes:
  -Wall -Wextra -Werror -Wno-unused-parameter -std=c99 -pedantic

Note: the strcasecmp()/strncasecmp() implementations are kind of
a hack and should at least be factored out into a compat file or
some such.
This commit is contained in:
Matthew Brush 2013-03-04 17:04:33 -08:00
parent da501075ce
commit 0d7d3bf9c8
7 changed files with 94 additions and 30 deletions

View File

@ -3,12 +3,37 @@
* Change log: * Change log:
*/ */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "cfuhash.h" #include "cfuhash.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#if defined(HAVE_STRNCASECMP) && defined(HAVE_STRINGS_H)
# include <strings.h>
#else /* If strncasecmp() isn't available use this one */
static inline int
strncasecmp(const char *s1, const char *s2, size_t n)
{
assert(s1 && s2);
if (n != 0) {
const unsigned char *us1 = (const unsigned char *) s1,
const unsigned char *us2 = (const unsigned char *) s2;
do {
if (tolower(*us1) != tolower(*us2++))
return (tolower(*us1) - tolower(*--us2));
if (*us1++ == '\0')
break;
} while (--n != 0);
}
return 0;
}
#endif
static int static int
remove_func(void *key, size_t key_size, void *data, size_t data_size, void *arg) { remove_func(void *key, size_t key_size, void *data, size_t data_size, void *arg) {
data_size = data_size; data_size = data_size;
@ -39,7 +64,7 @@ int main(int argc, char **argv) {
sprintf(list[i][0], "test_var%lu", i); sprintf(list[i][0], "test_var%lu", i);
sprintf(list[i][1], "value%lu", i); sprintf(list[i][1], "value%lu", i);
} }
cfuhash_put(hash, "var1", "value1"); cfuhash_put(hash, "var1", "value1");
cfuhash_put(hash, "var2", "value2"); cfuhash_put(hash, "var2", "value2");
cfuhash_put(hash, "var3", "value3"); cfuhash_put(hash, "var3", "value3");
@ -61,7 +86,7 @@ int main(int argc, char **argv) {
printf("%lu entries, %lu buckets used out of %lu\n", cfuhash_num_entries(hash), cfuhash_num_buckets_used(hash), cfuhash_num_buckets(hash)); printf("%lu entries, %lu buckets used out of %lu\n", cfuhash_num_entries(hash), cfuhash_num_buckets_used(hash), cfuhash_num_buckets(hash));
cfuhash_pretty_print(hash, stdout); cfuhash_pretty_print(hash, stdout);
cfuhash_clear(hash); cfuhash_clear(hash);
for (i = 0; i < 32; i++) { for (i = 0; i < 32; i++) {
@ -79,7 +104,7 @@ int main(int argc, char **argv) {
char **keys = NULL; char **keys = NULL;
size_t *key_sizes = NULL; size_t *key_sizes = NULL;
size_t key_count = 0; size_t key_count = 0;
keys = (char **)cfuhash_keys_data(hash, &key_count, &key_sizes, 0); keys = (char **)cfuhash_keys_data(hash, &key_count, &key_sizes, 0);
printf("\n\nkeys (%lu):\n", key_count); printf("\n\nkeys (%lu):\n", key_count);
@ -101,10 +126,10 @@ int main(int argc, char **argv) {
cfuhash_set_flag(hash, CFUHASH_IGNORE_CASE); cfuhash_set_flag(hash, CFUHASH_IGNORE_CASE);
cfuhash_put(hash, "Content-Type", "value1"); cfuhash_put(hash, "Content-Type", "value1");
cfuhash_put(hash, "Content-Length", "value2"); cfuhash_put(hash, "Content-Length", "value2");
cfuhash_put(hash2, "var3", "value3"); cfuhash_put(hash2, "var3", "value3");
cfuhash_put(hash2, "var4", "value4"); cfuhash_put(hash2, "var4", "value4");
cfuhash_pretty_print(hash, stdout); cfuhash_pretty_print(hash, stdout);
cfuhash_copy(hash2, hash); cfuhash_copy(hash2, hash);

View File

@ -27,7 +27,7 @@ dup_str(const char *str) {
static u_int32_t static u_int32_t
hash_func1(const void *key, size_t length) { hash_func1(const void *key, size_t length) {
size_t i = length; size_t i = length;
u_int hash = 0; unsigned int hash = 0;
char *s = (char *)key; char *s = (char *)key;
while (i--) hash = hash * 33 + *s++; while (i--) hash = hash * 33 + *s++;
@ -50,7 +50,7 @@ hash_func1(const void *key, size_t length) {
typedef u_int32_t ub4; typedef u_int32_t ub4;
static u_int static unsigned int
hash_func_tc(const void *key, size_t length) hash_func_tc(const void *key, size_t length)
{ {
register ub4 a,b,c,len; register ub4 a,b,c,len;
@ -98,7 +98,6 @@ hash_func_tc(const void *key, size_t length)
static int static int
time_it(cfuhash_function_t hf, double *elapsed_time, u_int32_t num_tests) { time_it(cfuhash_function_t hf, double *elapsed_time, u_int32_t num_tests) {
cfuhash_table_t *hash = cfuhash_new_with_initial_size(30); cfuhash_table_t *hash = cfuhash_new_with_initial_size(30);
u_int32_t flags = 0;
char key[32]; char key[32];
char value[32]; char value[32];
size_t i; size_t i;
@ -108,7 +107,7 @@ time_it(cfuhash_function_t hf, double *elapsed_time, u_int32_t num_tests) {
cfutime_t *time = cfutime_new(); cfutime_t *time = cfutime_new();
/* freeze the hash so that it won't shrink while we put in all the data */ /* freeze the hash so that it won't shrink while we put in all the data */
flags = cfuhash_set_flag(hash, CFUHASH_FROZEN_UNTIL_GROWS); cfuhash_set_flag(hash, CFUHASH_FROZEN_UNTIL_GROWS);
cfuhash_set_hash_function(hash, hf); cfuhash_set_hash_function(hash, hf);
cfutime_begin(time); cfutime_begin(time);
@ -125,7 +124,7 @@ time_it(cfuhash_function_t hf, double *elapsed_time, u_int32_t num_tests) {
num_entries = cfuhash_num_entries(hash); num_entries = cfuhash_num_entries(hash);
printf("%lu entries, %lu/%lu buckets (%.2f%%), %.2f%% threshold check\n", num_entries, used, num_buckets, 100.0 * (float)used/(float)num_buckets, 100.0 * (float)num_entries/(float)num_buckets); printf("%lu entries, %lu/%lu buckets (%.2f%%), %.2f%% threshold check\n", num_entries, used, num_buckets, 100.0 * (float)used/(float)num_buckets, 100.0 * (float)num_entries/(float)num_buckets);
cfuhash_destroy_with_free_fn(hash, free_data); cfuhash_destroy_with_free_fn(hash, free_data);
return 0; return 0;
} }

View File

@ -35,6 +35,10 @@
* OF THE POSSIBILITY OF SUCH DAMAGE. * OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "cfu.h" #include "cfu.h"
#include "cfuconf.h" #include "cfuconf.h"
@ -44,6 +48,21 @@
#include <stdarg.h> #include <stdarg.h>
#include <assert.h> #include <assert.h>
#if defined(HAVE_STRCASECMP) && defined(HAVE_STRINGS_H)
# include <strings.h>
#else /* If strcasecmp() isn't available use this one */
static inline int
strcasecmp(const char *s1, const char *s2, size_t n)
{
int c1, c2;
while (c1 = toupper(*s1++), c2 = toupper(*s2++), c1 == c2 && (c1 & c2))
;
if (c1 & c2)
return c1 < c2 ? -1 : 1;
return c1 ? 1 : (c2 ? -1 : 0);
}
#endif
struct cfuconf { struct cfuconf {
libcfu_type type; libcfu_type type;
cfuhash_table_t *containers; cfuhash_table_t *containers;
@ -650,6 +669,7 @@ print_indent(size_t depth, FILE *fp) {
} }
} }
#if 0 /* Not currently used */
static void static void
print_container(cfuhash_table_t *conf_hash, size_t depth) { print_container(cfuhash_table_t *conf_hash, size_t depth) {
char **keys = NULL; char **keys = NULL;
@ -702,6 +722,7 @@ print_container(cfuhash_table_t *conf_hash, size_t depth) {
} }
free(keys); free(keys);
} }
#endif /* End currently unused function print_container() */
typedef struct directive_foreach_ds { typedef struct directive_foreach_ds {
size_t depth; size_t depth;

View File

@ -52,6 +52,27 @@
# include <pthread.h> # include <pthread.h>
#endif #endif
#if defined(HAVE_STRNCASECMP) && defined(HAVE_STRINGS_H)
# include <strings.h>
#else /* If strncasecmp() isn't available use this one */
static inline int
strncasecmp(const char *s1, const char *s2, size_t n)
{
assert(s1 && s2);
if (n != 0) {
const unsigned char *us1 = (const unsigned char *) s1,
const unsigned char *us2 = (const unsigned char *) s2;
do {
if (tolower(*us1) != tolower(*us2++))
return (tolower(*us1) - tolower(*--us2));
if (*us1++ == '\0')
break;
} while (--n != 0);
}
return 0;
}
#endif
typedef struct cfuhash_event_flags { typedef struct cfuhash_event_flags {
int resized:1; int resized:1;
@ -89,8 +110,8 @@ struct cfuhash_table {
static u_int32_t static u_int32_t
hash_func(const void *key, size_t length) { hash_func(const void *key, size_t length) {
register size_t i = length; register size_t i = length;
register u_int hv = 0; /* could put a seed here instead of zero */ register unsigned int hv = 0; /* could put a seed here instead of zero */
register const unsigned char *s = (char *)key; register const unsigned char *s = (unsigned char *)key;
while (i--) { while (i--) {
hv += *s++; hv += *s++;
hv += (hv << 10); hv += (hv << 10);
@ -104,9 +125,9 @@ hash_func(const void *key, size_t length) {
} }
/* makes sure the real size of the buckets array is a power of 2 */ /* makes sure the real size of the buckets array is a power of 2 */
static u_int static unsigned int
hash_size(u_int s) { hash_size(unsigned int s) {
u_int i = 1; unsigned int i = 1;
while (i < s) i <<= 1; while (i < s) i <<= 1;
return i; return i;
} }
@ -127,9 +148,9 @@ hash_key_dup_lower_case(const void *key, size_t key_size) {
} }
/* returns the index into the buckets array */ /* returns the index into the buckets array */
static inline u_int static inline unsigned int
hash_value(cfuhash_table_t *ht, const void *key, size_t key_size, size_t num_buckets) { hash_value(cfuhash_table_t *ht, const void *key, size_t key_size, size_t num_buckets) {
u_int hv = 0; unsigned int hv = 0;
if (key) { if (key) {
if (ht->flags & CFUHASH_IGNORE_CASE) { if (ht->flags & CFUHASH_IGNORE_CASE) {
@ -320,7 +341,7 @@ cfuhash_unlock(cfuhash_table_t *ht) {
/* uses the convention that zero means a match, like memcmp */ /* uses the convention that zero means a match, like memcmp */
static inline int static inline int
hash_cmp(const void *key, size_t key_size, cfuhash_entry *he, u_int case_insensitive) { hash_cmp(const void *key, size_t key_size, cfuhash_entry *he, unsigned int case_insensitive) {
if (key_size != he->key_size) return 1; if (key_size != he->key_size) return 1;
if (key == he->key) return 0; if (key == he->key) return 0;
if (case_insensitive) { if (case_insensitive) {
@ -330,7 +351,7 @@ hash_cmp(const void *key, size_t key_size, cfuhash_entry *he, u_int case_insensi
} }
static inline cfuhash_entry * static inline cfuhash_entry *
hash_add_entry(cfuhash_table_t *ht, u_int hv, const void *key, size_t key_size, hash_add_entry(cfuhash_table_t *ht, unsigned int hv, const void *key, size_t key_size,
void *data, size_t data_size) { void *data, size_t data_size) {
cfuhash_entry *he = (cfuhash_entry *)calloc(1, sizeof(cfuhash_entry)); cfuhash_entry *he = (cfuhash_entry *)calloc(1, sizeof(cfuhash_entry));
@ -357,7 +378,7 @@ hash_add_entry(cfuhash_table_t *ht, u_int hv, const void *key, size_t key_size,
extern int extern int
cfuhash_get_data(cfuhash_table_t *ht, const void *key, size_t key_size, void **r, cfuhash_get_data(cfuhash_table_t *ht, const void *key, size_t key_size, void **r,
size_t *data_size) { size_t *data_size) {
u_int hv = 0; unsigned int hv = 0;
cfuhash_entry *hr = NULL; cfuhash_entry *hr = NULL;
if (!ht) return 0; if (!ht) return 0;
@ -424,7 +445,7 @@ cfuhash_exists(cfuhash_table_t *ht, const char *key) {
extern int extern int
cfuhash_put_data(cfuhash_table_t *ht, const void *key, size_t key_size, void *data, cfuhash_put_data(cfuhash_table_t *ht, const void *key, size_t key_size, void *data,
size_t data_size, void **r) { size_t data_size, void **r) {
u_int hv = 0; unsigned int hv = 0;
cfuhash_entry *he = NULL; cfuhash_entry *he = NULL;
int added_an_entry = 0; int added_an_entry = 0;
@ -513,7 +534,7 @@ cfuhash_clear(cfuhash_table_t *ht) {
extern void * extern void *
cfuhash_delete_data(cfuhash_table_t *ht, const void *key, size_t key_size) { cfuhash_delete_data(cfuhash_table_t *ht, const void *key, size_t key_size) {
u_int hv = 0; unsigned int hv = 0;
cfuhash_entry *he = NULL; cfuhash_entry *he = NULL;
cfuhash_entry *hep = NULL; cfuhash_entry *hep = NULL;
void *r = NULL; void *r = NULL;
@ -826,7 +847,7 @@ cfuhash_rehash(cfuhash_table_t *ht) {
cfuhash_entry *he = ht->buckets[i]; cfuhash_entry *he = ht->buckets[i];
while (he) { while (he) {
cfuhash_entry *nhe = he->next; cfuhash_entry *nhe = he->next;
u_int hv = hash_value(ht, he->key, he->key_size, new_size); unsigned int hv = hash_value(ht, he->key, he->key_size, new_size);
he->next = new_buckets[hv]; he->next = new_buckets[hv];
new_buckets[hv] = he; new_buckets[hv] = he;
he = nhe; he = nhe;

View File

@ -256,7 +256,6 @@ __cfustring_split_to_raw(cfustring_t *cfu_str, size_t *num_strings, size_t num_s
char **ret_strings = (char **)calloc(2, sizeof(char *)); char **ret_strings = (char **)calloc(2, sizeof(char *));
unsigned int max_ret_strings = 2; unsigned int max_ret_strings = 2;
unsigned int used_ret_strings = 0; unsigned int used_ret_strings = 0;
char *start = NULL;
char *end = NULL; char *end = NULL;
char *next_str = NULL; char *next_str = NULL;
int last = 0; int last = 0;
@ -274,7 +273,7 @@ __cfustring_split_to_raw(cfustring_t *cfu_str, size_t *num_strings, size_t num_s
sep_array[i] = _dup_str(sep); sep_array[i] = _dup_str(sep);
} }
start = end = cfustring_get_buffer(cfu_str); end = cfustring_get_buffer(cfu_str);
if (!end) { if (!end) {
*num_strings = 0; *num_strings = 0;
free(ret_strings); free(ret_strings);

View File

@ -88,7 +88,7 @@ _run_queue(void *arg) {
tq->init_fn(tq->init_arg); tq->init_fn(tq->init_arg);
} }
pthread_cleanup_push((void *)tq->cleanup_fn, tq->cleanup_arg); pthread_cleanup_push(tq->cleanup_fn, tq->cleanup_arg);
while (1) { while (1) {
pthread_mutex_lock(&tq->mutex); pthread_mutex_lock(&tq->mutex);

View File

@ -43,12 +43,11 @@
#include <assert.h> #include <assert.h>
#include <sys/time.h> #include <sys/time.h>
typedef struct cfutime { typedef struct cfutime {
libcfu_type type; libcfu_type type;
struct timeval begin_tv; struct timeval begin_tv;
struct timezone begin_tz;
struct timeval end_tv; struct timeval end_tv;
struct timezone end_tz;
} cfutime; } cfutime;
extern cfutime_t * extern cfutime_t *
@ -60,12 +59,12 @@ cfutime_new() {
extern void extern void
cfutime_begin(cfutime_t *time) { cfutime_begin(cfutime_t *time) {
gettimeofday(&(time->begin_tv), &(time->begin_tz)); gettimeofday(&(time->begin_tv), NULL);
} }
extern void extern void
cfutime_end(cfutime_t *time) { cfutime_end(cfutime_t *time) {
gettimeofday(&(time->end_tv), &(time->end_tz)); gettimeofday(&(time->end_tv), NULL);
} }
extern double extern double