030a11b0908d01c2b7174ae49c3ac6ab5e446bc9
[lttng-ust.git] / libust / ltt-context.c
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
11 #include <linux/module.h>
12 #include <linux/list.h>
13 #include <linux/mutex.h>
14 #include <linux/slab.h>
15 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
16 #include "ltt-events.h"
17 #include "ltt-tracer.h"
18
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) {
25 *ctx_p = kzalloc(sizeof(struct lttng_ctx), GFP_KERNEL);
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);
34 new_fields = kzalloc(ctx->allocated_fields * sizeof(struct lttng_ctx_field), GFP_KERNEL);
35 if (!new_fields)
36 return NULL;
37 if (ctx->fields)
38 memcpy(new_fields, ctx->fields, sizeof(*ctx->fields) * ctx->nr_fields);
39 kfree(ctx->fields);
40 ctx->fields = new_fields;
41 }
42 field = &ctx->fields[ctx->nr_fields];
43 ctx->nr_fields++;
44 return field;
45 }
46 EXPORT_SYMBOL_GPL(lttng_append_context);
47
48 /*
49 * Remove last context field.
50 */
51 void lttng_remove_context_field(struct lttng_ctx **ctx_p,
52 struct lttng_ctx_field *field)
53 {
54 struct lttng_ctx *ctx;
55
56 ctx = *ctx_p;
57 ctx->nr_fields--;
58 WARN_ON_ONCE(&ctx->fields[ctx->nr_fields] != field);
59 memset(&ctx->fields[ctx->nr_fields], 0, sizeof(struct lttng_ctx_field));
60 }
61 EXPORT_SYMBOL_GPL(lttng_remove_context_field);
62
63 void lttng_destroy_context(struct lttng_ctx *ctx)
64 {
65 int i;
66
67 if (!ctx)
68 return;
69 for (i = 0; i < ctx->nr_fields; i++) {
70 if (ctx->fields[i].destroy)
71 ctx->fields[i].destroy(&ctx->fields[i]);
72 }
73 kfree(ctx->fields);
74 kfree(ctx);
75 }
This page took 0.030511 seconds and 3 git commands to generate.