6 #include <linux/slab.h>
9 #include "util-queue.h"
11 void init_queue(struct gen_queue * queue, unsigned int max_entries) {
12 queue->num_entries = 0;
13 queue->max_entries = max_entries;
15 INIT_LIST_HEAD(&(queue->entries));
16 spin_lock_init(&(queue->lock));
19 void deinit_queue(struct gen_queue * queue) {
20 while (dequeue(queue)) {
21 ERROR("Freeing non-empty queue. PROBABLE MEMORY LEAK DETECTED\n");
25 struct gen_queue * create_queue(unsigned int max_entries) {
26 struct gen_queue * tmp_queue = palacios_alloc(sizeof(struct gen_queue));
28 ERROR("Unable to allocate a queue\n");
31 init_queue(tmp_queue, max_entries);
35 int enqueue(struct gen_queue * queue, void * entry) {
36 struct queue_entry * q_entry = NULL;
39 if (queue->num_entries >= queue->max_entries) {
43 q_entry = palacios_alloc(sizeof(struct queue_entry));
46 ERROR("Unable to allocate a queue entry on enqueue\n");
50 spin_lock_irqsave(&(queue->lock), flags);
52 q_entry->entry = entry;
53 list_add_tail(&(q_entry->node), &(queue->entries));
56 spin_unlock_irqrestore(&(queue->lock), flags);
62 void * dequeue(struct gen_queue * queue) {
66 spin_lock_irqsave(&(queue->lock), flags);
68 if (!list_empty(&(queue->entries))) {
69 struct list_head * q_entry = queue->entries.next;
70 struct queue_entry * tmp_entry = list_entry(q_entry, struct queue_entry, node);
72 entry_val = tmp_entry->entry;
74 palacios_free(tmp_entry);
80 spin_unlock_irqrestore(&(queue->lock), flags);