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)
38 if (rp_current() == rp)
43 rp_delete(struct respool *rp)
46 while (r = clist_tail(&rp->resources))
48 ASSERT(r->rpool == rp);
55 rp_detach(struct respool *rp)
58 while (r = clist_head(&rp->resources))
60 ASSERT(r->rpool == rp);
67 rp_dump(struct respool *rp)
69 printf("Resource pool %s at %p (%s):\n", (rp->name ? : "(noname)"), rp, (rp->mpool ? "mempool-based" : "freestanding"));
70 CLIST_FOR_EACH(struct resource *, r, rp->resources)
75 res_alloc(const struct res_class *rc)
77 struct respool *rp = rp_current();
81 uns size = (rc->res_size ? : sizeof(struct resource));
82 struct resource *r = (rp->mpool ? mp_alloc_fast(rp->mpool, size) : xmalloc(size));
84 clist_add_tail(&rp->resources, &r->n);
89 res_drop(struct resource *r)
97 res_detach(struct resource *r)
99 if (r->rclass->detach)
100 r->rclass->detach(r);
105 res_free(struct resource *r)
113 res_dump(struct resource *r)
115 printf("\t%p %s", r, r->rclass->name);
123 #include "ucw/fastbuf.h"
127 // struct mempool *mp = mp_new(4096);
128 struct respool *rp = rp_new("test", NULL);
130 struct fastbuf *f = bfdopen_shared(1, 0);
132 bputsn(f, "Hello, all worlds!");