summaryrefslogtreecommitdiff
path: root/toxcore/TCP_connection.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-02-24 11:50:13 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-02-24 11:53:43 +0000
commit4c8ee2a20f6525d74a6b9474310f397d355e2b10 (patch)
treec8e9c7c1dd22237ebaca58cccf05afebcad4e429 /toxcore/TCP_connection.c
parentb2a2a0bbc7c3aac655bc9efc1c8f385adfe90a3f (diff)
Minor cleanups: dead stores and avoiding complex macros.
Diffstat (limited to 'toxcore/TCP_connection.c')
-rw-r--r--toxcore/TCP_connection.c44
1 files changed, 26 insertions, 18 deletions
diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c
index 3ce75948..d10114f6 100644
--- a/toxcore/TCP_connection.c
+++ b/toxcore/TCP_connection.c
@@ -72,13 +72,28 @@ const uint8_t *tcp_connections_public_key(const TCP_Connections *tcp_c)
72 * return -1 if realloc fails. 72 * return -1 if realloc fails.
73 * return 0 if it succeeds. 73 * return 0 if it succeeds.
74 */ 74 */
75#define realloc_tox_array(array, element_type, num, temp_pointer) \ 75#define MAKE_REALLOC(T) \
76 (num \ 76static int realloc_##T(T **array, size_t num) \
77 ? (temp_pointer = (element_type *)realloc( \ 77{ \
78 array, \ 78 if (!num) { \
79 (num) * sizeof(element_type)), \ 79 free(*array); \
80 temp_pointer ? (array = temp_pointer, 0) : -1) \ 80 *array = nullptr; \
81 : (free(array), array = nullptr, 0)) 81 return 0; \
82 } \
83 \
84 T *temp_pointer = (T *)realloc(*array, num * sizeof(T)); \
85 \
86 if (!temp_pointer) { \
87 return -1; \
88 } \
89 \
90 *array = temp_pointer; \
91 \
92 return 0; \
93}
94
95MAKE_REALLOC(TCP_Connection_to)
96MAKE_REALLOC(TCP_con)
82 97
83 98
84/* return 1 if the connections_number is not valid. 99/* return 1 if the connections_number is not valid.
@@ -138,10 +153,7 @@ static int create_connection(TCP_Connections *tcp_c)
138 153
139 int id = -1; 154 int id = -1;
140 155
141 TCP_Connection_to *temp_pointer; 156 if (realloc_TCP_Connection_to(&tcp_c->connections, tcp_c->connections_length + 1) == 0) {
142
143 if (realloc_tox_array(tcp_c->connections, TCP_Connection_to, tcp_c->connections_length + 1,
144 temp_pointer) == 0) {
145 id = tcp_c->connections_length; 157 id = tcp_c->connections_length;
146 ++tcp_c->connections_length; 158 ++tcp_c->connections_length;
147 memset(&tcp_c->connections[id], 0, sizeof(TCP_Connection_to)); 159 memset(&tcp_c->connections[id], 0, sizeof(TCP_Connection_to));
@@ -167,9 +179,7 @@ static int create_tcp_connection(TCP_Connections *tcp_c)
167 179
168 int id = -1; 180 int id = -1;
169 181
170 TCP_con *temp_pointer; 182 if (realloc_TCP_con(&tcp_c->tcp_connections, tcp_c->tcp_connections_length + 1) == 0) {
171
172 if (realloc_tox_array(tcp_c->tcp_connections, TCP_con, tcp_c->tcp_connections_length + 1, temp_pointer) == 0) {
173 id = tcp_c->tcp_connections_length; 183 id = tcp_c->tcp_connections_length;
174 ++tcp_c->tcp_connections_length; 184 ++tcp_c->tcp_connections_length;
175 memset(&tcp_c->tcp_connections[id], 0, sizeof(TCP_con)); 185 memset(&tcp_c->tcp_connections[id], 0, sizeof(TCP_con));
@@ -200,8 +210,7 @@ static int wipe_connection(TCP_Connections *tcp_c, int connections_number)
200 210
201 if (tcp_c->connections_length != i) { 211 if (tcp_c->connections_length != i) {
202 tcp_c->connections_length = i; 212 tcp_c->connections_length = i;
203 TCP_Connection_to *temp_pointer; 213 realloc_TCP_Connection_to(&tcp_c->connections, tcp_c->connections_length);
204 realloc_tox_array(tcp_c->connections, TCP_Connection_to, tcp_c->connections_length, temp_pointer);
205 } 214 }
206 215
207 return 0; 216 return 0;
@@ -229,8 +238,7 @@ static int wipe_tcp_connection(TCP_Connections *tcp_c, int tcp_connections_numbe
229 238
230 if (tcp_c->tcp_connections_length != i) { 239 if (tcp_c->tcp_connections_length != i) {
231 tcp_c->tcp_connections_length = i; 240 tcp_c->tcp_connections_length = i;
232 TCP_con *temp_pointer; 241 realloc_TCP_con(&tcp_c->tcp_connections, tcp_c->tcp_connections_length);
233 realloc_tox_array(tcp_c->tcp_connections, TCP_con, tcp_c->tcp_connections_length, temp_pointer);
234 } 242 }
235 243
236 return 0; 244 return 0;