xref: /illumos-kvm-cmd/qfloat.c (revision 68396ea9)
1 /*
2  * QFloat Module
3  *
4  * Copyright IBM, Corp. 2009
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10  * See the COPYING.LIB file in the top-level directory.
11  *
12  */
13 
14 #include "qfloat.h"
15 #include "qobject.h"
16 #include "qemu-common.h"
17 
18 static void qfloat_destroy_obj(QObject *obj);
19 
20 static const QType qfloat_type = {
21     .code = QTYPE_QFLOAT,
22     .destroy = qfloat_destroy_obj,
23 };
24 
25 /**
26  * qfloat_from_int(): Create a new QFloat from a float
27  *
28  * Return strong reference.
29  */
qfloat_from_double(double value)30 QFloat *qfloat_from_double(double value)
31 {
32     QFloat *qf;
33 
34     qf = qemu_malloc(sizeof(*qf));
35     qf->value = value;
36     QOBJECT_INIT(qf, &qfloat_type);
37 
38     return qf;
39 }
40 
41 /**
42  * qfloat_get_double(): Get the stored float
43  */
qfloat_get_double(const QFloat * qf)44 double qfloat_get_double(const QFloat *qf)
45 {
46     return qf->value;
47 }
48 
49 /**
50  * qobject_to_qfloat(): Convert a QObject into a QFloat
51  */
qobject_to_qfloat(const QObject * obj)52 QFloat *qobject_to_qfloat(const QObject *obj)
53 {
54     if (qobject_type(obj) != QTYPE_QFLOAT)
55         return NULL;
56 
57     return container_of(obj, QFloat, base);
58 }
59 
60 /**
61  * qfloat_destroy_obj(): Free all memory allocated by a
62  * QFloat object
63  */
qfloat_destroy_obj(QObject * obj)64 static void qfloat_destroy_obj(QObject *obj)
65 {
66     assert(obj != NULL);
67     qemu_free(qobject_to_qfloat(obj));
68 }
69