hop-2012/experiments/cmsg/dataq.c

52 lines
966 B
C

/* Copyright (C) 2010, 2011 Tony Garnock-Jones. All rights reserved. */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "dataq.h"
#define QLINK(q,x) (*((void **)(((char *) x) + (q)->link_offset)))
void enqueue(queue_t *q, void *x) {
QLINK(q, x) = NULL;
if (q->head == NULL) {
q->head = x;
} else {
QLINK(q, q->tail) = x;
}
q->tail = x;
q->count++;
}
void *dequeue(queue_t *q) {
if (q->head == NULL) {
return NULL;
} else {
void *x = q->head;
q->head = QLINK(q, x);
QLINK(q, x) = NULL;
if (q->head == NULL) {
q->tail = NULL;
}
q->count--;
return x;
}
}
void queue_append(queue_t *q1, queue_t *q2) {
assert(q1->link_offset == q2->link_offset);
if (q2->head != NULL) {
if (q1->head != NULL) {
QLINK(q1, q1->tail) = q2->head;
} else {
q1->head = q2->head;
}
q1->tail = q2->tail;
q2->head = NULL;
q2->tail = NULL;
}
}