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 bitmap->num_bits = num_bits;
28 bitmap->bits = V3_Malloc(num_bytes);
30 if (bitmap->bits == NULL) {
31 PrintError("Could not allocate bitmap of %d bits\n", num_bits);
35 memset(bitmap->bits, 0, num_bytes);
41 void v3_bitmap_deinit(struct v3_bitmap * bitmap) {
42 V3_Free(bitmap->bits);
46 int v3_bitmap_reset(struct v3_bitmap * bitmap) {
47 int num_bytes = (bitmap->num_bits / 8) + ((bitmap->num_bits % 8) > 0);
49 memset(bitmap->bits, 0, num_bytes);
54 int v3_bitmap_set(struct v3_bitmap * bitmap, int index) {
55 int major = index / 8;
56 int minor = index % 8;
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);
65 old_val = (bitmap->bits[major] & (0x1 << minor));
66 bitmap->bits[major] |= (0x1 << minor);
72 int v3_bitmap_clear(struct v3_bitmap * bitmap, int index) {
73 int major = index / 8;
74 int minor = index % 8;
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);
83 old_val = (bitmap->bits[major] & (0x1 << minor));
84 bitmap->bits[major] &= ~(0x1 << minor);
89 int v3_bitmap_check(struct v3_bitmap * bitmap, int index) {
90 int major = index / 8;
91 int minor = index % 8;
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);
99 return ((bitmap->bits[major] & (0x1 << minor)) != 0);