# HG changeset patch # User Mike Becker # Date 1500144378 -7200 # Node ID 98dc2d3a9b1de03160a3fba93203275e93c094f5 # Parent 2e74828c5e94cc4f5f210f859f874030302eaf13 adds const qualifiers to compare, distance and copy function signatures diff -r 2e74828c5e94 -r 98dc2d3a9b1d test/avl_tests.c --- a/test/avl_tests.c Sat Jul 15 19:20:06 2017 +0200 +++ b/test/avl_tests.c Sat Jul 15 20:46:18 2017 +0200 @@ -224,7 +224,7 @@ ucx_avl_free(tree4); } -static intmax_t dist_int(void* a, void* b, void* n) { +static intmax_t dist_int(const void* a, const void* b, void* n) { return ((intmax_t)a)-((intmax_t)b); } diff -r 2e74828c5e94 -r 98dc2d3a9b1d ucx/ucx.h --- a/ucx/ucx.h Sat Jul 15 19:20:06 2017 +0200 +++ b/ucx/ucx.h Sat Jul 15 20:46:18 2017 +0200 @@ -87,7 +87,7 @@ * and 0 if both arguments are equal. If the third argument is * NULL, it shall be ignored. */ -typedef int(*cmp_func)(void*,void*,void*); +typedef int(*cmp_func)(const void*,const void*,void*); /** * Function pointer to a distance function. @@ -96,7 +96,7 @@ * the distance shall be computed and optional additional data. * The function shall then return the signed distance as integer value. */ -typedef intmax_t(*distance_func)(void*,void*,void*); +typedef intmax_t(*distance_func)(const void*,const void*,void*); /** * Function pointer to a copy function. @@ -109,7 +109,7 @@ * passed to free() depends on the implementation of the * respective copy_func. */ -typedef void*(*copy_func)(void*,void*); +typedef void*(*copy_func)(const void*,void*); /** * Function pointer to a write function. diff -r 2e74828c5e94 -r 98dc2d3a9b1d ucx/utils.c --- a/ucx/utils.c Sat Jul 15 19:20:06 2017 +0200 +++ b/ucx/utils.c Sat Jul 15 20:46:18 2017 +0200 @@ -33,15 +33,15 @@ #include /* COPY FUCNTIONS */ -void* ucx_strcpy(void* s, void* data) { - char *str = (char*) s; +void* ucx_strcpy(const void* s, void* data) { + const char *str = (const char*) s; size_t n = 1+strlen(str); char *cpy = (char*) malloc(n); memcpy(cpy, str, n); return cpy; } -void* ucx_memcpy(void* m, void* n) { +void* ucx_memcpy(const void* m, void* n) { size_t k = *((size_t*)n); void *cpy = malloc(k); memcpy(cpy, m, k); @@ -87,17 +87,17 @@ /* COMPARE FUNCTIONS */ -int ucx_strcmp(void *s1, void *s2, void *data) { - return strcmp((char*)s1, (char*)s2); +int ucx_strcmp(const void *s1, const void *s2, void *data) { + return strcmp((const char*)s1, (const char*)s2); } -int ucx_strncmp(void *s1, void *s2, void *n) { - return strncmp((char*)s1, (char*)s2, *((size_t*) n)); +int ucx_strncmp(const void *s1, const void *s2, void *n) { + return strncmp((const char*)s1, (const char*)s2, *((size_t*) n)); } -int ucx_intcmp(void *i1, void *i2, void *data) { - int a = *((int*) i1); - int b = *((int*) i2); +int ucx_intcmp(const void *i1, const void *i2, void *data) { + int a = *((const int*) i1); + int b = *((const int*) i2); if (a == b) { return 0; } else { @@ -105,9 +105,9 @@ } } -int ucx_floatcmp(void *f1, void *f2, void *epsilon) { - float a = *((float*) f1); - float b = *((float*) f2); +int ucx_floatcmp(const void *f1, const void *f2, void *epsilon) { + float a = *((const float*) f1); + float b = *((const float*) f2); float e = !epsilon ? 1e-6f : *((float*)epsilon); if (fabsf(a - b) < e) { return 0; @@ -116,9 +116,9 @@ } } -int ucx_doublecmp(void *d1, void *d2, void *epsilon) { - double a = *((float*) d1); - double b = *((float*) d2); +int ucx_doublecmp(const void *d1, const void *d2, void *epsilon) { + double a = *((const double*) d1); + double b = *((const double*) d2); double e = !epsilon ? 1e-14 : *((double*)epsilon); if (fabs(a - b) < e) { return 0; @@ -127,9 +127,9 @@ } } -int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) { - intptr_t p1 = (intptr_t) ptr1; - intptr_t p2 = (intptr_t) ptr2; +int ucx_ptrcmp(const void *ptr1, const void *ptr2, void *data) { + const intptr_t p1 = (const intptr_t) ptr1; + const intptr_t p2 = (const intptr_t) ptr2; if (p1 == p2) { return 0; } else { @@ -137,7 +137,7 @@ } } -int ucx_memcmp(void *ptr1, void *ptr2, void *n) { +int ucx_memcmp(const void *ptr1, const void *ptr2, void *n) { return memcmp(ptr1, ptr2, *((size_t*)n)); } diff -r 2e74828c5e94 -r 98dc2d3a9b1d ucx/utils.h --- a/ucx/utils.h Sat Jul 15 19:20:06 2017 +0200 +++ b/ucx/utils.h Sat Jul 15 20:46:18 2017 +0200 @@ -60,7 +60,7 @@ * @param data omitted * @return a pointer to a copy of s1 that can be passed to free(void*) */ -void *ucx_strcpy(void *s, void *data); +void *ucx_strcpy(const void *s, void *data); /** * Copies a memory area. @@ -69,7 +69,7 @@ * @return a pointer to a copy of the specified memory area that can * be passed to free(void*) */ -void *ucx_memcpy(void *m, void *n); +void *ucx_memcpy(const void *m, void *n); /** @@ -144,7 +144,7 @@ * @param data omitted * @return the result of strcmp(s1, s2) */ -int ucx_strcmp(void *s1, void *s2, void *data); +int ucx_strcmp(const void *s1, const void *s2, void *data); /** * Wraps the strncmp function. @@ -153,7 +153,7 @@ * @param n a pointer to the size_t containing the third strncmp parameter * @return the result of strncmp(s1, s2, *n) */ -int ucx_strncmp(void *s1, void *s2, void *n); +int ucx_strncmp(const void *s1, const void *s2, void *n); /** * Compares two integers of type int. @@ -163,7 +163,7 @@ * @return -1, if *i1 is less than *i2, 0 if both are equal, * 1 if *i1 is greater than *i2 */ -int ucx_intcmp(void *i1, void *i2, void *data); +int ucx_intcmp(const void *i1, const void *i2, void *data); /** * Compares two real numbers of type float. @@ -174,7 +174,7 @@ * 1 if *f1 is greater than *f2 */ -int ucx_floatcmp(void *f1, void *f2, void *data); +int ucx_floatcmp(const void *f1, const void *f2, void *data); /** * Compares two real numbers of type double. @@ -184,7 +184,7 @@ * @return -1, if *d1 is less than *d2, 0 if both are equal, * 1 if *d1 is greater than *d2 */ -int ucx_doublecmp(void *d1, void *d2, void *data); +int ucx_doublecmp(const void *d1, const void *d2, void *data); /** * Compares two pointers. @@ -194,7 +194,7 @@ * @return -1 if ptr1 is less than ptr2, 0 if both are equal, * 1 if ptr1 is greater than ptr2 */ -int ucx_ptrcmp(void *ptr1, void *ptr2, void *data); +int ucx_ptrcmp(const void *ptr1, const void *ptr2, void *data); /** * Compares two memory areas. @@ -203,7 +203,7 @@ * @param n a pointer to the size_t containing the third parameter for memcmp * @return the result of memcmp(ptr1, ptr2, *n) */ -int ucx_memcmp(void *ptr1, void *ptr2, void *n); +int ucx_memcmp(const void *ptr1, const void *ptr2, void *n); /** * A printf() like function which writes the output to a stream by