Remove unused USES_X509_AUTH macro from VNC sasl code
[qemu/ar7.git] / buffered_file.c
blob41b42c3d5a287395eb7b57e53058407e378019c3
1 /*
2 * QEMU buffered QEMUFile
4 * Copyright IBM, Corp. 2008
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "qemu-common.h"
15 #include "hw/hw.h"
16 #include "qemu-timer.h"
17 #include "qemu-char.h"
18 #include "buffered_file.h"
20 //#define DEBUG_BUFFERED_FILE
22 typedef struct QEMUFileBuffered
24 BufferedPutFunc *put_buffer;
25 BufferedPutReadyFunc *put_ready;
26 BufferedWaitForUnfreezeFunc *wait_for_unfreeze;
27 BufferedCloseFunc *close;
28 void *opaque;
29 QEMUFile *file;
30 int has_error;
31 int freeze_output;
32 size_t bytes_xfer;
33 size_t xfer_limit;
34 uint8_t *buffer;
35 size_t buffer_size;
36 size_t buffer_capacity;
37 QEMUTimer *timer;
38 } QEMUFileBuffered;
40 #ifdef DEBUG_BUFFERED_FILE
41 #define DPRINTF(fmt, ...) \
42 do { printf("buffered-file: " fmt, ## __VA_ARGS__); } while (0)
43 #else
44 #define DPRINTF(fmt, ...) \
45 do { } while (0)
46 #endif
48 static void buffered_append(QEMUFileBuffered *s,
49 const uint8_t *buf, size_t size)
51 if (size > (s->buffer_capacity - s->buffer_size)) {
52 void *tmp;
54 DPRINTF("increasing buffer capacity from %zu by %zu\n",
55 s->buffer_capacity, size + 1024);
57 s->buffer_capacity += size + 1024;
59 tmp = qemu_realloc(s->buffer, s->buffer_capacity);
60 if (tmp == NULL) {
61 fprintf(stderr, "qemu file buffer expansion failed\n");
62 exit(1);
65 s->buffer = tmp;
68 memcpy(s->buffer + s->buffer_size, buf, size);
69 s->buffer_size += size;
72 static void buffered_flush(QEMUFileBuffered *s)
74 size_t offset = 0;
76 if (s->has_error) {
77 DPRINTF("flush when error, bailing\n");
78 return;
81 DPRINTF("flushing %zu byte(s) of data\n", s->buffer_size);
83 while (offset < s->buffer_size) {
84 ssize_t ret;
86 ret = s->put_buffer(s->opaque, s->buffer + offset,
87 s->buffer_size - offset);
88 if (ret == -EAGAIN) {
89 DPRINTF("backend not ready, freezing\n");
90 s->freeze_output = 1;
91 break;
94 if (ret <= 0) {
95 DPRINTF("error flushing data, %zd\n", ret);
96 s->has_error = 1;
97 break;
98 } else {
99 DPRINTF("flushed %zd byte(s)\n", ret);
100 offset += ret;
104 DPRINTF("flushed %zu of %zu byte(s)\n", offset, s->buffer_size);
105 memmove(s->buffer, s->buffer + offset, s->buffer_size - offset);
106 s->buffer_size -= offset;
109 static int buffered_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
111 QEMUFileBuffered *s = opaque;
112 int offset = 0;
113 ssize_t ret;
115 DPRINTF("putting %d bytes at %" PRId64 "\n", size, pos);
117 if (s->has_error) {
118 DPRINTF("flush when error, bailing\n");
119 return -EINVAL;
122 DPRINTF("unfreezing output\n");
123 s->freeze_output = 0;
125 buffered_flush(s);
127 while (!s->freeze_output && offset < size) {
128 if (s->bytes_xfer > s->xfer_limit) {
129 DPRINTF("transfer limit exceeded when putting\n");
130 break;
133 ret = s->put_buffer(s->opaque, buf + offset, size - offset);
134 if (ret == -EAGAIN) {
135 DPRINTF("backend not ready, freezing\n");
136 s->freeze_output = 1;
137 break;
140 if (ret <= 0) {
141 DPRINTF("error putting\n");
142 s->has_error = 1;
143 offset = -EINVAL;
144 break;
147 DPRINTF("put %zd byte(s)\n", ret);
148 offset += ret;
149 s->bytes_xfer += ret;
152 if (offset >= 0) {
153 DPRINTF("buffering %d bytes\n", size - offset);
154 buffered_append(s, buf + offset, size - offset);
155 offset = size;
158 if (pos == 0 && size == 0) {
159 DPRINTF("file is ready\n");
160 if (s->bytes_xfer <= s->xfer_limit) {
161 DPRINTF("notifying client\n");
162 s->put_ready(s->opaque);
166 return offset;
169 static int buffered_close(void *opaque)
171 QEMUFileBuffered *s = opaque;
172 int ret;
174 DPRINTF("closing\n");
176 while (!s->has_error && s->buffer_size) {
177 buffered_flush(s);
178 if (s->freeze_output)
179 s->wait_for_unfreeze(s);
182 ret = s->close(s->opaque);
184 qemu_del_timer(s->timer);
185 qemu_free_timer(s->timer);
186 qemu_free(s->buffer);
187 qemu_free(s);
189 return ret;
192 static int buffered_rate_limit(void *opaque)
194 QEMUFileBuffered *s = opaque;
196 if (s->has_error)
197 return 0;
199 if (s->freeze_output)
200 return 1;
202 if (s->bytes_xfer > s->xfer_limit)
203 return 1;
205 return 0;
208 static int64_t buffered_set_rate_limit(void *opaque, int64_t new_rate)
210 QEMUFileBuffered *s = opaque;
211 if (s->has_error)
212 goto out;
214 if (new_rate > SIZE_MAX) {
215 new_rate = SIZE_MAX;
218 s->xfer_limit = new_rate / 10;
220 out:
221 return s->xfer_limit;
224 static int64_t buffered_get_rate_limit(void *opaque)
226 QEMUFileBuffered *s = opaque;
228 return s->xfer_limit;
231 static void buffered_rate_tick(void *opaque)
233 QEMUFileBuffered *s = opaque;
235 if (s->has_error) {
236 buffered_close(s);
237 return;
240 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 100);
242 if (s->freeze_output)
243 return;
245 s->bytes_xfer = 0;
247 buffered_flush(s);
249 /* Add some checks around this */
250 s->put_ready(s->opaque);
253 QEMUFile *qemu_fopen_ops_buffered(void *opaque,
254 size_t bytes_per_sec,
255 BufferedPutFunc *put_buffer,
256 BufferedPutReadyFunc *put_ready,
257 BufferedWaitForUnfreezeFunc *wait_for_unfreeze,
258 BufferedCloseFunc *close)
260 QEMUFileBuffered *s;
262 s = qemu_mallocz(sizeof(*s));
264 s->opaque = opaque;
265 s->xfer_limit = bytes_per_sec / 10;
266 s->put_buffer = put_buffer;
267 s->put_ready = put_ready;
268 s->wait_for_unfreeze = wait_for_unfreeze;
269 s->close = close;
271 s->file = qemu_fopen_ops(s, buffered_put_buffer, NULL,
272 buffered_close, buffered_rate_limit,
273 buffered_set_rate_limit,
274 buffered_get_rate_limit);
276 s->timer = qemu_new_timer_ms(rt_clock, buffered_rate_tick, s);
278 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 100);
280 return s->file;