Skip to content

Commit 932f4fe

Browse files
committed
Fix warnings.
1 parent 4f69a62 commit 932f4fe

File tree

5 files changed

+84
-50
lines changed

5 files changed

+84
-50
lines changed

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#override CC := clang
2-
override CFLAGS += -Wfatal-errors -O0 -g
2+
override CFLAGS += -fpic -Wall -Wfatal-errors -O0 -g -pedantic -std=c99
33
override CPPFLAGS += -I. -Iinclude -DDEBUG
44
override HEART_LDFLAGS += -Llib -lraft -ljansson
55

@@ -8,12 +8,12 @@ ARFLAGS = -cru
88

99
.PHONY: all clean bindir objdir libdir
1010

11-
all: lib/libraft.a bin/heart
12-
@echo Done.
13-
1411
lib/libraft.a: obj/raft.o obj/util.o | libdir objdir
1512
$(AR) $(ARFLAGS) lib/libraft.a obj/raft.o obj/util.o
1613

14+
all: lib/libraft.a bin/heart
15+
@echo Done.
16+
1717
bin/heart: obj/heart.o lib/libraft.a | bindir objdir
1818
$(CC) -o bin/heart $(CFLAGS) $(CPPFLAGS) \
1919
obj/heart.o $(HEART_LDFLAGS)

example/heart.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#define _POSIX_C_SOURCE 2
2+
#define _BSD_SOURCE
13
#include <stdio.h>
24
#include <stdlib.h>
35
#include <string.h>
@@ -54,8 +56,6 @@ static void applier(void *state, raft_update_t update, raft_bool_t snapshot) {
5456
}
5557

5658
static raft_update_t snapshooter(void *state) {
57-
json_error_t error;
58-
5959
raft_update_t shot;
6060
shot.data = json_dumps(state, JSON_SORT_KEYS);
6161
shot.len = strlen(shot.data);
@@ -98,7 +98,6 @@ static void main_loop(char *host, int port) {
9898
while (true) {
9999
int ms;
100100
raft_msg_t m;
101-
int applied;
102101

103102
ms = mstimer_reset(&t);
104103

include/raft.h

Lines changed: 62 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,26 @@ typedef int raft_bool_t;
1111
typedef struct raft_update_t {
1212
int len;
1313
char *data;
14-
void *userdata; // use this to track which query caused this update
14+
void *userdata; /* use this to track which query caused this update */
1515
} raft_update_t;
1616

17-
// --- Callbacks ---
17+
/* --- Callbacks --- */
1818

19-
// This should be a function that applies an 'update' to the state machine.
20-
// 'snapshot' is true if 'update' contains a snapshot. 'userdata' is the
21-
// userdata that raft was configured with.
19+
/*
20+
* This should be a function that applies an 'update' to the state machine.
21+
* 'snapshot' is true if 'update' contains a snapshot. 'userdata' is the
22+
* userdata that raft was configured with.
23+
*/
2224
typedef void (*raft_applier_t)(void *userdata, raft_update_t update, raft_bool_t snapshot);
2325

24-
// This should be a function that makes a snapshot of the state machine. Used
25-
// for raft log compaction. 'userdata' is the userdata that raft was configured
26-
// with.
26+
/*
27+
* This should be a function that makes a snapshot of the state machine. Used
28+
* for raft log compaction. 'userdata' is the userdata that raft was configured
29+
* with.
30+
*/
2731
typedef raft_update_t (*raft_snapshooter_t)(void *userdata);
2832

29-
// --- Configuration ---
33+
/* --- Configuration --- */
3034

3135
typedef struct raft_config_t {
3236
int peernum_max;
@@ -40,56 +44,80 @@ typedef struct raft_config_t {
4044
int chunk_len;
4145
int msg_len_max;
4246

43-
void *userdata; // this will get passed to applier() and snapshooter()
47+
void *userdata; /* this will get passed to applier() and snapshooter() */
4448
raft_applier_t applier;
4549
raft_snapshooter_t snapshooter;
4650
} raft_config_t;
4751

48-
// Initialize a raft instance. Returns NULL on failure.
52+
/*
53+
* Initialize a raft instance. Returns NULL on failure.
54+
*/
4955
raft_t raft_init(raft_config_t *config);
5056

51-
// Add a peer named 'id'. 'self' should be true, if that peer is this instance.
52-
// Only one peer should have 'self' == true.
57+
/*
58+
* Add a peer named 'id'. 'self' should be true, if that peer is this instance.
59+
* Only one peer should have 'self' == true.
60+
*/
5361
raft_bool_t raft_peer_up(raft_t r, int id, char *host, int port, raft_bool_t self);
5462

55-
// Remove a previously added peer named 'id'.
63+
/*
64+
* Remove a previously added peer named 'id'.
65+
*/
5666
raft_bool_t raft_peer_down(raft_t r, int id);
5767

58-
// --- Log Actions ---
68+
/* --- Log Actions --- */
5969

60-
// Emit an 'update'. Returns the log index if emitted successfully, or -1
61-
// otherwise.
70+
/*
71+
* Emit an 'update'. Returns the log index if emitted successfully, or -1
72+
* otherwise.
73+
*/
6274
int raft_emit(raft_t r, raft_update_t update);
6375

64-
// Checks whether an entry at 'index' has been applied by the peer named 'id'.
76+
/*
77+
* Checks whether an entry at 'index' has been applied by the peer named 'id'.
78+
*/
6579
raft_bool_t raft_applied(raft_t t, int id, int index);
6680

67-
// --- Control ---
68-
69-
// Note, that UDP socket and raft messages are exposed to the user. This gives
70-
// the user the opportunity to incorporate the socket with other sockets in
71-
// select() or poll(). Thus, the messages will be processed as soon as they
72-
// come, not as soon as we call raft_tick().
73-
74-
// Perform various raft logic tied to time. Call this function once in a while
75-
// and pass the elapsed 'msec' from the previous call. This function will only
76-
// trigger time-related events, and will not receive and process messages (see
77-
// the note above).
81+
/* --- Control --- */
82+
83+
/*
84+
* Note, that UDP socket and raft messages are exposed to the user. This gives
85+
* the user the opportunity to incorporate the socket with other sockets in
86+
* select() or poll(). Thus, the messages will be processed as soon as they
87+
* come, not as soon as we call raft_tick().
88+
*/
89+
90+
/*
91+
* Perform various raft logic tied to time. Call this function once in a while
92+
* and pass the elapsed 'msec' from the previous call. This function will only
93+
* trigger time-related events, and will not receive and process messages (see
94+
* the note above).
95+
*/
7896
void raft_tick(raft_t r, int msec);
7997

80-
// Receive a raft message. Returns NULL if no message available.
98+
/*
99+
* Receive a raft message. Returns NULL if no message available.
100+
*/
81101
raft_msg_t raft_recv_message(raft_t r);
82102

83-
// Process the message.
103+
/*
104+
* Process the message.
105+
*/
84106
void raft_handle_message(raft_t r, raft_msg_t m);
85107

86-
// Create the raft socket.
108+
/*
109+
* Create the raft socket.
110+
*/
87111
int raft_create_udp_socket(raft_t r);
88112

89-
// Returns true if this peer thinks it is the leader.
113+
/*
114+
* Returns true if this peer thinks it is the leader.
115+
*/
90116
raft_bool_t raft_is_leader(raft_t r);
91117

92-
// Returns the id of the current leader, or NOBODY if no leader.
118+
/*
119+
* Returns the id of the current leader, or NOBODY if no leader.
120+
*/
93121
int raft_get_leader(raft_t r);
94122

95123
#endif

include/util.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,19 @@ struct timeval ms2tv(int ms);
3232

3333
// ------ logging ------
3434

35-
#ifndef DEBUG
36-
#define debug(...)
35+
#ifdef DEBUG
36+
#define DEBUG_ENABLED 1
3737
#else
38+
#define DEBUG_ENABLED 0
39+
#endif
40+
3841
#define debug(...) \
3942
do { \
40-
fprintf(stderr, __VA_ARGS__); \
41-
fflush(stderr); \
43+
if (DEBUG_ENABLED) {\
44+
fprintf(stderr, __VA_ARGS__); \
45+
fflush(stderr); \
46+
}\
4247
} while (0)
43-
#endif
4448

4549
#define shout(...) \
4650
do { \

src/raft.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
#define _BSD_SOURCE
2+
#include <sys/socket.h>
3+
#include <netinet/in.h>
14
#include <arpa/inet.h>
5+
26
#include <assert.h>
37
#include <errno.h>
48
#include <limits.h>
@@ -448,7 +452,6 @@ static void raft_beat(raft_t r, int dst) {
448452

449453
if (p->acked.entries <= RAFT_LOG_LAST_INDEX(r)) {
450454
int sendindex;
451-
int sendbyte;
452455

453456
if (p->acked.entries < RAFT_LOG_FIRST_INDEX(r)) {
454457
// The peer has woken up from anabiosis. Send the first
@@ -721,8 +724,6 @@ static bool raft_restore(raft_t r, int previndex, raft_entry_t *e) {
721724
static bool raft_appendable(raft_t r, int previndex, int prevterm) {
722725
int low, high;
723726

724-
raft_log_t *l = &r->log;
725-
726727
low = RAFT_LOG_FIRST_INDEX(r);
727728
if (low == 0) low = -1; // allow appending at the start
728729
high = RAFT_LOG_LAST_INDEX(r);
@@ -743,6 +744,8 @@ static bool raft_appendable(raft_t r, int previndex, int prevterm) {
743744
return false;
744745
}
745746
}
747+
748+
return true;
746749
}
747750

748751
static bool raft_append(raft_t r, int previndex, int prevterm, raft_entry_t *e) {
@@ -754,7 +757,7 @@ static bool raft_append(raft_t r, int previndex, int prevterm, raft_entry_t *e)
754757
debug(
755758
"log_append(%p, previndex=%d, prevterm=%d,"
756759
" term=%d)\n",
757-
l, previndex, prevterm,
760+
(void *)l, previndex, prevterm,
758761
e->term
759762
);
760763

0 commit comments

Comments
 (0)