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 based on cppcheck pass (Linux module and user)
[palacios.git] / linux_module / util-hashtable.c
index e5ac64a..9ee832b 100644 (file)
@@ -1,27 +1,45 @@
 /*
- * This file is part of the Palacios Virtual Machine Monitor developed
- * by the V3VEE Project with funding from the United States National
- * Science Foundation and the Department of Energy.
- *
- * The V3VEE Project is a joint project between Northwestern University
- * and the University of New Mexico.  You can find out more at
- * http://www.v3vee.org
- *
- * Copyright (c) 2011, Lei Xia <lxia@northwestern.edu>
- * Copyright (c) 2011, The V3VEE Project <http://www.v3vee.org>
- * All rights reserved.
- *
- * This is free software.  You are permitted to use, redistribute,
- * and modify it under the terms of the GNU General Public License
- * Version 2 (GPLv2).  The accompanying COPYING file contains the
- * full text of the license.
- */
+  Copyright (c) 2002, 2004, Christopher Clark
+  All rights reserved.
+  
+  Redistribution and use in source and binary forms, with or without
+  modification, are permitted provided that the following conditions
+  are met:
+  
+  * Redistributions of source code must retain the above copyright
+    notice, this list of conditions and the following disclaimer.
+  
+  * Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in the
+    documentation and/or other materials provided with the distribution.
+  
+  * Neither the name of the original author; nor the names of any contributors
+    may be used to endorse or promote products derived from this software
+    without specific prior written permission.
+  
+  
+  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+  A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/* Modifications made by Lei Xia */
+
 #include <linux/string.h>
 #include <linux/errno.h>
 #include <linux/preempt.h>
 #include <linux/sched.h>
+#include <linux/slab.h>
+
+#include "palacios.h" 
 #include "util-hashtable.h"
 
 
@@ -121,18 +139,18 @@ static inline uint_t indexFor(uint_t table_length, uint_t hash_value) {
     return (hash_value % table_length);
 };
 
-#define freekey(X) kfree(X)
+#define freekey(X) palacios_free(X)
 
 
 static void * tmp_realloc(void * old_ptr, uint_t old_size, uint_t new_size) {
-    void * new_buf = kmalloc(new_size, GFP_KERNEL);
+    void * new_buf = palacios_alloc(new_size);
 
     if (new_buf == NULL) {
        return NULL;
     }
 
     memcpy(new_buf, old_ptr, old_size);
-    kfree(old_ptr);
+    palacios_free(old_ptr);
 
     return new_buf;
 }
@@ -185,16 +203,21 @@ struct hashtable * palacios_create_htable(uint_t min_size,
        }
     }
 
-    htable = (struct hashtable *)kmalloc(sizeof(struct hashtable), GFP_KERNEL);
+    if (prime_index==prime_table_len) { 
+       // cannot build large enough hash table
+       return NULL;
+    }
+
+    htable = (struct hashtable *)palacios_alloc(sizeof(struct hashtable));
 
     if (htable == NULL) {
        return NULL; /*oom*/
     }
 
-    htable->table = (struct hash_entry **)kmalloc(sizeof(struct hash_entry*) * size, GFP_KERNEL);
+    htable->table = (struct hash_entry **)palacios_alloc(sizeof(struct hash_entry*) * size);
 
     if (htable->table == NULL) { 
-       kfree(htable); 
+       palacios_free(htable); 
        return NULL;  /*oom*/
     }
 
@@ -227,7 +250,7 @@ static int hashtable_expand(struct hashtable * htable) {
 
     new_size = primes[++(htable->prime_index)];
 
-    new_table = (struct hash_entry **)kmalloc(sizeof(struct hash_entry*) * new_size, GFP_KERNEL);
+    new_table = (struct hash_entry **)palacios_alloc(sizeof(struct hash_entry*) * new_size);
 
     if (new_table != NULL) {
         memset(new_table, 0, new_size * sizeof(struct hash_entry *));
@@ -247,7 +270,7 @@ static int hashtable_expand(struct hashtable * htable) {
            }
         }
 
-        kfree(htable->table);
+        palacios_free(htable->table);
 
         htable->table = new_table;
     } else {
@@ -309,7 +332,7 @@ int palacios_htable_insert(struct hashtable * htable, addr_t key, addr_t value)
        hashtable_expand(htable);
     }
 
-    new_entry = (struct hash_entry *)kmalloc(sizeof(struct hash_entry), GFP_KERNEL);
+    new_entry = (struct hash_entry *)palacios_alloc(sizeof(struct hash_entry));
 
     if (new_entry == NULL) { 
        (htable->entry_count)--; 
@@ -347,7 +370,7 @@ int palacios_htable_change(struct hashtable * htable, addr_t key, addr_t value,
         if ((hash_value == tmp_entry->hash) && (htable->eq_fn(key, tmp_entry->key))) {
 
            if (free_value) {
-               kfree((void *)(tmp_entry->value));
+               palacios_free((void *)(tmp_entry->value));
            }
 
            tmp_entry->value = value;
@@ -464,7 +487,7 @@ addr_t palacios_htable_remove(struct hashtable * htable, addr_t key, int free_ke
            if (free_key) {
                freekey((void *)(cursor->key));
            }
-           kfree(cursor);
+           palacios_free(cursor);
       
            return value;
        }
@@ -494,8 +517,8 @@ void palacios_free_htable(struct hashtable * htable, int free_values, int free_k
                if (free_keys) {
                    freekey((void *)(tmp->key)); 
                }
-               kfree((void *)(tmp->value)); 
-               kfree(tmp); 
+               palacios_free((void *)(tmp->value)); 
+               palacios_free(tmp); 
            }
        }
     } else {
@@ -511,12 +534,12 @@ void palacios_free_htable(struct hashtable * htable, int free_values, int free_k
                if (free_keys) {
                    freekey((void *)(tmp->key)); 
                }
-               kfree(tmp); 
+               palacios_free(tmp); 
            }
        }
     }
   
-    kfree(htable->table);
-    kfree(htable);
+    palacios_free(htable->table);
+    palacios_free(htable);
 }