]> mj.ucw.cz Git - libucw.git/blob - ucw/conf-context.c
f4929644d5a74fb6da626c657d78724fc8a547d4
[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   // FIXME: Roll back all transactions
36   ASSERT(!cc->is_active);
37   ASSERT(cc != &cf_default_context);
38   xfree(cc->parser);
39   xfree(cc);
40 }
41
42 struct cf_context *
43 cf_switch_context(struct cf_context *cc)
44 {
45   struct ucwlib_context *uc = ucwlib_thread_context();
46   struct cf_context *prev = uc->cf_context;
47   if (prev)
48     prev->is_active = 0;
49   if (cc)
50     {
51       ASSERT(!cc->is_active);
52       cc->is_active = 1;
53     }
54   uc->cf_context = cc;
55   return prev;
56 }
57
58 static void CONSTRUCTOR_WITH_PRIORITY(10100)
59 cf_init_default_context(void)
60 {
61   cf_init_context(&cf_default_context);
62   ucwlib_thread_context()->cf_context = &cf_default_context;
63   cf_default_context.is_active = 1;
64 }
65
66 struct cf_context *
67 cf_obtain_context(void)
68 {
69   return cf_get_context();
70 }