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.


Further cleanup of vnet /proc interface
[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     bitmap->num_bits = num_bits;
28     bitmap->bits = V3_Malloc(num_bytes);
29
30     if (bitmap->bits == NULL) {
31         PrintError("Could not allocate bitmap of %d bits\n", num_bits);
32         return -1;
33     }
34     
35     memset(bitmap->bits, 0, num_bytes);
36
37     return 0;
38 }
39
40
41 void v3_bitmap_deinit(struct v3_bitmap * bitmap) {
42     V3_Free(bitmap->bits);
43 }
44
45
46 int v3_bitmap_reset(struct v3_bitmap * bitmap) {
47     int num_bytes = (bitmap->num_bits / 8) + ((bitmap->num_bits % 8) > 0);
48
49     memset(bitmap->bits, 0, num_bytes);
50
51     return 0;
52 }
53
54 int v3_bitmap_set(struct v3_bitmap * bitmap, int index) {
55     int major = index / 8;
56     int minor = index % 8;
57     int old_val = 0;
58
59     if (index > (bitmap->num_bits - 1)) {
60         PrintError("Index out of bitmap range: (pos = %d) (num_bits = %d)\n", 
61                    index, bitmap->num_bits);
62         return -1;
63     }
64
65     old_val = (bitmap->bits[major] & (0x1 << minor));
66     bitmap->bits[major] |= (0x1 << minor);
67
68     return old_val;
69 }
70
71
72 int v3_bitmap_clear(struct v3_bitmap * bitmap, int index) {
73     int major = index / 8;
74     int minor = index % 8;
75     int old_val = 0;
76
77     if (index > (bitmap->num_bits - 1)) {
78         PrintError("Index out of bitmap range: (pos = %d) (num_bits = %d)\n", 
79                    index, bitmap->num_bits);
80         return -1;
81     }
82
83     old_val = (bitmap->bits[major] & (0x1 << minor));
84     bitmap->bits[major] &= ~(0x1 << minor);
85
86     return old_val;
87 }
88
89 int v3_bitmap_check(struct v3_bitmap * bitmap, int index) {
90     int major = index / 8;
91     int minor = index % 8;
92
93     if (index > (bitmap->num_bits - 1)) {
94         PrintError("Index out of bitmap range: (pos = %d) (num_bits = %d)\n", 
95                    index, bitmap->num_bits);
96         return -1;
97     }
98
99     return ((bitmap->bits[major] & (0x1 << minor)) != 0);
100 }
101
102
103 int v3_bitmap_count(struct v3_bitmap * bitmap) {
104
105     int cnt = 0;
106     int i;
107     uint8_t x;
108     uint8_t * bytes = bitmap->bits;
109     int num_bytes = (bitmap->num_bits / 8) + ((bitmap->num_bits % 8) > 0);
110
111     for (i=0; i < num_bytes; i++) {
112         x = bytes[i];
113         while (x) { 
114             cnt += (x & 0x1);
115             x>>=1;
116         }
117     }     
118     
119     return cnt;
120 }
121
122 int v3_bitmap_copy(struct v3_bitmap * dst, struct v3_bitmap * src) {
123     
124     if (src->num_bits != dst->num_bits) {
125         PrintError("src and dst must be the same size.\n");
126         return -1;    
127     }
128     
129     int num_bytes = (src->num_bits / 8) + ((src->num_bits % 8)!=0);
130     
131     memcpy(dst->bits,src->bits,num_bytes);
132     
133     return 0;
134 }