Commit | Line | Data |
---|---|---|
2dccf128 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. | |
17baffe2 MD |
7 | * |
8 | * Dual LGPL v2.1/GPL v2 license. | |
2dccf128 MD |
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 | ||
0f034e0f | 33 | ctx->allocated_fields = max_t(size_t, 1, 2 * ctx->allocated_fields); |
2dccf128 MD |
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) | |
77aefe99 | 38 | memcpy(new_fields, ctx->fields, sizeof(*ctx->fields) * ctx->nr_fields); |
2dccf128 MD |
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 | ||
8289661d MD |
48 | void lttng_remove_context_field(struct lttng_ctx **ctx_p, |
49 | struct lttng_ctx_field *field) | |
50 | { | |
51 | struct lttng_ctx *ctx; | |
52 | ||
53 | ctx = *ctx_p; | |
54 | ctx->nr_fields--; | |
55 | memset(&ctx->fields[ctx->nr_fields], 0, sizeof(struct lttng_ctx_field)); | |
56 | } | |
57 | EXPORT_SYMBOL_GPL(lttng_remove_context_field); | |
58 | ||
2dccf128 MD |
59 | void lttng_destroy_context(struct lttng_ctx *ctx) |
60 | { | |
61 | int i; | |
62 | ||
8070f5c0 MD |
63 | if (!ctx) |
64 | return; | |
9e7e4892 MD |
65 | for (i = 0; i < ctx->nr_fields; i++) { |
66 | if (ctx->fields[i].destroy) | |
67 | ctx->fields[i].destroy(&ctx->fields[i]); | |
68 | } | |
2dccf128 MD |
69 | kfree(ctx->fields); |
70 | kfree(ctx); | |
71 | } |