Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/lib/mempool.h @ 6428:7cad076906eb HEAD
pool_unref() now takes ** pointer.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Sun, 16 Sep 2007 14:04:06 +0300 |
parents | 6a64e64fa3a3 |
children |
rev | line source |
---|---|
6410
e4eb71ae8e96
Changed .h ifdef/defines to use <NAME>_H format.
Timo Sirainen <tss@iki.fi>
parents:
5356
diff
changeset
|
1 #ifndef MEMPOOL_H |
e4eb71ae8e96
Changed .h ifdef/defines to use <NAME>_H format.
Timo Sirainen <tss@iki.fi>
parents:
5356
diff
changeset
|
2 #define MEMPOOL_H |
0 | 3 |
4 #include "macros.h" | |
5 | |
4980
8e88ecc64563
debug: MEMPOOL_GROWING prefix in alloconly pool names means that when
Timo Sirainen <tss@iki.fi>
parents:
4781
diff
changeset
|
6 /* When DEBUG is enabled, Dovecot warns whenever a memory pool is grown. |
8e88ecc64563
debug: MEMPOOL_GROWING prefix in alloconly pool names means that when
Timo Sirainen <tss@iki.fi>
parents:
4781
diff
changeset
|
7 This is done so that the initial pool size could be set large enough so that |
8e88ecc64563
debug: MEMPOOL_GROWING prefix in alloconly pool names means that when
Timo Sirainen <tss@iki.fi>
parents:
4781
diff
changeset
|
8 it wouldn't grow in normal use. For some memory pools it's too difficult |
8e88ecc64563
debug: MEMPOOL_GROWING prefix in alloconly pool names means that when
Timo Sirainen <tss@iki.fi>
parents:
4781
diff
changeset
|
9 to calculate a good initial size, so this prefix should be used with those |
8e88ecc64563
debug: MEMPOOL_GROWING prefix in alloconly pool names means that when
Timo Sirainen <tss@iki.fi>
parents:
4781
diff
changeset
|
10 pools to disable the warning. */ |
8e88ecc64563
debug: MEMPOOL_GROWING prefix in alloconly pool names means that when
Timo Sirainen <tss@iki.fi>
parents:
4781
diff
changeset
|
11 #define MEMPOOL_GROWING "GROWING-" |
8e88ecc64563
debug: MEMPOOL_GROWING prefix in alloconly pool names means that when
Timo Sirainen <tss@iki.fi>
parents:
4781
diff
changeset
|
12 |
0 | 13 /* Memory allocated and reallocated (the new data in it) in pools is always |
14 zeroed, it will cost only a few CPU cycles and may well save some debug | |
15 time. */ | |
16 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
17 typedef struct pool *pool_t; |
0 | 18 |
5026
d7e064f77f79
Don't keep the memory pool virtual functions in the pool structure itself,
Timo Sirainen <tss@iki.fi>
parents:
4980
diff
changeset
|
19 struct pool_vfuncs { |
1489
826bed85c807
Added pool_get_name(), for debugging mostly.
Timo Sirainen <tss@iki.fi>
parents:
944
diff
changeset
|
20 const char *(*get_name)(pool_t pool); |
826bed85c807
Added pool_get_name(), for debugging mostly.
Timo Sirainen <tss@iki.fi>
parents:
944
diff
changeset
|
21 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
22 void (*ref)(pool_t pool); |
3878
bf0a03691989
pool_unref(): set the pool pointer to NULL, so if we're trying to unref it
Timo Sirainen <tss@iki.fi>
parents:
3234
diff
changeset
|
23 void (*unref)(pool_t *pool); |
0 | 24 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
25 void *(*malloc)(pool_t pool, size_t size); |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
26 void (*free)(pool_t pool, void *mem); |
0 | 27 |
941
4d6b69558add
Added old_size parameter to p_realloc() - we rarely need it and this way
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
28 /* memory in old_size..new_size will be zeroed */ |
4d6b69558add
Added old_size parameter to p_realloc() - we rarely need it and this way
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
29 void *(*realloc)(pool_t pool, void *mem, |
4720
b0daeec3d416
Use malloc attribute for the most commonly used memory and string allocation
Timo Sirainen <tss@iki.fi>
parents:
4633
diff
changeset
|
30 size_t old_size, size_t new_size) |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6410
diff
changeset
|
31 ATTR_WARN_UNUSED_RESULT; |
0 | 32 |
33 /* Frees all the memory in pool. NOTE: system_pool doesn't support | |
34 this and crashes if it's used */ | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
35 void (*clear)(pool_t pool); |
944
4f38538aa4a1
Added alloconly_pool field for checking if pool supports free()
Timo Sirainen <tss@iki.fi>
parents:
941
diff
changeset
|
36 |
3233
6396b4c0a721
Added p_get_max_easy_alloc_size().
Timo Sirainen <tss@iki.fi>
parents:
1905
diff
changeset
|
37 /* Returns the maximum amount of bytes that can be allocated with |
6396b4c0a721
Added p_get_max_easy_alloc_size().
Timo Sirainen <tss@iki.fi>
parents:
1905
diff
changeset
|
38 minimal trouble. If there's no such concept, always returns 0. */ |
6396b4c0a721
Added p_get_max_easy_alloc_size().
Timo Sirainen <tss@iki.fi>
parents:
1905
diff
changeset
|
39 size_t (*get_max_easy_alloc_size)(pool_t pool); |
5026
d7e064f77f79
Don't keep the memory pool virtual functions in the pool structure itself,
Timo Sirainen <tss@iki.fi>
parents:
4980
diff
changeset
|
40 }; |
d7e064f77f79
Don't keep the memory pool virtual functions in the pool structure itself,
Timo Sirainen <tss@iki.fi>
parents:
4980
diff
changeset
|
41 |
d7e064f77f79
Don't keep the memory pool virtual functions in the pool structure itself,
Timo Sirainen <tss@iki.fi>
parents:
4980
diff
changeset
|
42 struct pool { |
d7e064f77f79
Don't keep the memory pool virtual functions in the pool structure itself,
Timo Sirainen <tss@iki.fi>
parents:
4980
diff
changeset
|
43 const struct pool_vfuncs *v; |
3233
6396b4c0a721
Added p_get_max_easy_alloc_size().
Timo Sirainen <tss@iki.fi>
parents:
1905
diff
changeset
|
44 |
944
4f38538aa4a1
Added alloconly_pool field for checking if pool supports free()
Timo Sirainen <tss@iki.fi>
parents:
941
diff
changeset
|
45 unsigned int alloconly_pool:1; |
1782
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
1489
diff
changeset
|
46 unsigned int datastack_pool:1; |
0 | 47 }; |
48 | |
49 /* system_pool uses calloc() + realloc() + free() */ | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
50 extern pool_t system_pool; |
5356
48fe4fe9ef64
Added system_clean_pool. default_pool is now set statically, so it can be
Timo Sirainen <tss@iki.fi>
parents:
5026
diff
changeset
|
51 extern struct pool static_system_pool; |
48fe4fe9ef64
Added system_clean_pool. default_pool is now set statically, so it can be
Timo Sirainen <tss@iki.fi>
parents:
5026
diff
changeset
|
52 /* Similar to pool_alloconly_create_clean(), but uses system_pool to |
48fe4fe9ef64
Added system_clean_pool. default_pool is now set statically, so it can be
Timo Sirainen <tss@iki.fi>
parents:
5026
diff
changeset
|
53 allocate the memory. */ |
48fe4fe9ef64
Added system_clean_pool. default_pool is now set statically, so it can be
Timo Sirainen <tss@iki.fi>
parents:
5026
diff
changeset
|
54 extern pool_t system_clean_pool; |
0 | 55 |
1782
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
1489
diff
changeset
|
56 /* memory allocated from data_stack is valid only until next t_pop() call. |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
1489
diff
changeset
|
57 No checks are performed. */ |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
1489
diff
changeset
|
58 extern pool_t unsafe_data_stack_pool; |
402 | 59 |
825
8afbafd5deac
We don't have separate read-write pools, so renamed pool_create(.., FALSE)
Timo Sirainen <tss@iki.fi>
parents:
743
diff
changeset
|
60 /* Create a new alloc-only pool. Note that `size' specifies the initial |
8afbafd5deac
We don't have separate read-write pools, so renamed pool_create(.., FALSE)
Timo Sirainen <tss@iki.fi>
parents:
743
diff
changeset
|
61 malloc()ed block size, part of it is used internally. */ |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
62 pool_t pool_alloconly_create(const char *name, size_t size); |
4781
e55f5f2dbb2c
Updated pool_alloconly_create_clean() comment
Timo Sirainen <tss@iki.fi>
parents:
4780
diff
changeset
|
63 /* Like alloconly pool, but clear the memory before freeing it. The idea is |
e55f5f2dbb2c
Updated pool_alloconly_create_clean() comment
Timo Sirainen <tss@iki.fi>
parents:
4780
diff
changeset
|
64 that you could allocate memory for storing sensitive information from this |
e55f5f2dbb2c
Updated pool_alloconly_create_clean() comment
Timo Sirainen <tss@iki.fi>
parents:
4780
diff
changeset
|
65 pool, and be sure that it gets cleared from the memory when it's no longer |
e55f5f2dbb2c
Updated pool_alloconly_create_clean() comment
Timo Sirainen <tss@iki.fi>
parents:
4780
diff
changeset
|
66 needed. */ |
4780
11001523027f
Added pool_alloconly_create_clean() to create an alloconly pool which clears
Timo Sirainen <tss@iki.fi>
parents:
4720
diff
changeset
|
67 pool_t pool_alloconly_create_clean(const char *name, size_t size); |
0 | 68 |
1782
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
1489
diff
changeset
|
69 /* When allocating memory from returned pool, the data stack frame must be |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
1489
diff
changeset
|
70 the same as it was when calling this function. pool_unref() also checks |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
1489
diff
changeset
|
71 that the stack frame is the same. This should make it quite safe to use. */ |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
1489
diff
changeset
|
72 pool_t pool_datastack_create(void); |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
1489
diff
changeset
|
73 |
3234
06f9da4ff7a5
Added pool_get_exp_grown_size(). Use it for buffers, istreams and ostreams
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
74 /* Similar to nearest_power(), but try not to exceed buffer's easy |
06f9da4ff7a5
Added pool_get_exp_grown_size(). Use it for buffers, istreams and ostreams
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
75 allocation size. If you don't have any explicit minimum size, use |
06f9da4ff7a5
Added pool_get_exp_grown_size(). Use it for buffers, istreams and ostreams
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
76 old_size + 1. */ |
06f9da4ff7a5
Added pool_get_exp_grown_size(). Use it for buffers, istreams and ostreams
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
77 size_t pool_get_exp_grown_size(pool_t pool, size_t old_size, size_t min_size); |
06f9da4ff7a5
Added pool_get_exp_grown_size(). Use it for buffers, istreams and ostreams
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
78 |
0 | 79 /* Pools should be used through these macros: */ |
5026
d7e064f77f79
Don't keep the memory pool virtual functions in the pool structure itself,
Timo Sirainen <tss@iki.fi>
parents:
4980
diff
changeset
|
80 #define pool_get_name(pool) (pool)->v->get_name(pool) |
d7e064f77f79
Don't keep the memory pool virtual functions in the pool structure itself,
Timo Sirainen <tss@iki.fi>
parents:
4980
diff
changeset
|
81 #define pool_ref(pool) (pool)->v->ref(pool) |
6428
7cad076906eb
pool_unref() now takes ** pointer.
Timo Sirainen <tss@iki.fi>
parents:
6411
diff
changeset
|
82 #define pool_unref(pool) ((*pool))->v->unref(pool) |
1784
0e72d6ab85ad
Make i_free(), p_free() and pool_unref() calls also set the given parameter
Timo Sirainen <tss@iki.fi>
parents:
1783
diff
changeset
|
83 |
0e72d6ab85ad
Make i_free(), p_free() and pool_unref() calls also set the given parameter
Timo Sirainen <tss@iki.fi>
parents:
1783
diff
changeset
|
84 #define p_new(pool, type, count) \ |
0e72d6ab85ad
Make i_free(), p_free() and pool_unref() calls also set the given parameter
Timo Sirainen <tss@iki.fi>
parents:
1783
diff
changeset
|
85 ((type *) p_malloc(pool, sizeof(type) * (count))) |
0 | 86 |
5026
d7e064f77f79
Don't keep the memory pool virtual functions in the pool structure itself,
Timo Sirainen <tss@iki.fi>
parents:
4980
diff
changeset
|
87 #define p_malloc(pool, size) (pool)->v->malloc(pool, size) |
941
4d6b69558add
Added old_size parameter to p_realloc() - we rarely need it and this way
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
88 #define p_realloc(pool, mem, old_size, new_size) \ |
5026
d7e064f77f79
Don't keep the memory pool virtual functions in the pool structure itself,
Timo Sirainen <tss@iki.fi>
parents:
4980
diff
changeset
|
89 (pool)->v->realloc(pool, mem, old_size, new_size) |
4633
9de853d23279
Added p_free_and_null() and did some cleanups.
Timo Sirainen <tss@iki.fi>
parents:
3878
diff
changeset
|
90 |
9de853d23279
Added p_free_and_null() and did some cleanups.
Timo Sirainen <tss@iki.fi>
parents:
3878
diff
changeset
|
91 /* Free the memory. Currently it also sets memory to NULL, but that shouldn't |
9de853d23279
Added p_free_and_null() and did some cleanups.
Timo Sirainen <tss@iki.fi>
parents:
3878
diff
changeset
|
92 be relied on as it's only an extra safety check. It might as well be later |
9de853d23279
Added p_free_and_null() and did some cleanups.
Timo Sirainen <tss@iki.fi>
parents:
3878
diff
changeset
|
93 changed to some invalid pointer causing a segfault, or removed completely |
9de853d23279
Added p_free_and_null() and did some cleanups.
Timo Sirainen <tss@iki.fi>
parents:
3878
diff
changeset
|
94 in some "optimization".. */ |
1784
0e72d6ab85ad
Make i_free(), p_free() and pool_unref() calls also set the given parameter
Timo Sirainen <tss@iki.fi>
parents:
1783
diff
changeset
|
95 #define p_free(pool, mem) \ |
0e72d6ab85ad
Make i_free(), p_free() and pool_unref() calls also set the given parameter
Timo Sirainen <tss@iki.fi>
parents:
1783
diff
changeset
|
96 STMT_START { \ |
5026
d7e064f77f79
Don't keep the memory pool virtual functions in the pool structure itself,
Timo Sirainen <tss@iki.fi>
parents:
4980
diff
changeset
|
97 (pool)->v->free(pool, mem); \ |
1784
0e72d6ab85ad
Make i_free(), p_free() and pool_unref() calls also set the given parameter
Timo Sirainen <tss@iki.fi>
parents:
1783
diff
changeset
|
98 (mem) = NULL; \ |
0e72d6ab85ad
Make i_free(), p_free() and pool_unref() calls also set the given parameter
Timo Sirainen <tss@iki.fi>
parents:
1783
diff
changeset
|
99 } STMT_END |
0 | 100 |
4633
9de853d23279
Added p_free_and_null() and did some cleanups.
Timo Sirainen <tss@iki.fi>
parents:
3878
diff
changeset
|
101 /* A macro that's guaranteed to set mem = NULL. */ |
9de853d23279
Added p_free_and_null() and did some cleanups.
Timo Sirainen <tss@iki.fi>
parents:
3878
diff
changeset
|
102 #define p_free_and_null(pool, mem) p_free(pool, mem) |
9de853d23279
Added p_free_and_null() and did some cleanups.
Timo Sirainen <tss@iki.fi>
parents:
3878
diff
changeset
|
103 |
5026
d7e064f77f79
Don't keep the memory pool virtual functions in the pool structure itself,
Timo Sirainen <tss@iki.fi>
parents:
4980
diff
changeset
|
104 #define p_clear(pool) (pool)->v->clear(pool) |
0 | 105 |
5026
d7e064f77f79
Don't keep the memory pool virtual functions in the pool structure itself,
Timo Sirainen <tss@iki.fi>
parents:
4980
diff
changeset
|
106 #define p_get_max_easy_alloc_size(pool) \ |
d7e064f77f79
Don't keep the memory pool virtual functions in the pool structure itself,
Timo Sirainen <tss@iki.fi>
parents:
4980
diff
changeset
|
107 (pool)->v->get_max_easy_alloc_size(pool) |
3233
6396b4c0a721
Added p_get_max_easy_alloc_size().
Timo Sirainen <tss@iki.fi>
parents:
1905
diff
changeset
|
108 |
0 | 109 #endif |