X-Git-Url: http://git.cascardo.eti.br/?p=cascardo%2Fkernel%2Fsamples%2Fhello2%2F.git;a=blobdiff_plain;f=hello.c;h=bcf8de0017019adf29f02befa938f9ba3acb5b83;hp=8a5a20b0c6b8b3832f856dfa7e2078308945082c;hb=fcd4b1543f2ade695126260e5093432e682d57fb;hpb=832abd818e6f5b988e3ae64ce422c3cf2fc5d4e7 diff --git a/hello.c b/hello.c index 8a5a20b..bcf8de0 100644 --- a/hello.c +++ b/hello.c @@ -22,39 +22,50 @@ #include #include #include +#include MODULE_LICENSE("GPL"); MODULE_AUTHOR("Thadeu Lima de Souza Cascardo "); MODULE_DESCRIPTION("Hello, world"); -static int size = 1; -module_param_named(size, size, int, S_IRUGO | S_IWUSR); - struct hello { int times; + struct kref ref; char greeting[0]; }; -static struct hello *hello; +static struct hello *global_hello; static char default_greeting[] = "Hello, world!\n"; +static void hello_release(struct kref *ref) +{ + struct hello *hello = container_of(ref, struct hello, ref); + printk(KERN_DEBUG "releasing hello\n"); + kfree(hello); +} + static int hello_init(void) { + static struct hello *hello; hello = kmalloc(sizeof(*hello) + sizeof(default_greeting), GFP_KERNEL); if (!hello) return -ENOMEM; + kref_init(&hello->ref); + kref_get(&hello->ref); + global_hello = hello; memcpy(hello->greeting, default_greeting, sizeof(default_greeting)); printk("%s\n", hello->greeting); printk("%p %p %p\n", hello, hello->greeting, container_of(&hello->greeting, struct hello, greeting)); printk("size %d\n", ARRAY_SIZE(default_greeting)); + kref_put(&hello->ref, hello_release); return 0; } static __exit void hello_exit(void) { - kfree(hello); + kref_put(&global_hello->ref, hello_release); } module_init(hello_init);