Version 2.3.2
[lttng-modules.git] / lttng-context.c
CommitLineData
2dccf128 1/*
a90917c3 2 * lttng-context.c
2dccf128 3 *
2dccf128 4 * LTTng trace/channel/event context management.
17baffe2 5 *
886d51a3
MD
6 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2dccf128
MD
21 */
22
23#include <linux/module.h>
24#include <linux/list.h>
25#include <linux/mutex.h>
26#include <linux/slab.h>
27#include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
a90917c3
MD
28#include "lttng-events.h"
29#include "lttng-tracer.h"
2dccf128 30
44252f0f
MD
31int lttng_find_context(struct lttng_ctx *ctx, const char *name)
32{
33 unsigned int i;
34
35 for (i = 0; i < ctx->nr_fields; i++) {
4b58a8e4
MD
36 /* Skip allocated (but non-initialized) contexts */
37 if (!ctx->fields[i].event_field.name)
38 continue;
44252f0f
MD
39 if (!strcmp(ctx->fields[i].event_field.name, name))
40 return 1;
41 }
42 return 0;
43}
44EXPORT_SYMBOL_GPL(lttng_find_context);
45
2001023e
MD
46/*
47 * Note: as we append context information, the pointer location may change.
48 */
2dccf128
MD
49struct lttng_ctx_field *lttng_append_context(struct lttng_ctx **ctx_p)
50{
51 struct lttng_ctx_field *field;
52 struct lttng_ctx *ctx;
53
54 if (!*ctx_p) {
55 *ctx_p = kzalloc(sizeof(struct lttng_ctx), GFP_KERNEL);
56 if (!*ctx_p)
57 return NULL;
58 }
59 ctx = *ctx_p;
60 if (ctx->nr_fields + 1 > ctx->allocated_fields) {
61 struct lttng_ctx_field *new_fields;
62
0f034e0f 63 ctx->allocated_fields = max_t(size_t, 1, 2 * ctx->allocated_fields);
2dccf128
MD
64 new_fields = kzalloc(ctx->allocated_fields * sizeof(struct lttng_ctx_field), GFP_KERNEL);
65 if (!new_fields)
66 return NULL;
67 if (ctx->fields)
77aefe99 68 memcpy(new_fields, ctx->fields, sizeof(*ctx->fields) * ctx->nr_fields);
2dccf128
MD
69 kfree(ctx->fields);
70 ctx->fields = new_fields;
71 }
72 field = &ctx->fields[ctx->nr_fields];
73 ctx->nr_fields++;
74 return field;
75}
76EXPORT_SYMBOL_GPL(lttng_append_context);
77
4cae220c
MD
78/*
79 * Remove last context field.
80 */
8289661d
MD
81void lttng_remove_context_field(struct lttng_ctx **ctx_p,
82 struct lttng_ctx_field *field)
83{
84 struct lttng_ctx *ctx;
85
86 ctx = *ctx_p;
87 ctx->nr_fields--;
4cae220c 88 WARN_ON_ONCE(&ctx->fields[ctx->nr_fields] != field);
8289661d
MD
89 memset(&ctx->fields[ctx->nr_fields], 0, sizeof(struct lttng_ctx_field));
90}
91EXPORT_SYMBOL_GPL(lttng_remove_context_field);
92
2dccf128
MD
93void lttng_destroy_context(struct lttng_ctx *ctx)
94{
95 int i;
96
8070f5c0
MD
97 if (!ctx)
98 return;
9e7e4892
MD
99 for (i = 0; i < ctx->nr_fields; i++) {
100 if (ctx->fields[i].destroy)
101 ctx->fields[i].destroy(&ctx->fields[i]);
102 }
2dccf128
MD
103 kfree(ctx->fields);
104 kfree(ctx);
105}
This page took 0.029719 seconds and 4 git commands to generate.