blob: bad012bda595732471719d55afb72b623c90d094 [file] [log] [blame]
Harald Welte2d906112019-03-18 17:17:43 +01001/*! \file context.c
2 * talloc context handling.
3 *
4 * (C) 2019 by Harald Welte <laforge@gnumonks.org>
5 * All Rights Reserverd.
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 * MA 02110-1301, USA.
23 */
24#include <string.h>
25#include <errno.h>
26#include <osmocom/core/talloc.h>
27#include <osmocom/core/utils.h>
28
29__thread struct osmo_talloc_contexts *osmo_ctx;
30
31int osmo_ctx_init(const char *id)
32{
33 osmo_ctx = talloc_named(NULL, sizeof(*osmo_ctx), "global-%s", id);
34 if (!osmo_ctx)
35 return -ENOMEM;
36 memset(osmo_ctx, 0, sizeof(*osmo_ctx));
37 osmo_ctx->global = osmo_ctx;
38 osmo_ctx->select = talloc_named_const(osmo_ctx->global, 0, "select");
39 if (!osmo_ctx->select) {
40 talloc_free(osmo_ctx);
41 return -ENOMEM;
42 }
43 return 0;
44}
45
46/* initialize osmo_ctx on main tread */
47static __attribute__((constructor)) void on_dso_load_ctx(void)
48{
49 OSMO_ASSERT(osmo_ctx_init("main") == 0);
50}
51
52/*! @} */