~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/samples/kfifo/bytestream-example.c

Version: ~ [ linux-5.8-rc5 ] ~ [ linux-5.7.8 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.51 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.132 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.188 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.230 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.230 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp