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_delete(struct respool *rp)
37 while (r = clist_tail(&rp->resources))
39 ASSERT(r->rpool == rp);
44 if (rp_current() == rp)
49 rp_dump(struct respool *rp)
51 printf("Resource pool %s at %p (%s):\n", (rp->name ? : "(noname)"), rp, (rp->mpool ? "mempool-based" : "freestanding"));
52 CLIST_FOR_EACH(struct resource *, r, rp->resources)
57 res_alloc(const struct res_class *rc)
59 struct respool *rp = rp_current();
63 uns size = (rc->res_size ? : sizeof(struct resource));
64 struct resource *r = (rp->mpool ? mp_alloc_fast(rp->mpool, size) : xmalloc(size));
66 clist_add_tail(&rp->resources, &r->n);
71 res_drop(struct resource *r)
79 res_detach(struct resource *r)
81 if (r->rclass->detach)
87 res_free(struct resource *r)
95 res_dump(struct resource *r)
97 printf("\t%p %s", r, r->rclass->name);
105 #include "ucw/fastbuf.h"
109 // struct mempool *mp = mp_new(4096);
110 struct respool *rp = rp_new("test", NULL);
112 struct fastbuf *f = bfdopen_shared(1, 0);
114 bputsn(f, "Hello, all worlds!");