#include #include #include #include "../include/utils.h" #include "../include/queue.h" #include "../include/message.h" /** * init_queue() * * Initializes empty queue and returns a pointer to it. */ queue *init_queue(void) { queue *Q; Q = kmalloc(sizeof(queue), GFP_KERNEL); if (!Q) { return NULL; } Q->len = 0; Q->total_size = 0; INIT_LIST_HEAD(&Q->list); return Q; } /** * enqueue() * * @q: pointer to queue. * @m: pointer to message. * * Inserts the message at the end of the given queue. */ void enqueue(queue *q, message *m) { queue *new_q; new_q = kmalloc(sizeof(queue), GFP_KERNEL); if (!new_q) { printk("%s - [ERROR]: Failed to allocate memory for queue element\n", MODNAME); return; } new_q->m = m; q->len++; q->total_size += m->length; // new entry is added at the end of the list list_add_tail(&(new_q->list), &(q->list)); } /** * dequeue() * * @q: pointer to queue. * * Extract message on top of the queue and returns a pointer to it */ message *dequeue(queue *q) { queue *tmp_q; message *m; if (q->len == 0) return NULL; // get message on top of queue tmp_q = list_entry(q->list.next, queue, list); list_del(q->list.next); m = tmp_q->m; // decrement size and free memory q->len--; q->total_size -= m->length; kfree(tmp_q); return m; } /** * flush_queue() * * @q: pointer to queue. * * Empties the content of a queue. */ void flush_queue(queue *q) { int i; int length = q->len; message *m; for (i = 0; i < length; i++) { m = dequeue(q); destroy_message(m); } }