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.


Cleanup and sanity-checking of before/after null-check and copy+paste errors (Coverit...
[palacios.git] / palacios / src / palacios / vmm_bitmap.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) 2011, Jack Lange <jacklange@cs.pitt.edu> 
11  * Copyright (c) 2011, The V3VEE Project <http://www.v3vee.org> 
12  * All rights reserved.
13  *
14  * Author: Jack Lange <jacklange@cs.pitt.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 <palacios/vmm_bitmap.h>
21 #include <palacios/vmm.h>
22
23
24 int v3_bitmap_init(struct v3_bitmap * bitmap, int num_bits) {
25     int num_bytes = (num_bits / 8) + ((num_bits % 8) > 0);
26
27     v3_lock_init(&(bitmap->lock));
28     bitmap->num_bits = num_bits;
29     bitmap->bits = V3_Malloc(num_bytes);
30
31
32     if (bitmap->bits == NULL) {
33         PrintError(VM_NONE, VCORE_NONE, "Could not allocate bitmap of %d bits\n", num_bits);
34         return -1;
35     }
36     
37     memset(bitmap->bits, 0, num_bytes);
38
39     return 0;
40 }
41
42
43 void v3_bitmap_deinit(struct v3_bitmap * bitmap) {
44     v3_lock_deinit(&(bitmap->lock));
45     V3_Free(bitmap->bits);
46 }
47
48
49 int v3_bitmap_reset(struct v3_bitmap * bitmap) {
50     int num_bytes = (bitmap->num_bits / 8) + ((bitmap->num_bits % 8) > 0);
51
52     memset(bitmap->bits, 0, num_bytes);
53
54     return 0;
55 }
56
57 int v3_bitmap_set(struct v3_bitmap * bitmap, int index) {
58     int major = index / 8;
59     int minor = index % 8;
60     int old_val = 0;
61     uint32_t flags = 0;
62
63     if (index > (bitmap->num_bits - 1)) {
64         PrintError(VM_NONE, VCORE_NONE, "Index out of bitmap range: (pos = %d) (num_bits = %d)\n", 
65                    index, bitmap->num_bits);
66         return -1;
67     }
68
69
70     flags = v3_lock_irqsave(bitmap->lock);
71
72     old_val = (bitmap->bits[major] & (0x1 << minor));
73     bitmap->bits[major] |= (0x1 << minor);
74
75     v3_unlock_irqrestore(bitmap->lock, flags);
76
77     return old_val;
78 }
79
80
81 int v3_bitmap_clear(struct v3_bitmap * bitmap, int index) {
82     int major = index / 8;
83     int minor = index % 8;
84     int old_val = 0;
85     uint32_t flags = 0;
86
87     if (index > (bitmap->num_bits - 1)) {
88         PrintError(VM_NONE, VCORE_NONE, "Index out of bitmap range: (pos = %d) (num_bits = %d)\n", 
89                    index, bitmap->num_bits);
90         return -1;
91     }
92
93     flags = v3_lock_irqsave(bitmap->lock);
94
95     old_val = (bitmap->bits[major] & (0x1 << minor));
96     bitmap->bits[major] &= ~(0x1 << minor);
97
98     v3_unlock_irqrestore(bitmap->lock, flags);
99
100     return old_val;
101 }
102
103 int v3_bitmap_check(struct v3_bitmap * bitmap, int index) {
104     int major = index / 8;
105     int minor = index % 8;
106
107     if (index > (bitmap->num_bits - 1)) {
108         PrintError(VM_NONE, VCORE_NONE, "Index out of bitmap range: (pos = %d) (num_bits = %d)\n", 
109                    index, bitmap->num_bits);
110         return -1;
111     }
112
113     return ((bitmap->bits[major] & (0x1 << minor)) != 0);
114 }
115
116
117 int v3_bitmap_count(struct v3_bitmap * bitmap) {
118
119     int cnt = 0;
120     int i;
121     uint8_t x;
122     uint8_t * bytes = bitmap->bits;
123     int num_bytes = (bitmap->num_bits / 8) + ((bitmap->num_bits % 8) > 0);
124
125     for (i=0; i < num_bytes; i++) {
126         x = bytes[i];
127         while (x) { 
128             cnt += (x & 0x1);
129             x>>=1;
130         }
131     }     
132     
133     return cnt;
134 }
135
136 int v3_bitmap_copy(struct v3_bitmap * dst, struct v3_bitmap * src) {
137     
138     if (src->num_bits != dst->num_bits) {
139         PrintError(VM_NONE, VCORE_NONE, "src and dst must be the same size.\n");
140         return -1;    
141     }
142     
143     int num_bytes = (src->num_bits / 8) + ((src->num_bits % 8)!=0);
144     
145     memcpy(dst->bits,src->bits,num_bytes);
146     
147     return 0;
148 }