Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Avoid strict-aliasing related issues when compiling with optimization
[palacios.git] / linux_module / util-queue.c
1 /* 
2  * Queue implementation
3  * Jack Lange 2011
4  */
5
6 #include <linux/slab.h>
7
8 #include "palacios.h"
9 #include "util-queue.h"
10
11 void init_queue(struct gen_queue * queue, unsigned int max_entries) {
12     queue->num_entries = 0;
13     queue->max_entries = max_entries;
14
15     INIT_LIST_HEAD(&(queue->entries));
16     palacios_spinlock_init(&(queue->lock));
17 }
18
19 void deinit_queue(struct gen_queue * queue) {
20     while (dequeue(queue)) {
21         ERROR("Freeing non-empty queue. PROBABLE MEMORY LEAK DETECTED\n");
22     }
23     palacios_spinlock_deinit(&(queue->lock));
24 }
25
26 struct gen_queue * create_queue(unsigned int max_entries) {
27     struct gen_queue * tmp_queue = palacios_alloc(sizeof(struct gen_queue));
28     if (!tmp_queue) { 
29         ERROR("Unable to allocate a queue\n");
30         return NULL;
31     }
32     init_queue(tmp_queue, max_entries);
33     return tmp_queue;
34 }
35
36 int enqueue(struct gen_queue * queue, void * entry) {
37     struct queue_entry * q_entry = NULL;
38     unsigned long flags;
39
40     if (queue->num_entries >= queue->max_entries) {
41         return -1;
42     }
43
44     q_entry = palacios_alloc(sizeof(struct queue_entry));
45     
46     if (!q_entry) { 
47         ERROR("Unable to allocate a queue entry on enqueue\n");
48         return -1;
49     }
50
51     palacios_spinlock_lock_irqsave(&(queue->lock), flags);
52
53     q_entry->entry = entry;
54     list_add_tail(&(q_entry->node), &(queue->entries));
55     queue->num_entries++;
56
57     palacios_spinlock_unlock_irqrestore(&(queue->lock), flags);
58
59     return 0;
60 }
61
62
63 void * dequeue(struct gen_queue * queue) {
64     void * entry_val = 0;
65     unsigned long flags;
66
67     palacios_spinlock_lock_irqsave(&(queue->lock), flags);
68
69     if (!list_empty(&(queue->entries))) {
70         struct list_head * q_entry = queue->entries.next;
71         struct queue_entry * tmp_entry = list_entry(q_entry, struct queue_entry, node);
72         
73         entry_val = tmp_entry->entry;
74         list_del(q_entry);
75         palacios_free(tmp_entry);
76
77         queue->num_entries--;
78
79     }
80
81     palacios_spinlock_unlock_irqrestore(&(queue->lock), flags);
82
83     return entry_val;
84 }