Add missing context.h/context.c
[babeltrace.git] / lib / context.c
CommitLineData
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
26struct 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;
38
39 return ctx;
40
41error:
42 return NULL;
43}
44
45int bt_context_destroy(struct bt_context *ctx)
46{
47 if (ctx) {
48 if (ctx->refcount >= 1)
49 goto ctx_used;
50
51 free(ctx);
52 }
53 return 0;
54
55ctx_used:
56 return -1;
57}
58
59int bt_context_get(struct bt_context *ctx)
60{
61 if (!ctx)
62 return -1;
63 ctx->refcount++;
64 return 0;
65}
66
67int bt_context_put(struct bt_context *ctx)
68{
69 if (!ctx)
70 return -1;
71
72 ctx->refcount--;
73 if (ctx->refcount == 0)
74 return bt_context_destroy(ctx);
75 return 0;
76}
This page took 0.035806 seconds and 4 git commands to generate.