diff options
Diffstat (limited to 'protocols/Tox/libtox/src/toxcore/util.c')
-rw-r--r-- | protocols/Tox/libtox/src/toxcore/util.c | 43 |
1 files changed, 38 insertions, 5 deletions
diff --git a/protocols/Tox/libtox/src/toxcore/util.c b/protocols/Tox/libtox/src/toxcore/util.c index 402977a680..1851e58a08 100644 --- a/protocols/Tox/libtox/src/toxcore/util.c +++ b/protocols/Tox/libtox/src/toxcore/util.c @@ -9,22 +9,22 @@ */ #ifndef _XOPEN_SOURCE #define _XOPEN_SOURCE 600 -#endif +#endif /* _XOPEN_SOURCE */ #include "util.h" #include <stdlib.h> #include <string.h> -#include <time.h> #include "ccompat.h" +#include "mem.h" bool is_power_of_2(uint64_t x) { return x != 0 && (x & (~x + 1)) == x; } -void free_uint8_t_pointer_array(uint8_t **ary, size_t n_items) +void free_uint8_t_pointer_array(const Memory *mem, uint8_t **ary, size_t n_items) { if (ary == nullptr) { return; @@ -32,11 +32,11 @@ void free_uint8_t_pointer_array(uint8_t **ary, size_t n_items) for (size_t i = 0; i < n_items; ++i) { if (ary[i] != nullptr) { - free(ary[i]); + mem_delete(mem, ary[i]); } } - free(ary); + mem_delete(mem, ary); } uint16_t data_checksum(const uint8_t *data, uint32_t length) @@ -81,6 +81,30 @@ bool memeq(const uint8_t *a, size_t a_size, const uint8_t *b, size_t b_size) return a_size == b_size && memcmp(a, b, a_size) == 0; } +uint8_t *memdup(const uint8_t *data, size_t data_size) +{ + if (data == nullptr || data_size == 0) { + return nullptr; + } + + uint8_t *copy = (uint8_t *)malloc(data_size); + + if (copy != nullptr) { + memcpy(copy, data, data_size); + } + + return copy; +} + +void memzero(uint8_t *data, size_t data_size) +{ + if (data == nullptr || data_size == 0) { + return; + } + + memset(data, 0, data_size); +} + int16_t max_s16(int16_t a, int16_t b) { return a > b ? a : b; @@ -107,6 +131,10 @@ int64_t min_s64(int64_t a, int64_t b) return a < b ? a : b; } +uint8_t max_u08(uint8_t a, uint8_t b) +{ + return a > b ? a : b; +} uint16_t max_u16(uint16_t a, uint16_t b) { return a > b ? a : b; @@ -133,6 +161,11 @@ uint64_t min_u64(uint64_t a, uint64_t b) return a < b ? a : b; } +int cmp_uint(uint64_t a, uint64_t b) +{ + return (a > b ? 1 : 0) - (a < b ? 1 : 0); +} + uint32_t jenkins_one_at_a_time_hash(const uint8_t *key, size_t len) { uint32_t hash = 0; |