qemu/util/buffer.c
Peter Lieven 5c10dbb7b5 buffer: make the Buffer capacity increase in powers of two
This makes sure the number of reallocs is in O(log N).

Signed-off-by: Peter Lieven <pl@kamp.de>
Reviewed-by: Daniel P. Berrange <berrange@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
Message-id: 1446203414-4013-2-git-send-email-kraxel@redhat.com

[ rebased to util/buffer.c ]

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2015-11-05 09:08:29 +01:00

69 lines
1.8 KiB
C

/*
* 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"
#define BUFFER_MIN_INIT_SIZE 4096
void buffer_reserve(Buffer *buffer, size_t len)
{
if ((buffer->capacity - buffer->offset) < len) {
buffer->capacity = pow2ceil(buffer->offset + len);
buffer->capacity = MAX(buffer->capacity, BUFFER_MIN_INIT_SIZE);
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;
}