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.


58221a64d8cf9c384d8b915d32048ad73311bd79
[palacios.git] / linux_module / util-queue.c
1 /* 
2  * This file is part of the Palacios Virtual Machine Monitor developed
3  * by the V3VEE Project with funding from the United States National 
4  * Science Foundation and the Department of Energy.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
10  * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu> 
11  * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org> 
12  * All rights reserved.
13  *
14  * Author: Jack Lange <jarusl@cs.northwestern.edu>
15  *
16  * This is free software.  You are permitted to use,
17  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
18  */
19
20 #include <linux/slab.h>
21
22 #include "util-queue.h"
23
24 void init_queue(struct gen_queue * queue, unsigned int max_entries) {
25     queue->num_entries = 0;
26     queue->max_entries = max_entries;
27
28     INIT_LIST_HEAD(&(queue->entries));
29     spin_lock_init(&(queue->lock));
30 }
31
32 struct gen_queue * create_queue(unsigned int max_entries) {
33     struct gen_queue * tmp_queue = kmalloc(sizeof(struct gen_queue), GFP_KERNEL);
34     init_queue(tmp_queue, max_entries);
35     return tmp_queue;
36 }
37
38 int enqueue(struct gen_queue * queue, void * entry) {
39     struct queue_entry * q_entry = NULL;
40     unsigned long flags;
41
42     if (queue->num_entries >= queue->max_entries) {
43         return -1;
44     }
45
46     q_entry = kmalloc(sizeof(struct queue_entry), GFP_KERNEL);
47
48     spin_lock_irqsave(&(queue->lock), flags);
49
50     q_entry->entry = entry;
51     list_add_tail(&(q_entry->node), &(queue->entries));
52     queue->num_entries++;
53
54     spin_unlock_irqrestore(&(queue->lock), flags);
55
56     return 0;
57 }
58
59
60 void * dequeue(struct gen_queue * queue) {
61     void * entry_val = 0;
62     unsigned long flags;
63
64     spin_lock_irqsave(&(queue->lock), flags);
65
66     if (!list_empty(&(queue->entries))) {
67         struct list_head * q_entry = queue->entries.next;
68         struct queue_entry * tmp_entry = list_entry(q_entry, struct queue_entry, node);
69         
70         entry_val = tmp_entry->entry;
71         list_del(q_entry);
72         kfree(tmp_entry);
73
74         queue->num_entries--;
75
76     }
77
78     spin_unlock_irqrestore(&(queue->lock), flags);
79
80     return entry_val;
81 }