Mercurial > dovecot > core-2.2
annotate src/lib/mempool-alloconly.c @ 3238:889a584bd953 HEAD
Changed alloconly pools to call malloc() only once when creating the pool.
Pool names aren't used anymore without DEBUG. Don't immediately double the
pool's initial size for the first allocated block.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Sun, 27 Mar 2005 19:38:38 +0300 |
parents | 6396b4c0a721 |
children | 4cbb7b67bc98 |
rev | line source |
---|---|
1741
9df02b1533b3
Removed most of the license comments from src/lib/*.c. It's just fine to
Timo Sirainen <tss@iki.fi>
parents:
1666
diff
changeset
|
1 /* Copyright (c) 2002-2003 Timo Sirainen */ |
0 | 2 |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
699
diff
changeset
|
3 /* @UNSAFE: whole file */ |
0 | 4 #include "lib.h" |
5 #include "mempool.h" | |
6 | |
7 #include <stdlib.h> | |
8 | |
1786
49b6103dd2e0
Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents:
1782
diff
changeset
|
9 #ifdef HAVE_GC_GC_H |
49b6103dd2e0
Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents:
1782
diff
changeset
|
10 # include <gc/gc.h> |
49b6103dd2e0
Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents:
1782
diff
changeset
|
11 #elif defined (HAVE_GC_H) |
49b6103dd2e0
Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents:
1782
diff
changeset
|
12 # include <gc.h> |
49b6103dd2e0
Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents:
1782
diff
changeset
|
13 #endif |
49b6103dd2e0
Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents:
1782
diff
changeset
|
14 |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
72
diff
changeset
|
15 #define MAX_ALLOC_SIZE SSIZE_T_MAX |
0 | 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 struct alloconly_pool { |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
18 struct pool pool; |
0 | 19 int refcount; |
20 | |
3238
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
21 size_t base_size; |
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 struct pool_block *block; |
3238
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
23 #ifdef DEBUG |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
24 const char *name; |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
25 #endif |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
26 }; |
0 | 27 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
28 struct pool_block { |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
29 struct pool_block *prev; |
0 | 30 |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
72
diff
changeset
|
31 size_t size; |
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
72
diff
changeset
|
32 size_t left; |
857
3d437b1e5257
Removed pool->realloc_min() which nothing used. A few small fixes to
Timo Sirainen <tss@iki.fi>
parents:
839
diff
changeset
|
33 size_t last_alloc_size; |
0 | 34 |
72 | 35 /* unsigned char data[]; */ |
0 | 36 }; |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
37 #define SIZEOF_POOLBLOCK (MEM_ALIGN(sizeof(struct pool_block))) |
72 | 38 |
39 #define POOL_BLOCK_DATA(block) \ | |
40 ((char *) (block) + SIZEOF_POOLBLOCK) | |
0 | 41 |
1489
826bed85c807
Added pool_get_name(), for debugging mostly.
Timo Sirainen <tss@iki.fi>
parents:
1350
diff
changeset
|
42 static const char *pool_alloconly_get_name(pool_t pool); |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
43 static void pool_alloconly_ref(pool_t pool); |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
44 static void pool_alloconly_unref(pool_t pool); |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
45 static void *pool_alloconly_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
|
46 static void pool_alloconly_free(pool_t pool, void *mem); |
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
|
47 static void *pool_alloconly_realloc(pool_t pool, void *mem, |
4d6b69558add
Added old_size parameter to p_realloc() - we rarely need it and this way
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
48 size_t old_size, size_t new_size); |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
49 static void pool_alloconly_clear(pool_t pool); |
3233
6396b4c0a721
Added p_get_max_easy_alloc_size().
Timo Sirainen <tss@iki.fi>
parents:
3198
diff
changeset
|
50 static size_t pool_alloconly_get_max_easy_alloc_size(pool_t pool); |
0 | 51 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
52 static void block_alloc(struct alloconly_pool *pool, size_t size); |
330 | 53 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
54 static struct pool static_alloconly_pool = { |
1489
826bed85c807
Added pool_get_name(), for debugging mostly.
Timo Sirainen <tss@iki.fi>
parents:
1350
diff
changeset
|
55 pool_alloconly_get_name, |
826bed85c807
Added pool_get_name(), for debugging mostly.
Timo Sirainen <tss@iki.fi>
parents:
1350
diff
changeset
|
56 |
330 | 57 pool_alloconly_ref, |
58 pool_alloconly_unref, | |
59 | |
60 pool_alloconly_malloc, | |
61 pool_alloconly_free, | |
62 | |
63 pool_alloconly_realloc, | |
64 | |
944
4f38538aa4a1
Added alloconly_pool field for checking if pool supports free()
Timo Sirainen <tss@iki.fi>
parents:
941
diff
changeset
|
65 pool_alloconly_clear, |
3233
6396b4c0a721
Added p_get_max_easy_alloc_size().
Timo Sirainen <tss@iki.fi>
parents:
3198
diff
changeset
|
66 pool_alloconly_get_max_easy_alloc_size, |
944
4f38538aa4a1
Added alloconly_pool field for checking if pool supports free()
Timo Sirainen <tss@iki.fi>
parents:
941
diff
changeset
|
67 |
1782
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
1741
diff
changeset
|
68 TRUE, |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
1741
diff
changeset
|
69 FALSE |
330 | 70 }; |
0 | 71 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
72 pool_t pool_alloconly_create(const char *name, size_t size) |
0 | 73 { |
3238
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
74 struct alloconly_pool apool, *new_apool; |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
75 size_t min_alloc = sizeof(struct alloconly_pool) + SIZEOF_POOLBLOCK; |
1786
49b6103dd2e0
Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents:
1782
diff
changeset
|
76 |
3238
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
77 #ifdef DEBUG |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
78 min_alloc += strlen(name) + 1; |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
79 #endif |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
80 |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
81 /* create a fake alloconly_pool so we can call block_alloc() */ |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
82 memset(&apool, 0, sizeof(apool)); |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
83 apool.pool = static_alloconly_pool; |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
84 apool.refcount = 1; |
0 | 85 |
3238
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
86 if (size < min_alloc) |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
87 size = nearest_power(size + min_alloc); |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
88 block_alloc(&apool, size); |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
89 |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
90 /* now allocate the actual alloconly_pool from the created block */ |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
91 new_apool = p_new(&apool.pool, struct alloconly_pool, 1); |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
92 *new_apool = apool; |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
93 #ifdef DEBUG |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
94 new_apool->name = p_strdup(&new_apool->pool, name); |
1786
49b6103dd2e0
Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents:
1782
diff
changeset
|
95 #endif |
0 | 96 |
3238
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
97 /* set base_size so p_clear() doesn't trash alloconly_pool structure. */ |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
98 new_apool->base_size = new_apool->block->size - new_apool->block->left; |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
99 new_apool->block->last_alloc_size = 0; |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
100 |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
101 return &new_apool->pool; |
0 | 102 } |
103 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
104 static void pool_alloconly_destroy(struct alloconly_pool *apool) |
0 | 105 { |
3238
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
106 void *block; |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
107 |
0 | 108 /* destroy all but the last block */ |
109 pool_alloconly_clear(&apool->pool); | |
110 | |
111 /* destroy the last block */ | |
3238
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
112 block = apool->block; |
1111
1ae6cf799802
DEBUG: fill memory with 0xde before freeing it
Timo Sirainen <tss@iki.fi>
parents:
944
diff
changeset
|
113 #ifdef DEBUG |
3238
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
114 memset(block, 0xde, SIZEOF_POOLBLOCK + apool->block->size); |
1111
1ae6cf799802
DEBUG: fill memory with 0xde before freeing it
Timo Sirainen <tss@iki.fi>
parents:
944
diff
changeset
|
115 #endif |
1786
49b6103dd2e0
Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents:
1782
diff
changeset
|
116 |
49b6103dd2e0
Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents:
1782
diff
changeset
|
117 #ifndef USE_GC |
3238
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
118 free(block); |
1786
49b6103dd2e0
Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents:
1782
diff
changeset
|
119 #endif |
0 | 120 } |
121 | |
1489
826bed85c807
Added pool_get_name(), for debugging mostly.
Timo Sirainen <tss@iki.fi>
parents:
1350
diff
changeset
|
122 static const char *pool_alloconly_get_name(pool_t pool) |
826bed85c807
Added pool_get_name(), for debugging mostly.
Timo Sirainen <tss@iki.fi>
parents:
1350
diff
changeset
|
123 { |
826bed85c807
Added pool_get_name(), for debugging mostly.
Timo Sirainen <tss@iki.fi>
parents:
1350
diff
changeset
|
124 struct alloconly_pool *apool = (struct alloconly_pool *) pool; |
826bed85c807
Added pool_get_name(), for debugging mostly.
Timo Sirainen <tss@iki.fi>
parents:
1350
diff
changeset
|
125 |
3238
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
126 #ifdef DEBUG |
1489
826bed85c807
Added pool_get_name(), for debugging mostly.
Timo Sirainen <tss@iki.fi>
parents:
1350
diff
changeset
|
127 return apool->name; |
3238
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
128 #else |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
129 return "alloconly"; |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
130 #endif |
1489
826bed85c807
Added pool_get_name(), for debugging mostly.
Timo Sirainen <tss@iki.fi>
parents:
1350
diff
changeset
|
131 } |
826bed85c807
Added pool_get_name(), for debugging mostly.
Timo Sirainen <tss@iki.fi>
parents:
1350
diff
changeset
|
132 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
133 static void pool_alloconly_ref(pool_t pool) |
0 | 134 { |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
135 struct alloconly_pool *apool = (struct alloconly_pool *) pool; |
0 | 136 |
137 apool->refcount++; | |
138 } | |
139 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
140 static void pool_alloconly_unref(pool_t pool) |
0 | 141 { |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
142 struct alloconly_pool *apool = (struct alloconly_pool *) pool; |
0 | 143 |
144 if (--apool->refcount == 0) | |
145 pool_alloconly_destroy(apool); | |
146 } | |
147 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
148 static void block_alloc(struct alloconly_pool *apool, size_t size) |
0 | 149 { |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
150 struct pool_block *block; |
0 | 151 |
3238
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
152 i_assert(size > SIZEOF_POOLBLOCK); |
0 | 153 |
3238
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
154 if (apool->block != NULL) { |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
155 /* each block is at least twice the size of the previous one */ |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
156 if (size <= apool->block->size) |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
157 size += apool->block->size; |
0 | 158 |
3238
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
159 size = nearest_power(size); |
826 | 160 #ifdef DEBUG |
161 i_warning("Growing pool '%s' with: %"PRIuSIZE_T, | |
162 apool->name, size); | |
3238
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
163 #endif |
826 | 164 } |
165 | |
1786
49b6103dd2e0
Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents:
1782
diff
changeset
|
166 #ifndef USE_GC |
0 | 167 block = calloc(size, 1); |
1786
49b6103dd2e0
Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents:
1782
diff
changeset
|
168 #else |
49b6103dd2e0
Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents:
1782
diff
changeset
|
169 block = GC_malloc(size); |
49b6103dd2e0
Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents:
1782
diff
changeset
|
170 memset(block, 0, size); |
49b6103dd2e0
Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents:
1782
diff
changeset
|
171 #endif |
409
849f3846212a
Check return values for malloc()s and calloc()s.
Timo Sirainen <tss@iki.fi>
parents:
330
diff
changeset
|
172 if (block == NULL) |
3198
cb285bd5d8c9
If we run out of memory, exit with FATAL_OUTOFMEM status instead of dumping
Timo Sirainen <tss@iki.fi>
parents:
2297
diff
changeset
|
173 i_fatal_status(FATAL_OUTOFMEM, "block_alloc(): Out of memory"); |
0 | 174 block->prev = apool->block; |
175 apool->block = block; | |
176 | |
177 block->size = size - SIZEOF_POOLBLOCK; | |
178 block->left = block->size; | |
179 } | |
180 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
181 static void *pool_alloconly_malloc(pool_t pool, size_t size) |
0 | 182 { |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
183 struct alloconly_pool *apool = (struct alloconly_pool *) pool; |
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
|
184 void *mem; |
0 | 185 |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
826
diff
changeset
|
186 if (size == 0 || size > SSIZE_T_MAX) |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
826
diff
changeset
|
187 i_panic("Trying to allocate %"PRIuSIZE_T" bytes", size); |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
826
diff
changeset
|
188 |
0 | 189 size = MEM_ALIGN(size); |
190 | |
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
|
191 if (apool->block->left < size) { |
0 | 192 /* we need a new block */ |
3238
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
193 block_alloc(apool, size + SIZEOF_POOLBLOCK); |
0 | 194 } |
195 | |
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
|
196 mem = POOL_BLOCK_DATA(apool->block) + |
4d6b69558add
Added old_size parameter to p_realloc() - we rarely need it and this way
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
197 (apool->block->size - apool->block->left); |
0 | 198 |
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
|
199 apool->block->left -= size; |
857
3d437b1e5257
Removed pool->realloc_min() which nothing used. A few small fixes to
Timo Sirainen <tss@iki.fi>
parents:
839
diff
changeset
|
200 apool->block->last_alloc_size = 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
|
201 return mem; |
0 | 202 } |
203 | |
2286
ee918986f94e
pool_alloconly_free() can free the last allocation.
Timo Sirainen <tss@iki.fi>
parents:
1786
diff
changeset
|
204 static void pool_alloconly_free(pool_t pool, void *mem) |
0 | 205 { |
2286
ee918986f94e
pool_alloconly_free() can free the last allocation.
Timo Sirainen <tss@iki.fi>
parents:
1786
diff
changeset
|
206 struct alloconly_pool *apool = (struct alloconly_pool *) pool; |
ee918986f94e
pool_alloconly_free() can free the last allocation.
Timo Sirainen <tss@iki.fi>
parents:
1786
diff
changeset
|
207 |
ee918986f94e
pool_alloconly_free() can free the last allocation.
Timo Sirainen <tss@iki.fi>
parents:
1786
diff
changeset
|
208 /* we can free only the last allocation */ |
ee918986f94e
pool_alloconly_free() can free the last allocation.
Timo Sirainen <tss@iki.fi>
parents:
1786
diff
changeset
|
209 if (POOL_BLOCK_DATA(apool->block) + |
ee918986f94e
pool_alloconly_free() can free the last allocation.
Timo Sirainen <tss@iki.fi>
parents:
1786
diff
changeset
|
210 (apool->block->size - apool->block->left - |
ee918986f94e
pool_alloconly_free() can free the last allocation.
Timo Sirainen <tss@iki.fi>
parents:
1786
diff
changeset
|
211 apool->block->last_alloc_size) == mem) { |
2297
59062bb4eaac
If p_free() frees memory, it must also zero it..
Timo Sirainen <tss@iki.fi>
parents:
2286
diff
changeset
|
212 memset(mem, 0, apool->block->last_alloc_size); |
2286
ee918986f94e
pool_alloconly_free() can free the last allocation.
Timo Sirainen <tss@iki.fi>
parents:
1786
diff
changeset
|
213 apool->block->left += apool->block->last_alloc_size; |
ee918986f94e
pool_alloconly_free() can free the last allocation.
Timo Sirainen <tss@iki.fi>
parents:
1786
diff
changeset
|
214 apool->block->last_alloc_size = 0; |
ee918986f94e
pool_alloconly_free() can free the last allocation.
Timo Sirainen <tss@iki.fi>
parents:
1786
diff
changeset
|
215 } |
0 | 216 } |
217 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
218 static int pool_try_grow(struct alloconly_pool *apool, void *mem, size_t size) |
0 | 219 { |
220 /* see if we want to grow the memory we allocated last */ | |
857
3d437b1e5257
Removed pool->realloc_min() which nothing used. A few small fixes to
Timo Sirainen <tss@iki.fi>
parents:
839
diff
changeset
|
221 if (POOL_BLOCK_DATA(apool->block) + |
3d437b1e5257
Removed pool->realloc_min() which nothing used. A few small fixes to
Timo Sirainen <tss@iki.fi>
parents:
839
diff
changeset
|
222 (apool->block->size - apool->block->left - |
3d437b1e5257
Removed pool->realloc_min() which nothing used. A few small fixes to
Timo Sirainen <tss@iki.fi>
parents:
839
diff
changeset
|
223 apool->block->last_alloc_size) == mem) { |
0 | 224 /* yeah, see if we can grow */ |
857
3d437b1e5257
Removed pool->realloc_min() which nothing used. A few small fixes to
Timo Sirainen <tss@iki.fi>
parents:
839
diff
changeset
|
225 if (apool->block->left >= size-apool->block->last_alloc_size) { |
0 | 226 /* just shrink the available size */ |
857
3d437b1e5257
Removed pool->realloc_min() which nothing used. A few small fixes to
Timo Sirainen <tss@iki.fi>
parents:
839
diff
changeset
|
227 apool->block->left -= |
3d437b1e5257
Removed pool->realloc_min() which nothing used. A few small fixes to
Timo Sirainen <tss@iki.fi>
parents:
839
diff
changeset
|
228 size - apool->block->last_alloc_size; |
3d437b1e5257
Removed pool->realloc_min() which nothing used. A few small fixes to
Timo Sirainen <tss@iki.fi>
parents:
839
diff
changeset
|
229 apool->block->last_alloc_size = size; |
0 | 230 return TRUE; |
231 } | |
232 } | |
233 | |
234 return FALSE; | |
235 } | |
236 | |
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
|
237 static void *pool_alloconly_realloc(pool_t pool, void *mem, |
4d6b69558add
Added old_size parameter to p_realloc() - we rarely need it and this way
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
238 size_t old_size, size_t new_size) |
0 | 239 { |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
240 struct alloconly_pool *apool = (struct alloconly_pool *) pool; |
0 | 241 unsigned char *new_mem; |
242 | |
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
|
243 if (new_size == 0 || new_size > SSIZE_T_MAX) |
4d6b69558add
Added old_size parameter to p_realloc() - we rarely need it and this way
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
244 i_panic("Trying to allocate %"PRIuSIZE_T" bytes", new_size); |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
826
diff
changeset
|
245 |
857
3d437b1e5257
Removed pool->realloc_min() which nothing used. A few small fixes to
Timo Sirainen <tss@iki.fi>
parents:
839
diff
changeset
|
246 if (mem == NULL) |
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
|
247 return pool_alloconly_malloc(pool, new_size); |
0 | 248 |
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
|
249 if (new_size <= old_size) |
0 | 250 return mem; |
251 | |
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
|
252 new_size = MEM_ALIGN(new_size); |
0 | 253 |
254 /* see if we can directly grow it */ | |
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
|
255 if (!pool_try_grow(apool, mem, new_size)) { |
422 | 256 /* slow way - allocate + copy */ |
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
|
257 new_mem = pool_alloconly_malloc(pool, new_size); |
422 | 258 memcpy(new_mem, mem, old_size); |
259 mem = new_mem; | |
260 } | |
0 | 261 |
422 | 262 return mem; |
0 | 263 } |
264 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
265 static void pool_alloconly_clear(pool_t pool) |
0 | 266 { |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
267 struct alloconly_pool *apool = (struct alloconly_pool *) pool; |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
268 struct pool_block *block; |
3238
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
269 size_t avail_size; |
0 | 270 |
3238
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
271 /* destroy all blocks but the oldest, which contains the |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
272 struct alloconly_pool allocation. */ |
0 | 273 while (apool->block->prev != NULL) { |
3238
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
274 block = apool->block; |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
275 apool->block = block->prev; |
0 | 276 |
1111
1ae6cf799802
DEBUG: fill memory with 0xde before freeing it
Timo Sirainen <tss@iki.fi>
parents:
944
diff
changeset
|
277 #ifdef DEBUG |
1126
e106cde5255c
DEBUG: we cleared wrong memory block..
Timo Sirainen <tss@iki.fi>
parents:
1111
diff
changeset
|
278 memset(block, 0xde, SIZEOF_POOLBLOCK + block->size); |
1111
1ae6cf799802
DEBUG: fill memory with 0xde before freeing it
Timo Sirainen <tss@iki.fi>
parents:
944
diff
changeset
|
279 #endif |
1786
49b6103dd2e0
Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents:
1782
diff
changeset
|
280 #ifndef USE_GC |
0 | 281 free(block); |
1786
49b6103dd2e0
Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents:
1782
diff
changeset
|
282 #endif |
0 | 283 } |
284 | |
3238
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
285 /* clear the first block */ |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
286 avail_size = apool->block->size - apool->base_size; |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
287 memset(PTR_OFFSET(POOL_BLOCK_DATA(apool->block), apool->base_size), 0, |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
288 avail_size - apool->block->left); |
889a584bd953
Changed alloconly pools to call malloc() only once when creating the pool.
Timo Sirainen <tss@iki.fi>
parents:
3233
diff
changeset
|
289 apool->block->left = avail_size; |
857
3d437b1e5257
Removed pool->realloc_min() which nothing used. A few small fixes to
Timo Sirainen <tss@iki.fi>
parents:
839
diff
changeset
|
290 apool->block->last_alloc_size = 0; |
0 | 291 } |
3233
6396b4c0a721
Added p_get_max_easy_alloc_size().
Timo Sirainen <tss@iki.fi>
parents:
3198
diff
changeset
|
292 |
6396b4c0a721
Added p_get_max_easy_alloc_size().
Timo Sirainen <tss@iki.fi>
parents:
3198
diff
changeset
|
293 static size_t pool_alloconly_get_max_easy_alloc_size(pool_t pool) |
6396b4c0a721
Added p_get_max_easy_alloc_size().
Timo Sirainen <tss@iki.fi>
parents:
3198
diff
changeset
|
294 { |
6396b4c0a721
Added p_get_max_easy_alloc_size().
Timo Sirainen <tss@iki.fi>
parents:
3198
diff
changeset
|
295 struct alloconly_pool *apool = (struct alloconly_pool *) pool; |
6396b4c0a721
Added p_get_max_easy_alloc_size().
Timo Sirainen <tss@iki.fi>
parents:
3198
diff
changeset
|
296 |
6396b4c0a721
Added p_get_max_easy_alloc_size().
Timo Sirainen <tss@iki.fi>
parents:
3198
diff
changeset
|
297 return apool->block->left; |
6396b4c0a721
Added p_get_max_easy_alloc_size().
Timo Sirainen <tss@iki.fi>
parents:
3198
diff
changeset
|
298 } |