]> mj.ucw.cz Git - libucw.git/blob - ucw/respool.h
e06762e322cce04e88d2771c97689b89b0dfef47
[libucw.git] / ucw / respool.h
1 /*
2  *      The UCW Library -- Resource Pools
3  *
4  *      (c) 2008 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 #ifndef _UCW_RESPOOL_H
11 #define _UCW_RESPOOL_H
12
13 #include "ucw/clists.h"
14 #include "ucw/threads.h"
15
16 struct respool {
17   clist resources;
18   const char *name;
19   struct mempool *mpool;                                // If set, resources are allocated from the mempool, otherwise by xmalloc()
20 };
21
22 struct resource {
23   cnode n;
24   struct respool *rpool;
25   struct res_class *rclass;
26   void *priv;                                           // Private to the class
27 };
28
29 struct res_class {
30   const char *name;
31   void (*detach)(struct resource *r);
32   void (*free)(struct resource *r);
33   void (*dump)(struct resource *r);
34 };
35
36 struct respool *rp_new(const char *name, struct mempool *mp);
37 void rp_delete(struct respool *rp);
38 void rp_dump(struct respool *rp);
39
40 static inline struct respool *
41 rp_current(void)
42 {
43   return ucwlib_thread_context()->current_respool;      // May be NULL
44 }
45
46 static inline struct respool *
47 rp_switch(struct respool *rp)
48 {
49   struct ucwlib_context *ctx = ucwlib_thread_context();
50   struct respool *orp = ctx->current_respool;
51   ctx->current_respool = rp;
52   return orp;
53 }
54
55 struct resource *res_alloc(void);                       // Returns NULL if there is no pool active
56 void res_detach(struct resource *r);
57 void res_free(struct resource *r);
58 void res_dump(struct resource *r);
59
60 static inline struct resource *                         // Returns NULL if there is no pool active
61 res_new(struct res_class *rc, void *priv)
62 {
63   struct resource *r = res_alloc();
64   if (r)
65     {
66       r->rclass = rc;
67       r->priv = priv;
68     }
69   return r;
70 }
71
72 #endif