vmxnet3: Use pci_dma_* API instead of cpu_physical_memory_*
[qemu/ar7.git] / qapi / string-output-visitor.c
blobd01319628be741d69be3f73286ea34308b912709
1 /*
2 * String printing Visitor
4 * Copyright Red Hat, Inc. 2012-2016
6 * Author: Paolo Bonzini <pbonzini@redhat.com>
8 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
9 * See the COPYING.LIB file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "qemu-common.h"
15 #include "qapi/string-output-visitor.h"
16 #include "qapi/visitor-impl.h"
17 #include "qemu/host-utils.h"
18 #include <math.h>
19 #include "qemu/range.h"
21 enum ListMode {
22 LM_NONE, /* not traversing a list of repeated options */
23 LM_STARTED, /* next_list() ready to be called */
25 LM_IN_PROGRESS, /* next_list() has been called.
27 * Generating the next list link will consume the most
28 * recently parsed QemuOpt instance of the repeated
29 * option.
31 * Parsing a value into the list link will examine the
32 * next QemuOpt instance of the repeated option, and
33 * possibly enter LM_SIGNED_INTERVAL or
34 * LM_UNSIGNED_INTERVAL.
37 LM_SIGNED_INTERVAL, /* next_list() has been called.
39 * Generating the next list link will consume the most
40 * recently stored element from the signed interval,
41 * parsed from the most recent QemuOpt instance of the
42 * repeated option. This may consume QemuOpt itself
43 * and return to LM_IN_PROGRESS.
45 * Parsing a value into the list link will store the
46 * next element of the signed interval.
49 LM_UNSIGNED_INTERVAL,/* Same as above, only for an unsigned interval. */
51 LM_END, /* next_list() called, about to see last element. */
54 typedef enum ListMode ListMode;
56 struct StringOutputVisitor
58 Visitor visitor;
59 bool human;
60 GString *string;
61 ListMode list_mode;
62 union {
63 int64_t s;
64 uint64_t u;
65 } range_start, range_end;
66 GList *ranges;
69 static StringOutputVisitor *to_sov(Visitor *v)
71 return container_of(v, StringOutputVisitor, visitor);
74 static void string_output_set(StringOutputVisitor *sov, char *string)
76 if (sov->string) {
77 g_string_free(sov->string, true);
79 sov->string = g_string_new(string);
80 g_free(string);
83 static void string_output_append(StringOutputVisitor *sov, int64_t a)
85 Range *r = g_malloc0(sizeof(*r));
86 r->begin = a;
87 r->end = a + 1;
88 sov->ranges = g_list_insert_sorted_merged(sov->ranges, r, range_compare);
91 static void string_output_append_range(StringOutputVisitor *sov,
92 int64_t s, int64_t e)
94 Range *r = g_malloc0(sizeof(*r));
95 r->begin = s;
96 r->end = e + 1;
97 sov->ranges = g_list_insert_sorted_merged(sov->ranges, r, range_compare);
100 static void format_string(StringOutputVisitor *sov, Range *r, bool next,
101 bool human)
103 if (r->end - r->begin > 1) {
104 if (human) {
105 g_string_append_printf(sov->string, "0x%" PRIx64 "-0x%" PRIx64,
106 r->begin, r->end - 1);
108 } else {
109 g_string_append_printf(sov->string, "%" PRId64 "-%" PRId64,
110 r->begin, r->end - 1);
112 } else {
113 if (human) {
114 g_string_append_printf(sov->string, "0x%" PRIx64, r->begin);
115 } else {
116 g_string_append_printf(sov->string, "%" PRId64, r->begin);
119 if (next) {
120 g_string_append(sov->string, ",");
124 static void print_type_int64(Visitor *v, const char *name, int64_t *obj,
125 Error **errp)
127 StringOutputVisitor *sov = to_sov(v);
128 GList *l;
130 switch (sov->list_mode) {
131 case LM_NONE:
132 string_output_append(sov, *obj);
133 break;
135 case LM_STARTED:
136 sov->range_start.s = *obj;
137 sov->range_end.s = *obj;
138 sov->list_mode = LM_IN_PROGRESS;
139 return;
141 case LM_IN_PROGRESS:
142 if (sov->range_end.s + 1 == *obj) {
143 sov->range_end.s++;
144 } else {
145 if (sov->range_start.s == sov->range_end.s) {
146 string_output_append(sov, sov->range_end.s);
147 } else {
148 assert(sov->range_start.s < sov->range_end.s);
149 string_output_append_range(sov, sov->range_start.s,
150 sov->range_end.s);
153 sov->range_start.s = *obj;
154 sov->range_end.s = *obj;
156 return;
158 case LM_END:
159 if (sov->range_end.s + 1 == *obj) {
160 sov->range_end.s++;
161 assert(sov->range_start.s < sov->range_end.s);
162 string_output_append_range(sov, sov->range_start.s,
163 sov->range_end.s);
164 } else {
165 if (sov->range_start.s == sov->range_end.s) {
166 string_output_append(sov, sov->range_end.s);
167 } else {
168 assert(sov->range_start.s < sov->range_end.s);
170 string_output_append_range(sov, sov->range_start.s,
171 sov->range_end.s);
173 string_output_append(sov, *obj);
175 break;
177 default:
178 abort();
181 l = sov->ranges;
182 while (l) {
183 Range *r = l->data;
184 format_string(sov, r, l->next != NULL, false);
185 l = l->next;
188 if (sov->human) {
189 l = sov->ranges;
190 g_string_append(sov->string, " (");
191 while (l) {
192 Range *r = l->data;
193 format_string(sov, r, l->next != NULL, true);
194 l = l->next;
196 g_string_append(sov->string, ")");
200 static void print_type_uint64(Visitor *v, const char *name, uint64_t *obj,
201 Error **errp)
203 /* FIXME: print_type_int64 mishandles values over INT64_MAX */
204 int64_t i = *obj;
205 print_type_int64(v, name, &i, errp);
208 static void print_type_size(Visitor *v, const char *name, uint64_t *obj,
209 Error **errp)
211 StringOutputVisitor *sov = to_sov(v);
212 static const char suffixes[] = { 'B', 'K', 'M', 'G', 'T', 'P', 'E' };
213 uint64_t div, val;
214 char *out;
215 int i;
217 if (!sov->human) {
218 out = g_strdup_printf("%"PRIu64, *obj);
219 string_output_set(sov, out);
220 return;
223 val = *obj;
225 /* The exponent (returned in i) minus one gives us
226 * floor(log2(val * 1024 / 1000). The correction makes us
227 * switch to the higher power when the integer part is >= 1000.
229 frexp(val / (1000.0 / 1024.0), &i);
230 i = (i - 1) / 10;
231 assert(i < ARRAY_SIZE(suffixes));
232 div = 1ULL << (i * 10);
234 out = g_strdup_printf("%"PRIu64" (%0.3g %c%s)", val,
235 (double)val/div, suffixes[i], i ? "iB" : "");
236 string_output_set(sov, out);
239 static void print_type_bool(Visitor *v, const char *name, bool *obj,
240 Error **errp)
242 StringOutputVisitor *sov = to_sov(v);
243 string_output_set(sov, g_strdup(*obj ? "true" : "false"));
246 static void print_type_str(Visitor *v, const char *name, char **obj,
247 Error **errp)
249 StringOutputVisitor *sov = to_sov(v);
250 char *out;
252 if (sov->human) {
253 out = *obj ? g_strdup_printf("\"%s\"", *obj) : g_strdup("<null>");
254 } else {
255 out = g_strdup(*obj ? *obj : "");
257 string_output_set(sov, out);
260 static void print_type_number(Visitor *v, const char *name, double *obj,
261 Error **errp)
263 StringOutputVisitor *sov = to_sov(v);
264 string_output_set(sov, g_strdup_printf("%f", *obj));
267 static void
268 start_list(Visitor *v, const char *name, GenericList **list, size_t size,
269 Error **errp)
271 StringOutputVisitor *sov = to_sov(v);
273 /* we can't traverse a list in a list */
274 assert(sov->list_mode == LM_NONE);
275 /* We don't support visits without a list */
276 assert(list);
277 /* List handling is only needed if there are at least two elements */
278 if (*list && (*list)->next) {
279 sov->list_mode = LM_STARTED;
283 static GenericList *next_list(Visitor *v, GenericList *tail, size_t size)
285 StringOutputVisitor *sov = to_sov(v);
286 GenericList *ret = tail->next;
288 if (ret && !ret->next) {
289 sov->list_mode = LM_END;
291 return ret;
294 static void end_list(Visitor *v)
296 StringOutputVisitor *sov = to_sov(v);
298 assert(sov->list_mode == LM_STARTED ||
299 sov->list_mode == LM_END ||
300 sov->list_mode == LM_NONE ||
301 sov->list_mode == LM_IN_PROGRESS);
302 sov->list_mode = LM_NONE;
305 char *string_output_get_string(StringOutputVisitor *sov)
307 char *string = g_string_free(sov->string, false);
308 sov->string = NULL;
309 return string;
312 Visitor *string_output_get_visitor(StringOutputVisitor *sov)
314 return &sov->visitor;
317 static void free_range(void *range, void *dummy)
319 g_free(range);
322 void string_output_visitor_cleanup(StringOutputVisitor *sov)
324 if (sov->string) {
325 g_string_free(sov->string, true);
328 g_list_foreach(sov->ranges, free_range, NULL);
329 g_list_free(sov->ranges);
330 g_free(sov);
333 StringOutputVisitor *string_output_visitor_new(bool human)
335 StringOutputVisitor *v;
337 v = g_malloc0(sizeof(*v));
339 v->string = g_string_new(NULL);
340 v->human = human;
341 v->visitor.type = VISITOR_OUTPUT;
342 v->visitor.type_int64 = print_type_int64;
343 v->visitor.type_uint64 = print_type_uint64;
344 v->visitor.type_size = print_type_size;
345 v->visitor.type_bool = print_type_bool;
346 v->visitor.type_str = print_type_str;
347 v->visitor.type_number = print_type_number;
348 v->visitor.start_list = start_list;
349 v->visitor.next_list = next_list;
350 v->visitor.end_list = end_list;
352 return v;