| 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 <ust/lttng-events.h> |
| 16 | #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */ |
| 17 | #include "ltt-tracer.h" |
| 18 | |
| 19 | /* |
| 20 | * Note: as we append context information, the pointer location may change. |
| 21 | */ |
| 22 | struct lttng_ctx_field *lttng_append_context(struct lttng_ctx **ctx_p) |
| 23 | { |
| 24 | struct lttng_ctx_field *field; |
| 25 | struct lttng_ctx *ctx; |
| 26 | |
| 27 | if (!*ctx_p) { |
| 28 | *ctx_p = kzalloc(sizeof(struct lttng_ctx), GFP_KERNEL); |
| 29 | if (!*ctx_p) |
| 30 | return NULL; |
| 31 | } |
| 32 | ctx = *ctx_p; |
| 33 | if (ctx->nr_fields + 1 > ctx->allocated_fields) { |
| 34 | struct lttng_ctx_field *new_fields; |
| 35 | |
| 36 | ctx->allocated_fields = max_t(size_t, 1, 2 * ctx->allocated_fields); |
| 37 | new_fields = kzalloc(ctx->allocated_fields * sizeof(struct lttng_ctx_field), GFP_KERNEL); |
| 38 | if (!new_fields) |
| 39 | return NULL; |
| 40 | if (ctx->fields) |
| 41 | memcpy(new_fields, ctx->fields, sizeof(*ctx->fields) * ctx->nr_fields); |
| 42 | kfree(ctx->fields); |
| 43 | ctx->fields = new_fields; |
| 44 | } |
| 45 | field = &ctx->fields[ctx->nr_fields]; |
| 46 | ctx->nr_fields++; |
| 47 | return field; |
| 48 | } |
| 49 | EXPORT_SYMBOL_GPL(lttng_append_context); |
| 50 | |
| 51 | /* |
| 52 | * Remove last context field. |
| 53 | */ |
| 54 | void lttng_remove_context_field(struct lttng_ctx **ctx_p, |
| 55 | struct lttng_ctx_field *field) |
| 56 | { |
| 57 | struct lttng_ctx *ctx; |
| 58 | |
| 59 | ctx = *ctx_p; |
| 60 | ctx->nr_fields--; |
| 61 | WARN_ON_ONCE(&ctx->fields[ctx->nr_fields] != field); |
| 62 | memset(&ctx->fields[ctx->nr_fields], 0, sizeof(struct lttng_ctx_field)); |
| 63 | } |
| 64 | EXPORT_SYMBOL_GPL(lttng_remove_context_field); |
| 65 | |
| 66 | void lttng_destroy_context(struct lttng_ctx *ctx) |
| 67 | { |
| 68 | int i; |
| 69 | |
| 70 | if (!ctx) |
| 71 | return; |
| 72 | for (i = 0; i < ctx->nr_fields; i++) { |
| 73 | if (ctx->fields[i].destroy) |
| 74 | ctx->fields[i].destroy(&ctx->fields[i]); |
| 75 | } |
| 76 | kfree(ctx->fields); |
| 77 | kfree(ctx); |
| 78 | } |