]> mj.ucw.cz Git - libucw.git/blob - ucw/res-subpool.c
Table: renamed table_col_order[_by_name] -> table_set_col_order[_by_name]
[libucw.git] / ucw / res-subpool.c
1 /*
2  *      The UCW Library -- Resources for Sub-pools
3  *
4  *      (c) 2011 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/resource.h>
12
13 #include <stdio.h>
14
15 static void
16 subpool_res_free(struct resource *r)
17 {
18   struct respool *rp = r->priv;
19   rp->subpool_of = NULL;
20   rp_delete(rp);
21 }
22
23 static void
24 subpool_res_detach(struct resource *r)
25 {
26   struct respool *rp = r->priv;
27   rp->subpool_of = NULL;
28 }
29
30 static void
31 subpool_res_dump(struct resource *r, uint indent)
32 {
33   printf(":\n");
34   rp_dump(r->priv, indent);
35 }
36
37 static const struct res_class subpool_res_class = {
38   .name = "subpool",
39   .dump = subpool_res_dump,
40   .detach = subpool_res_detach,
41   .free = subpool_res_free,
42 };
43
44 struct resource *
45 res_subpool(struct respool *rp)
46 {
47   ASSERT(!rp->subpool_of);
48   struct resource *r = res_new(&subpool_res_class, rp);
49   ASSERT(r->rpool != rp);               // Avoid simple loops
50   rp->subpool_of = r;
51   return r;
52 }
53
54 #ifdef TEST
55
56 int main(void)
57 {
58   struct respool *rp = rp_new("interior", NULL);
59   struct respool *rp2 = rp_new("exterior", NULL);
60   rp_switch(rp);
61   res_malloc(10, NULL);
62   rp_switch(rp2);
63   res_malloc(7, NULL);
64   res_subpool(rp);
65   rp_dump(rp2, 0);
66   // rp_delete(rp);
67   // rp_dump(rp2, 0);
68   rp_delete(rp2);
69   return 0;
70 }
71
72 #endif