]> mj.ucw.cz Git - libucw.git/blob - ucw/conf-context.c
1584143cb98a24b959dda540935105d2ea084620
[libucw.git] / ucw / conf-context.c
1 /*
2  *      UCW Library -- Configuration files: Contexts
3  *
4  *      (c) 2012 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include <ucw/lib.h>
11 #include <ucw/conf.h>
12 #include <ucw/conf-internal.h>
13 #include <ucw/threads.h>
14
15 #ifndef CONFIG_UCW_DEFAULT_CONFIG
16 #define CONFIG_UCW_DEFAULT_CONFIG NULL
17 #endif
18
19 #ifndef CONFIG_UCW_ENV_VAR_CONFIG
20 #define CONFIG_UCW_ENV_VAR_CONFIG NULL
21 #endif
22
23 struct cf_context *
24 cf_new_context(void)
25 {
26   struct cf_context *cc = xmalloc_zero(sizeof(*cc));
27   cc->need_journal = 1;
28   clist_init(&cc->conf_entries);
29   return cc;
30 }
31
32 void
33 cf_free_context(struct cf_context *cc)
34 {
35   ASSERT(!cc->is_active);
36   xfree(cc->parser);
37   xfree(cc);
38 }
39
40 struct cf_context *
41 cf_switch_context(struct cf_context *cc)
42 {
43   struct ucwlib_context *uc = ucwlib_thread_context();
44   struct cf_context *prev = uc->cf_context;
45   if (prev)
46     prev->is_active = 0;
47   if (cc)
48     {
49       ASSERT(!cc->is_active);
50       cc->is_active = 1;
51     }
52   uc->cf_context = cc;
53   return prev;
54 }
55
56 struct cf_context *
57 cf_obtain_context(void)
58 {
59   struct ucwlib_context *uc = ucwlib_thread_context();
60   if (unlikely(!uc->cf_context))
61     {
62       struct cf_context *cc = cf_new_context();
63       uc->cf_context = cc;
64       cc->is_active = 1;
65     }
66   return uc->cf_context;
67 }