diff options
author | Stef Walter <stefw@gnome.org> | 2013-01-23 14:29:25 +0100 |
---|---|---|
committer | Stef Walter <stefw@gnome.org> | 2013-01-23 14:29:25 +0100 |
commit | b28c936bd281c4b7ff9ed0f621b840f6d5a4b328 (patch) | |
tree | 9645b90b794908d378970aafd73e7726c5267341 /common | |
parent | 4671352fe2a4f56c6707322dcab0015e2e8600c4 (diff) |
Use the stdbool.h C99 bool type
It was getting really wild knowing whether a function returning
an int would return -1 on failure or 0 or whether the int return
value was actually a number etc..
Diffstat (limited to 'common')
-rw-r--r-- | common/array.c | 16 | ||||
-rw-r--r-- | common/array.h | 4 | ||||
-rw-r--r-- | common/attrs.c | 12 | ||||
-rw-r--r-- | common/compat.h | 8 | ||||
-rw-r--r-- | common/debug.c | 8 | ||||
-rw-r--r-- | common/debug.h | 14 | ||||
-rw-r--r-- | common/dict.c | 41 | ||||
-rw-r--r-- | common/dict.h | 26 | ||||
-rw-r--r-- | common/library.c | 4 | ||||
-rw-r--r-- | common/tests/test-dict.c | 90 |
10 files changed, 119 insertions, 104 deletions
diff --git a/common/array.c b/common/array.c index f645fd1..f2930a9 100644 --- a/common/array.c +++ b/common/array.c @@ -33,11 +33,12 @@ #include "config.h" #include "array.h" +#include "debug.h" #include <stdlib.h> #include <string.h> -static int +static bool maybe_expand_array (p11_array *array, unsigned int length) { @@ -45,19 +46,18 @@ maybe_expand_array (p11_array *array, void **new_memory; if (length <= array->allocated) - return 1; + return true; new_allocated = array->allocated + 16; if (new_allocated < length) new_allocated = length; new_memory = realloc (array->elem, new_allocated * sizeof (void*)); - if (new_memory == NULL) - return 0; + return_val_if_fail (new_memory != NULL, false); array->elem = new_memory; array->allocated = new_allocated; - return 1; + return true; } p11_array * @@ -95,16 +95,16 @@ p11_array_free (p11_array *array) free (array); } -int +bool p11_array_push (p11_array *array, void *value) { if (!maybe_expand_array (array, array->num + 1)) - return 0; + return_val_if_reached (false); array->elem[array->num] = value; array->num++; - return 1; + return true; } void diff --git a/common/array.h b/common/array.h index 6ed7866..8964b83 100644 --- a/common/array.h +++ b/common/array.h @@ -35,7 +35,7 @@ #ifndef __P11_ARRAY_H__ #define __P11_ARRAY_H__ -#include <sys/types.h> +#include "compat.h" #ifndef P11_DESTROYER_DEFINED #define P11_DESTROYER_DEFINED @@ -57,7 +57,7 @@ p11_array * p11_array_new (p11_destroyer destroyer); void p11_array_free (p11_array *array); -int p11_array_push (p11_array *array, +bool p11_array_push (p11_array *array, void *value); void p11_array_remove (p11_array *array, diff --git a/common/attrs.c b/common/attrs.c index 2f4dca7..dd91afc 100644 --- a/common/attrs.c +++ b/common/attrs.c @@ -56,7 +56,7 @@ p11_attrs_count (CK_ATTRIBUTE *attrs) CK_ULONG count; if (attrs == NULL) - return 0; + return 0UL; for (count = 0; !p11_attrs_is_empty (attrs); count++, attrs++); @@ -80,7 +80,7 @@ p11_attrs_free (void *attrs) static CK_ATTRIBUTE * attrs_build (CK_ATTRIBUTE *attrs, CK_ULONG count_to_add, - int copy, + bool copy, CK_ATTRIBUTE * (*generator) (void *), void *state) { @@ -147,14 +147,14 @@ p11_attrs_build (CK_ATTRIBUTE *attrs, CK_ULONG count; va_list va; - count = 0; + count = 0UL; va_start (va, attrs); while (va_arg (va, CK_ATTRIBUTE *)) count++; va_end (va); va_start (va, attrs); - attrs = attrs_build (attrs, count, 1, vararg_generator, va); + attrs = attrs_build (attrs, count, true, vararg_generator, va); va_end (va); return attrs; @@ -172,7 +172,7 @@ p11_attrs_buildn (CK_ATTRIBUTE *attrs, CK_ATTRIBUTE *add, CK_ULONG count) { - return attrs_build (attrs, count, 1, template_generator, &add); + return attrs_build (attrs, count, true, template_generator, &add); } CK_ATTRIBUTE * @@ -183,7 +183,7 @@ p11_attrs_take (CK_ATTRIBUTE *attrs, { CK_ATTRIBUTE attr = { type, value, length }; CK_ATTRIBUTE *add = &attr; - return attrs_build (attrs, 1, 0, template_generator, &add); + return attrs_build (attrs, 1, false, template_generator, &add); } CK_ATTRIBUTE * diff --git a/common/compat.h b/common/compat.h index 5061b9f..12b038d 100644 --- a/common/compat.h +++ b/common/compat.h @@ -39,6 +39,8 @@ #include <sys/types.h> +typedef enum { false, true } bool; + #if !defined(__cplusplus) && (__GNUC__ > 2) #define GNUC_PRINTF(x, y) __attribute__((__format__(__printf__, x, y))) #else @@ -203,4 +205,10 @@ void * memdup (void *data, #endif /* HAVE_MEMDUP */ +#ifdef HAVE_STDBOOL_H +#include <stdbool.h> +#else +typedef enum { false, true } bool; +#endif + #endif /* __COMPAT_H__ */ diff --git a/common/debug.c b/common/debug.c index cca9aaf..2728bf6 100644 --- a/common/debug.c +++ b/common/debug.c @@ -58,8 +58,8 @@ static struct DebugKey debug_keys[] = { { 0, } }; -static int debug_inited = 0; -static int debug_strict = 0; +static bool debug_inited = false; +static bool debug_strict = false; /* global variable exported in debug.h */ int p11_debug_current_flags = ~0; @@ -75,7 +75,7 @@ parse_environ_flags (void) env = getenv ("P11_KIT_STRICT"); if (env && env[0] != '\0') - debug_strict = 1; + debug_strict = true; env = getenv ("P11_KIT_DEBUG"); if (!env) @@ -117,7 +117,7 @@ void p11_debug_init (void) { p11_debug_current_flags = parse_environ_flags (); - debug_inited = 1; + debug_inited = true; } void diff --git a/common/debug.h b/common/debug.h index e4759d4..2374b6f 100644 --- a/common/debug.h +++ b/common/debug.h @@ -57,31 +57,31 @@ void p11_debug_precond (const char *format, ...) GNUC_PRINTF (1, 2); #define assert_not_reached() \ - (assert (0 && "this code should not be reached")) + (assert (false && "this code should not be reached")) #define return_val_if_fail(x, v) \ do { if (!(x)) { \ p11_debug_precond ("p11-kit: '%s' not true at %s\n", #x, __func__); \ return v; \ - } } while (0) + } } while (false) #define return_if_fail(x) \ do { if (!(x)) { \ p11_debug_precond ("p11-kit: '%s' not true at %s\n", #x, __func__); \ return; \ - } } while (0) + } } while (false) #define return_if_reached() \ do { \ p11_debug_precond ("p11-kit: shouldn't be reached at %s\n", __func__); \ return; \ - } while (0) + } while (false) #define return_val_if_reached(v) \ do { \ p11_debug_precond ("p11-kit: shouldn't be reached at %s\n", __func__); \ return v; \ - } while (0) + } while (false) #endif /* DEBUG_H */ @@ -119,10 +119,10 @@ void p11_debug_precond (const char *format, #undef p11_debug #define p11_debug(format, ...) \ - do {} while (0) + do {} while (false) #undef p11_debugging -#define p11_debugging 0 +#define p11_debugging (0) #endif /* !defined (WITH_DEBUG) */ diff --git a/common/dict.c b/common/dict.c index 2f976c1..df8c7ac 100644 --- a/common/dict.c +++ b/common/dict.c @@ -33,6 +33,7 @@ #include "config.h" +#include "debug.h" #include "dict.h" #include <sys/types.h> @@ -73,19 +74,19 @@ next_entry (p11_dictiter *iter) } -int +bool p11_dict_next (p11_dictiter *iter, void **key, void **value) { dictbucket *bucket = next_entry (iter); if (bucket == NULL) - return 0; + return false; if (key) *key = bucket->key; if (value) *value = bucket->value; - return 1; + return true; } void @@ -100,7 +101,7 @@ p11_dict_iterate (p11_dict *dict, static dictbucket ** lookup_or_create_bucket (p11_dict *dict, const void *key, - int create) + bool create) { dictbucket **bucketp; unsigned int hash; @@ -136,14 +137,14 @@ p11_dict_get (p11_dict *dict, { dictbucket **bucketp; - bucketp = lookup_or_create_bucket (dict, key, 0); + bucketp = lookup_or_create_bucket (dict, key, false); if (bucketp && *bucketp) return (void*)((*bucketp)->value); else return NULL; } -int +bool p11_dict_set (p11_dict *dict, void *key, void *val) @@ -154,7 +155,7 @@ p11_dict_set (p11_dict *dict, dictbucket **new_buckets; unsigned int num_buckets; - bucketp = lookup_or_create_bucket (dict, key, 1); + bucketp = lookup_or_create_bucket (dict, key, true); if(bucketp && *bucketp) { /* Destroy the previous key */ @@ -189,13 +190,13 @@ p11_dict_set (p11_dict *dict, } } - return 1; + return true; } - return 0; + return_val_if_reached (false); } -int +bool p11_dict_steal (p11_dict *dict, const void *key, void **stolen_key, @@ -203,7 +204,7 @@ p11_dict_steal (p11_dict *dict, { dictbucket **bucketp; - bucketp = lookup_or_create_bucket (dict, key, 0); + bucketp = lookup_or_create_bucket (dict, key, false); if (bucketp && *bucketp) { dictbucket *old = *bucketp; *bucketp = (*bucketp)->next; @@ -213,14 +214,14 @@ p11_dict_steal (p11_dict *dict, if (stolen_value) *stolen_value = old->value; free (old); - return 1; + return true; } - return 0; + return false; } -int +bool p11_dict_remove (p11_dict *dict, const void *key) { @@ -228,13 +229,13 @@ p11_dict_remove (p11_dict *dict, void *old_value; if (!p11_dict_steal (dict, key, &old_key, &old_value)) - return 0; + return false; if (dict->key_destroy_func) dict->key_destroy_func (old_key); if (dict->value_destroy_func) dict->value_destroy_func (old_value); - return 1; + return true; } void @@ -335,7 +336,7 @@ p11_dict_str_hash (const void *string) return hash; } -int +bool p11_dict_str_equal (const void *string_one, const void *string_two) { @@ -352,7 +353,7 @@ p11_dict_ulongptr_hash (const void *to_ulong) return (unsigned int)*((unsigned long*)to_ulong); } -int +bool p11_dict_ulongptr_equal (const void *ulong_one, const void *ulong_two) { @@ -368,7 +369,7 @@ p11_dict_intptr_hash (const void *to_int) return (unsigned int)*((int*)to_int); } -int +bool p11_dict_intptr_equal (const void *int_one, const void *int_two) { @@ -383,7 +384,7 @@ p11_dict_direct_hash (const void *ptr) return (unsigned int)(size_t)ptr; } -int +bool p11_dict_direct_equal (const void *ptr_one, const void *ptr_two) { diff --git a/common/dict.h b/common/dict.h index 1ba7185..e13c5d6 100644 --- a/common/dict.h +++ b/common/dict.h @@ -36,7 +36,7 @@ #ifndef P11_DICT_H_ #define P11_DICT_H_ -#include <sys/types.h> +#include "compat.h" /* * ARGUMENT DOCUMENTATION @@ -64,7 +64,7 @@ typedef struct _p11_dictiter { typedef unsigned int (*p11_dict_hasher) (const void *data); -typedef int (*p11_dict_equals) (const void *one, +typedef bool (*p11_dict_equals) (const void *one, const void *two); #ifndef P11_DESTROYER_DEFINED @@ -107,25 +107,25 @@ void* p11_dict_get (p11_dict *dict, /* * p11_dict_set: Set a value in the hash table - * - returns 1 if the entry was added properly + * - returns true if the entry was added properly */ -int p11_dict_set (p11_dict *dict, +bool p11_dict_set (p11_dict *dict, void *key, void *value); /* * p11_dict_remove: Remove a value from the hash table - * - returns 1 if the entry was found + * - returns true if the entry was found */ -int p11_dict_remove (p11_dict *dict, +bool p11_dict_remove (p11_dict *dict, const void *key); /* * p11_dict_steal: Remove a value from the hash table without calling * destroy funcs - * - returns 1 if the entry was found + * - returns true if the entry was found */ -int p11_dict_steal (p11_dict *dict, +bool p11_dict_steal (p11_dict *dict, const void *key, void **stolen_key, void **stolen_value); @@ -142,7 +142,7 @@ void p11_dict_iterate (p11_dict *dict, * - sets key and value to key and/or value * - returns whether there was another entry */ -int p11_dict_next (p11_dictiter *iter, +bool p11_dict_next (p11_dictiter *iter, void **key, void **value); @@ -157,22 +157,22 @@ void p11_dict_clear (p11_dict *dict); unsigned int p11_dict_str_hash (const void *string); -int p11_dict_str_equal (const void *string_one, +bool p11_dict_str_equal (const void *string_one, const void *string_two); unsigned int p11_dict_ulongptr_hash (const void *to_ulong); -int p11_dict_ulongptr_equal (const void *ulong_one, +bool p11_dict_ulongptr_equal (const void *ulong_one, const void *ulong_two); unsigned int p11_dict_intptr_hash (const void *to_int); -int p11_dict_intptr_equal (const void *int_one, +bool p11_dict_intptr_equal (const void *int_one, const void *int_two); unsigned int p11_dict_direct_hash (const void *ptr); -int p11_dict_direct_equal (const void *ptr_one, +bool p11_dict_direct_equal (const void *ptr_one, const void *ptr_two); #endif /* __P11_DICT_H__ */ diff --git a/common/library.c b/common/library.c index 48fd0c9..d0c246c 100644 --- a/common/library.c +++ b/common/library.c @@ -65,7 +65,7 @@ p11_mutex_t p11_library_mutex; pthread_once_t p11_library_once; #endif -static int print_messages = 1; +static bool print_messages = true; void p11_message_store (const char* msg, @@ -112,7 +112,7 @@ void p11_message_quiet (void) { p11_lock (); - print_messages = 0; + print_messages = false; p11_unlock (); } diff --git a/common/tests/test-dict.c b/common/tests/test-dict.c index 3af3daa..00b64c5 100644 --- a/common/tests/test-dict.c +++ b/common/tests/test-dict.c @@ -60,7 +60,7 @@ test_free_null (CuTest *tc) typedef struct { int value; - int freed; + bool freed; } Key; static unsigned int @@ -71,7 +71,7 @@ key_hash (const void *ptr) return p11_dict_intptr_hash (&k->value); } -static int +static bool key_equal (const void *one, const void *two) { @@ -87,7 +87,7 @@ key_destroy (void *data) { Key *k = data; assert (!k->freed); - k->freed = 1; + k->freed = true; } static void @@ -110,7 +110,7 @@ test_free_destroys (CuTest *tc) CuFail (tc, "should not be reached"); p11_dict_free (map); - CuAssertIntEquals (tc, 1, key.freed); + CuAssertIntEquals (tc, true, key.freed); CuAssertIntEquals (tc, 2, value); } @@ -166,7 +166,7 @@ test_set_get_remove (CuTest *tc) char *value = "VALUE"; char *check; p11_dict *map; - int ret; + bool ret; map = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, NULL, NULL); @@ -177,9 +177,9 @@ test_set_get_remove (CuTest *tc) CuAssertPtrEquals (tc, check, value); ret = p11_dict_remove (map, key); - CuAssertIntEquals (tc, ret, 1); + CuAssertIntEquals (tc, true, ret); ret = p11_dict_remove (map, key); - CuAssertIntEquals (tc, ret, 0); + CuAssertIntEquals (tc, false, ret); check = p11_dict_get (map, key); CuAssert (tc, "should be null", check == NULL); @@ -214,7 +214,7 @@ test_remove_destroys (CuTest *tc) p11_dict *map; Key key = { 8, 0 }; int value = 0; - int ret; + bool ret; map = p11_dict_new (key_hash, key_equal, key_destroy, value_destroy); CuAssertPtrNotNull (tc, map); @@ -222,26 +222,26 @@ test_remove_destroys (CuTest *tc) CuFail (tc, "should not be reached"); ret = p11_dict_remove (map, &key); - CuAssertIntEquals (tc, ret, 1); - CuAssertIntEquals (tc, 1, key.freed); + CuAssertIntEquals (tc, true, ret); + CuAssertIntEquals (tc, true, key.freed); CuAssertIntEquals (tc, 2, value); /* should not be destroyed again */ - key.freed = 0; + key.freed = false; value = 0; ret = p11_dict_remove (map, &key); - CuAssertIntEquals (tc, ret, 0); - CuAssertIntEquals (tc, 0, key.freed); + CuAssertIntEquals (tc, false, ret); + CuAssertIntEquals (tc, false, key.freed); CuAssertIntEquals (tc, 0, value); /* should not be destroyed again */ - key.freed = 0; + key.freed = false; value = 0; p11_dict_free (map); - CuAssertIntEquals (tc, 0, key.freed); + CuAssertIntEquals (tc, false, key.freed); CuAssertIntEquals (tc, 0, value); } @@ -252,59 +252,64 @@ test_set_destroys (CuTest *tc) Key key = { 8, 0 }; Key key2 = { 8, 0 }; int value, value2; - int ret; + bool ret; map = p11_dict_new (key_hash, key_equal, key_destroy, value_destroy); CuAssertPtrNotNull (tc, map); if (!p11_dict_set (map, &key, &value)) CuFail (tc, "should not be reached"); - key.freed = key2.freed = value = value2 = 0; + key.freed = key2.freed = false; + value = value2 = 0; /* Setting same key and value, should not be destroyed */ ret = p11_dict_set (map, &key, &value); - CuAssertIntEquals (tc, ret, 1); - CuAssertIntEquals (tc, 0, key.freed); - CuAssertIntEquals (tc, 0, key2.freed); + CuAssertIntEquals (tc, true, ret); + CuAssertIntEquals (tc, false, key.freed); + CuAssertIntEquals (tc, false, key2.freed); CuAssertIntEquals (tc, 0, value); CuAssertIntEquals (tc, 0, value2); - key.freed = key2.freed = value = value2 = 0; + key.freed = key2.freed = false; + value = value2 = 0; /* Setting a new key same value, key should be destroyed */ ret = p11_dict_set (map, &key2, &value); - CuAssertIntEquals (tc, ret, 1); - CuAssertIntEquals (tc, 1, key.freed); - CuAssertIntEquals (tc, 0, key2.freed); + CuAssertIntEquals (tc, true, ret); + CuAssertIntEquals (tc, true, key.freed); + CuAssertIntEquals (tc, false, key2.freed); CuAssertIntEquals (tc, 0, value); CuAssertIntEquals (tc, 0, value2); - key.freed = key2.freed = value = value2 = 0; + key.freed = key2.freed = false; + value = value2 = 0; /* Setting same key, new value, value should be destroyed */ ret = p11_dict_set (map, &key2, &value2); - CuAssertIntEquals (tc, ret, 1); - CuAssertIntEquals (tc, 0, key.freed); - CuAssertIntEquals (tc, 0, key2.freed); + CuAssertIntEquals (tc, true, ret); + CuAssertIntEquals (tc, false, key.freed); + CuAssertIntEquals (tc, false, key2.freed); CuAssertIntEquals (tc, 2, value); CuAssertIntEquals (tc, 0, value2); - key.freed = key2.freed = value = value2 = 0; + key.freed = key2.freed = false; + value = value2 = 0; /* Setting new key new value, both should be destroyed */ ret = p11_dict_set (map, &key, &value); - CuAssertIntEquals (tc, ret, 1); - CuAssertIntEquals (tc, 0, key.freed); - CuAssertIntEquals (tc, 1, key2.freed); + CuAssertIntEquals (tc, true, ret); + CuAssertIntEquals (tc, false, key.freed); + CuAssertIntEquals (tc, true, key2.freed); CuAssertIntEquals (tc, 0, value); CuAssertIntEquals (tc, 2, value2); - key.freed = key2.freed = value = value2 = 0; + key.freed = key2.freed = false; + value = value2 = 0; p11_dict_free (map); - CuAssertIntEquals (tc, 1, key.freed); + CuAssertIntEquals (tc, true, key.freed); CuAssertIntEquals (tc, 2, value); - CuAssertIntEquals (tc, 0, key2.freed); + CuAssertIntEquals (tc, false, key2.freed); CuAssertIntEquals (tc, 0, value2); } @@ -322,24 +327,24 @@ test_clear_destroys (CuTest *tc) CuFail (tc, "should not be reached"); p11_dict_clear (map); - CuAssertIntEquals (tc, 1, key.freed); + CuAssertIntEquals (tc, true, key.freed); CuAssertIntEquals (tc, 2, value); /* should not be destroyed again */ - key.freed = 0; + key.freed = false; value = 0; p11_dict_clear (map); - CuAssertIntEquals (tc, 0, key.freed); + CuAssertIntEquals (tc, false, key.freed); CuAssertIntEquals (tc, 0, value); /* should not be destroyed again */ - key.freed = 0; + key.freed = false; value = 0; p11_dict_free (map); - CuAssertIntEquals (tc, 0, key.freed); + CuAssertIntEquals (tc, false, key.freed); CuAssertIntEquals (tc, 0, value); } @@ -381,7 +386,8 @@ test_hash_count (CuTest *tc) { p11_dict *map; int *value; - int i, ret; + int i; + bool ret; map = p11_dict_new (p11_dict_intptr_hash, p11_dict_intptr_equal, NULL, free); @@ -397,7 +403,7 @@ test_hash_count (CuTest *tc) for (i = 0; i < 20000; ++i) { ret = p11_dict_remove (map, &i); - CuAssertIntEquals (tc, 1, ret); + CuAssertIntEquals (tc, true, ret); CuAssertIntEquals (tc, 20000 - (i + 1), p11_dict_size (map)); } |