buffer.c 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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"
G
Gerd Hoffmann 已提交
22
#include "trace.h"
23

G
Gerd Hoffmann 已提交
24 25
#define BUFFER_MIN_INIT_SIZE     4096
#define BUFFER_MIN_SHRINK_SIZE  65536
26

G
Gerd Hoffmann 已提交
27 28 29 30 31 32 33 34 35
void buffer_init(Buffer *buffer, const char *name, ...)
{
    va_list ap;

    va_start(ap, name);
    buffer->name = g_strdup_vprintf(name, ap);
    va_end(ap);
}

G
Gerd Hoffmann 已提交
36 37
void buffer_shrink(Buffer *buffer)
{
G
Gerd Hoffmann 已提交
38 39
    size_t old;

G
Gerd Hoffmann 已提交
40 41 42 43 44 45 46 47 48 49
    /*
     * Only shrink in case the used size is *much* smaller than the
     * capacity, to avoid bumping up & down the buffers all the time.
     * realloc() isn't exactly cheap ...
     */
    if (buffer->offset < (buffer->capacity >> 3) &&
        buffer->capacity > BUFFER_MIN_SHRINK_SIZE) {
        return;
    }

G
Gerd Hoffmann 已提交
50
    old = buffer->capacity;
G
Gerd Hoffmann 已提交
51 52 53
    buffer->capacity = pow2ceil(buffer->offset);
    buffer->capacity = MAX(buffer->capacity, BUFFER_MIN_SHRINK_SIZE);
    buffer->buffer = g_realloc(buffer->buffer, buffer->capacity);
G
Gerd Hoffmann 已提交
54 55
    trace_buffer_resize(buffer->name ?: "unnamed",
                        old, buffer->capacity);
G
Gerd Hoffmann 已提交
56 57
}

58 59
void buffer_reserve(Buffer *buffer, size_t len)
{
G
Gerd Hoffmann 已提交
60 61
    size_t old;

62
    if ((buffer->capacity - buffer->offset) < len) {
G
Gerd Hoffmann 已提交
63
        old = buffer->capacity;
64 65
        buffer->capacity = pow2ceil(buffer->offset + len);
        buffer->capacity = MAX(buffer->capacity, BUFFER_MIN_INIT_SIZE);
66
        buffer->buffer = g_realloc(buffer->buffer, buffer->capacity);
G
Gerd Hoffmann 已提交
67 68
        trace_buffer_resize(buffer->name ?: "unnamed",
                            old, buffer->capacity);
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    }
}

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
Gerd Hoffmann 已提交
89
    trace_buffer_free(buffer->name ?: "unnamed", buffer->capacity);
90
    g_free(buffer->buffer);
G
Gerd Hoffmann 已提交
91
    g_free(buffer->name);
92 93 94
    buffer->offset = 0;
    buffer->capacity = 0;
    buffer->buffer = NULL;
G
Gerd Hoffmann 已提交
95
    buffer->name = NULL;
96 97 98 99 100 101 102 103 104 105 106 107 108 109
}

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;
}
G
Gerd Hoffmann 已提交
110 111 112

void buffer_move_empty(Buffer *to, Buffer *from)
{
G
Gerd Hoffmann 已提交
113 114 115
    trace_buffer_move_empty(to->name ?: "unnamed",
                            from->offset,
                            from->name ?: "unnamed");
G
Gerd Hoffmann 已提交
116 117 118 119 120 121 122 123 124 125 126
    assert(to->offset == 0);

    g_free(to->buffer);
    to->offset = from->offset;
    to->capacity = from->capacity;
    to->buffer = from->buffer;

    from->offset = 0;
    from->capacity = 0;
    from->buffer = NULL;
}
G
Gerd Hoffmann 已提交
127 128 129 130 131 132 133 134

void buffer_move(Buffer *to, Buffer *from)
{
    if (to->offset == 0) {
        buffer_move_empty(to, from);
        return;
    }

G
Gerd Hoffmann 已提交
135 136 137
    trace_buffer_move(to->name ?: "unnamed",
                      from->offset,
                      from->name ?: "unnamed");
G
Gerd Hoffmann 已提交
138 139 140 141 142 143 144 145
    buffer_reserve(to, from->offset);
    buffer_append(to, from->buffer, from->offset);

    g_free(from->buffer);
    from->offset = 0;
    from->capacity = 0;
    from->buffer = NULL;
}