From e667ec23b94b908402de1ad18140af89802df332 Mon Sep 17 00:00:00 2001 From: endoffile78 Date: Thu, 5 Jan 2017 20:20:47 -0600 Subject: [PATCH 1/3] Replace make_quick_sort with qsort --- CMakeLists.txt | 1 - auto_tests/crypto_test.c | 7 ++-- toxcore/DHT.c | 1 - toxcore/Makefile.inc | 1 - toxcore/misc_tools.h | 70 ---------------------------------------- 5 files changed, 2 insertions(+), 78 deletions(-) delete mode 100644 toxcore/misc_tools.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ebd76a37a1..4d049bc898 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -301,7 +301,6 @@ target_link_modules(toxgroup toxmessenger) apidsl( toxcore/tox.api.h) add_module(toxcore - toxcore/misc_tools.h toxcore/tox_api.c toxcore/tox.c toxcore/tox.h) diff --git a/auto_tests/crypto_test.c b/auto_tests/crypto_test.c index 7c32b43a90..e433833e46 100644 --- a/auto_tests/crypto_test.c +++ b/auto_tests/crypto_test.c @@ -2,7 +2,6 @@ #include "config.h" #endif -#include "../toxcore/misc_tools.h" #include "../toxcore/net_crypto.h" #include @@ -343,9 +342,7 @@ END_TEST #define CRYPTO_TEST_MEMCMP_COUNT 500 #define CRYPTO_TEST_MEMCMP_EPS 10 -static make_quick_sort(clock_t) - -static int cmp(clock_t a, clock_t b) +static int cmp(const void *a, const void *b) { if (a < b) { return -1; @@ -374,7 +371,7 @@ static clock_t memcmp_median(void *a, void *b, size_t len) results[i] = memcmp_time(a, b, len); } - clock_t_quick_sort(results, CRYPTO_TEST_MEMCMP_COUNT, cmp); + qsort(results, CRYPTO_TEST_MEMCMP_COUNT, sizeof(results), cmp); return results[CRYPTO_TEST_MEMCMP_COUNT / 2]; } diff --git a/toxcore/DHT.c b/toxcore/DHT.c index abe7b1edd2..83a728c584 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -31,7 +31,6 @@ #include "LAN_discovery.h" #include "logger.h" -#include "misc_tools.h" #include "network.h" #include "ping.h" #include "util.h" diff --git a/toxcore/Makefile.inc b/toxcore/Makefile.inc index b81087f045..1d165522cc 100644 --- a/toxcore/Makefile.inc +++ b/toxcore/Makefile.inc @@ -49,7 +49,6 @@ libtoxcore_la_SOURCES = ../toxcore/DHT.h \ ../toxcore/TCP_connection.c \ ../toxcore/list.c \ ../toxcore/list.h \ - ../toxcore/misc_tools.h libtoxcore_la_CFLAGS = -I$(top_srcdir) \ -I$(top_srcdir)/toxcore \ diff --git a/toxcore/misc_tools.h b/toxcore/misc_tools.h deleted file mode 100644 index 543338b50b..0000000000 --- a/toxcore/misc_tools.h +++ /dev/null @@ -1,70 +0,0 @@ -/* misc_tools.h - * - * Miscellaneous functions and data structures for doing random things. - * - * Copyright (C) 2013 Tox project All Rights Reserved. - * - * This file is part of Tox. - * - * Tox is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Tox is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Tox. If not, see . - * - */ - -#ifndef MISC_TOOLS_H -#define MISC_TOOLS_H - -/****************************Algorithms*************************** - * Macro/generic definitions for useful algorithms - *****************************************************************/ - -/* Creates a new quick_sort implementation for arrays of the specified type. - * For a type T (eg: int, char), creates a function named T_quick_sort. - * - * Quick Sort: Complexity O(nlogn) - * arr - the array to sort - * n - the sort index (should be called with n = length(arr)) - * cmpfn - a function that compares two values of type type. - * Must return -1, 0, 1 for a < b, a == b, and a > b respectively. - */ -/* Must be called in the header file. */ -#define declare_quick_sort(type) \ -void type##_quick_sort(type *arr, int n, int (*cmpfn)(type, type)); - -/* Must be called in the C file. */ -#define make_quick_sort(type) \ -void type##_quick_sort(type *arr, int n, int (*cmpfn)(type, type)) \ -{ \ - if ((n) < 2) \ - return; \ - type _p_ = (arr)[(n) / 2]; \ - type *_l_ = (arr); \ - type *_r_ = (arr) + n - 1; \ - while (_l_ <= _r_) { \ - if (cmpfn(*_l_, _p_) == -1) { \ - ++_l_; \ - continue; \ - } \ - if (cmpfn(*_r_, _p_) == 1) { \ - --_r_; \ - continue; \ - } \ - type _t_ = *_l_; \ - *_l_++ = *_r_; \ - *_r_-- = _t_; \ - } \ - type##_quick_sort((arr), _r_ - (arr) + 1, cmpfn); \ - type##_quick_sort(_l_, (arr) + n - _l_, cmpfn); \ -} - -#endif // MISC_TOOLS_H From d6d14b54308b76bb5e7f47abd0c0b5757f1d9eb2 Mon Sep 17 00:00:00 2001 From: iphydf Date: Fri, 6 Jan 2017 13:59:59 +0000 Subject: [PATCH 2/3] Remove \ at end of list in makefile. --- toxcore/Makefile.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toxcore/Makefile.inc b/toxcore/Makefile.inc index 1d165522cc..f30e6c0648 100644 --- a/toxcore/Makefile.inc +++ b/toxcore/Makefile.inc @@ -48,7 +48,7 @@ libtoxcore_la_SOURCES = ../toxcore/DHT.h \ ../toxcore/TCP_connection.h \ ../toxcore/TCP_connection.c \ ../toxcore/list.c \ - ../toxcore/list.h \ + ../toxcore/list.h libtoxcore_la_CFLAGS = -I$(top_srcdir) \ -I$(top_srcdir)/toxcore \ From d41bb3a8d48b3acba6a5191cf72a7c577b8e1ea8 Mon Sep 17 00:00:00 2001 From: endoffile78 Date: Sat, 7 Jan 2017 11:38:31 -0600 Subject: [PATCH 3/3] Fix issues in crypto_test.c --- auto_tests/crypto_test.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/auto_tests/crypto_test.c b/auto_tests/crypto_test.c index e433833e46..57a2a43b16 100644 --- a/auto_tests/crypto_test.c +++ b/auto_tests/crypto_test.c @@ -344,11 +344,14 @@ END_TEST static int cmp(const void *a, const void *b) { - if (a < b) { + const clock_t *first = (const clock_t *) a; + const clock_t *second = (const clock_t *) b; + + if (*first < *second) { return -1; } - if (a > b) { + if (*first > *second) { return 1; } @@ -371,7 +374,7 @@ static clock_t memcmp_median(void *a, void *b, size_t len) results[i] = memcmp_time(a, b, len); } - qsort(results, CRYPTO_TEST_MEMCMP_COUNT, sizeof(results), cmp); + qsort(results, CRYPTO_TEST_MEMCMP_COUNT, sizeof(*results), cmp); return results[CRYPTO_TEST_MEMCMP_COUNT / 2]; }