projects
/
cascardo
/
kernel
/
samples
/
char2
/
.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Use global buffer.
[cascardo/kernel/samples/char2/.git]
/
hellochar.c
diff --git
a/hellochar.c
b/hellochar.c
index
1649fe3
..
ee09b1e
100644
(file)
--- a/
hellochar.c
+++ b/
hellochar.c
@@
-33,22
+33,16
@@
struct hello_buffer {
size_t len;
char buffer[0];
};
size_t len;
char buffer[0];
};
+struct hello_buffer *hello;
static int hello_open(struct inode *ino, struct file *fp)
{
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)
{
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;
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)
{
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;
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)
{
static int hello_release(struct inode *ino, struct file *fp)
{
- kfree(fp->private_data);
return 0;
}
return 0;
}
@@
-92,9
+84,16
@@
static const struct file_operations hello_fops = {
static int __init ch_init(void)
{
int r = 0;
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)
r = alloc_chrdev_region(&devnum, 0, 256, "hello");
if (r)
- goto out;
+ goto
reg_
out;
dev = cdev_alloc();
if (!dev) {
r = -ENOMEM;
dev = cdev_alloc();
if (!dev) {
r = -ENOMEM;
@@
-110,6
+109,8
@@
add_out:
kfree(dev);
cdev_out:
unregister_chrdev_region(devnum, 256);
kfree(dev);
cdev_out:
unregister_chrdev_region(devnum, 256);
+reg_out:
+ kfree(hello);
out:
return r;
}
out:
return r;
}
@@
-118,6
+119,7
@@
static void __exit ch_exit(void)
{
cdev_del(dev);
unregister_chrdev_region(devnum, 256);
{
cdev_del(dev);
unregister_chrdev_region(devnum, 256);
+ kfree(hello);
}
module_init(ch_init);
}
module_init(ch_init);