monitor: Drop str_allocated[]
[qemu/aliguori-queue.git] / qstring.c
blob9fa2e3024eddff618c1d4f113901144758212881
1 /*
2 * QString data type.
4 * Copyright (C) 2009 Red Hat Inc.
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
12 #include "qobject.h"
13 #include "qstring.h"
14 #include "qemu-common.h"
16 static const QType qstring_type;
18 /**
19 * qstring_from_str(): Create a new QString from a regular C string
21 * Return strong reference.
23 QString *qstring_from_str(const char *str)
25 QString *qstring;
27 qstring = qemu_malloc(sizeof(*qstring));
28 qstring->string = qemu_strdup(str);
29 QOBJECT_INIT(qstring, &qstring_type);
31 return qstring;
34 /**
35 * qobject_to_qstring(): Convert a QObject to a QString
37 QString *qobject_to_qstring(const QObject *obj)
39 if (qobject_type(obj) != QTYPE_QSTRING)
40 return NULL;
42 return container_of(obj, QString, base);
45 /**
46 * qstring_get_str(): Return a pointer to the stored string
48 * NOTE: Should be used with caution, if the object is deallocated
49 * this pointer becomes invalid.
51 const char *qstring_get_str(const QString *qstring)
53 return qstring->string;
56 /**
57 * qstring_destroy_obj(): Free all memory allocated by a QString
58 * object
60 static void qstring_destroy_obj(QObject *obj)
62 QString *qs;
64 assert(obj != NULL);
65 qs = qobject_to_qstring(obj);
66 qemu_free(qs->string);
67 qemu_free(qs);
70 static const QType qstring_type = {
71 .code = QTYPE_QSTRING,
72 .destroy = qstring_destroy_obj,