Skip to content

Commit 165bc32

Browse files
committed
Uses typeof to clarify intentions of list_create
Also moves function type cast into centralized location to prevent repetitive type cast aliasing for each and every type.
1 parent 0983e62 commit 165bc32

File tree

6 files changed

+35
-30
lines changed

6 files changed

+35
-30
lines changed

src/client.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,6 @@ int client_check_and_send_keepalive(client_t *client, struct timeval curr_tv);
8989
void client_reset_keepalive(client_t *client);
9090
int client_timed_out(client_t *client, struct timeval curr_tv);
9191

92-
/* Function pointers to use when making a list_t of clients */
93-
#define p_client_copy ((void* (*)(void *, const void *, size_t))&client_copy)
94-
#define p_client_cmp ((int (*)(const void *, const void *, size_t))&client_cmp)
95-
#define p_client_free ((void (*)(void *))&client_free)
96-
9792
/* Inline functions as wrappers for handling the file descriptors in the
9893
* client's sockets */
9994

src/destination.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ typedef struct destination {
3030
char *data;
3131
} destination_t;
3232

33-
#define p_destination_copy ((void* (*)(void *, const void *, size_t))&destination_copy)
34-
#define p_destination_cmp ((int (*)(const void *, const void *, size_t))&destination_cmp)
35-
#define p_destination_free ((void (*)(void *))&destination_free)
36-
3733
destination_t *destination_create(const char *address);
3834
destination_t *destination_copy(destination_t *dst, destination_t *src, size_t len);
3935
int destination_cmp(destination_t *c1, destination_t *c2, size_t len);

src/list.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828
* and uses the cmp, copy, and free functions. If any of the function pointers
2929
* are NULL, they will be set to memcmp, memcpy, or free.
3030
*/
31-
list_t *list_create(int obj_sz,
32-
int (*obj_cmp)(const void *, const void *, size_t),
33-
void* (*obj_copy)(void *, const void *, size_t),
34-
void (*obj_free)(void *))
35-
{
31+
list_t *list_create_impl(
32+
int obj_sz,
33+
typeof(memcmp)* obj_cmp,
34+
typeof(memcpy)* obj_copy,
35+
typeof(free)* obj_free
36+
) {
3637
list_t *new;
3738

3839
new = malloc(sizeof(*new));

src/list.h

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
#ifndef LIST_H
2323
#define LIST_H
2424

25+
#include <stdlib.h>
26+
#include <string.h>
27+
2528
#include "common.h"
2629

2730
#define LIST_INIT_SIZE 10 /* Start off an array with 10 elements */
@@ -33,17 +36,27 @@ typedef struct list {
3336
int length; /* Actual length of the pointer array */
3437

3538
/* Function pointers to use for specific type of data types */
36-
int (*obj_cmp)(const void *, const void *, size_t);
37-
void* (*obj_copy)(void *, const void *, size_t);
38-
void (*obj_free)(void *);
39+
typeof(memcmp)* obj_cmp;
40+
typeof(memcpy)* obj_copy;
41+
typeof(free)* obj_free;
3942
} list_t;
4043

4144
#define LIST_LEN(l) ((l)->num_objs)
4245

43-
list_t *list_create(int obj_sz,
44-
int (*obj_cmp)(const void *, const void *, size_t),
45-
void* (*obj_copy)(void *, const void *, size_t),
46-
void (*obj_free)(void *));
46+
list_t *list_create_impl(
47+
int obj_sz,
48+
typeof(memcmp)* obj_cmp,
49+
typeof(memcpy)* obj_copy,
50+
typeof(free)* obj_free);
51+
52+
#define list_create(obj_sz, obj_cmp, obj_copy, obj_free) \
53+
list_create_impl( \
54+
obj_sz, \
55+
(typeof(memcmp)*)&obj_cmp, \
56+
(typeof(memcpy)*)&obj_copy, \
57+
(typeof(free) *)&obj_free \
58+
)
59+
4760
void *list_add(list_t *list, void *obj);
4861
void *list_get(list_t *list, void *obj);
4962
void *list_get_at(list_t *list, int i);

src/udpclient.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,13 @@ int udpclient(int argc, char* argv[])
162162
next_req_id = rand() % 0xffff;
163163

164164
/* Create an empty list for the clients */
165-
clients = list_create(sizeof(client_t), p_client_cmp, p_client_copy,
166-
p_client_free);
165+
clients = list_create(sizeof(client_t), client_cmp, client_copy,
166+
client_free);
167167
ERROR_GOTO(clients == NULL, "Error creating clients list.", done);
168168

169169
/* Create and empty list for the connecting clients */
170-
conn_clients = list_create(sizeof(client_t), p_client_cmp, p_client_copy,
171-
p_client_free);
170+
conn_clients = list_create(sizeof(client_t), client_cmp, client_copy,
171+
client_free);
172172
ERROR_GOTO(conn_clients == NULL, "Error creating clients list.", done);
173173

174174
/* Create a TCP server socket to listen for incoming connections */

src/udpserver.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ int udpserver(int argc, char *argv[])
149149
if (argc > allowed_start)
150150
{
151151
allowed_destinations = list_create(sizeof(destination_t),
152-
p_destination_cmp,
153-
p_destination_copy,
154-
p_destination_free);
152+
destination_cmp,
153+
destination_copy,
154+
destination_free);
155155
if (!allowed_destinations)
156156
goto done;
157157
for (i = allowed_start; i < argc; i++)
@@ -166,8 +166,8 @@ int udpserver(int argc, char *argv[])
166166
}
167167

168168
/* Create an empty list for the clients */
169-
clients = list_create(sizeof(client_t), p_client_cmp, p_client_copy,
170-
p_client_free);
169+
clients = list_create(sizeof(client_t), client_cmp, client_copy,
170+
client_free);
171171
if(!clients)
172172
goto done;
173173

0 commit comments

Comments
 (0)