blob: a0b3a555bd3ccc507c9dc8149a1b8ed3a71ad1b7 [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>
Harald Welte6dda35a2022-11-09 16:54:50 +01005 * All Rights Reserved.
Harald Welte2d906112019-03-18 17:17:43 +01006 *
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.
Harald Welte2d906112019-03-18 17:17:43 +010018 */
19#include <string.h>
20#include <errno.h>
21#include <osmocom/core/talloc.h>
22#include <osmocom/core/utils.h>
23
24__thread struct osmo_talloc_contexts *osmo_ctx;
25
26int osmo_ctx_init(const char *id)
27{
28 osmo_ctx = talloc_named(NULL, sizeof(*osmo_ctx), "global-%s", id);
29 if (!osmo_ctx)
30 return -ENOMEM;
31 memset(osmo_ctx, 0, sizeof(*osmo_ctx));
32 osmo_ctx->global = osmo_ctx;
33 osmo_ctx->select = talloc_named_const(osmo_ctx->global, 0, "select");
34 if (!osmo_ctx->select) {
35 talloc_free(osmo_ctx);
36 return -ENOMEM;
37 }
38 return 0;
39}
40
41/* initialize osmo_ctx on main tread */
Oliver Smithb46cfba2023-03-14 15:05:03 +010042static __attribute__((constructor(101))) void on_dso_load_ctx(void)
Harald Welte2d906112019-03-18 17:17:43 +010043{
44 OSMO_ASSERT(osmo_ctx_init("main") == 0);
45}
46
47/*! @} */