Harald Welte | 2d90611 | 2019-03-18 17:17:43 +0100 | [diff] [blame] | 1 | /*! \file context.c |
| 2 | * talloc context handling. |
| 3 | * |
| 4 | * (C) 2019 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 6dda35a | 2022-11-09 16:54:50 +0100 | [diff] [blame] | 5 | * All Rights Reserved. |
Harald Welte | 2d90611 | 2019-03-18 17:17:43 +0100 | [diff] [blame] | 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. |
Harald Welte | 2d90611 | 2019-03-18 17:17:43 +0100 | [diff] [blame] | 18 | */ |
| 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 | |
| 26 | int 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 */ |
| 42 | static __attribute__((constructor)) void on_dso_load_ctx(void) |
| 43 | { |
| 44 | OSMO_ASSERT(osmo_ctx_init("main") == 0); |
| 45 | } |
| 46 | |
| 47 | /*! @} */ |