diff options
author | Guillermo Ramos | 2011-09-19 01:00:27 +0200 |
---|---|---|
committer | Guillermo Ramos | 2011-09-19 01:00:27 +0200 |
commit | 68459ada2db3d8626b1957e220a9abe8aff6c1e3 (patch) | |
tree | b7a07d789b37d2d9dcab5c7b94c41241347896d4 /khashmap | |
parent | 974a8595ed3c09ccef62df7e3748baddb9038d8e (diff) | |
download | evspy-68459ada2db3d8626b1957e220a9abe8aff6c1e3.tar.gz |
Added license and done a little cleanup of code
Diffstat (limited to 'khashmap')
-rw-r--r-- | khashmap/khashmap.c | 133 | ||||
-rw-r--r-- | khashmap/khashmap.h | 20 | ||||
-rw-r--r-- | khashmap/test_khashmap.c | 61 |
3 files changed, 0 insertions, 214 deletions
diff --git a/khashmap/khashmap.c b/khashmap/khashmap.c deleted file mode 100644 index fac95f7..0000000 --- a/khashmap/khashmap.c +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Copyright (c) 2011 Guillermo Ramos <0xwille@gmail.com> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Should you need to contact me, the author, you can mail your message to - * <0xwille@gmail.com> - */ - -#include <linux/errno.h> -#include <linux/list.h> -#include <linux/slab.h> -#include "khashmap.h" - -static inline struct khashmap *khm_search(struct khashmap *head, int value) -{ - struct list_head *list; - struct khashmap *node; - - list_for_each(list, &head->l) { - node = list_entry(list, struct khashmap, l); - if (node->value == value) - return node; - } - - return NULL; -} - -void khm_display(struct khashmap *head) -{ - struct khashmap *node; - struct list_head *list; - int i = 0; - int limit = 10; - - printk(KERN_ALERT "Displaying..."); - list_for_each(list, &head->l) { - node = list_entry(list, struct khashmap, l); - printk(KERN_ALERT " N%d (%p) v=%d d=%s\n", i++, node, - node->value, (char*)node->data); - if (i >= limit) - break; - } -} - -inline struct khashmap *khm_create(void) -{ - struct khashmap *head; - - head = kmalloc(sizeof(struct khashmap), GFP_KERNEL); - if (!head) - return NULL; - - INIT_LIST_HEAD(&head->l); - - return head; -} - -void khm_destroy(struct khashmap *head) -{ - struct khashmap *node; - struct list_head *list = head->l.next; - - while (list != &head->l) { - node = list_entry(list, struct khashmap, l); - list = list->next; - kfree(node); - } - - kfree(head); -} - -int khm_insert(struct khashmap *head, int value, void *data) -{ - struct khashmap *new; - - // Key already exists - if (khm_search(head, value)) - return -EINVAL; - - new = khm_create(); - if (!new) - return -ENOMEM; - - new->value = value; - new->data = data; - list_add_tail(&new->l, &head->l); - - return 0; -} - -int khm_delete(struct khashmap *head, int value) -{ - struct khashmap *node = khm_search(head, value); - - if (node) { - list_del(&node->l); - kfree(node); - return 0; - } else { - return -EINVAL; - } -} - -void *khm_get(struct khashmap *head, int value) -{ - struct khashmap *node = khm_search(head, value); - - if (node) - return node->data; - else - return NULL; -} - -void khm_set(struct khashmap *head, int value, void *data) -{ - struct khashmap *node = khm_search(head, value); - - if (node) - node->data = data; -} diff --git a/khashmap/khashmap.h b/khashmap/khashmap.h deleted file mode 100644 index b5ee506..0000000 --- a/khashmap/khashmap.h +++ /dev/null @@ -1,20 +0,0 @@ -#include <linux/list.h> - -#ifndef HASHMAP -#define HASHMAP - -struct khashmap { - struct list_head l; - int value; - void *data; -}; - -struct khashmap *khm_create(void); -void khm_destroy(struct khashmap *head); -int khm_insert(struct khashmap *head, int value, void *data); -int khm_delete(struct khashmap *head, int value); -void *khm_get(struct khashmap *head, int value); -void khm_set(struct khashmap *head, int value, void *data); -void khm_display(struct khashmap *head); - -#endif diff --git a/khashmap/test_khashmap.c b/khashmap/test_khashmap.c deleted file mode 100644 index 16a351f..0000000 --- a/khashmap/test_khashmap.c +++ /dev/null @@ -1,61 +0,0 @@ -#include <linux/module.h> -#include <linux/init.h> -#include "khashmap.h" - -#define Z_ASD 1 -#define Z_QWE 2 -#define Z_BLR 3 - -static struct khashmap *hm; - -static int __init test_init(void) -{ - char *values; - - hm = khm_create(); - - if (khm_insert(hm, Z_ASD, "ASDF")) - goto insert_err; - if (khm_insert(hm, Z_QWE, "QWERTY")) - goto insert_err; - if (khm_insert(hm, Z_BLR, "BLRBLRBLR")) - goto insert_err; - - khm_display(hm); - khm_delete(hm, Z_QWE); - khm_display(hm); - khm_set(hm, Z_ASD, "ASDF NEW!!"); - khm_display(hm); - - if ((values = (char*)khm_get(hm, Z_ASD))) - printk(KERN_ALERT "Z_ASD: %s\n", values); - else - printk(KERN_ALERT "Z_ASD does not exist\n"); - if ((values = (char*)khm_get(hm, Z_QWE))) - printk(KERN_ALERT "Z_QWE: %s\n", values); - else - printk(KERN_ALERT "Z_QWE does not exist\n"); - if ((values = (char*)khm_get(hm, Z_BLR))) - printk(KERN_ALERT "Z_BLR: %s\n", values); - else - printk(KERN_ALERT "Z_BLR does not exist\n"); - - if (khm_insert(hm, Z_ASD, "MALO!!")) - goto insert_err; - - return 0; - -insert_err: - printk(KERN_ALERT "Error inserting!\n"); - return 0; -} - -static void __exit test_exit(void) -{ - khm_destroy(hm); -} - -module_init(test_init); -module_exit(test_exit); - -MODULE_LICENSE("GPL"); |