]> mj.ucw.cz Git - libucw.git/blob - ucw/conf-context.c
8a7b4a6b113a772e552fcf2225b9f6bbec5147a1
[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 static struct cf_context cf_default_context;
16
17 static void
18 cf_init_context(struct cf_context *cc)
19 {
20   cc->need_journal = 1;
21   clist_init(&cc->conf_entries);
22 }
23
24 struct cf_context *
25 cf_new_context(void)
26 {
27   struct cf_context *cc = xmalloc_zero(sizeof(*cc));
28   cf_init_context(cc);
29   return cc;
30 }
31
32 void
33 cf_free_context(struct cf_context *cc)
34 {
35   ASSERT(!cc->is_active);
36   ASSERT(cc != &cf_default_context);
37   xfree(cc->parser);
38   xfree(cc);
39 }
40
41 struct cf_context *
42 cf_switch_context(struct cf_context *cc)
43 {
44   struct ucwlib_context *uc = ucwlib_thread_context();
45   struct cf_context *prev = uc->cf_context;
46   if (prev)
47     prev->is_active = 0;
48   if (cc)
49     {
50       ASSERT(!cc->is_active);
51       cc->is_active = 1;
52     }
53   uc->cf_context = cc;
54   return prev;
55 }
56
57 static void CONSTRUCTOR_WITH_PRIORITY(10100)
58 cf_init_default_context(void)
59 {
60   cf_init_context(&cf_default_context);
61   ucwlib_thread_context()->cf_context = &cf_default_context;
62   cf_default_context.is_active = 1;
63 }
64
65 struct cf_context *
66 cf_obtain_context(void)
67 {
68   return cf_get_context();
69 }