Commit 4bbb2e8d authored by Andreas Schmidt's avatar Andreas Schmidt

Refactor deliveredPacketTable.

parent aff42db9
......@@ -5,73 +5,78 @@
#include "deliveredPacketTable.h"
static bool is_position_relevant(const PrrtDeliveredPacketTable *fpt_ptr, prrtSequenceNumber_t seqno)
static bool is_position_relevant(const PrrtDeliveredPacketTable *table,
prrtSequenceNumber_t seqno)
{
prrtSequenceNumber_t stop = (prrtSequenceNumber_t) (SEQNO_SPACE / 2 + fpt_ptr->start);
if(fpt_ptr->start < stop && !(fpt_ptr->start <= seqno && seqno <= stop)) {
prrtSequenceNumber_t stop = (prrtSequenceNumber_t) (SEQNO_SPACE / 2 + table->start);
if(table->start < stop && !(table->start <= seqno && seqno <= stop)) {
return false;
} else if(!(seqno <= stop || fpt_ptr->start <= seqno)) {
} else if(!(seqno <= stop || table->start <= seqno)) {
return false;
} else {
return Bitmap_get(fpt_ptr->bitmap, seqno);
return Bitmap_get(table->bitmap, seqno);
}
}
static void move_start(PrrtDeliveredPacketTable *fpt_ptr)
static void move_start(PrrtDeliveredPacketTable *table)
{
prrtSequenceNumber_t seqno = fpt_ptr->start;
prrtSequenceNumber_t seqno = table->start;
while(1) {
if(Bitmap_get(fpt_ptr->bitmap, seqno)) {
if(Bitmap_get(table->bitmap, seqno)) {
break;
}
Bitmap_set(fpt_ptr->bitmap, seqno, true);
Bitmap_set(table->bitmap, seqno, true);
seqno++;
}
// TODO: make more efficient by using the Bitmap_set_range function
fpt_ptr->start = seqno;
table->start = seqno;
}
PrrtDeliveredPacketTable *PrrtDeliveredPacketTable_create()
{
PrrtDeliveredPacketTable *fpt_prt = calloc(1, sizeof(PrrtDeliveredPacketTable));
check_mem(fpt_prt);
fpt_prt->start = 1;
fpt_prt->bitmap = Bitmap_create(true, SEQNO_SPACE);
pthread_mutex_init(&fpt_prt->lock, NULL);
return fpt_prt;
PrrtDeliveredPacketTable *table = calloc(1, sizeof(PrrtDeliveredPacketTable));
check_mem(table);
table->start = 1;
table->bitmap = Bitmap_create(true, SEQNO_SPACE);
pthread_mutex_init(&table->lock, NULL);
return table;
error:
PERROR("Out of memory.%s", "");
return NULL;
}
bool PrrtDeliveredPacketTable_destroy(PrrtDeliveredPacketTable *t)
bool PrrtDeliveredPacketTable_destroy(PrrtDeliveredPacketTable *table)
{
Bitmap_destroy(t->bitmap);
free(t);
Bitmap_destroy(table->bitmap);
free(table);
return true;
}
bool PrrtDeliveredPacketTable_test_set_is_number_relevant(PrrtDeliveredPacketTable *t, prrtSequenceNumber_t seqno)
bool PrrtDeliveredPacketTable_test_set_is_number_relevant(PrrtDeliveredPacketTable *table,
prrtSequenceNumber_t seqno)
{
pthread_mutex_lock(&t->lock);
bool res = is_position_relevant(t, seqno);
check(pthread_mutex_lock(&table->lock) == EXIT_SUCCESS, "Lock failed.");
bool res = is_position_relevant(table, seqno);
if(res) {
Bitmap_set(t->bitmap, seqno, false);
if(seqno == t->start) {
move_start(t);
Bitmap_set(table->bitmap, seqno, false);
if(seqno == table->start) {
move_start(table);
}
}
pthread_mutex_unlock(&t->lock);
check(pthread_mutex_unlock(&table->lock) == EXIT_SUCCESS, "Unlock failed.");
return res;
error:
PERROR("PrrtDeliveredPacketTable_test_set_is_number_relevant() failed.");
return false;
}
bool PrrtDeliveredPacketTable_test_is_block_relevant(PrrtDeliveredPacketTable *t,
bool PrrtDeliveredPacketTable_test_is_block_relevant(PrrtDeliveredPacketTable *table,
prrtSequenceNumber_t start,
prrtSequenceNumber_t length)
{
......@@ -79,27 +84,34 @@ bool PrrtDeliveredPacketTable_test_is_block_relevant(PrrtDeliveredPacketTable *t
bool res = false;
int i;
pthread_mutex_lock(&t->lock);
check(pthread_mutex_lock(&table->lock) == EXIT_SUCCESS, "Lock failed.");
for(i = 0; i < length; i++) {
res = res || is_position_relevant(t, (prrtSequenceNumber_t) (start + i));
res = res || is_position_relevant(table, (prrtSequenceNumber_t) (start + i));
}
pthread_mutex_unlock(&t->lock);
check(pthread_mutex_unlock(&table->lock) == EXIT_SUCCESS, "Unlock failed.");
return res;
error:
PERROR("PrrtDeliveredPacketTable_test_is_block_relevant() failed.");
return false;
}
void PrrtDeliveredPacketTable_forward_start(PrrtDeliveredPacketTable *fpt_ptr, prrtSequenceNumber_t new_start)
void PrrtDeliveredPacketTable_forward_start(PrrtDeliveredPacketTable *table,
prrtSequenceNumber_t new_start)
{
pthread_mutex_lock(&fpt_ptr->lock);
prrtSequenceNumber_t seqno = fpt_ptr->start;
check(pthread_mutex_lock(&table->lock) == EXIT_SUCCESS, "Lock failed.");
prrtSequenceNumber_t seqno = table->start;
while (seqno != new_start) {
Bitmap_set(fpt_ptr->bitmap, seqno, true);
Bitmap_set(table->bitmap, seqno, true);
seqno++;
}
// TODO: make more efficient by using the Bitmap_set_range function
fpt_ptr->start = seqno;
pthread_mutex_unlock(&fpt_ptr->lock);
table->start = seqno;
check(pthread_mutex_unlock(&table->lock) == EXIT_SUCCESS, "Unlock failed.");
return;
error:
PERROR("PrrtDeliveredPacketTable_forward_start() failed.");
}
......@@ -14,15 +14,15 @@ typedef struct {
} PrrtDeliveredPacketTable;
PrrtDeliveredPacketTable * PrrtDeliveredPacketTable_create(void);
bool PrrtDeliveredPacketTable_destroy(PrrtDeliveredPacketTable *t);
bool PrrtDeliveredPacketTable_destroy(PrrtDeliveredPacketTable *table);
bool PrrtDeliveredPacketTable_test_set_is_number_relevant(PrrtDeliveredPacketTable *t, prrtSequenceNumber_t seqno);
bool PrrtDeliveredPacketTable_test_set_is_number_relevant(PrrtDeliveredPacketTable *table, prrtSequenceNumber_t seqno);
bool PrrtDeliveredPacketTable_test_is_block_relevant(PrrtDeliveredPacketTable *t,
bool PrrtDeliveredPacketTable_test_is_block_relevant(PrrtDeliveredPacketTable *table,
prrtSequenceNumber_t start,
prrtSequenceNumber_t length);
void PrrtDeliveredPacketTable_forward_start(PrrtDeliveredPacketTable *fpt_ptr, prrtSequenceNumber_t new_start);
void PrrtDeliveredPacketTable_forward_start(PrrtDeliveredPacketTable *table, prrtSequenceNumber_t new_start);
#endif //PRRT_DELIVERED_PACKET_TABLE_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment