Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/lib/mempool-system.c @ 9532:00cd9aacd03c HEAD
Updated copyright notices to include year 2010.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Mon, 25 Jan 2010 01:18:58 +0200 |
parents | d6fcb6d050ba |
children |
rev | line source |
---|---|
9532
00cd9aacd03c
Updated copyright notices to include year 2010.
Timo Sirainen <tss@iki.fi>
parents:
9254
diff
changeset
|
1 /* Copyright (c) 2002-2010 Dovecot authors, see the included COPYING file */ |
0 | 2 |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
330
diff
changeset
|
3 /* @UNSAFE: whole file */ |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
330
diff
changeset
|
4 |
0 | 5 #include "lib.h" |
6 #include "mempool.h" | |
7 | |
8 #include <stdlib.h> | |
9254
d6fcb6d050ba
Don't include malloc.h if it doesn't have malloc_usable_size().
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
9 #ifndef HAVE_MALLOC_USABLE_SIZE |
d6fcb6d050ba
Don't include malloc.h if it doesn't have malloc_usable_size().
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
10 /* no extra includes needed */ |
d6fcb6d050ba
Don't include malloc.h if it doesn't have malloc_usable_size().
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
11 #elif defined (HAVE_MALLOC_NP_H) |
d6fcb6d050ba
Don't include malloc.h if it doesn't have malloc_usable_size().
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
12 # include <malloc_np.h> /* FreeBSD */ |
d6fcb6d050ba
Don't include malloc.h if it doesn't have malloc_usable_size().
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
13 #elif defined (HAVE_MALLOC_H) |
d6fcb6d050ba
Don't include malloc.h if it doesn't have malloc_usable_size().
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
14 # include <malloc.h> /* Linux */ |
7863 | 15 #endif |
0 | 16 |
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
|
17 #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
|
18 # 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
|
19 #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
|
20 # 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
|
21 #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
|
22 |
1489
826bed85c807
Added pool_get_name(), for debugging mostly.
Timo Sirainen <tss@iki.fi>
parents:
944
diff
changeset
|
23 static const char *pool_system_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
|
24 static void pool_system_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:
3233
diff
changeset
|
25 static void pool_system_unref(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
|
26 static void *pool_system_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
|
27 static void pool_system_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
|
28 static void *pool_system_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
|
29 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
|
30 static void pool_system_clear(pool_t pool); |
3233
6396b4c0a721
Added p_get_max_easy_alloc_size().
Timo Sirainen <tss@iki.fi>
parents:
3198
diff
changeset
|
31 static size_t pool_system_get_max_easy_alloc_size(pool_t pool); |
330 | 32 |
5026
d7e064f77f79
Don't keep the memory pool virtual functions in the pool structure itself,
Timo Sirainen <tss@iki.fi>
parents:
4823
diff
changeset
|
33 static struct pool_vfuncs static_system_pool_vfuncs = { |
1489
826bed85c807
Added pool_get_name(), for debugging mostly.
Timo Sirainen <tss@iki.fi>
parents:
944
diff
changeset
|
34 pool_system_get_name, |
826bed85c807
Added pool_get_name(), for debugging mostly.
Timo Sirainen <tss@iki.fi>
parents:
944
diff
changeset
|
35 |
330 | 36 pool_system_ref, |
37 pool_system_unref, | |
38 | |
39 pool_system_malloc, | |
40 pool_system_free, | |
41 | |
42 pool_system_realloc, | |
43 | |
944
4f38538aa4a1
Added alloconly_pool field for checking if pool supports free()
Timo Sirainen <tss@iki.fi>
parents:
943
diff
changeset
|
44 pool_system_clear, |
5026
d7e064f77f79
Don't keep the memory pool virtual functions in the pool structure itself,
Timo Sirainen <tss@iki.fi>
parents:
4823
diff
changeset
|
45 pool_system_get_max_easy_alloc_size |
d7e064f77f79
Don't keep the memory pool virtual functions in the pool structure itself,
Timo Sirainen <tss@iki.fi>
parents:
4823
diff
changeset
|
46 }; |
944
4f38538aa4a1
Added alloconly_pool field for checking if pool supports free()
Timo Sirainen <tss@iki.fi>
parents:
943
diff
changeset
|
47 |
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
|
48 struct pool static_system_pool = { |
5026
d7e064f77f79
Don't keep the memory pool virtual functions in the pool structure itself,
Timo Sirainen <tss@iki.fi>
parents:
4823
diff
changeset
|
49 MEMBER(v) &static_system_pool_vfuncs, |
d7e064f77f79
Don't keep the memory pool virtual functions in the pool structure itself,
Timo Sirainen <tss@iki.fi>
parents:
4823
diff
changeset
|
50 |
d7e064f77f79
Don't keep the memory pool virtual functions in the pool structure itself,
Timo Sirainen <tss@iki.fi>
parents:
4823
diff
changeset
|
51 MEMBER(alloconly_pool) FALSE, |
d7e064f77f79
Don't keep the memory pool virtual functions in the pool structure itself,
Timo Sirainen <tss@iki.fi>
parents:
4823
diff
changeset
|
52 MEMBER(datastack_pool) FALSE |
330 | 53 }; |
0 | 54 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
55 pool_t system_pool = &static_system_pool; |
0 | 56 |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6300
diff
changeset
|
57 static const char *pool_system_get_name(pool_t pool ATTR_UNUSED) |
1489
826bed85c807
Added pool_get_name(), for debugging mostly.
Timo Sirainen <tss@iki.fi>
parents:
944
diff
changeset
|
58 { |
826bed85c807
Added pool_get_name(), for debugging mostly.
Timo Sirainen <tss@iki.fi>
parents:
944
diff
changeset
|
59 return "system"; |
826bed85c807
Added pool_get_name(), for debugging mostly.
Timo Sirainen <tss@iki.fi>
parents:
944
diff
changeset
|
60 } |
826bed85c807
Added pool_get_name(), for debugging mostly.
Timo Sirainen <tss@iki.fi>
parents:
944
diff
changeset
|
61 |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6300
diff
changeset
|
62 static void pool_system_ref(pool_t pool ATTR_UNUSED) |
0 | 63 { |
64 } | |
65 | |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6300
diff
changeset
|
66 static void pool_system_unref(pool_t *pool ATTR_UNUSED) |
0 | 67 { |
68 } | |
69 | |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6300
diff
changeset
|
70 static void *pool_system_malloc(pool_t pool ATTR_UNUSED, size_t size) |
0 | 71 { |
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
|
72 void *mem; |
0 | 73 |
6825
85385079b044
Use likely() and unlikely() macros to make commonly checked error handling
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
74 if (unlikely(size == 0 || size > SSIZE_T_MAX)) |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
75 i_panic("Trying to allocate %"PRIuSIZE_T" bytes", size); |
0 | 76 |
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
|
77 #ifndef USE_GC |
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
|
78 mem = 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
|
79 #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
|
80 mem = 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
|
81 #endif |
6825
85385079b044
Use likely() and unlikely() macros to make commonly checked error handling
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
82 if (unlikely(mem == NULL)) { |
8206
53ad035a2d0b
If memory allocation fails, log how large allocation we were trying to do.
Timo Sirainen <tss@iki.fi>
parents:
7863
diff
changeset
|
83 i_fatal_status(FATAL_OUTOFMEM, "pool_system_malloc(%"PRIuSIZE_T |
53ad035a2d0b
If memory allocation fails, log how large allocation we were trying to do.
Timo Sirainen <tss@iki.fi>
parents:
7863
diff
changeset
|
84 "): Out of memory", size); |
3198
cb285bd5d8c9
If we run out of memory, exit with FATAL_OUTOFMEM status instead of dumping
Timo Sirainen <tss@iki.fi>
parents:
1786
diff
changeset
|
85 } |
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
|
86 return mem; |
0 | 87 } |
88 | |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6300
diff
changeset
|
89 static void pool_system_free(pool_t pool ATTR_UNUSED, |
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6300
diff
changeset
|
90 void *mem ATTR_UNUSED) |
0 | 91 { |
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
|
92 #ifndef USE_GC |
6826
bca08d6f001d
Don't check if pointer is NULL before passing it to free(). ANSI-C requires
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
93 free(mem); |
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
|
94 #endif |
0 | 95 } |
96 | |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6300
diff
changeset
|
97 static void *pool_system_realloc(pool_t pool ATTR_UNUSED, 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
|
98 size_t old_size, size_t new_size) |
0 | 99 { |
6825
85385079b044
Use likely() and unlikely() macros to make commonly checked error handling
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
100 if (unlikely(new_size == 0 || new_size > SSIZE_T_MAX)) |
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
|
101 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:
805
diff
changeset
|
102 |
6300
7d82a232b5b0
pool_system_realloc(): Moved malloc_usable_size() check before realloc() so
Timo Sirainen <tss@iki.fi>
parents:
5356
diff
changeset
|
103 #if !defined(USE_GC) && defined(HAVE_MALLOC_USABLE_SIZE) |
7d82a232b5b0
pool_system_realloc(): Moved malloc_usable_size() check before realloc() so
Timo Sirainen <tss@iki.fi>
parents:
5356
diff
changeset
|
104 i_assert(old_size == (size_t)-1 || mem == NULL || |
7d82a232b5b0
pool_system_realloc(): Moved malloc_usable_size() check before realloc() so
Timo Sirainen <tss@iki.fi>
parents:
5356
diff
changeset
|
105 old_size <= malloc_usable_size(mem)); |
7d82a232b5b0
pool_system_realloc(): Moved malloc_usable_size() check before realloc() so
Timo Sirainen <tss@iki.fi>
parents:
5356
diff
changeset
|
106 #endif |
7d82a232b5b0
pool_system_realloc(): Moved malloc_usable_size() check before realloc() so
Timo Sirainen <tss@iki.fi>
parents:
5356
diff
changeset
|
107 |
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
|
108 #ifndef USE_GC |
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
|
109 mem = realloc(mem, new_size); |
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
|
110 #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
|
111 mem = GC_realloc(mem, new_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
|
112 #endif |
6825
85385079b044
Use likely() and unlikely() macros to make commonly checked error handling
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
113 if (unlikely(mem == NULL)) { |
8206
53ad035a2d0b
If memory allocation fails, log how large allocation we were trying to do.
Timo Sirainen <tss@iki.fi>
parents:
7863
diff
changeset
|
114 i_fatal_status(FATAL_OUTOFMEM, "pool_system_realloc(%"PRIuSIZE_T |
53ad035a2d0b
If memory allocation fails, log how large allocation we were trying to do.
Timo Sirainen <tss@iki.fi>
parents:
7863
diff
changeset
|
115 "): Out of memory", new_size); |
3198
cb285bd5d8c9
If we run out of memory, exit with FATAL_OUTOFMEM status instead of dumping
Timo Sirainen <tss@iki.fi>
parents:
1786
diff
changeset
|
116 } |
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
|
117 |
4d6b69558add
Added old_size parameter to p_realloc() - we rarely need it and this way
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
118 if (old_size < new_size) { |
4d6b69558add
Added old_size parameter to p_realloc() - we rarely need it and this way
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
119 /* clear new data */ |
943 | 120 memset((char *) mem + old_size, 0, new_size - old_size); |
0 | 121 } |
122 | |
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
|
123 return mem; |
0 | 124 } |
125 | |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6300
diff
changeset
|
126 static void ATTR_NORETURN |
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6300
diff
changeset
|
127 pool_system_clear(pool_t pool ATTR_UNUSED) |
0 | 128 { |
129 i_panic("pool_system_clear() must not be called"); | |
130 } | |
3233
6396b4c0a721
Added p_get_max_easy_alloc_size().
Timo Sirainen <tss@iki.fi>
parents:
3198
diff
changeset
|
131 |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6300
diff
changeset
|
132 static size_t pool_system_get_max_easy_alloc_size(pool_t pool ATTR_UNUSED) |
3233
6396b4c0a721
Added p_get_max_easy_alloc_size().
Timo Sirainen <tss@iki.fi>
parents:
3198
diff
changeset
|
133 { |
6396b4c0a721
Added p_get_max_easy_alloc_size().
Timo Sirainen <tss@iki.fi>
parents:
3198
diff
changeset
|
134 return 0; |
6396b4c0a721
Added p_get_max_easy_alloc_size().
Timo Sirainen <tss@iki.fi>
parents:
3198
diff
changeset
|
135 } |