Use global buffer.
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Wed, 19 May 2010 16:15:38 +0000 (12:15 -0400)
committerThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Wed, 19 May 2010 16:15:38 +0000 (12:15 -0400)
hellochar.c

index 1649fe3..ee09b1e 100644 (file)
@@ -33,22 +33,16 @@ struct hello_buffer {
        size_t len;
        char buffer[0];
 };
+struct hello_buffer *hello;
 
 static int hello_open(struct inode *ino, struct file *fp)
 {
-       struct hello_buffer *hello = kzalloc(sizeof(*hello) + MAXLEN, GFP_KERNEL);
-       if (!hello)
-               return -ENOMEM;
-       memcpy(hello->buffer, default_greeting, sizeof(default_greeting));
-       hello->len = sizeof(default_greeting);
-       fp->private_data = hello;
        return 0;
 }
 
 static ssize_t hello_read(struct file *fp, char __user *buf, size_t sz,
        loff_t *pos)
 {
-       struct hello_buffer *hello = fp->private_data;
        int r;
        if (sz + *pos > hello->len)
                sz = hello->len - *pos;
@@ -62,7 +56,6 @@ static ssize_t hello_read(struct file *fp, char __user *buf, size_t sz,
 static ssize_t hello_write(struct file *fp, const char __user *buf, size_t sz,
        loff_t *pos)
 {
-       struct hello_buffer *hello = fp->private_data;
        int r;
        if (sz + *pos > MAXLEN)
                sz = MAXLEN - *pos;
@@ -77,7 +70,6 @@ static ssize_t hello_write(struct file *fp, const char __user *buf, size_t sz,
 
 static int hello_release(struct inode *ino, struct file *fp)
 {
-       kfree(fp->private_data);
        return 0;
 }
 
@@ -92,9 +84,16 @@ static const struct file_operations hello_fops = {
 static int __init ch_init(void)
 {
        int r = 0;
+       hello = kzalloc(sizeof(*hello) + MAXLEN, GFP_KERNEL);
+       if (!hello) {
+               r = -ENOMEM;
+               goto out;
+       }
+       memcpy(hello->buffer, default_greeting, sizeof(default_greeting));
+       hello->len = sizeof(default_greeting);
        r = alloc_chrdev_region(&devnum, 0, 256, "hello");
        if (r)
-               goto out;
+               goto reg_out;
        dev = cdev_alloc();
        if (!dev) {
                r = -ENOMEM;
@@ -110,6 +109,8 @@ add_out:
        kfree(dev);
 cdev_out:
        unregister_chrdev_region(devnum, 256);
+reg_out:
+       kfree(hello);
 out:
        return r;
 }
@@ -118,6 +119,7 @@ static void __exit ch_exit(void)
 {
        cdev_del(dev);
        unregister_chrdev_region(devnum, 256);
+       kfree(hello);
 }
 
 module_init(ch_init);