]> mj.ucw.cz Git - libucw.git/blob - ucw/respool.c
Resources: Allow res_free(NULL) and res_detach(NULL)
[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)
125     return;
126   if (r->rclass->detach)
127     r->rclass->detach(r);
128   res_drop(r);
129 }
130
131 void
132 res_free(struct resource *r)
133 {
134   if (!r)
135     return;
136   if (r->rclass->free)
137     r->rclass->free(r);
138   res_drop(r);
139 }
140
141 void
142 res_dump(struct resource *r, uns indent)
143 {
144   printf("%*s%p %s %s", indent, "", r, ((r->flags & RES_FLAG_TEMP) ? "TEMP" : "PERM"), r->rclass->name);
145   if (r->rclass->dump)
146     r->rclass->dump(r, indent+4);
147   else
148     putchar('\n');
149 }
150
151 #ifdef TEST
152
153 #include "ucw/fastbuf.h"
154
155 int main(void)
156 {
157   // struct mempool *mp = mp_new(4096);
158   struct respool *rp = rp_new("test", NULL);
159   rp_switch(rp);
160   struct fastbuf *f = bfdopen_shared(1, 0);
161   rp_dump(rp, 0);
162   bputsn(f, "Hello, all worlds!");
163   bclose(f);
164   rp_delete(rp);
165   return 0;
166 }
167
168 #endif