util: pull Buffer code out of VNC module

The Buffer code in the VNC server is useful for the IO channel
code, so pull it out into a shared module, QIOBuffer.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrange 2015-03-03 17:13:42 +00:00
parent 10817bf09d
commit 88c5f205fa
6 changed files with 191 additions and 58 deletions

View File

@ -1200,6 +1200,12 @@ F: util/*coroutine*
F: include/qemu/coroutine*
F: tests/test-coroutine.c
Buffers
M: Daniel P. Berrange <berrange@redhat.com>
S: Odd fixes
F: util/buffer.c
F: include/qemu/buffer.h
Usermode Emulation
------------------
Overall

118
include/qemu/buffer.h Normal file
View File

@ -0,0 +1,118 @@
/*
* QEMU generic buffers
*
* Copyright (c) 2015 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef QEMU_BUFFER_H__
#define QEMU_BUFFER_H__
#include "qemu-common.h"
typedef struct Buffer Buffer;
/**
* Buffer:
*
* The Buffer object provides a simple dynamically resizing
* array, with separate tracking of capacity and usage. This
* is typically useful when buffering I/O or processing data.
*/
struct Buffer {
size_t capacity;
size_t offset;
uint8_t *buffer;
};
/**
* buffer_reserve:
* @buffer: the buffer object
* @len: the minimum required free space
*
* Ensure that the buffer has space allocated for at least
* @len bytes. If the current buffer is too small, it will
* be reallocated, possibly to a larger size than requested.
*/
void buffer_reserve(Buffer *buffer, size_t len);
/**
* buffer_reset:
* @buffer: the buffer object
*
* Reset the length of the stored data to zero, but do
* not free / reallocate the memory buffer
*/
void buffer_reset(Buffer *buffer);
/**
* buffer_free:
* @buffer: the buffer object
*
* Reset the length of the stored data to zero and also
* free the internal memory buffer
*/
void buffer_free(Buffer *buffer);
/**
* buffer_append:
* @buffer: the buffer object
* @data: the data block to append
* @len: the length of @data in bytes
*
* Append the contents of @data to the end of the buffer.
* The caller must ensure that the buffer has sufficient
* free space for @len bytes, typically by calling the
* buffer_reserve() method prior to appending.
*/
void buffer_append(Buffer *buffer, const void *data, size_t len);
/**
* buffer_advance:
* @buffer: the buffer object
* @len: the number of bytes to skip
*
* Remove @len bytes of data from the head of the buffer.
* The internal buffer will not be reallocated, so will
* have at least @len bytes of free space after this
* call completes
*/
void buffer_advance(Buffer *buffer, size_t len);
/**
* buffer_end:
* @buffer: the buffer object
*
* Get a pointer to the tail end of the internal buffer
* The returned pointer is only valid until the next
* call to buffer_reserve().
*
* Returns: the tail of the buffer
*/
uint8_t *buffer_end(Buffer *buffer);
/**
* buffer_empty:
* @buffer: the buffer object
*
* Determine if the buffer contains any current data
*
* Returns: true if the buffer holds data, false otherwise
*/
gboolean buffer_empty(Buffer *buffer);
#endif /* QEMU_BUFFER_H__ */

View File

@ -647,49 +647,6 @@ void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
vnc_write_s32(vs, encoding);
}
void buffer_reserve(Buffer *buffer, size_t len)
{
if ((buffer->capacity - buffer->offset) < len) {
buffer->capacity += (len + 1024);
buffer->buffer = g_realloc(buffer->buffer, buffer->capacity);
}
}
static int buffer_empty(Buffer *buffer)
{
return buffer->offset == 0;
}
uint8_t *buffer_end(Buffer *buffer)
{
return buffer->buffer + buffer->offset;
}
void buffer_reset(Buffer *buffer)
{
buffer->offset = 0;
}
void buffer_free(Buffer *buffer)
{
g_free(buffer->buffer);
buffer->offset = 0;
buffer->capacity = 0;
buffer->buffer = NULL;
}
void buffer_append(Buffer *buffer, const void *data, size_t len)
{
memcpy(buffer->buffer + buffer->offset, data, len);
buffer->offset += len;
}
void buffer_advance(Buffer *buf, size_t len)
{
memmove(buf->buffer, buf->buffer + len,
(buf->offset - len));
buf->offset -= len;
}
static void vnc_desktop_resize(VncState *vs)
{

View File

@ -34,6 +34,7 @@
#include "audio/audio.h"
#include "qemu/bitmap.h"
#include "crypto/tlssession.h"
#include "qemu/buffer.h"
#include <zlib.h>
#include <stdbool.h>
@ -56,13 +57,6 @@
*
*****************************************************************************/
typedef struct Buffer
{
size_t capacity;
size_t offset;
uint8_t *buffer;
} Buffer;
typedef struct VncState VncState;
typedef struct VncJob VncJob;
typedef struct VncRect VncRect;
@ -535,14 +529,6 @@ ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, int last_errno);
void start_client_init(VncState *vs);
void start_auth_vnc(VncState *vs);
/* Buffer management */
void buffer_reserve(Buffer *buffer, size_t len);
void buffer_reset(Buffer *buffer);
void buffer_free(Buffer *buffer);
void buffer_append(Buffer *buffer, const void *data, size_t len);
void buffer_advance(Buffer *buf, size_t len);
uint8_t *buffer_end(Buffer *buffer);
/* Misc helpers */

View File

@ -21,3 +21,4 @@ util-obj-y += rcu.o
util-obj-y += qemu-coroutine.o qemu-coroutine-lock.o qemu-coroutine-io.o
util-obj-y += qemu-coroutine-sleep.o
util-obj-y += coroutine-$(CONFIG_COROUTINE_BACKEND).o
util-obj-y += buffer.o

65
util/buffer.c Normal file
View File

@ -0,0 +1,65 @@
/*
* QEMU generic buffers
*
* Copyright (c) 2015 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
*/
#include "qemu/buffer.h"
void buffer_reserve(Buffer *buffer, size_t len)
{
if ((buffer->capacity - buffer->offset) < len) {
buffer->capacity += (len + 1024);
buffer->buffer = g_realloc(buffer->buffer, buffer->capacity);
}
}
gboolean buffer_empty(Buffer *buffer)
{
return buffer->offset == 0;
}
uint8_t *buffer_end(Buffer *buffer)
{
return buffer->buffer + buffer->offset;
}
void buffer_reset(Buffer *buffer)
{
buffer->offset = 0;
}
void buffer_free(Buffer *buffer)
{
g_free(buffer->buffer);
buffer->offset = 0;
buffer->capacity = 0;
buffer->buffer = NULL;
}
void buffer_append(Buffer *buffer, const void *data, size_t len)
{
memcpy(buffer->buffer + buffer->offset, data, len);
buffer->offset += len;
}
void buffer_advance(Buffer *buffer, size_t len)
{
memmove(buffer->buffer, buffer->buffer + len,
(buffer->offset - len));
buffer->offset -= len;
}