Skip to content

Commit

Permalink
Removed debug.h; use emalloc() in more places; minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstorm committed Jun 3, 2013
1 parent eb0247e commit 9ec4017
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 25 deletions.
6 changes: 3 additions & 3 deletions include/chord/chord_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ typedef u_long ulong;

enum {
CHORD_ID_BITS = 160,
CHORD_ID_LEN = CHORD_ID_BITS/8,
CHORD_ID_BYTES = CHORD_ID_BITS/8,
};

typedef struct {
unsigned char x[CHORD_ID_LEN];
unsigned char x[CHORD_ID_BYTES];
} chordID;

struct ChordServer;

void chord_get_range(struct ChordServer *srv, chordID *l, chordID *r);
int chord_is_local(struct ChordServer *srv, chordID *x);
void chord_print_circle(struct ChordServer *srv, FILE *fp);

int chord_id_is_local(struct ChordServer *srv, chordID *x);
chordID chord_id_successor(chordID id, int n);
chordID chord_id_predecessor(chordID id, int n);

Expand Down
5 changes: 4 additions & 1 deletion include/chord/chord_opts.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
#ifdef __cplusplus
extern "C" {
#endif

#undef CHORD_MESSAGE_DEBUG
#undef CHORD_PRINT_LONG_IDS
#undef CHORD_PRINT_LONG_TIME

enum {
CHORD_WIRE_VERSION = 1,
TICKET_TIMEOUT = 1000000, /* seconds for which a ticket is valid */
TICKET_HASH_LEN = 4,
TICKET_SALT_LEN = 16,
ADDRESS_SALTS = 3, /* number of IDs an address can have */
NFINGERS = CHORD_ID_BITS, /* # fingers per node */
NSUCCESSORS = 8, /* # successors kept */
NPREDECESSORS = 3, /* # predecessors kept */
Expand Down
3 changes: 1 addition & 2 deletions include/chord/eprintf.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ void *emalloc(size_t);
void *erealloc(void *, size_t);
void *ecalloc(size_t, size_t);
void setprogname(const char *);
const char* getprogname(void);

#define eprintf(fmt, ...) eprintf_impl (clog_file_logger(), fmt, ##__VA_ARGS__)
#define weprintf(fmt, ...) weprintf_impl(clog_file_logger(), fmt, ##__VA_ARGS__)
Expand All @@ -34,4 +33,4 @@ const char* getprogname(void);
}
#endif

#endif
#endif
8 changes: 4 additions & 4 deletions src/chord.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ ChordServer *new_server(struct event_base *ev_base)

void server_initialize_from_file(ChordServer *srv, char *conf_file)
{
char id[4*CHORD_ID_LEN];
char id[4*CHORD_ID_BYTES];
int ip_ver;

FILE *fp = fopen(conf_file, "r");
Expand Down Expand Up @@ -226,7 +226,7 @@ static void init_ticket_key(ChordServer *srv)
exit(2);
}

srv->ticket_salt = malloc(TICKET_SALT_LEN);
srv->ticket_salt = emalloc(TICKET_SALT_LEN);
srv->ticket_salt_len = TICKET_SALT_LEN;
srv->ticket_hash_len = TICKET_HASH_LEN;
if (!RAND_bytes(srv->ticket_salt, TICKET_SALT_LEN)) {
Expand Down Expand Up @@ -287,7 +287,7 @@ long double id_to_radians(const chordID *id)
int i;
long double rad = 0.0;

for (i = 0; i < CHORD_ID_LEN; i++) {
for (i = 0; i < CHORD_ID_BYTES; i++) {
long double numerator = id->x[i];
long double denominator = powl(256, i+1);
rad += numerator/denominator;
Expand Down Expand Up @@ -397,7 +397,7 @@ void chord_get_range(ChordServer *srv, chordID *l, chordID *r)
*r = srv->node.id;
}

int chord_is_local(ChordServer *srv, chordID *x)
int chord_id_is_local(ChordServer *srv, chordID *x)
{
return id_equals(x, &srv->node.id) || id_is_between(x, &srv->pred_bound,
&srv->node.id);
Expand Down
12 changes: 6 additions & 6 deletions src/dispatcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct odd_packet_handler

Dispatcher *new_dispatcher(int size)
{
Dispatcher *d = malloc(sizeof(Dispatcher));
Dispatcher *d = emalloc(sizeof(Dispatcher));
d->handlers = calloc(1, sizeof(struct packet_handler)*size);
d->size = size;
d->odd = 0;
Expand Down Expand Up @@ -98,10 +98,10 @@ void dispatcher_set_error_handlers(Dispatcher *d, unpack_error_fn u_err,
static void init_handler(struct packet_handler *handler, int value, char *name,
void *arg, unpack_fn unpack, process_fn process)
{
handler->name = malloc(strlen(name)+1);
handler->name = emalloc(strlen(name)+1);
strcpy(handler->name, name);

handler->process_args = malloc(sizeof(void *));
handler->process_args = emalloc(sizeof(void *));
handler->process_args[0] = arg;
handler->nargs = 1;

Expand Down Expand Up @@ -142,7 +142,7 @@ void dispatcher_create_handler(Dispatcher *d, int value, char *name, void *arg,
else {
struct odd_packet_handler *odd;
if (!d->odd) {
d->odd = malloc(sizeof(struct packet_handler));
d->odd = emalloc(sizeof(struct packet_handler));
d->odd->next = 0;
odd = d->odd;
}
Expand All @@ -151,15 +151,15 @@ void dispatcher_create_handler(Dispatcher *d, int value, char *name, void *arg,
odd = odd->next;

if (!odd->next) {
odd->next = malloc(sizeof(struct packet_handler));
odd->next = emalloc(sizeof(struct packet_handler));
odd->next->next = 0;
odd = odd->next;
}
else if (odd->next->value == value)
odd = odd->next;
else {
struct odd_packet_handler *next = odd->next;
odd->next = malloc(sizeof(struct packet_handler));
odd->next = emalloc(sizeof(struct packet_handler));
odd->next->next = next;
odd = odd->next;
}
Expand Down
6 changes: 0 additions & 6 deletions src/eprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,4 @@ void setprogname(const char *name)
{
progname = name;
}

/* getprogname: return name of program */
const char *getprogname(void)
{
return progname;
}
#endif
2 changes: 1 addition & 1 deletion src/pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int pack_data(uchar *buf, int last, uchar ttl, chordID *id, ushort len,
const uchar *data)
{
Data msg = DATA__INIT;
msg.id.len = CHORD_ID_LEN;
msg.id.len = CHORD_ID_BYTES;
msg.id.data = id->x;

msg.ttl = ttl;
Expand Down
4 changes: 2 additions & 2 deletions src/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ int process_data(Header *header, ChordPacketArgs *args, Data *msg, Node *from)
{
ChordServer *srv = args->srv;
chordID id;
memcpy(id.x, msg->id.data, CHORD_ID_LEN);
memcpy(id.x, msg->id.data, CHORD_ID_BYTES);

if (IN6_IS_ADDR_UNSPECIFIED(&srv->node.addr))
return CHORD_ADDR_UNDISCOVERED;
Expand All @@ -113,7 +113,7 @@ int process_data(Header *header, ChordPacketArgs *args, Data *msg, Node *from)
}

/* handle request locally? */
if (chord_is_local(srv, &id)) {
if (chord_id_is_local(srv, &id)) {
/* Upcall goes here... */
Debug("id is local");
//chord_deliver(len, data, from);
Expand Down

0 comments on commit 9ec4017

Please sign in to comment.