]> mj.ucw.cz Git - libucw.git/blob - ucw/resource.c
strtonum: Added small bits of documentation
[libucw.git] / ucw / resource.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/resource.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   ASSERT(rp);
103
104   uns size = (rc->res_size ? : sizeof(struct resource));
105   struct resource *r = (rp->mpool ? mp_alloc_fast(rp->mpool, size) : xmalloc(size));
106   r->rpool = rp;
107   clist_add_tail(&rp->resources, &r->n);
108   r->flags = rp->default_res_flags;
109   return r;
110 }
111
112 void
113 res_drop(struct resource *r)
114 {
115   clist_remove(&r->n);
116   if (!r->rpool->mpool)
117     xfree(r);
118 }
119
120 void
121 res_detach(struct resource *r)
122 {
123   if (!r)
124     return;
125   if (r->rclass->detach)
126     r->rclass->detach(r);
127   res_drop(r);
128 }
129
130 void
131 res_free(struct resource *r)
132 {
133   if (!r)
134     return;
135   if (r->rclass->free)
136     r->rclass->free(r);
137   res_drop(r);
138 }
139
140 void
141 res_dump(struct resource *r, uns indent)
142 {
143   printf("%*s%p %s %s", indent, "", r, ((r->flags & RES_FLAG_TEMP) ? "TEMP" : "PERM"), r->rclass->name);
144   if (r->rclass->dump)
145     r->rclass->dump(r, indent+4);
146   else
147     putchar('\n');
148 }
149
150 #ifdef TEST
151
152 #include <ucw/fastbuf.h>
153
154 int main(void)
155 {
156   // struct mempool *mp = mp_new(4096);
157   struct respool *rp = rp_new("test", NULL);
158   rp_switch(rp);
159   struct fastbuf *f = bfdopen_shared(1, 0);
160   rp_dump(rp, 0);
161   bputsn(f, "Hello, all worlds!");
162   bclose(f);
163   rp_delete(rp);
164   return 0;
165 }
166
167 #endif