]> bbs.cooldavid.org Git - net-next-2.6.git/blame - samples/kfifo/bytestream-example.c
kfifo: add kfifo_skip() testcase
[net-next-2.6.git] / samples / kfifo / bytestream-example.c
CommitLineData
5bf2b193
SS
1/*
2 * Sample kfifo byte stream implementation
3 *
4 * Copyright (C) 2010 Stefani Seibold <stefani@seibold.net>
5 *
6 * Released under the GPL version 2 only.
7 *
8 */
9
10#include <linux/init.h>
11#include <linux/module.h>
12#include <linux/proc_fs.h>
13#include <linux/mutex.h>
14#include <linux/kfifo.h>
15
16/*
17 * This module shows how to create a byte stream fifo.
18 */
19
20/* fifo size in elements (bytes) */
21#define FIFO_SIZE 32
22
23/* name of the proc entry */
24#define PROC_FIFO "bytestream-fifo"
25
26/* lock for procfs read access */
27static DEFINE_MUTEX(read_lock);
28
29/* lock for procfs write access */
30static DEFINE_MUTEX(write_lock);
31
32/*
33 * define DYNAMIC in this example for a dynamically allocated fifo.
34 *
35 * Otherwise the fifo storage will be a part of the fifo structure.
36 */
37#if 0
38#define DYNAMIC
39#endif
40
41#ifdef DYNAMIC
42static struct kfifo test;
43#else
44static DECLARE_KFIFO(test, unsigned char, FIFO_SIZE);
45#endif
46
47static int __init testfunc(void)
48{
49 unsigned char buf[6];
50 unsigned char i;
51 unsigned int ret;
52
53 printk(KERN_INFO "byte stream fifo test start\n");
54
55 /* put string into the fifo */
56 kfifo_in(&test, "hello", 5);
57
58 /* put values into the fifo */
59 for (i = 0; i != 10; i++)
60 kfifo_put(&test, &i);
61
62 /* show the number of used elements */
63 printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test));
64
65 /* get max of 5 bytes from the fifo */
66 i = kfifo_out(&test, buf, 5);
67 printk(KERN_INFO "buf: %.*s\n", i, buf);
68
69 /* get max of 2 elements from the fifo */
70 ret = kfifo_out(&test, buf, 2);
71 printk(KERN_INFO "ret: %d\n", ret);
72 /* and put it back to the end of the fifo */
73 ret = kfifo_in(&test, buf, ret);
74 printk(KERN_INFO "ret: %d\n", ret);
75
5ddf8391
AR
76 /* skip first element of the fifo */
77 printk(KERN_INFO "skip 1st element\n");
78 kfifo_skip(&test);
79
5bf2b193
SS
80 /* put values into the fifo until is full */
81 for (i = 20; kfifo_put(&test, &i); i++)
82 ;
83
84 printk(KERN_INFO "queue len: %u\n", kfifo_len(&test));
85
86 /* print out all values in the fifo */
87 while (kfifo_get(&test, &i))
88 printk("%d ", i);
89 printk("\n");
90
91 return 0;
92}
93
94static ssize_t fifo_write(struct file *file, const char __user *buf,
95 size_t count, loff_t *ppos)
96{
97 int ret;
98 unsigned int copied;
99
100 if (mutex_lock_interruptible(&write_lock))
101 return -ERESTARTSYS;
102
103 ret = kfifo_from_user(&test, buf, count, &copied);
104
105 mutex_unlock(&write_lock);
106
107 return ret ? ret : copied;
108}
109
110static ssize_t fifo_read(struct file *file, char __user *buf,
111 size_t count, loff_t *ppos)
112{
113 int ret;
114 unsigned int copied;
115
116 if (mutex_lock_interruptible(&read_lock))
117 return -ERESTARTSYS;
118
119 ret = kfifo_to_user(&test, buf, count, &copied);
120
121 mutex_unlock(&read_lock);
122
123 return ret ? ret : copied;
124}
125
126static const struct file_operations fifo_fops = {
127 .owner = THIS_MODULE,
128 .read = fifo_read,
129 .write = fifo_write,
130};
131
132static int __init example_init(void)
133{
134#ifdef DYNAMIC
135 int ret;
136
137 ret = kfifo_alloc(&test, FIFO_SIZE, GFP_KERNEL);
138 if (ret) {
139 printk(KERN_ERR "error kfifo_alloc\n");
140 return ret;
141 }
142#else
143 INIT_KFIFO(test);
144#endif
145 testfunc();
146
147 if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) {
148#ifdef DYNAMIC
149 kfifo_free(&test);
150#endif
151 return -ENOMEM;
152 }
153 return 0;
154}
155
156static void __exit example_exit(void)
157{
158 remove_proc_entry(PROC_FIFO, NULL);
159#ifdef DYNAMIC
160 kfifo_free(&test);
161#endif
162}
163
164module_init(example_init);
165module_exit(example_exit);
166MODULE_LICENSE("GPL");
167MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>");