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.


Merge branch 'devel' of newskysaw.cs.northwestern.edu:/home/palacios/palacios into...
[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 "util-queue.h"
9
10 void init_queue(struct gen_queue * queue, unsigned int max_entries) {
11     queue->num_entries = 0;
12     queue->max_entries = max_entries;
13
14     INIT_LIST_HEAD(&(queue->entries));
15     spin_lock_init(&(queue->lock));
16 }
17
18 struct gen_queue * create_queue(unsigned int max_entries) {
19     struct gen_queue * tmp_queue = kmalloc(sizeof(struct gen_queue), GFP_KERNEL);
20     init_queue(tmp_queue, max_entries);
21     return tmp_queue;
22 }
23
24 int enqueue(struct gen_queue * queue, void * entry) {
25     struct queue_entry * q_entry = NULL;
26     unsigned long flags;
27
28     if (queue->num_entries >= queue->max_entries) {
29         return -1;
30     }
31
32     q_entry = kmalloc(sizeof(struct queue_entry), GFP_KERNEL);
33
34     spin_lock_irqsave(&(queue->lock), flags);
35
36     q_entry->entry = entry;
37     list_add_tail(&(q_entry->node), &(queue->entries));
38     queue->num_entries++;
39
40     spin_unlock_irqrestore(&(queue->lock), flags);
41
42     return 0;
43 }
44
45
46 void * dequeue(struct gen_queue * queue) {
47     void * entry_val = 0;
48     unsigned long flags;
49
50     spin_lock_irqsave(&(queue->lock), flags);
51
52     if (!list_empty(&(queue->entries))) {
53         struct list_head * q_entry = queue->entries.next;
54         struct queue_entry * tmp_entry = list_entry(q_entry, struct queue_entry, node);
55         
56         entry_val = tmp_entry->entry;
57         list_del(q_entry);
58         kfree(tmp_entry);
59
60         queue->num_entries--;
61
62     }
63
64     spin_unlock_irqrestore(&(queue->lock), flags);
65
66     return entry_val;
67 }