#define COMM_SIZE 10
#define COMP_SIZE 0
+#define MAILBOX_NAME_SIZE 10
static int nb_bits = 24;
static int nb_keys = 0;
*/
typedef struct finger {
int id;
- char* mailbox;
+ char mailbox[MAILBOX_NAME_SIZE]; // string representation of the id
} s_finger_t, *finger_t;
/**
*/
typedef struct node {
int id; // my id
- char* mailbox; // my usual mailbox name
+ char mailbox[MAILBOX_NAME_SIZE]; // my mailbox name (string representation of the id)
s_finger_t *fingers; // finger table, of size nb_bits (fingers[0] is my successor)
int pred_id; // predecessor id
- char* pred_mailbox; // predecessor's mailbox name
+ char pred_mailbox[MAILBOX_NAME_SIZE]; // predecessor's mailbox name
int next_finger_to_fix; // index of the next finger to fix in fix_fingers()
msg_comm_t comm_receive; // current communication to receive
xbt_dynar_t comms; // current communications being sent
int request_id; // id paramater (used by some types of tasks)
int request_finger; // finger parameter (used by some types of tasks)
int answer_id; // answer (used by some types of tasks)
- char* answer_to; // mailbox to send an answer to (or NULL)
+ char answer_to[MAILBOX_NAME_SIZE]; // mailbox to send an answer to (if any)
const char* issuer_host_name; // used for logging
} s_task_data_t, *task_data_t;
static void chord_initialize(void);
static int normalize(int id);
static int is_in_interval(int id, int start, int end);
-static char* get_mailbox(int host_id);
+static void get_mailbox(int host_id, char* mailbox);
static void task_data_destroy(task_data_t task_data);
static void print_finger_table(node_t node);
static void set_finger(node_t node, int finger_index, int id);
/**
* \brief Gets the mailbox name of a host given its chord id.
* \param node_id id of a node
- * \return the name of its mailbox
+ * \param mailbox pointer to where the mailbox name should be written
+ * (there must be enough space)
*/
-static char* get_mailbox(int node_id)
+static void get_mailbox(int node_id, char* mailbox)
{
- return bprintf("mailbox%d", node_id);
+ snprintf(mailbox, MAILBOX_NAME_SIZE - 1, "%d", node_id);
}
/**
*/
static void task_data_destroy(task_data_t task_data)
{
- xbt_free(task_data->answer_to);
xbt_free(task_data);
}
{
if (XBT_LOG_ISENABLED(msg_chord, xbt_log_priority_verbose)) {
int i;
- int pow = 1;
VERB0("My finger table:");
VERB0("Start | Succ ");
for (i = 0; i < nb_bits; i++) {
- VERB2(" %3d | %3d ", (node->id + pow) % nb_keys, node->fingers[i].id);
- pow = pow << 1;
+ VERB2(" %3d | %3d ", (node->id + powers2[i]) % nb_keys, node->fingers[i].id);
}
VERB1("Predecessor: %d", node->pred_id);
}
{
if (id != node->fingers[finger_index].id) {
node->fingers[finger_index].id = id;
- xbt_free(node->fingers[finger_index].mailbox);
- node->fingers[finger_index].mailbox = get_mailbox(id);
+ get_mailbox(id, node->fingers[finger_index].mailbox);
node->last_change_date = MSG_get_clock();
DEBUG2("My new finger #%d is %d", finger_index, id);
}
{
if (predecessor_id != node->pred_id) {
node->pred_id = predecessor_id;
- xbt_free(node->pred_mailbox);
if (predecessor_id != -1) {
- node->pred_mailbox = get_mailbox(predecessor_id);
+ get_mailbox(predecessor_id, node->pred_mailbox);
}
node->last_change_date = MSG_get_clock();
// initialize my node
s_node_t node = {0};
node.id = atoi(argv[1]);
- node.mailbox = get_mailbox(node.id);
+ get_mailbox(node.id, node.mailbox);
node.next_finger_to_fix = 0;
node.comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
node.fingers = xbt_new0(s_finger_t, nb_bits);
node.last_change_date = init_time;
for (i = 0; i < nb_bits; i++) {
+ node.fingers[i].id = -1;
set_finger(&node, i, node.id);
}
// stop the simulation
xbt_dynar_free(&node.comms);
- xbt_free(node.mailbox);
- xbt_free(node.pred_mailbox);
- for (i = 0; i < nb_bits - 1; i++) {
- xbt_free(node.fingers[i].mailbox);
- }
xbt_free(node.fingers);
return 0;
}
int closest = closest_preceding_node(node, task_data->request_id);
DEBUG2("Forwarding the 'Find Successor' request for id %d to my closest preceding finger %d",
task_data->request_id, closest);
- mailbox = get_mailbox(closest);
+ get_mailbox(closest, mailbox);
comm = MSG_task_isend(task, mailbox);
xbt_dynar_push(node->comms, &comm);
xbt_free(mailbox);
{
int successor = -1;
int stop = 0;
- char* mailbox = get_mailbox(ask_to);
+ char mailbox[MAILBOX_NAME_SIZE];
+ get_mailbox(ask_to, mailbox);
task_data_t req_data = xbt_new0(s_task_data_t, 1);
req_data->type = TASK_FIND_SUCCESSOR;
req_data->request_id = id;
- req_data->answer_to = xbt_strdup(node->mailbox);
+ get_mailbox(node->id, req_data->answer_to);
req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
// send a "Find Successor" request to ask_to_id
task_data_destroy(req_data);
}
}
+ MSG_comm_destroy(node->comm_receive);
node->comm_receive = NULL;
} while (!stop);
}
- xbt_free(mailbox);
return successor;
}
{
int predecessor_id = -1;
int stop = 0;
- char* mailbox = get_mailbox(ask_to);
+ char mailbox[MAILBOX_NAME_SIZE];
+ get_mailbox(ask_to, mailbox);
task_data_t req_data = xbt_new0(s_task_data_t, 1);
req_data->type = TASK_GET_PREDECESSOR;
- req_data->answer_to = xbt_strdup(node->mailbox);
+ get_mailbox(node->id, req_data->answer_to);
req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
// send a "Get Predecessor" request to ask_to_id
task_data_destroy(req_data);
}
}
+ MSG_comm_destroy(node->comm_receive);
node->comm_receive = NULL;
} while (!stop);
}
- xbt_free(mailbox);
return predecessor_id;
}
req_data->type = TASK_NOTIFY;
req_data->request_id = predecessor_candidate_id;
req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
- req_data->answer_to = NULL;
// send a "Notify" request to notify_id
m_task_t task = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
DEBUG2("Sending a 'Notify' request (task %p) to %d", task, notify_id);
- char* mailbox = get_mailbox(notify_id);
+ char mailbox[MAILBOX_NAME_SIZE];
+ get_mailbox(notify_id, mailbox);
msg_comm_t comm = MSG_task_isend(task, mailbox);
xbt_dynar_push(node->comms, &comm);
- xbt_free(mailbox);
}
/**