ia64/xen-unstable

view tools/libxutil/kernel_stream.c @ 2621:9402048e2325

bitkeeper revision 1.1159.1.218 (416a8128OiHXHyk_Sy8FsA0YUQcEnA)

Merge freefall.cl.cam.ac.uk:/auto/groups/xeno/users/cl349/BK/xeno.bk-26dom0
into freefall.cl.cam.ac.uk:/local/scratch/cl349/xeno.bk-26dom0
author cl349@freefall.cl.cam.ac.uk
date Mon Oct 11 12:48:40 2004 +0000 (2004-10-11)
parents 236a9f2698a3
children beb02da3f69f 42bdac6c8985 0a4b76b6b5a0
line source
1 /*
2 * Copyright (C) 2001 - 2004 Mike Wray <mike.wray@hp.com>
3 *
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
19 /** @file
20 * An IOStream implementation using printk() for output.
21 * Input is not implemented.
22 */
23 #ifdef __KERNEL__
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/types.h>
29 #include <linux/errno.h>
30 #include <linux/slab.h>
31 #include <linux/spinlock.h>
33 #include "kernel_stream.h"
34 #include "allocate.h"
36 /** Number of characters in the output buffer.
37 * The kernel uses 1024 for printk, so that should suffice.
38 */
39 #define BUF_N 1024
41 /** State for a kernel stream. */
42 typedef struct KernelData {
43 /** Stream lock. We need a lock to serialize access to the stream. */
44 spinlock_t lock;
45 /** Saved flags for locking. */
46 unsigned long flags;
47 /** Size of the output buffer. */
48 int buf_n;
49 /** Output buffer. */
50 char buf[BUF_N];
51 } KernelData;
53 static int kernel_write(IOStream *s, const char *msg, int n);
54 static void kernel_free(IOStream *s);
55 static void kernel_stream_lock(IOStream *s);
56 static void kernel_stream_unlock(IOStream *s);
58 /** Methods for a kernel stream. Output only. */
59 static const IOMethods kernel_methods = {
60 write: kernel_write,
61 free: kernel_free,
62 lock: kernel_stream_lock,
63 unlock: kernel_stream_unlock,
64 };
66 /** Shared state for kernel streams.
67 * All implementations write using printk, so we can use
68 * shared state and avoid allocating it.
69 */
70 static const KernelData kernel_data = {
71 lock: SPIN_LOCK_UNLOCKED,
72 flags: 0,
73 buf_n: BUF_N,
74 };
76 /** Stream for kernel printk. */
77 static IOStream iokernel = {
78 methods: &kernel_methods,
79 data: &kernel_data,
80 };
82 /** Stream for kernel printk. */
83 IOStream *iostdout = &iokernel;
85 /** Stream for kernel printk. */
86 IOStream *iostdin = &iokernel;
88 /** Stream for kernel printk. */
89 IOStream *iostderr = &iokernel;
91 /** Get an output-only stream implementation using
92 * printk(). The stream uses static storage, and must not be freed.
93 *
94 * @return kernel stream
95 */
96 IOStream get_stream_kernel(void){
97 return iokernel;
98 }
100 /** Obtain the lock on the stream state.
101 *
102 * @param kdata stream state
103 */
104 static inline void KernelData_lock(KernelData *kdata){
105 spin_lock_irqsave(&kdata->lock, kdata->flags);
106 }
108 /** Release the lock on the stream state.
109 *
110 * @param kdata stream state
111 */
112 static inline void KernelData_unlock(KernelData *kdata){
113 spin_unlock_irqrestore(&kdata->lock, kdata->flags);
114 }
116 /** Get the stream state.
117 *
118 * @param s kernel stream
119 * @return stream state
120 */
121 static inline KernelData *get_kernel_data(IOStream *s){
122 return (KernelData*)s->data;
123 }
125 /** Obtain the lock on the stream state.
126 *
127 * @param s stream
128 */
129 void kernel_stream_lock(IOStream *s){
130 KernelData_lock(get_kernel_data(s));
131 }
133 /** Release the lock on the stream state.
134 *
135 * @param s stream
136 */
137 void kernel_stream_unlock(IOStream *s){
138 KernelData_unlock(get_kernel_data(s));
139 }
141 /** Write to a kernel stream.
142 *
143 * @param stream kernel stream
144 * @param format print format
145 * @param args print arguments
146 * @return result of the print
147 */
148 static int kernel_write(IOStream *stream, const char *buf, int n){
149 KernelData *kdata = get_kernel_data(stream);
150 int k;
151 k = kdata->buf_n - 1;
152 if(n < k) k = n;
153 memcpy(kdata->buf, buf, k);
154 kdata->buf[k] = '\0'
155 printk(kdata->buf);
156 return k;
157 }
159 /** Free a kernel stream.
160 * Frees the internal state of the stream.
161 * Do not call this unless the stream was dynamically allocated.
162 * Do not call this on a stream returned from get_stream_kernel().
163 *
164 * @param io stream to free
165 */
166 static void kernel_free(IOStream *io){
167 KernelData *kdata;
168 if(io == &iokernel) return;
169 kdata = get_kernel_data(io);
170 zero(kdata, sizeof(*kdata));
171 deallocate(kdata);
172 }
173 #endif /* __KERNEL__ */