Skip to content

Commit 9d5cf09

Browse files
committed
util/mstring: Use GString
1 parent ab811bf commit 9d5cf09

File tree

3 files changed

+53
-89
lines changed

3 files changed

+53
-89
lines changed

include/qemu/mstring.h

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,83 @@
22
#define MSTRING_H
33

44
#include "qemu/osdep.h"
5-
#include <string.h>
5+
#include "glib.h"
66

77
typedef struct {
8-
int ref;
9-
gchar *string;
8+
GString *gstr;
9+
int refcnt;
1010
} MString;
1111

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)
1527

16-
static inline
17-
void mstring_ref(MString *mstr)
28+
static inline MString *mstring_new(void)
1829
{
19-
mstr->ref++;
30+
MString *mstr = g_malloc(sizeof(MString));
31+
mstr->refcnt = 1;
32+
mstr->gstr = g_string_new("");
33+
return mstr;
2034
}
2135

22-
static inline
23-
void mstring_unref(MString *mstr)
36+
static inline MString *mstring_from_str(const char *str)
2437
{
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;
3042
}
3143

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, ...)
3446
{
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;
3858
}
3959

40-
static inline
41-
MString *mstring_new(void)
60+
static inline void mstring_append(MString *mstr, const char *str)
4261
{
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);
4763
}
4864

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, ...)
5167
{
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);
5672
}
5773

58-
static inline
59-
const gchar *mstring_get_str(MString *mstr)
74+
static inline const gchar *mstring_get_str(MString *mstr)
6075
{
61-
return mstr->string;
76+
return mstr->gstr->str;
6277
}
6378

64-
static inline
65-
size_t mstring_get_length(MString *mstr)
79+
static inline size_t mstring_get_length(MString *mstr)
6680
{
67-
return strlen(mstr->string);
81+
return mstr->gstr->len;
6882
}
6983

7084
#endif

util/meson.build

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ if host_os == 'windows'
6868
util_ss.add(files('miniz/miniz.c'))
6969
endif
7070
util_ss.add(files('fast-hash.c'))
71-
util_ss.add(files('mstring.c'))
7271

7372
if have_user
7473
util_ss.add(files('selfmap.c'))

util/mstring.c

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)