Commit | Line | Data |
---|---|---|
b469d2dd JD |
1 | /* |
2 | * context.c | |
3 | * | |
4 | * Babeltrace Library | |
5 | * | |
6 | * Copyright 2011-2012 EfficiOS Inc. and Linux Foundation | |
7 | * | |
8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
9 | * Julien Desfossez <julien.desfossez@efficios.com> | |
10 | * | |
11 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
12 | * of this software and associated documentation files (the "Software"), to deal | |
13 | * in the Software without restriction, including without limitation the rights | |
14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
15 | * copies of the Software, and to permit persons to whom the Software is | |
16 | * furnished to do so, subject to the following conditions: | |
17 | * | |
18 | * The above copyright notice and this permission notice shall be included in | |
19 | * all copies or substantial portions of the Software. | |
20 | */ | |
21 | ||
22 | #include <babeltrace/babeltrace.h> | |
23 | #include <babeltrace/context.h> | |
24 | #include <stdlib.h> | |
25 | ||
26 | struct bt_context *bt_context_create(struct trace_collection *tc) | |
27 | { | |
28 | struct bt_context *ctx; | |
29 | ||
30 | ctx = calloc(1, sizeof(struct bt_context)); | |
31 | if (ctx == NULL) { | |
32 | perror("allocating context"); | |
33 | goto error; | |
34 | } | |
35 | ||
36 | ctx->tc = tc; | |
37 | ctx->refcount = 1; | |
842c2b97 | 38 | ctx->last_trace_handle_id = 0; |
b469d2dd JD |
39 | |
40 | return ctx; | |
41 | ||
42 | error: | |
43 | return NULL; | |
44 | } | |
45 | ||
46 | int bt_context_destroy(struct bt_context *ctx) | |
47 | { | |
48 | if (ctx) { | |
49 | if (ctx->refcount >= 1) | |
50 | goto ctx_used; | |
51 | ||
52 | free(ctx); | |
53 | } | |
54 | return 0; | |
55 | ||
56 | ctx_used: | |
57 | return -1; | |
58 | } | |
59 | ||
60 | int bt_context_get(struct bt_context *ctx) | |
61 | { | |
62 | if (!ctx) | |
63 | return -1; | |
64 | ctx->refcount++; | |
65 | return 0; | |
66 | } | |
67 | ||
68 | int bt_context_put(struct bt_context *ctx) | |
69 | { | |
70 | if (!ctx) | |
71 | return -1; | |
72 | ||
73 | ctx->refcount--; | |
74 | if (ctx->refcount == 0) | |
75 | return bt_context_destroy(ctx); | |
76 | return 0; | |
77 | } |