2 * The UCW Library -- Resource Pools
4 * (c) 2008 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include "ucw/respool.h"
12 #include "ucw/mempool.h"
17 rp_new(const char *name, struct mempool *mp)
23 rp = mp_alloc_zero(mp, sizeof(*rp));
27 rp = xmalloc_zero(sizeof(*rp));
28 clist_init(&rp->resources);
34 rp_free(struct respool *rp)
37 res_detach(rp->subpool_of);
40 if (rp_current() == rp)
45 rp_delete(struct respool *rp)
48 while (r = clist_tail(&rp->resources))
50 ASSERT(r->rpool == rp);
57 rp_detach(struct respool *rp)
60 while (r = clist_head(&rp->resources))
62 ASSERT(r->rpool == rp);
69 rp_dump(struct respool *rp, uns indent)
71 printf("%*sResource pool %s at %p (%s)%s:\n",
73 (rp->name ? : "(noname)"),
75 (rp->mpool ? "mempool-based" : "freestanding"),
76 (rp->subpool_of ? " (subpool)" : "")
78 CLIST_FOR_EACH(struct resource *, r, rp->resources)
79 res_dump(r, indent+4);
83 res_alloc(const struct res_class *rc)
85 struct respool *rp = rp_current();
89 uns size = (rc->res_size ? : sizeof(struct resource));
90 struct resource *r = (rp->mpool ? mp_alloc_fast(rp->mpool, size) : xmalloc(size));
92 clist_add_tail(&rp->resources, &r->n);
97 res_drop(struct resource *r)
100 if (!r->rpool->mpool)
105 res_detach(struct resource *r)
107 if (r->rclass->detach)
108 r->rclass->detach(r);
113 res_free(struct resource *r)
121 res_dump(struct resource *r, uns indent)
123 printf("%*s%p %s", indent, "", r, r->rclass->name);
125 r->rclass->dump(r, indent+4);
132 #include "ucw/fastbuf.h"
136 // struct mempool *mp = mp_new(4096);
137 struct respool *rp = rp_new("test", NULL);
139 struct fastbuf *f = bfdopen_shared(1, 0);
141 bputsn(f, "Hello, all worlds!");