xhci: fix logging
[qemu/ar7.git] / util / buffer.c
blobd8bb8745343dc58004dbb69549f8796bba33dbf8
1 /*
2 * QEMU generic buffers
4 * Copyright (c) 2015 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "qemu/host-utils.h"
23 #include "qemu/buffer.h"
24 #include "trace.h"
26 #define BUFFER_MIN_INIT_SIZE 4096
27 #define BUFFER_MIN_SHRINK_SIZE 65536
29 /* define the factor alpha for the exponential smoothing
30 * that is used in the average size calculation. a shift
31 * of 7 results in an alpha of 1/2^7. */
32 #define BUFFER_AVG_SIZE_SHIFT 7
34 static size_t buffer_req_size(Buffer *buffer, size_t len)
36 return MAX(BUFFER_MIN_INIT_SIZE,
37 pow2ceil(buffer->offset + len));
40 static void buffer_adj_size(Buffer *buffer, size_t len)
42 size_t old = buffer->capacity;
43 buffer->capacity = buffer_req_size(buffer, len);
44 buffer->buffer = g_realloc(buffer->buffer, buffer->capacity);
45 trace_buffer_resize(buffer->name ?: "unnamed",
46 old, buffer->capacity);
48 /* make it even harder for the buffer to shrink, reset average size
49 * to current capacity if it is larger than the average. */
50 buffer->avg_size = MAX(buffer->avg_size,
51 buffer->capacity << BUFFER_AVG_SIZE_SHIFT);
54 void buffer_init(Buffer *buffer, const char *name, ...)
56 va_list ap;
58 va_start(ap, name);
59 buffer->name = g_strdup_vprintf(name, ap);
60 va_end(ap);
63 static uint64_t buffer_get_avg_size(Buffer *buffer)
65 return buffer->avg_size >> BUFFER_AVG_SIZE_SHIFT;
68 void buffer_shrink(Buffer *buffer)
70 size_t new;
72 /* Calculate the average size of the buffer as
73 * avg_size = avg_size * ( 1 - a ) + required_size * a
74 * where a is 1 / 2 ^ BUFFER_AVG_SIZE_SHIFT. */
75 buffer->avg_size *= (1 << BUFFER_AVG_SIZE_SHIFT) - 1;
76 buffer->avg_size >>= BUFFER_AVG_SIZE_SHIFT;
77 buffer->avg_size += buffer_req_size(buffer, 0);
79 /* And then only shrink if the average size of the buffer is much
80 * too big, to avoid bumping up & down the buffers all the time.
81 * realloc() isn't exactly cheap ... */
82 new = buffer_req_size(buffer, buffer_get_avg_size(buffer));
83 if (new < buffer->capacity >> 3 &&
84 new >= BUFFER_MIN_SHRINK_SIZE) {
85 buffer_adj_size(buffer, buffer_get_avg_size(buffer));
88 buffer_adj_size(buffer, 0);
91 void buffer_reserve(Buffer *buffer, size_t len)
93 if ((buffer->capacity - buffer->offset) < len) {
94 buffer_adj_size(buffer, len);
98 gboolean buffer_empty(Buffer *buffer)
100 return buffer->offset == 0;
103 uint8_t *buffer_end(Buffer *buffer)
105 return buffer->buffer + buffer->offset;
108 void buffer_reset(Buffer *buffer)
110 buffer->offset = 0;
111 buffer_shrink(buffer);
114 void buffer_free(Buffer *buffer)
116 trace_buffer_free(buffer->name ?: "unnamed", buffer->capacity);
117 g_free(buffer->buffer);
118 g_free(buffer->name);
119 buffer->offset = 0;
120 buffer->capacity = 0;
121 buffer->buffer = NULL;
122 buffer->name = NULL;
125 void buffer_append(Buffer *buffer, const void *data, size_t len)
127 memcpy(buffer->buffer + buffer->offset, data, len);
128 buffer->offset += len;
131 void buffer_advance(Buffer *buffer, size_t len)
133 memmove(buffer->buffer, buffer->buffer + len,
134 (buffer->offset - len));
135 buffer->offset -= len;
136 buffer_shrink(buffer);
139 void buffer_move_empty(Buffer *to, Buffer *from)
141 trace_buffer_move_empty(to->name ?: "unnamed",
142 from->offset,
143 from->name ?: "unnamed");
144 assert(to->offset == 0);
146 g_free(to->buffer);
147 to->offset = from->offset;
148 to->capacity = from->capacity;
149 to->buffer = from->buffer;
151 from->offset = 0;
152 from->capacity = 0;
153 from->buffer = NULL;
156 void buffer_move(Buffer *to, Buffer *from)
158 if (to->offset == 0) {
159 buffer_move_empty(to, from);
160 return;
163 trace_buffer_move(to->name ?: "unnamed",
164 from->offset,
165 from->name ?: "unnamed");
166 buffer_reserve(to, from->offset);
167 buffer_append(to, from->buffer, from->offset);
169 g_free(from->buffer);
170 from->offset = 0;
171 from->capacity = 0;
172 from->buffer = NULL;