Refactoring: introduce lttng_tracker_ids data structure
[lttng-tools.git] / src / common / tracker.c
CommitLineData
2d97a006
JR
1/*
2 * Copyright (C) 2019 - Jonathan Rajotte-Julien <jonathan.rajotte-julien@efficios.com>
3 *
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
7 *
8 * This library is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
11 * for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18#include <assert.h>
19#include <common/defaults.h>
20#include <common/error.h>
21#include <common/macros.h>
22#include <common/sessiond-comm/sessiond-comm.h>
23#include <common/uri.h>
24#include <lttng/tracker-internal.h>
25#include <stdio.h>
26#include <time.h>
27
28struct lttng_tracker_id *lttng_tracker_id_create(void)
29{
30 struct lttng_tracker_id *id;
31
32 id = zmalloc(sizeof(*id));
33 if (!id) {
34 goto error;
35 }
36
37 id->type = LTTNG_ID_UNKNOWN;
38 id->string = NULL;
39 id->value = -1;
40 return id;
41error:
42 lttng_tracker_id_destroy(id);
43 return NULL;
44}
45
46enum lttng_tracker_id_status lttng_tracker_id_set_value(
47 struct lttng_tracker_id *id, int value)
48{
49 assert(id);
50
51 if (value < 0) {
52 return LTTNG_TRACKER_ID_STATUS_INVALID;
53 }
54
55 id->type = LTTNG_ID_VALUE;
56 id->value = value;
57 return LTTNG_TRACKER_ID_STATUS_OK;
58}
59
60enum lttng_tracker_id_status lttng_tracker_id_set_string(
61 struct lttng_tracker_id *id, const char *value)
62{
63 assert(id);
64 assert(value);
65
66 id->type = LTTNG_ID_STRING;
67 id->string = strdup(value);
68 if (id->string == NULL) {
69 /* No memory left */
70 goto error;
71 }
72
73 return LTTNG_TRACKER_ID_STATUS_OK;
74error:
75 return LTTNG_TRACKER_ID_STATUS_INVALID;
76}
77
78enum lttng_tracker_id_status lttng_tracker_id_set_all(
79 struct lttng_tracker_id *id)
80{
81 assert(id);
82
83 id->type = LTTNG_ID_ALL;
84
85 return LTTNG_TRACKER_ID_STATUS_OK;
86}
87
a7a533cd 88static void lttng_tracker_id_reset(struct lttng_tracker_id *id)
2d97a006
JR
89{
90 if (id == NULL) {
91 return;
92 }
93
94 if (id->string != NULL) {
95 free(id->string);
a7a533cd 96 id->string = NULL;
2d97a006
JR
97 }
98
a7a533cd
JR
99 id->type = LTTNG_ID_UNKNOWN;
100 id->value = -1;
2d97a006
JR
101}
102
a7a533cd 103void lttng_tracker_id_destroy(struct lttng_tracker_id *id)
2d97a006 104{
a7a533cd 105 if (id == NULL) {
2d97a006
JR
106 return;
107 }
108
a7a533cd
JR
109 lttng_tracker_id_reset(id);
110
111 free(id);
2d97a006
JR
112}
113
114enum lttng_tracker_id_type lttng_tracker_id_get_type(
115 const struct lttng_tracker_id *id)
116{
117 assert(id);
118 return id->type;
119}
120
121enum lttng_tracker_id_status lttng_tracker_id_get_value(
122 const struct lttng_tracker_id *id, int *value)
123{
124 assert(id);
125 if (id->type == LTTNG_ID_UNKNOWN) {
126 return LTTNG_TRACKER_ID_STATUS_UNSET;
127 }
128
129 if (id->type != LTTNG_ID_VALUE) {
130 return LTTNG_TRACKER_ID_STATUS_INVALID;
131 }
132
133 *value = id->value;
134 return LTTNG_TRACKER_ID_STATUS_OK;
135}
136
137bool lttng_tracker_id_is_equal(const struct lttng_tracker_id *left,
138 const struct lttng_tracker_id *right)
139{
140 if (left->type != right->type) {
141 return 0;
142 }
143
144 switch (left->type) {
145 case LTTNG_ID_ALL:
146 return 1;
147 case LTTNG_ID_VALUE:
148 if (left->value != right->value) {
149 return 0;
150 }
151 break;
152 case LTTNG_ID_STRING:
153 if (strcmp(left->string, right->string) != 0) {
154 return 0;
155 }
156 break;
157 default:
158 /*
159 * Normally this should return true, but comparing unset tracker
160 * id is "invalid".
161 */
162 return 0;
163 }
164 return 1;
165}
166
a7a533cd 167int lttng_tracker_id_copy(struct lttng_tracker_id *dest,
2d97a006
JR
168 const struct lttng_tracker_id *orig)
169{
a7a533cd 170 int ret = 0;
2d97a006
JR
171 enum lttng_tracker_id_status status;
172
a7a533cd
JR
173 assert(dest);
174 assert(orig);
2d97a006
JR
175
176 switch (orig->type) {
177 case LTTNG_ID_ALL:
a7a533cd 178 status = lttng_tracker_id_set_all(dest);
2d97a006
JR
179 break;
180 case LTTNG_ID_VALUE:
a7a533cd 181 status = lttng_tracker_id_set_value(dest, orig->value);
2d97a006
JR
182 break;
183 case LTTNG_ID_STRING:
a7a533cd 184 status = lttng_tracker_id_set_string(dest, orig->string);
2d97a006
JR
185 break;
186 default:
187 status = LTTNG_TRACKER_ID_STATUS_OK;
188 break;
189 }
190
191 if (status != LTTNG_TRACKER_ID_STATUS_OK) {
a7a533cd
JR
192 ret = -1;
193 goto error;
194 }
195error:
196 return ret;
197}
198
199struct lttng_tracker_id *lttng_tracker_id_duplicate(
200 const struct lttng_tracker_id *orig)
201{
202 int ret;
203 struct lttng_tracker_id *copy = NULL;
204
205 copy = lttng_tracker_id_create();
206 if (copy == NULL) {
207 goto error;
208 }
209
210 ret = lttng_tracker_id_copy(copy, orig);
211 if (ret) {
2d97a006
JR
212 goto error;
213 }
214
215 return copy;
216error:
217 lttng_tracker_id_destroy(copy);
218 return NULL;
219}
220
221enum lttng_tracker_id_status lttng_tracker_id_get_string(
222 const struct lttng_tracker_id *id, const char **value)
223{
224 assert(id);
225 if (id->type == LTTNG_ID_UNKNOWN) {
226 *value = NULL;
227 return LTTNG_TRACKER_ID_STATUS_UNSET;
228 }
229
230 if (id->type != LTTNG_ID_STRING) {
231 *value = NULL;
232 return LTTNG_TRACKER_ID_STATUS_INVALID;
233 }
234
235 *value = id->string;
236 return LTTNG_TRACKER_ID_STATUS_OK;
237}
a7a533cd
JR
238
239struct lttng_tracker_ids *lttng_tracker_ids_create(unsigned int count)
240{
241 struct lttng_tracker_ids *ids = NULL;
242
243 ids = zmalloc(sizeof(*ids));
244 if (!ids) {
245 goto error;
246 }
247
248 ids->id_array = zmalloc(sizeof(struct lttng_tracker_id) * count);
249 if (!ids->id_array) {
250 goto error;
251 }
252
253 ids->count = count;
254
255 return ids;
256error:
257 free(ids);
258 return NULL;
259}
260
261LTTNG_HIDDEN
262struct lttng_tracker_id *lttng_tracker_ids_get_pointer_of_index(
263 const struct lttng_tracker_ids *ids, unsigned int index)
264{
265 assert(ids);
266 if (index >= ids->count) {
267 return NULL;
268 }
269
270 return &ids->id_array[index];
271}
272
273const struct lttng_tracker_id *lttng_tracker_ids_get_at_index(
274 const struct lttng_tracker_ids *ids, unsigned int index)
275{
276 assert(ids);
277 return lttng_tracker_ids_get_pointer_of_index(ids, index);
278}
279
280int lttng_tracker_ids_get_count(const struct lttng_tracker_ids *ids)
281{
282 assert(ids);
283 return ids->count;
284}
285
286void lttng_tracker_ids_destroy(struct lttng_tracker_ids *ids)
287{
288 if (!ids) {
289 return;
290 }
291
292 for (int i = 0; i < ids->count; i++) {
293 lttng_tracker_id_reset(&ids->id_array[i]);
294 }
295 free(ids->id_array);
296 free(ids);
297}
This page took 0.052277 seconds and 4 git commands to generate.