Harald Welte | 2d90611 | 2019-03-18 17:17:43 +0100 | [diff] [blame] | 1 | /*! \file talloc.h */ |
Harald Welte | 90e614f | 2015-12-05 23:38:18 +0100 | [diff] [blame] | 2 | #pragma once |
| 3 | #include <talloc.h> |
Harald Welte | 2d90611 | 2019-03-18 17:17:43 +0100 | [diff] [blame] | 4 | |
| 5 | /*! per-thread talloc contexts. This works around the problem that talloc is not |
| 6 | * thread-safe. However, one can simply have a different set of talloc contexts for each |
| 7 | * thread, and ensure that allocations made on one thread are always only free'd on that |
| 8 | * very same thread. |
| 9 | * WARNING: Users must make sure they free() on the same thread as they allocate!! */ |
| 10 | struct osmo_talloc_contexts { |
| 11 | /*! global per-thread talloc context. */ |
| 12 | void *global; |
| 13 | /*! volatile select-dispatch context. This context is completely free'd and |
| 14 | * re-created every time the main select loop in osmo_select_main() returns from |
| 15 | * select(2) and calls per-fd callback functions. This allows users of this |
| 16 | * facility to allocate temporary objects like string buffers, message buffers |
| 17 | * and the like which are automatically free'd when going into the next select() |
| 18 | * system call */ |
| 19 | void *select; |
| 20 | }; |
| 21 | |
| 22 | extern __thread struct osmo_talloc_contexts *osmo_ctx; |
| 23 | |
| 24 | /* short-hand #defines for the osmo talloc contexts (OTC) that can be used to pass |
| 25 | * to the various _c functions like msgb_alloc_c() */ |
| 26 | #define OTC_GLOBAL (osmo_ctx->global) |
| 27 | #define OTC_SELECT (osmo_ctx->select) |
Daniel Willmann | 0a2b257 | 2020-12-28 22:25:45 +0100 | [diff] [blame] | 28 | |
| 29 | int osmo_ctx_init(const char *id); |