]> mj.ucw.cz Git - libucw.git/blob - ucw/respool.c
Implemented temporary resources
[libucw.git] / ucw / respool.c
1 /*
2  *      The UCW Library -- Resource Pools
3  *
4  *      (c) 2008--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/respool.h"
12 #include "ucw/mempool.h"
13
14 #include <stdio.h>
15
16 struct respool *
17 rp_new(const char *name, struct mempool *mp)
18 {
19   struct respool *rp;
20
21   if (mp)
22     {
23       rp = mp_alloc_zero(mp, sizeof(*rp));
24       rp->mpool = mp;
25     }
26   else
27     rp = xmalloc_zero(sizeof(*rp));
28   clist_init(&rp->resources);
29   rp->name = name;
30   rp->default_res_flags = RES_FLAG_TEMP;
31   return rp;
32 }
33
34 static void
35 rp_free(struct respool *rp)
36 {
37   if (rp->subpool_of)
38     res_detach(rp->subpool_of);
39   if (!rp->mpool)
40     xfree(rp);
41   if (rp_current() == rp)
42     rp_switch(NULL);
43 }
44
45 void
46 rp_delete(struct respool *rp)
47 {
48   struct resource *r;
49   while (r = clist_tail(&rp->resources))
50     {
51       ASSERT(r->rpool == rp);
52       res_free(r);
53     }
54   rp_free(rp);
55 }
56
57 void
58 rp_detach(struct respool *rp)
59 {
60   struct resource *r;
61   while (r = clist_head(&rp->resources))
62     {
63       ASSERT(r->rpool == rp);
64       res_detach(r);
65     }
66   rp_free(rp);
67 }
68
69 void
70 rp_commit(struct respool *rp)
71 {
72   struct resource *r;
73   while (r = clist_head(&rp->resources))
74     {
75       ASSERT(r->rpool == rp);
76       if (r->flags & RES_FLAG_TEMP)
77         res_free(r);
78       else
79         res_detach(r);
80     }
81   rp_free(rp);
82 }
83
84 void
85 rp_dump(struct respool *rp, uns indent)
86 {
87   printf("%*sResource pool %s at %p (%s)%s:\n",
88          indent, "",
89          (rp->name ? : "(noname)"),
90          rp,
91          (rp->mpool ? "mempool-based" : "freestanding"),
92          (rp->subpool_of ? " (subpool)" : "")
93          );
94   CLIST_FOR_EACH(struct resource *, r, rp->resources)
95     res_dump(r, indent+4);
96 }
97
98 struct resource *
99 res_alloc(const struct res_class *rc)
100 {
101   struct respool *rp = rp_current();
102   if (!rp)
103     return NULL;
104
105   uns size = (rc->res_size ? : sizeof(struct resource));
106   struct resource *r = (rp->mpool ? mp_alloc_fast(rp->mpool, size) : xmalloc(size));
107   r->rpool = rp;
108   clist_add_tail(&rp->resources, &r->n);
109   r->flags = rp->default_res_flags;
110   return r;
111 }
112
113 void
114 res_drop(struct resource *r)
115 {
116   clist_remove(&r->n);
117   if (!r->rpool->mpool)
118     xfree(r);
119 }
120
121 void
122 res_detach(struct resource *r)
123 {
124   if (r->rclass->detach)
125     r->rclass->detach(r);
126   res_drop(r);
127 }
128
129 void
130 res_free(struct resource *r)
131 {
132   if (r->rclass->free)
133     r->rclass->free(r);
134   res_drop(r);
135 }
136
137 void
138 res_dump(struct resource *r, uns indent)
139 {
140   printf("%*s%p %s %s", indent, "", r, ((r->flags & RES_FLAG_TEMP) ? "TEMP" : "PERM"), r->rclass->name);
141   if (r->rclass->dump)
142     r->rclass->dump(r, indent+4);
143   else
144     putchar('\n');
145 }
146
147 #ifdef TEST
148
149 #include "ucw/fastbuf.h"
150
151 int main(void)
152 {
153   // struct mempool *mp = mp_new(4096);
154   struct respool *rp = rp_new("test", NULL);
155   rp_switch(rp);
156   struct fastbuf *f = bfdopen_shared(1, 0);
157   rp_dump(rp, 0);
158   bputsn(f, "Hello, all worlds!");
159   bclose(f);
160   rp_delete(rp);
161   return 0;
162 }
163
164 #endif