|
2 | 2 | #define MSTRING_H
|
3 | 3 |
|
4 | 4 | #include "qemu/osdep.h"
|
5 |
| -#include <string.h> |
| 5 | +#include "glib.h" |
6 | 6 |
|
7 | 7 | typedef struct {
|
8 |
| - int ref; |
9 |
| - gchar *string; |
| 8 | + GString *gstr; |
| 9 | + int refcnt; |
10 | 10 | } MString;
|
11 | 11 |
|
12 |
| -void mstring_append_fmt(MString *mstring, const char *fmt, ...) __attribute__ ((format (printf, 2, 3))); |
13 |
| -MString *mstring_from_fmt(const char *fmt, ...) __attribute__ ((format (printf, 1, 2))); |
14 |
| -void mstring_append_va(MString *mstring, const char *fmt, va_list va) __attribute__ ((format (printf, 2, 0))); |
| 12 | +static inline void mstring_ref(MString *mstr) |
| 13 | +{ |
| 14 | + mstr->refcnt++; |
| 15 | +} |
| 16 | + |
| 17 | +static inline void mstring_unref(MString *mstr) |
| 18 | +{ |
| 19 | + mstr->refcnt--; |
| 20 | + if (mstr->refcnt == 0) { |
| 21 | + g_string_free(mstr->gstr, true); |
| 22 | + g_free(mstr); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +G_DEFINE_AUTOPTR_CLEANUP_FUNC(MString, mstring_unref) |
15 | 27 |
|
16 |
| -static inline |
17 |
| -void mstring_ref(MString *mstr) |
| 28 | +static inline MString *mstring_new(void) |
18 | 29 | {
|
19 |
| - mstr->ref++; |
| 30 | + MString *mstr = g_malloc(sizeof(MString)); |
| 31 | + mstr->refcnt = 1; |
| 32 | + mstr->gstr = g_string_new(""); |
| 33 | + return mstr; |
20 | 34 | }
|
21 | 35 |
|
22 |
| -static inline |
23 |
| -void mstring_unref(MString *mstr) |
| 36 | +static inline MString *mstring_from_str(const char *str) |
24 | 37 | {
|
25 |
| - mstr->ref--; |
26 |
| - if (!mstr->ref) { |
27 |
| - g_free(mstr->string); |
28 |
| - g_free(mstr); |
29 |
| - } |
| 38 | + MString *mstr = g_malloc(sizeof(MString)); |
| 39 | + mstr->refcnt = 1; |
| 40 | + mstr->gstr = g_string_new(str); |
| 41 | + return mstr; |
30 | 42 | }
|
31 | 43 |
|
32 |
| -static inline |
33 |
| -void mstring_append(MString *mstr, const char *str) |
| 44 | +static inline __attribute__((format(printf, 1, 2))) MString * |
| 45 | +mstring_from_fmt(const char *fmt, ...) |
34 | 46 | {
|
35 |
| - gchar *n = g_strconcat(mstr->string, str, NULL); |
36 |
| - g_free(mstr->string); |
37 |
| - mstr->string = n; |
| 47 | + MString *mstr = g_malloc(sizeof(MString)); |
| 48 | + mstr->refcnt = 1; |
| 49 | + |
| 50 | + va_list args; |
| 51 | + va_start(args, fmt); |
| 52 | + // FIXME: Use g_string_new_take (GLib 2.78+) |
| 53 | + g_autofree gchar *str = g_strdup_vprintf(fmt, args); |
| 54 | + mstr->gstr = g_string_new(str); |
| 55 | + va_end(args); |
| 56 | + |
| 57 | + return mstr; |
38 | 58 | }
|
39 | 59 |
|
40 |
| -static inline |
41 |
| -MString *mstring_new(void) |
| 60 | +static inline void mstring_append(MString *mstr, const char *str) |
42 | 61 | {
|
43 |
| - MString *mstr = g_malloc(sizeof(MString)); |
44 |
| - mstr->ref = 1; |
45 |
| - mstr->string = g_strdup(""); |
46 |
| - return mstr; |
| 62 | + g_string_append(mstr->gstr, str); |
47 | 63 | }
|
48 | 64 |
|
49 |
| -static inline |
50 |
| -MString *mstring_from_str(const char *str) |
| 65 | +static inline __attribute__((format(printf, 2, 3))) void |
| 66 | +mstring_append_fmt(MString *mstr, const char *fmt, ...) |
51 | 67 | {
|
52 |
| - MString *mstr = g_malloc(sizeof(MString)); |
53 |
| - mstr->ref = 1; |
54 |
| - mstr->string = g_strdup(str); |
55 |
| - return mstr; |
| 68 | + va_list args; |
| 69 | + va_start(args, fmt); |
| 70 | + g_string_append_vprintf(mstr->gstr, fmt, args); |
| 71 | + va_end(args); |
56 | 72 | }
|
57 | 73 |
|
58 |
| -static inline |
59 |
| -const gchar *mstring_get_str(MString *mstr) |
| 74 | +static inline const gchar *mstring_get_str(MString *mstr) |
60 | 75 | {
|
61 |
| - return mstr->string; |
| 76 | + return mstr->gstr->str; |
62 | 77 | }
|
63 | 78 |
|
64 |
| -static inline |
65 |
| -size_t mstring_get_length(MString *mstr) |
| 79 | +static inline size_t mstring_get_length(MString *mstr) |
66 | 80 | {
|
67 |
| - return strlen(mstr->string); |
| 81 | + return mstr->gstr->len; |
68 | 82 | }
|
69 | 83 |
|
70 | 84 | #endif
|
0 commit comments