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/buffer.h"
24 #define BUFFER_MIN_INIT_SIZE 4096
25 #define BUFFER_MIN_SHRINK_SIZE 65536
27 static size_t buffer_req_size(Buffer
*buffer
, size_t len
)
29 return MAX(BUFFER_MIN_INIT_SIZE
,
30 pow2ceil(buffer
->offset
+ len
));
34 void buffer_init(Buffer
*buffer
, const char *name
, ...)
39 buffer
->name
= g_strdup_vprintf(name
, ap
);
43 void buffer_shrink(Buffer
*buffer
)
48 * Only shrink in case the used size is *much* smaller than the
49 * capacity, to avoid bumping up & down the buffers all the time.
50 * realloc() isn't exactly cheap ...
52 if (buffer
->offset
< (buffer
->capacity
>> 3) &&
53 buffer
->capacity
> BUFFER_MIN_SHRINK_SIZE
) {
57 old
= buffer
->capacity
;
58 buffer
->capacity
= pow2ceil(buffer
->offset
);
59 buffer
->capacity
= MAX(buffer
->capacity
, BUFFER_MIN_SHRINK_SIZE
);
60 buffer
->buffer
= g_realloc(buffer
->buffer
, buffer
->capacity
);
61 trace_buffer_resize(buffer
->name
?: "unnamed",
62 old
, buffer
->capacity
);
65 void buffer_reserve(Buffer
*buffer
, size_t len
)
69 if ((buffer
->capacity
- buffer
->offset
) < len
) {
70 old
= buffer
->capacity
;
71 buffer
->capacity
= buffer_req_size(buffer
, len
);
72 buffer
->buffer
= g_realloc(buffer
->buffer
, buffer
->capacity
);
73 trace_buffer_resize(buffer
->name
?: "unnamed",
74 old
, buffer
->capacity
);
78 gboolean
buffer_empty(Buffer
*buffer
)
80 return buffer
->offset
== 0;
83 uint8_t *buffer_end(Buffer
*buffer
)
85 return buffer
->buffer
+ buffer
->offset
;
88 void buffer_reset(Buffer
*buffer
)
93 void buffer_free(Buffer
*buffer
)
95 trace_buffer_free(buffer
->name
?: "unnamed", buffer
->capacity
);
96 g_free(buffer
->buffer
);
100 buffer
->buffer
= NULL
;
104 void buffer_append(Buffer
*buffer
, const void *data
, size_t len
)
106 memcpy(buffer
->buffer
+ buffer
->offset
, data
, len
);
107 buffer
->offset
+= len
;
110 void buffer_advance(Buffer
*buffer
, size_t len
)
112 memmove(buffer
->buffer
, buffer
->buffer
+ len
,
113 (buffer
->offset
- len
));
114 buffer
->offset
-= len
;
117 void buffer_move_empty(Buffer
*to
, Buffer
*from
)
119 trace_buffer_move_empty(to
->name
?: "unnamed",
121 from
->name
?: "unnamed");
122 assert(to
->offset
== 0);
125 to
->offset
= from
->offset
;
126 to
->capacity
= from
->capacity
;
127 to
->buffer
= from
->buffer
;
134 void buffer_move(Buffer
*to
, Buffer
*from
)
136 if (to
->offset
== 0) {
137 buffer_move_empty(to
, from
);
141 trace_buffer_move(to
->name
?: "unnamed",
143 from
->name
?: "unnamed");
144 buffer_reserve(to
, from
->offset
);
145 buffer_append(to
, from
->buffer
, from
->offset
);
147 g_free(from
->buffer
);