Commit | Line | Data |
---|---|---|
312c056a PP |
1 | /* |
2 | * Copyright 2018 Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 | * of this software and associated documentation files (the "Software"), to deal | |
6 | * in the Software without restriction, including without limitation the rights | |
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 | * copies of the Software, and to permit persons to whom the Software is | |
9 | * furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | * | |
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
20 | * SOFTWARE. | |
21 | */ | |
22 | ||
23 | #define BT_LOG_TAG "FIELD-WRAPPER" | |
3fadfbc0 | 24 | #include <babeltrace2/lib-logging-internal.h> |
312c056a | 25 | |
3fadfbc0 MJ |
26 | #include <babeltrace2/trace-ir/field-wrapper-internal.h> |
27 | #include <babeltrace2/trace-ir/field-internal.h> | |
28 | #include <babeltrace2/object-pool-internal.h> | |
29 | #include <babeltrace2/object-internal.h> | |
312c056a PP |
30 | #include <glib.h> |
31 | ||
32 | BT_HIDDEN | |
33 | struct bt_field_wrapper *bt_field_wrapper_new(void *data) | |
34 | { | |
35 | struct bt_field_wrapper *field_wrapper = | |
36 | g_new0(struct bt_field_wrapper, 1); | |
37 | ||
38 | BT_LOGD_STR("Creating empty field wrapper object."); | |
39 | ||
40 | if (!field_wrapper) { | |
41 | BT_LOGE("Failed to allocate one field wrapper."); | |
42 | goto end; | |
43 | } | |
44 | ||
3fea54f6 | 45 | bt_object_init_unique(&field_wrapper->base); |
312c056a PP |
46 | BT_LOGD("Created empty field wrapper object: addr=%p", |
47 | field_wrapper); | |
48 | ||
49 | end: | |
50 | return field_wrapper; | |
51 | } | |
52 | ||
53 | BT_HIDDEN | |
54 | void bt_field_wrapper_destroy(struct bt_field_wrapper *field_wrapper) | |
55 | { | |
56 | BT_LOGD("Destroying field wrapper: addr=%p", field_wrapper); | |
57 | ||
58 | if (field_wrapper->field) { | |
59 | BT_LOGD_STR("Destroying field."); | |
44c440bc | 60 | bt_field_destroy((void *) field_wrapper->field); |
238b7404 | 61 | field_wrapper->field = NULL; |
312c056a PP |
62 | } |
63 | ||
64 | BT_LOGD_STR("Putting stream class."); | |
65 | g_free(field_wrapper); | |
66 | } | |
67 | ||
68 | BT_HIDDEN | |
69 | struct bt_field_wrapper *bt_field_wrapper_create( | |
5cd6d0e5 | 70 | struct bt_object_pool *pool, struct bt_field_class *fc) |
312c056a PP |
71 | { |
72 | struct bt_field_wrapper *field_wrapper = NULL; | |
73 | ||
74 | BT_ASSERT(pool); | |
5cd6d0e5 | 75 | BT_ASSERT(fc); |
312c056a PP |
76 | field_wrapper = bt_object_pool_create_object(pool); |
77 | if (!field_wrapper) { | |
78 | BT_LIB_LOGE("Cannot allocate one field wrapper from field wrapper pool: " | |
79 | "%![pool-]+o", pool); | |
80 | goto error; | |
81 | } | |
82 | ||
83 | if (!field_wrapper->field) { | |
5cd6d0e5 | 84 | field_wrapper->field = (void *) bt_field_create(fc); |
312c056a | 85 | if (!field_wrapper->field) { |
e6276565 | 86 | BT_LIB_LOGE("Cannot create field wrapper from field class: " |
5cd6d0e5 | 87 | "%![fc-]+F", fc); |
312c056a PP |
88 | goto error; |
89 | } | |
90 | ||
91 | BT_LIB_LOGD("Created initial field wrapper object: " | |
92 | "wrapper-addr=%p, %![field-]+f", field_wrapper, | |
93 | field_wrapper->field); | |
94 | } | |
95 | ||
96 | goto end; | |
97 | ||
98 | error: | |
99 | if (field_wrapper) { | |
100 | bt_field_wrapper_destroy(field_wrapper); | |
101 | field_wrapper = NULL; | |
102 | } | |
103 | ||
104 | end: | |
105 | return field_wrapper; | |
106 | } |