clock.h: ensure all functions are trivial (10 lines or less)
[lttng-ust.git] / liblttng-ust / ltt-context.c
CommitLineData
8020ceb5
MD
1/*
2 * ltt-context.c
3 *
4 * Copyright 2011 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
8173ec7c 6 * LTTng UST trace/channel/event context management.
8020ceb5
MD
7 *
8 * Dual LGPL v2.1/GPL v2 license.
9 */
10
4318ae1b
MD
11#include <lttng/ust-events.h>
12#include <lttng/ust-tracer.h>
13#include <lttng/core.h>
8d8a24c8 14#include <string.h>
8173ec7c
MD
15#include <assert.h>
16
17int lttng_find_context(struct lttng_ctx *ctx, const char *name)
18{
19 unsigned int i;
20
21 for (i = 0; i < ctx->nr_fields; i++) {
22 /* Skip allocated (but non-initialized) contexts */
23 if (!ctx->fields[i].event_field.name)
24 continue;
25 if (!strcmp(ctx->fields[i].event_field.name, name))
26 return 1;
27 }
28 return 0;
29}
8020ceb5 30
a0a748b8
MD
31/*
32 * Note: as we append context information, the pointer location may change.
33 */
8020ceb5
MD
34struct lttng_ctx_field *lttng_append_context(struct lttng_ctx **ctx_p)
35{
36 struct lttng_ctx_field *field;
37 struct lttng_ctx *ctx;
38
39 if (!*ctx_p) {
8d8a24c8 40 *ctx_p = zmalloc(sizeof(struct lttng_ctx));
8020ceb5
MD
41 if (!*ctx_p)
42 return NULL;
43 }
44 ctx = *ctx_p;
45 if (ctx->nr_fields + 1 > ctx->allocated_fields) {
46 struct lttng_ctx_field *new_fields;
47
48 ctx->allocated_fields = max_t(size_t, 1, 2 * ctx->allocated_fields);
8d8a24c8 49 new_fields = zmalloc(ctx->allocated_fields * sizeof(struct lttng_ctx_field));
8020ceb5
MD
50 if (!new_fields)
51 return NULL;
52 if (ctx->fields)
53 memcpy(new_fields, ctx->fields, sizeof(*ctx->fields) * ctx->nr_fields);
8d8a24c8 54 free(ctx->fields);
8020ceb5
MD
55 ctx->fields = new_fields;
56 }
57 field = &ctx->fields[ctx->nr_fields];
58 ctx->nr_fields++;
59 return field;
60}
8020ceb5 61
b13d13b1
MD
62/*
63 * Remove last context field.
64 */
8020ceb5
MD
65void lttng_remove_context_field(struct lttng_ctx **ctx_p,
66 struct lttng_ctx_field *field)
67{
68 struct lttng_ctx *ctx;
69
70 ctx = *ctx_p;
71 ctx->nr_fields--;
8173ec7c 72 assert(&ctx->fields[ctx->nr_fields] == field);
8020ceb5
MD
73 memset(&ctx->fields[ctx->nr_fields], 0, sizeof(struct lttng_ctx_field));
74}
8020ceb5
MD
75
76void lttng_destroy_context(struct lttng_ctx *ctx)
77{
78 int i;
79
80 if (!ctx)
81 return;
82 for (i = 0; i < ctx->nr_fields; i++) {
83 if (ctx->fields[i].destroy)
84 ctx->fields[i].destroy(&ctx->fields[i]);
85 }
8d8a24c8
MD
86 free(ctx->fields);
87 free(ctx);
8020ceb5 88}
This page took 0.026393 seconds and 4 git commands to generate.