133 lines
2.1 KiB
C
133 lines
2.1 KiB
C
#include <socks/queue.h>
|
|
|
|
size_t queue_length(struct queue *q)
|
|
{
|
|
size_t i = 0;
|
|
struct queue_entry *x = q->q_first;
|
|
while (x) {
|
|
i++;
|
|
x = x->qe_next;
|
|
}
|
|
|
|
return i;
|
|
}
|
|
|
|
void queue_insert_before(struct queue *q, struct queue_entry *entry, struct queue_entry *before)
|
|
{
|
|
struct queue_entry *x = before->qe_prev;
|
|
if (x) {
|
|
x->qe_next = entry;
|
|
} else {
|
|
q->q_first = entry;
|
|
}
|
|
|
|
entry->qe_prev = x;
|
|
|
|
before->qe_prev = entry;
|
|
entry->qe_next = before;
|
|
}
|
|
|
|
void queue_insert_after(struct queue *q, struct queue_entry *entry, struct queue_entry *after)
|
|
{
|
|
struct queue_entry *x = after->qe_next;
|
|
if (x) {
|
|
x->qe_prev = entry;
|
|
} else {
|
|
q->q_last = entry;
|
|
}
|
|
|
|
entry->qe_prev = x;
|
|
|
|
after->qe_next = entry;
|
|
entry->qe_prev = after;
|
|
}
|
|
|
|
void queue_push_front(struct queue *q, struct queue_entry *entry)
|
|
{
|
|
if (q->q_first) {
|
|
q->q_first->qe_prev = entry;
|
|
}
|
|
|
|
entry->qe_next = q->q_first;
|
|
entry->qe_prev = NULL;
|
|
|
|
q->q_first = entry;
|
|
|
|
if (!q->q_last) {
|
|
q->q_last = entry;
|
|
}
|
|
}
|
|
|
|
void queue_push_back(struct queue *q, struct queue_entry *entry)
|
|
{
|
|
if (q->q_last) {
|
|
q->q_last->qe_next = entry;
|
|
}
|
|
|
|
entry->qe_prev = q->q_last;
|
|
entry->qe_next = NULL;
|
|
|
|
q->q_last = entry;
|
|
|
|
if (!q->q_first) {
|
|
q->q_first = entry;
|
|
}
|
|
}
|
|
|
|
struct queue_entry *queue_pop_front(struct queue *q)
|
|
{
|
|
struct queue_entry *x = q->q_first;
|
|
if (x) {
|
|
queue_delete(q, x);
|
|
}
|
|
|
|
return x;
|
|
}
|
|
|
|
struct queue_entry *queue_pop_back(struct queue *q)
|
|
{
|
|
struct queue_entry *x = q->q_last;
|
|
if (x) {
|
|
queue_delete(q, x);
|
|
}
|
|
|
|
return x;
|
|
}
|
|
|
|
void queue_delete(struct queue *q, struct queue_entry *entry)
|
|
{
|
|
if (!entry) {
|
|
return;
|
|
}
|
|
|
|
if (entry == q->q_first) {
|
|
q->q_first = q->q_first->qe_next;
|
|
}
|
|
|
|
if (entry == q->q_last) {
|
|
q->q_last = q->q_last->qe_prev;
|
|
}
|
|
|
|
if (entry->qe_next) {
|
|
entry->qe_next->qe_prev = entry->qe_prev;
|
|
}
|
|
|
|
if (entry->qe_prev) {
|
|
entry->qe_prev->qe_next = entry->qe_next;
|
|
}
|
|
|
|
entry->qe_next = entry->qe_prev = NULL;
|
|
}
|
|
|
|
void queue_delete_all(struct queue *q)
|
|
{
|
|
struct queue_entry *x = q->q_first;
|
|
while (x) {
|
|
struct queue_entry *next = x->qe_next;
|
|
x->qe_next = x->qe_prev = NULL;
|
|
x = next;
|
|
}
|
|
|
|
q->q_first = q->q_last = NULL;
|
|
}
|