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.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
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.
14 * Author: Jack Lange <jacklange@cs.pitt.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #include <palacios/vmm_bitmap.h>
21 #include <palacios/vmm.h>
24 int v3_bitmap_init(struct v3_bitmap * bitmap, int num_bits) {
25 int num_bytes = (num_bits / 8) + ((num_bits % 8) > 0);
27 v3_lock_init(&(bitmap->lock));
28 bitmap->num_bits = num_bits;
29 bitmap->bits = V3_Malloc(num_bytes);
32 if (bitmap->bits == NULL) {
33 PrintError("Could not allocate bitmap of %d bits\n", num_bits);
37 memset(bitmap->bits, 0, num_bytes);
43 void v3_bitmap_deinit(struct v3_bitmap * bitmap) {
44 V3_Free(bitmap->bits);
48 int v3_bitmap_reset(struct v3_bitmap * bitmap) {
49 int num_bytes = (bitmap->num_bits / 8) + ((bitmap->num_bits % 8) > 0);
51 memset(bitmap->bits, 0, num_bytes);
56 int v3_bitmap_set(struct v3_bitmap * bitmap, int index) {
57 int major = index / 8;
58 int minor = index % 8;
62 if (index > (bitmap->num_bits - 1)) {
63 PrintError("Index out of bitmap range: (pos = %d) (num_bits = %d)\n",
64 index, bitmap->num_bits);
69 flags = v3_lock_irqsave(bitmap->lock);
71 old_val = (bitmap->bits[major] & (0x1 << minor));
72 bitmap->bits[major] |= (0x1 << minor);
74 v3_unlock_irqrestore(bitmap->lock, flags);
80 int v3_bitmap_clear(struct v3_bitmap * bitmap, int index) {
81 int major = index / 8;
82 int minor = index % 8;
86 if (index > (bitmap->num_bits - 1)) {
87 PrintError("Index out of bitmap range: (pos = %d) (num_bits = %d)\n",
88 index, bitmap->num_bits);
92 flags = v3_lock_irqsave(bitmap->lock);
94 old_val = (bitmap->bits[major] & (0x1 << minor));
95 bitmap->bits[major] &= ~(0x1 << minor);
97 v3_unlock_irqrestore(bitmap->lock, flags);
102 int v3_bitmap_check(struct v3_bitmap * bitmap, int index) {
103 int major = index / 8;
104 int minor = index % 8;
106 if (index > (bitmap->num_bits - 1)) {
107 PrintError("Index out of bitmap range: (pos = %d) (num_bits = %d)\n",
108 index, bitmap->num_bits);
112 return ((bitmap->bits[major] & (0x1 << minor)) != 0);
116 int v3_bitmap_count(struct v3_bitmap * bitmap) {
121 uint8_t * bytes = bitmap->bits;
122 int num_bytes = (bitmap->num_bits / 8) + ((bitmap->num_bits % 8) > 0);
124 for (i=0; i < num_bytes; i++) {
135 int v3_bitmap_copy(struct v3_bitmap * dst, struct v3_bitmap * src) {
137 if (src->num_bits != dst->num_bits) {
138 PrintError("src and dst must be the same size.\n");
142 int num_bytes = (src->num_bits / 8) + ((src->num_bits % 8)!=0);
144 memcpy(dst->bits,src->bits,num_bytes);