2 * Copyright (c) 2005-2008 Zmanda, Inc. All Rights Reserved.
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License version 2.1 as
6 * published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
11 * License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17 * Contact information: Zmanda Inc., 465 S Mathlida Ave, Suite 300
18 * Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
22 #include "device-queueing.h"
25 producer_result_t device_read_producer(gpointer devicep,
26 queue_buffer_t *buffer,
27 size_t hint_size G_GNUC_UNUSED) {
30 device = (Device*) devicep;
31 g_assert(IS_DEVICE(device));
35 int result, read_size;
36 read_size = buffer->alloc_size;
37 result = device_read_block(device, buffer->data, &read_size);
39 buffer->data_size = read_size;
41 } else if (result == 0) {
42 /* unfortunately, the best "memory" we have of needing a larger
43 * block size is the next time this buffer comes around, and even
44 * this is incomplete as buffers may be resized periodically. So
45 * we'll end up calling read_block with small buffers more often
46 * than strictly necessary. */
47 buffer->data = realloc(buffer->data, read_size);
48 buffer->alloc_size = read_size;
49 } else if (device->is_eof) {
50 return PRODUCER_FINISHED;
52 buffer->data_size = 0;
53 return PRODUCER_ERROR;
58 ssize_t device_write_consumer(gpointer devicep, queue_buffer_t *buffer) {
63 device = DEVICE(devicep);
65 block_size = device->block_size;
66 write_size = MIN(buffer->data_size, block_size);
68 /* we assume that the queueing module is providing us with
69 * appropriately-sized blocks until the last block. */
70 if (device_write_block(device, write_size,
71 buffer->data + buffer->offset)) {
75 /* Nope, really an error. */