Coverage Report

Created: 2023-11-22 10:24

/src/c-toxcore/toxcore/util.c
Line
Count
Source (jump to first uncovered line)
1
/* SPDX-License-Identifier: GPL-3.0-or-later
2
 * Copyright © 2016-2018 The TokTok team.
3
 * Copyright © 2013 Tox project.
4
 * Copyright © 2013 plutooo
5
 */
6
7
/**
8
 * Utilities.
9
 */
10
#ifndef _XOPEN_SOURCE
11
#define _XOPEN_SOURCE 600
12
#endif
13
14
#include "util.h"
15
16
#include <stdlib.h>
17
#include <string.h>
18
#include <time.h>
19
20
#include "ccompat.h"
21
22
bool is_power_of_2(uint64_t x)
23
0
{
24
0
    return x != 0 && (x & (~x + 1)) == x;
25
0
}
26
27
void free_uint8_t_pointer_array(const Memory *mem, uint8_t **ary, size_t n_items)
28
4.45k
{
29
4.45k
    if (ary == nullptr) {
30
4.14k
        return;
31
4.14k
    }
32
33
627
    for (size_t i = 0; i < n_items; ++i) {
34
316
        if (ary[i] != nullptr) {
35
316
            mem_delete(mem, ary[i]);
36
316
        }
37
316
    }
38
39
311
    mem_delete(mem, ary);
40
311
}
41
42
uint16_t data_checksum(const uint8_t *data, uint32_t length)
43
237
{
44
237
    uint8_t checksum[2] = {0};
45
237
    uint16_t check;
46
47
8.76k
    for (uint32_t i = 0; i < length; ++i) {
48
8.53k
        checksum[i % 2] ^= data[i];
49
8.53k
    }
50
51
237
    memcpy(&check, checksum, sizeof(check));
52
237
    return check;
53
237
}
54
55
int create_recursive_mutex(pthread_mutex_t *mutex)
56
3.08k
{
57
3.08k
    pthread_mutexattr_t attr;
58
59
3.08k
    if (pthread_mutexattr_init(&attr) != 0) {
60
0
        return -1;
61
0
    }
62
63
3.08k
    if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0) {
64
0
        pthread_mutexattr_destroy(&attr);
65
0
        return -1;
66
0
    }
67
68
    /* Create queue mutex */
69
3.08k
    if (pthread_mutex_init(mutex, &attr) != 0) {
70
0
        pthread_mutexattr_destroy(&attr);
71
0
        return -1;
72
0
    }
73
74
3.08k
    pthread_mutexattr_destroy(&attr);
75
76
3.08k
    return 0;
77
3.08k
}
78
79
bool memeq(const uint8_t *a, size_t a_size, const uint8_t *b, size_t b_size)
80
1.87k
{
81
1.87k
    return a_size == b_size && memcmp(a, b, a_size) == 0;
82
1.87k
}
83
84
int16_t max_s16(int16_t a, int16_t b)
85
0
{
86
0
    return a > b ? a : b;
87
0
}
88
int32_t max_s32(int32_t a, int32_t b)
89
50.4k
{
90
50.4k
    return a > b ? a : b;
91
50.4k
}
92
int64_t max_s64(int64_t a, int64_t b)
93
0
{
94
0
    return a > b ? a : b;
95
0
}
96
97
int16_t min_s16(int16_t a, int16_t b)
98
0
{
99
0
    return a < b ? a : b;
100
0
}
101
int32_t min_s32(int32_t a, int32_t b)
102
50.4k
{
103
50.4k
    return a < b ? a : b;
104
50.4k
}
105
int64_t min_s64(int64_t a, int64_t b)
106
0
{
107
0
    return a < b ? a : b;
108
0
}
109
110
uint16_t max_u16(uint16_t a, uint16_t b)
111
0
{
112
0
    return a > b ? a : b;
113
0
}
114
uint32_t max_u32(uint32_t a, uint32_t b)
115
22
{
116
22
    return a > b ? a : b;
117
22
}
118
uint64_t max_u64(uint64_t a, uint64_t b)
119
0
{
120
0
    return a > b ? a : b;
121
0
}
122
123
uint16_t min_u16(uint16_t a, uint16_t b)
124
16.9k
{
125
16.9k
    return a < b ? a : b;
126
16.9k
}
127
uint32_t min_u32(uint32_t a, uint32_t b)
128
72
{
129
72
    return a < b ? a : b;
130
72
}
131
uint64_t min_u64(uint64_t a, uint64_t b)
132
22
{
133
22
    return a < b ? a : b;
134
22
}
135
136
uint32_t jenkins_one_at_a_time_hash(const uint8_t *key, size_t len)
137
283
{
138
283
    uint32_t hash = 0;
139
140
9.33k
    for (uint32_t i = 0; i < len; ++i) {
141
9.05k
        hash += key[i];
142
9.05k
        hash += (uint32_t)((uint64_t)hash << 10);
143
9.05k
        hash ^= hash >> 6;
144
9.05k
    }
145
146
283
    hash += (uint32_t)((uint64_t)hash << 3);
147
283
    hash ^= hash >> 11;
148
283
    hash += (uint32_t)((uint64_t)hash << 15);
149
283
    return hash;
150
283
}