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
index c056a5b..f53ead1 100644 (file)
 int v3_bitmap_init(struct v3_bitmap * bitmap, int num_bits) {
     int num_bytes = (num_bits / 8) + ((num_bits % 8) > 0);
 
+    v3_lock_init(&(bitmap->lock));
     bitmap->num_bits = num_bits;
     bitmap->bits = V3_Malloc(num_bytes);
 
+
     if (bitmap->bits == NULL) {
-       PrintError("Could not allocate bitmap of %d bits\n", num_bits);
+       PrintError(VM_NONE, VCORE_NONE, "Could not allocate bitmap of %d bits\n", num_bits);
        return -1;
     }
     
@@ -39,6 +41,7 @@ int v3_bitmap_init(struct v3_bitmap * bitmap, int num_bits) {
 
 
 void v3_bitmap_deinit(struct v3_bitmap * bitmap) {
+    v3_lock_deinit(&(bitmap->lock));
     V3_Free(bitmap->bits);
 }
 
@@ -55,16 +58,22 @@ int v3_bitmap_set(struct v3_bitmap * bitmap, int index) {
     int major = index / 8;
     int minor = index % 8;
     int old_val = 0;
+    uint32_t flags = 0;
 
     if (index > (bitmap->num_bits - 1)) {
-       PrintError("Index out of bitmap range: (pos = %d) (num_bits = %d)\n", 
+       PrintError(VM_NONE, VCORE_NONE, "Index out of bitmap range: (pos = %d) (num_bits = %d)\n", 
                   index, bitmap->num_bits);
        return -1;
     }
 
+
+    flags = v3_lock_irqsave(bitmap->lock);
+
     old_val = (bitmap->bits[major] & (0x1 << minor));
     bitmap->bits[major] |= (0x1 << minor);
 
+    v3_unlock_irqrestore(bitmap->lock, flags);
+
     return old_val;
 }
 
@@ -73,16 +82,21 @@ int v3_bitmap_clear(struct v3_bitmap * bitmap, int index) {
     int major = index / 8;
     int minor = index % 8;
     int old_val = 0;
+    uint32_t flags = 0;
 
     if (index > (bitmap->num_bits - 1)) {
-       PrintError("Index out of bitmap range: (pos = %d) (num_bits = %d)\n", 
+       PrintError(VM_NONE, VCORE_NONE, "Index out of bitmap range: (pos = %d) (num_bits = %d)\n", 
                   index, bitmap->num_bits);
        return -1;
     }
 
+    flags = v3_lock_irqsave(bitmap->lock);
+
     old_val = (bitmap->bits[major] & (0x1 << minor));
     bitmap->bits[major] &= ~(0x1 << minor);
 
+    v3_unlock_irqrestore(bitmap->lock, flags);
+
     return old_val;
 }
 
@@ -91,11 +105,44 @@ int v3_bitmap_check(struct v3_bitmap * bitmap, int index) {
     int minor = index % 8;
 
     if (index > (bitmap->num_bits - 1)) {
-       PrintError("Index out of bitmap range: (pos = %d) (num_bits = %d)\n", 
+       PrintError(VM_NONE, VCORE_NONE, "Index out of bitmap range: (pos = %d) (num_bits = %d)\n", 
                   index, bitmap->num_bits);
        return -1;
     }
 
-    return (bitmap->bits[major] & (0x1 << minor));
+    return ((bitmap->bits[major] & (0x1 << minor)) != 0);
+}
+
+
+int v3_bitmap_count(struct v3_bitmap * bitmap) {
+
+    int cnt = 0;
+    int i;
+    uint8_t x;
+    uint8_t * bytes = bitmap->bits;
+    int num_bytes = (bitmap->num_bits / 8) + ((bitmap->num_bits % 8) > 0);
+
+    for (i=0; i < num_bytes; i++) {
+        x = bytes[i];
+        while (x) { 
+           cnt += (x & 0x1);
+           x>>=1;
+       }
+    }     
+    
+    return cnt;
 }
 
+int v3_bitmap_copy(struct v3_bitmap * dst, struct v3_bitmap * src) {
+    
+    if (src->num_bits != dst->num_bits) {
+        PrintError(VM_NONE, VCORE_NONE, "src and dst must be the same size.\n");
+       return -1;    
+    }
+    
+    int num_bytes = (src->num_bits / 8) + ((src->num_bits % 8)!=0);
+    
+    memcpy(dst->bits,src->bits,num_bytes);
+    
+    return 0;
+}