]> mj.ucw.cz Git - libucw.git/blob - ucw/respool.h
Implemented exceptions.
[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 /*
11  * FIXME:
12  *      - check other candidates for resourcification
13  *      - respool as a resource in another respool?
14  *      - unit tests
15  *      - automatic freeing of trans pool on thread exit
16  */
17
18 #ifndef _UCW_RESPOOL_H
19 #define _UCW_RESPOOL_H
20
21 #include "ucw/clists.h"
22 #include "ucw/threads.h"
23
24 struct respool {
25   clist resources;
26   const char *name;
27   struct mempool *mpool;                                // If set, resources are allocated from the mempool, otherwise by xmalloc()
28 };
29
30 struct resource {
31   cnode n;
32   struct respool *rpool;
33   const struct res_class *rclass;
34   void *priv;                                           // Private to the class
35   // More data specific for the particular class can follow
36 };
37
38 struct res_class {
39   const char *name;
40   void (*detach)(struct resource *r);
41   void (*free)(struct resource *r);
42   void (*dump)(struct resource *r);
43   uns res_size;                                         // Size of the resource structure (0=default)
44 };
45
46 struct respool *rp_new(const char *name, struct mempool *mp);
47 void rp_delete(struct respool *rp);
48 void rp_detach(struct respool *rp);
49 void rp_dump(struct respool *rp);
50
51 static inline struct respool *
52 rp_current(void)
53 {
54   return ucwlib_thread_context()->current_respool;      // May be NULL
55 }
56
57 static inline struct respool *
58 rp_switch(struct respool *rp)
59 {
60   struct ucwlib_context *ctx = ucwlib_thread_context();
61   struct respool *orp = ctx->current_respool;
62   ctx->current_respool = rp;
63   return orp;
64 }
65
66 struct resource *res_alloc(const struct res_class *rc); // Returns NULL if there is no pool active
67 void res_drop(struct resource *r);
68 void res_detach(struct resource *r);
69 void res_free(struct resource *r);
70 void res_dump(struct resource *r);
71
72 static inline struct resource *                         // Returns NULL if there is no pool active
73 res_new(const struct res_class *rc, void *priv)
74 {
75   struct resource *r = res_alloc(rc);
76   if (r)
77     {
78       r->rclass = rc;
79       r->priv = priv;
80     }
81   return r;
82 }
83
84 /* Various special types of resources */
85
86 struct resource *res_for_fd(int fd);                    // Creates a resource that closes a given file descriptor
87
88 void *res_malloc(size_t size, struct resource **ptr);   // Allocates memory and creates a resource for it
89 void *res_malloc_zero(size_t size, struct resource **ptr);      // Allocates zero-initialized memory and creates a resource for it
90 void *res_realloc(struct resource *res, size_t size);
91
92 #endif