Commit | Line | Data |
---|---|---|
8020ceb5 MD |
1 | /* |
2 | * ltt-context.c | |
3 | * | |
4 | * Copyright 2011 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
5 | * | |
6 | * LTTng trace/channel/event context management. | |
7 | * | |
8 | * Dual LGPL v2.1/GPL v2 license. | |
9 | */ | |
10 | ||
f4681817 | 11 | #include <ust/lttng-events.h> |
8d8a24c8 MD |
12 | #include <ust/core.h> |
13 | #include <string.h> | |
1dbfff0c | 14 | #include <ust/usterr-signal-safe.h> |
8020ceb5 | 15 | |
a0a748b8 MD |
16 | /* |
17 | * Note: as we append context information, the pointer location may change. | |
18 | */ | |
8020ceb5 MD |
19 | struct lttng_ctx_field *lttng_append_context(struct lttng_ctx **ctx_p) |
20 | { | |
21 | struct lttng_ctx_field *field; | |
22 | struct lttng_ctx *ctx; | |
23 | ||
24 | if (!*ctx_p) { | |
8d8a24c8 | 25 | *ctx_p = zmalloc(sizeof(struct lttng_ctx)); |
8020ceb5 MD |
26 | if (!*ctx_p) |
27 | return NULL; | |
28 | } | |
29 | ctx = *ctx_p; | |
30 | if (ctx->nr_fields + 1 > ctx->allocated_fields) { | |
31 | struct lttng_ctx_field *new_fields; | |
32 | ||
33 | ctx->allocated_fields = max_t(size_t, 1, 2 * ctx->allocated_fields); | |
8d8a24c8 | 34 | new_fields = zmalloc(ctx->allocated_fields * sizeof(struct lttng_ctx_field)); |
8020ceb5 MD |
35 | if (!new_fields) |
36 | return NULL; | |
37 | if (ctx->fields) | |
38 | memcpy(new_fields, ctx->fields, sizeof(*ctx->fields) * ctx->nr_fields); | |
8d8a24c8 | 39 | free(ctx->fields); |
8020ceb5 MD |
40 | ctx->fields = new_fields; |
41 | } | |
42 | field = &ctx->fields[ctx->nr_fields]; | |
43 | ctx->nr_fields++; | |
44 | return field; | |
45 | } | |
8020ceb5 | 46 | |
b13d13b1 MD |
47 | /* |
48 | * Remove last context field. | |
49 | */ | |
8020ceb5 MD |
50 | void lttng_remove_context_field(struct lttng_ctx **ctx_p, |
51 | struct lttng_ctx_field *field) | |
52 | { | |
53 | struct lttng_ctx *ctx; | |
54 | ||
55 | ctx = *ctx_p; | |
56 | ctx->nr_fields--; | |
b13d13b1 | 57 | WARN_ON_ONCE(&ctx->fields[ctx->nr_fields] != field); |
8020ceb5 MD |
58 | memset(&ctx->fields[ctx->nr_fields], 0, sizeof(struct lttng_ctx_field)); |
59 | } | |
8020ceb5 MD |
60 | |
61 | void lttng_destroy_context(struct lttng_ctx *ctx) | |
62 | { | |
63 | int i; | |
64 | ||
65 | if (!ctx) | |
66 | return; | |
67 | for (i = 0; i < ctx->nr_fields; i++) { | |
68 | if (ctx->fields[i].destroy) | |
69 | ctx->fields[i].destroy(&ctx->fields[i]); | |
70 | } | |
8d8a24c8 MD |
71 | free(ctx->fields); |
72 | free(ctx); | |
8020ceb5 | 73 | } |