From 55a2212feb5042e65fbf0ed8891c0cab55479621 Mon Sep 17 00:00:00 2001 From: Guillermo Ramos Date: Sat, 24 Sep 2011 18:13:15 +0200 Subject: Renamed map ADT to kmap --- Makefile | 2 +- README | 2 +- evspy-core.h | 6 +-- khm/Makefile | 8 --- khm/khm.c | 154 ------------------------------------------------------- khm/khm.h | 41 --------------- khm/test_khm.c | 66 ------------------------ kmap/Makefile | 8 +++ kmap/kmap.c | 154 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ kmap/kmap.h | 41 +++++++++++++++ kmap/test_kmap.c | 66 ++++++++++++++++++++++++ maps/map_en.h | 8 +-- maps/map_es.h | 16 +++--- maps/maps.h | 6 +-- 14 files changed, 289 insertions(+), 289 deletions(-) delete mode 100644 khm/Makefile delete mode 100644 khm/khm.c delete mode 100644 khm/khm.h delete mode 100644 khm/test_khm.c create mode 100644 kmap/Makefile create mode 100644 kmap/kmap.c create mode 100644 kmap/kmap.h create mode 100644 kmap/test_kmap.c diff --git a/Makefile b/Makefile index 3d10b64..573b77f 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ obj-m += evspy.o -evspy-objs := khm/khm.o evspy-core.o +evspy-objs := kmap/kmap.o evspy-core.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules diff --git a/README b/README index 684a29c..b5a58cf 100644 --- a/README +++ b/README @@ -41,7 +41,7 @@ Once it has been installed, you can load it when you want with A patch is supplied (evspy.patch) to be able to compile a kernel with evspy included. If KERN is the directory where your kernel is located, just copy the -patch there (KERN/) and copy all the evspy files (*.c, *.h, maps, khm) to +patch there (KERN/) and copy all the evspy files (*.c, *.h, maps, kmap) to KERN/drivers/input/. Then, cd to KERN and apply the patch: $ patch -p1 < evspy.patch diff --git a/evspy-core.h b/evspy-core.h index eec489c..dfc0b2e 100644 --- a/evspy-core.h +++ b/evspy-core.h @@ -30,7 +30,7 @@ #include #include #include -#include "khm/khm.h" +#include "kmap/kmap.h" // Keyboard layouts #define EVS_KLAY_EN 0 @@ -130,7 +130,7 @@ } else if (is_ascii(c) || !shift_on) { \ __c = map[c]; \ } else { \ - if ((__vp = khm_get(shm, (c)))) \ + if ((__vp = kmap_get(skm, (c)))) \ __c = *(char *)__vp; \ else \ __c = map[c]; \ @@ -146,7 +146,7 @@ ({ \ void *__vp; \ char __c; \ - __vp = khm_get(ahm, (c)); \ + __vp = kmap_get(akm, (c)); \ if (__vp) __c = *(char *)__vp; \ else __c = map[c]; \ __c; \ diff --git a/khm/Makefile b/khm/Makefile deleted file mode 100644 index 74ffd5f..0000000 --- a/khm/Makefile +++ /dev/null @@ -1,8 +0,0 @@ -obj-m += test.o -test-objs := khm.o test_khm.o - -all: - make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules - -clean: - make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean diff --git a/khm/khm.c b/khm/khm.c deleted file mode 100644 index a22b955..0000000 --- a/khm/khm.c +++ /dev/null @@ -1,154 +0,0 @@ -/* - * khm - (Linux) kernel hash map implementation - * - * Copyright (c) 2011 Guillermo Ramos <0xwille@gmail.com> - * - * This file is part of khm - * - * khm 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 3 of the License, or - * (at your option) any later version. - * - * khm 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 khm. If not, see . - */ - -#include -#include -#include -#include "khm.h" - -/* - * Returns the node with the value passed, or NULL in case it does not exist - */ -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; -} - -/* - * Prints in klog a representation of all the nodes in the hashmap - */ -void khm_display(struct khashmap *head) -{ - struct khashmap *node; - struct list_head *list; - - printk(KERN_ALERT "Displaying..."); - list_for_each(list, &head->l) { - node = list_entry(list, struct khashmap, l); - printk(KERN_ALERT " %p: v=%d d=%s\n", - node, node->value, (char*)node->data); - } -} - -/* - * Creates and returns a new head node - */ -inline struct khashmap *khm_create(void) -{ - struct khashmap *head; - - head = kmalloc(sizeof(struct khashmap), GFP_KERNEL); - if (unlikely(!head)) - return NULL; - - INIT_LIST_HEAD(&head->l); - - return head; -} - -/* - * Deletes the hashmap and frees the memory used by its nodes - */ -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); -} - -/* - * Creates a new node with the given value and data, and adds it to the hm 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 (unlikely(!new)) - return -ENOMEM; - - new->value = value; - new->data = data; - list_add_tail(&new->l, &head->l); - - return 0; -} - -/* - * Removes from the hashmap the node with the given value. - */ -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; - } -} - -/* - * Returns the data mapped to the given value, or NULL if that value has no data - * associated to it - */ -void *khm_get(struct khashmap *head, int value) -{ - struct khashmap *node = khm_search(head, value); - - if (node) - return node->data; - else - return NULL; -} - -/* - * Associates the given value with new data, if the value was already in the hm - */ -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/khm/khm.h b/khm/khm.h deleted file mode 100644 index f917f9b..0000000 --- a/khm/khm.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * khm - (Linux) kernel hash map implementation - * - * Copyright (c) 2011 Guillermo Ramos <0xwille@gmail.com> - * - * This file is part of khm - * - * khm 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 3 of the License, or - * (at your option) any later version. - * - * khm 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 khm. If not, see . - */ - -#include - -#ifndef KHM -#define KHM - -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 // KHM diff --git a/khm/test_khm.c b/khm/test_khm.c deleted file mode 100644 index d8b0180..0000000 --- a/khm/test_khm.c +++ /dev/null @@ -1,66 +0,0 @@ -#include -#include -#include "khm.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 ((values = (char*)khm_get(hm, Z_BLR))) - printk(KERN_ALERT "ERROR: Z_BLR exists: %s\n", values); - else - printk(KERN_ALERT "Z_BLR does not exist (good)\n"); - - 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, "REPEATED!!")) - goto insert_err; - - return 0; - -insert_err: - printk(KERN_ALERT "Error inserting (good)\n"); - return 0; -} - -static void __exit test_exit(void) -{ - khm_destroy(hm); -} - -module_init(test_init); -module_exit(test_exit); - -MODULE_LICENSE("GPL"); diff --git a/kmap/Makefile b/kmap/Makefile new file mode 100644 index 0000000..62aa5c8 --- /dev/null +++ b/kmap/Makefile @@ -0,0 +1,8 @@ +obj-m += test.o +test-objs := kmap.o test_kmap.o + +all: + make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules + +clean: + make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean diff --git a/kmap/kmap.c b/kmap/kmap.c new file mode 100644 index 0000000..2db6b82 --- /dev/null +++ b/kmap/kmap.c @@ -0,0 +1,154 @@ +/* + * kmap - (Linux) kernel map implementation + * + * Copyright (c) 2011 Guillermo Ramos <0xwille@gmail.com> + * + * This file is part of kmap + * + * kmap 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 3 of the License, or + * (at your option) any later version. + * + * kmap 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 kmap. If not, see . + */ + +#include +#include +#include +#include "kmap.h" + +/* + * Returns the node with the value passed, or NULL in case it does not exist + */ +static inline struct kmap *kmap_search(struct kmap *head, int value) +{ + struct list_head *list; + struct kmap *node; + + list_for_each(list, &head->l) { + node = list_entry(list, struct kmap, l); + if (node->value == value) + return node; + } + + return NULL; +} + +/* + * Prints in klog a representation of all the nodes in the map + */ +void kmap_display(struct kmap *head) +{ + struct kmap *node; + struct list_head *list; + + printk(KERN_ALERT "Displaying..."); + list_for_each(list, &head->l) { + node = list_entry(list, struct kmap, l); + printk(KERN_ALERT " %p: v=%d d=%s\n", + node, node->value, (char*)node->data); + } +} + +/* + * Creates and returns a new head node + */ +inline struct kmap *kmap_create(void) +{ + struct kmap *head; + + head = kmalloc(sizeof(struct kmap), GFP_KERNEL); + if (unlikely(!head)) + return NULL; + + INIT_LIST_HEAD(&head->l); + + return head; +} + +/* + * Deletes the map and frees the memory used by its nodes + */ +void kmap_destroy(struct kmap *head) +{ + struct kmap *node; + struct list_head *list = head->l.next; + + while (list != &head->l) { + node = list_entry(list, struct kmap, l); + list = list->next; + kfree(node); + } + + kfree(head); +} + +/* + * Creates a new node with the given value and data, and adds it to the map head + */ +int kmap_insert(struct kmap *head, int value, void *data) +{ + struct kmap *new; + + // Key already exists + if (kmap_search(head, value)) + return -EINVAL; + + new = kmap_create(); + if (unlikely(!new)) + return -ENOMEM; + + new->value = value; + new->data = data; + list_add_tail(&new->l, &head->l); + + return 0; +} + +/* + * Removes from the map the node with the given value. + */ +int kmap_delete(struct kmap *head, int value) +{ + struct kmap *node = kmap_search(head, value); + + if (node) { + list_del(&node->l); + kfree(node); + return 0; + } else { + return -EINVAL; + } +} + +/* + * Returns the data mapped to the given value, or NULL if that value has no data + * associated to it + */ +void *kmap_get(struct kmap *head, int value) +{ + struct kmap *node = kmap_search(head, value); + + if (node) + return node->data; + else + return NULL; +} + +/* + * Associates the given value with new data, if the value was already in the map + */ +void kmap_set(struct kmap *head, int value, void *data) +{ + struct kmap *node = kmap_search(head, value); + + if (node) + node->data = data; +} diff --git a/kmap/kmap.h b/kmap/kmap.h new file mode 100644 index 0000000..7d6e8ad --- /dev/null +++ b/kmap/kmap.h @@ -0,0 +1,41 @@ +/* + * kmap - (Linux) kernel map implementation + * + * Copyright (c) 2011 Guillermo Ramos <0xwille@gmail.com> + * + * This file is part of kmap + * + * kmap 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 3 of the License, or + * (at your option) any later version. + * + * kmap 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 kmap. If not, see . + */ + +#include + +#ifndef KHM +#define KHM + +struct kmap { + struct list_head l; + int value; + void *data; +}; + +struct kmap *kmap_create(void); +void kmap_destroy(struct kmap *head); +int kmap_insert(struct kmap *head, int value, void *data); +int kmap_delete(struct kmap *head, int value); +void *kmap_get(struct kmap *head, int value); +void kmap_set(struct kmap *head, int value, void *data); +void kmap_display(struct kmap *head); + +#endif // KHM diff --git a/kmap/test_kmap.c b/kmap/test_kmap.c new file mode 100644 index 0000000..577eeb5 --- /dev/null +++ b/kmap/test_kmap.c @@ -0,0 +1,66 @@ +#include +#include +#include "kmap.h" + +#define Z_ASD 1 +#define Z_QWE 2 +#define Z_BLR 3 + +static struct kmap *km; + +static int __init test_init(void) +{ + char *values; + + km = kmap_create(); + + if ((values = (char*)kmap_get(km, Z_BLR))) + printk(KERN_ALERT "ERROR: Z_BLR exists: %s\n", values); + else + printk(KERN_ALERT "Z_BLR does not exist (good)\n"); + + if (kmap_insert(km, Z_ASD, "ASDF")) + goto insert_err; + if (kmap_insert(km, Z_QWE, "QWERTY")) + goto insert_err; + if (kmap_insert(km, Z_BLR, "BLRBLRBLR")) + goto insert_err; + + kmap_display(km); + kmap_delete(km, Z_QWE); + kmap_display(km); + kmap_set(km, Z_ASD, "ASDF NEW!!"); + kmap_display(km); + + if ((values = (char*)kmap_get(km, Z_ASD))) + printk(KERN_ALERT "Z_ASD: %s\n", values); + else + printk(KERN_ALERT "Z_ASD does not exist\n"); + if ((values = (char*)kmap_get(km, Z_QWE))) + printk(KERN_ALERT "Z_QWE: %s\n", values); + else + printk(KERN_ALERT "Z_QWE does not exist\n"); + if ((values = (char*)kmap_get(km, Z_BLR))) + printk(KERN_ALERT "Z_BLR: %s\n", values); + else + printk(KERN_ALERT "Z_BLR does not exist\n"); + + if (kmap_insert(km, Z_ASD, "REPEATED!!")) + goto insert_err; + + return 0; + +insert_err: + printk(KERN_ALERT "Error inserting (good)\n"); + return 0; +} + +static void __exit test_exit(void) +{ + kmap_destroy(km); +} + +module_init(test_init); +module_exit(test_exit); + +MODULE_LICENSE("GPL"); diff --git a/maps/map_en.h b/maps/map_en.h index 93b9db7..56fd895 100644 --- a/maps/map_en.h +++ b/maps/map_en.h @@ -8,13 +8,13 @@ // Shift map -#define EVS_SHIFT_BIND(KEY, VALUE) khm_insert(shm, (KEY), (VALUE)) +#define EVS_SHIFT_BIND(KEY, VALUE) kmap_insert(skm, (KEY), (VALUE)) -static struct khashmap *shm; +static struct kmap *skm; static void init_shiftmap(void) { - shm = khm_create(); + skm = kmap_create(); EVS_SHIFT_BIND(KEY_1, "!"); EVS_SHIFT_BIND(KEY_2, "@"); EVS_SHIFT_BIND(KEY_3, "#"); @@ -41,7 +41,7 @@ static void init_shiftmap(void) static inline void exit_shiftmap(void) { - khm_destroy(shm); + kmap_destroy(skm); } static char map[] = { diff --git a/maps/map_es.h b/maps/map_es.h index 1b0946f..b20f120 100644 --- a/maps/map_es.h +++ b/maps/map_es.h @@ -9,13 +9,13 @@ // Right Alt (ALTGR) map #define EVS_ALTGR_ENABLED -#define EVS_ALTGR_BIND(KEY, VALUE) khm_insert(ahm, (KEY), (VALUE)) +#define EVS_ALTGR_BIND(KEY, VALUE) kmap_insert(akm, (KEY), (VALUE)) -static struct khashmap *ahm; +static struct kmap *akm; static void init_altgrmap(void) { - ahm = khm_create(); + akm = kmap_create(); EVS_ALTGR_BIND(KEY_1, "|"); EVS_ALTGR_BIND(KEY_2, "@"); EVS_ALTGR_BIND(KEY_3, "#"); @@ -41,17 +41,17 @@ static void init_altgrmap(void) static inline void exit_altgrmap(void) { - khm_destroy(ahm); + kmap_destroy(akm); } // Shift map -#define EVS_SHIFT_BIND(KEY, VALUE) khm_insert(shm, (KEY), (VALUE)) +#define EVS_SHIFT_BIND(KEY, VALUE) kmap_insert(skm, (KEY), (VALUE)) -static struct khashmap *shm; +static struct kmap *skm; static void init_shiftmap(void) { - shm = khm_create(); + skm = kmap_create(); EVS_SHIFT_BIND(KEY_1, "!"); EVS_SHIFT_BIND(KEY_2, "\""); EVS_SHIFT_BIND(KEY_4, "$"); @@ -75,7 +75,7 @@ static void init_shiftmap(void) static inline void exit_shiftmap(void) { - khm_destroy(shm); + kmap_destroy(skm); } static char map[] = { diff --git a/maps/maps.h b/maps/maps.h index c6e3095..969dc6f 100644 --- a/maps/maps.h +++ b/maps/maps.h @@ -11,13 +11,13 @@ * position is the key value associated with the event code (index). It also * defines a function called init_shiftmap that can register any number of shift * keys to be parsed when shift or capslock has been pressed, and exit_shiftmap, - * that cleans up the map (by default, it just destroys the hashmap used by the + * that cleans up the map (by default, it just destroys the map used by the * previous function). * * For example, * EVS_SHIFT(KEY_7, "/"); * associates the key of number 7 to the slash character when shift is pressed. - * Yeah, the character associated is a string: this is because of the hashmap + * Yeah, the character associated is a string: this is because of the map (kmap) * implementation, [int -> (void *)]. * * From version 0.3 up, you can also define how evspy should register AltGr-key @@ -34,7 +34,7 @@ #define __EVS_MAPS #include -#include "../khm/khm.h" +#include "../kmap/kmap.h" // Load the map associated with the given keyboard layout -- cgit v1.2.3