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