X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=ucw%2Frespool.h;h=9764f12948bb0c5a7dada6ddbe855f7e0e1efcbc;hb=903e6530eaa344851bf0aa6a9f270c205aa97300;hp=e06762e322cce04e88d2771c97689b89b0dfef47;hpb=847bb5125836ac3c89a9118be60cb077090f4756;p=libucw.git diff --git a/ucw/respool.h b/ucw/respool.h index e06762e3..9764f129 100644 --- a/ucw/respool.h +++ b/ucw/respool.h @@ -1,12 +1,20 @@ /* * The UCW Library -- Resource Pools * - * (c) 2008 Martin Mares + * (c) 2008--2011 Martin Mares * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. */ +/* + * FIXME: + * - check other candidates for resourcification + * - respool as a resource in another respool? + * - unit tests + * - automatic freeing of trans pool on thread exit + */ + #ifndef _UCW_RESPOOL_H #define _UCW_RESPOOL_H @@ -22,8 +30,9 @@ struct respool { struct resource { cnode n; struct respool *rpool; - struct res_class *rclass; + const struct res_class *rclass; void *priv; // Private to the class + // More data specific for the particular class can follow }; struct res_class { @@ -31,10 +40,12 @@ struct res_class { void (*detach)(struct resource *r); void (*free)(struct resource *r); void (*dump)(struct resource *r); + uns res_size; // Size of the resource structure (0=default) }; struct respool *rp_new(const char *name, struct mempool *mp); void rp_delete(struct respool *rp); +void rp_detach(struct respool *rp); void rp_dump(struct respool *rp); static inline struct respool * @@ -52,15 +63,16 @@ rp_switch(struct respool *rp) return orp; } -struct resource *res_alloc(void); // Returns NULL if there is no pool active +struct resource *res_alloc(const struct res_class *rc) LIKE_MALLOC; // Returns NULL if there is no pool active +void res_drop(struct resource *r); void res_detach(struct resource *r); void res_free(struct resource *r); void res_dump(struct resource *r); static inline struct resource * // Returns NULL if there is no pool active -res_new(struct res_class *rc, void *priv) +res_new(const struct res_class *rc, void *priv) { - struct resource *r = res_alloc(); + struct resource *r = res_alloc(rc); if (r) { r->rclass = rc; @@ -69,4 +81,12 @@ res_new(struct res_class *rc, void *priv) return r; } +/* Various special types of resources */ + +struct resource *res_for_fd(int fd); // Creates a resource that closes a given file descriptor + +void *res_malloc(size_t size, struct resource **ptr) LIKE_MALLOC; // Allocates memory and creates a resource for it +void *res_malloc_zero(size_t size, struct resource **ptr) LIKE_MALLOC; // Allocates zero-initialized memory and creates a resource for it +void *res_realloc(struct resource *res, size_t size); + #endif