Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/lib/mempool-unsafe-datastack.c @ 8590:b9faf4db2a9f HEAD
Updated copyright notices to include year 2009.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Tue, 06 Jan 2009 09:25:38 -0500 |
parents | 7ed926ed7aa4 |
children | 00cd9aacd03c |
rev | line source |
---|---|
8590
b9faf4db2a9f
Updated copyright notices to include year 2009.
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
1 /* Copyright (c) 2002-2009 Dovecot authors, see the included COPYING file */ |
1782
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
2 |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
3 #include "lib.h" |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
4 #include "mempool.h" |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
5 |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
6 #include <stdlib.h> |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
7 |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
8 static const char *pool_unsafe_data_stack_get_name(pool_t pool); |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
9 static void pool_unsafe_data_stack_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
|
10 static void pool_unsafe_data_stack_unref(pool_t *pool); |
1782
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
11 static void *pool_unsafe_data_stack_malloc(pool_t pool, size_t size); |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
12 static void pool_unsafe_data_stack_free(pool_t pool, void *mem); |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
13 static void *pool_unsafe_data_stack_realloc(pool_t pool, void *mem, |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
14 size_t old_size, size_t new_size); |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
15 static void pool_unsafe_data_stack_clear(pool_t pool); |
3233
6396b4c0a721
Added p_get_max_easy_alloc_size().
Timo Sirainen <tss@iki.fi>
parents:
1782
diff
changeset
|
16 static size_t pool_unsafe_data_stack_get_max_easy_alloc_size(pool_t pool); |
1782
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
17 |
5026
d7e064f77f79
Don't keep the memory pool virtual functions in the pool structure itself,
Timo Sirainen <tss@iki.fi>
parents:
3878
diff
changeset
|
18 static struct pool_vfuncs static_unsafe_data_stack_pool_vfuncs = { |
1782
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
19 pool_unsafe_data_stack_get_name, |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
20 |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
21 pool_unsafe_data_stack_ref, |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
22 pool_unsafe_data_stack_unref, |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
23 |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
24 pool_unsafe_data_stack_malloc, |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
25 pool_unsafe_data_stack_free, |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
26 |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
27 pool_unsafe_data_stack_realloc, |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
28 |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
29 pool_unsafe_data_stack_clear, |
5026
d7e064f77f79
Don't keep the memory pool virtual functions in the pool structure itself,
Timo Sirainen <tss@iki.fi>
parents:
3878
diff
changeset
|
30 pool_unsafe_data_stack_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:
3878
diff
changeset
|
31 }; |
1782
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
32 |
5026
d7e064f77f79
Don't keep the memory pool virtual functions in the pool structure itself,
Timo Sirainen <tss@iki.fi>
parents:
3878
diff
changeset
|
33 static struct pool static_unsafe_data_stack_pool = { |
d7e064f77f79
Don't keep the memory pool virtual functions in the pool structure itself,
Timo Sirainen <tss@iki.fi>
parents:
3878
diff
changeset
|
34 MEMBER(v) &static_unsafe_data_stack_pool_vfuncs, |
d7e064f77f79
Don't keep the memory pool virtual functions in the pool structure itself,
Timo Sirainen <tss@iki.fi>
parents:
3878
diff
changeset
|
35 |
d7e064f77f79
Don't keep the memory pool virtual functions in the pool structure itself,
Timo Sirainen <tss@iki.fi>
parents:
3878
diff
changeset
|
36 MEMBER(alloconly_pool) TRUE, |
d7e064f77f79
Don't keep the memory pool virtual functions in the pool structure itself,
Timo Sirainen <tss@iki.fi>
parents:
3878
diff
changeset
|
37 MEMBER(datastack_pool) TRUE |
1782
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
38 }; |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
39 |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
40 pool_t unsafe_data_stack_pool = &static_unsafe_data_stack_pool; |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
41 |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
5026
diff
changeset
|
42 static const char *pool_unsafe_data_stack_get_name(pool_t pool ATTR_UNUSED) |
1782
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
43 { |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
44 return "unsafe data stack"; |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
45 } |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
46 |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
5026
diff
changeset
|
47 static void pool_unsafe_data_stack_ref(pool_t pool ATTR_UNUSED) |
1782
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
48 { |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
49 } |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
50 |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
5026
diff
changeset
|
51 static void pool_unsafe_data_stack_unref(pool_t *pool ATTR_UNUSED) |
1782
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
52 { |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
53 } |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
54 |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
5026
diff
changeset
|
55 static void *pool_unsafe_data_stack_malloc(pool_t pool ATTR_UNUSED, |
1782
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
56 size_t size) |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
57 { |
6825
85385079b044
Use likely() and unlikely() macros to make commonly checked error handling
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
58 if (unlikely(size == 0 || size > SSIZE_T_MAX)) |
1782
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
59 i_panic("Trying to allocate %"PRIuSIZE_T" bytes", size); |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
60 |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
61 return t_malloc0(size); |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
62 } |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
63 |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
5026
diff
changeset
|
64 static void pool_unsafe_data_stack_free(pool_t pool ATTR_UNUSED, |
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
5026
diff
changeset
|
65 void *mem ATTR_UNUSED) |
1782
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
66 { |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
67 } |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
68 |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
5026
diff
changeset
|
69 static void *pool_unsafe_data_stack_realloc(pool_t pool ATTR_UNUSED, |
1782
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
70 void *mem, |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
71 size_t old_size, size_t new_size) |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
72 { |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
73 void *new_mem; |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
74 |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
75 /* @UNSAFE */ |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
76 if (new_size == 0 || new_size > SSIZE_T_MAX) |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
77 i_panic("Trying to allocate %"PRIuSIZE_T" bytes", new_size); |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
78 |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
79 if (mem == NULL) |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
80 return pool_unsafe_data_stack_malloc(pool, new_size); |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
81 |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
82 if (old_size >= new_size) |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
83 return mem; |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
84 |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
85 if (!t_try_realloc(mem, new_size)) { |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
86 new_mem = t_malloc(new_size); |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
87 memcpy(new_mem, mem, old_size); |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
88 mem = new_mem; |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
89 } |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
90 |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
91 memset((char *) mem + old_size, 0, new_size - old_size); |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
92 return mem; |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
93 } |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
94 |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
5026
diff
changeset
|
95 static void pool_unsafe_data_stack_clear(pool_t pool ATTR_UNUSED) |
1782
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
96 { |
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
97 } |
3233
6396b4c0a721
Added p_get_max_easy_alloc_size().
Timo Sirainen <tss@iki.fi>
parents:
1782
diff
changeset
|
98 |
6396b4c0a721
Added p_get_max_easy_alloc_size().
Timo Sirainen <tss@iki.fi>
parents:
1782
diff
changeset
|
99 static size_t |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
5026
diff
changeset
|
100 pool_unsafe_data_stack_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:
1782
diff
changeset
|
101 { |
6396b4c0a721
Added p_get_max_easy_alloc_size().
Timo Sirainen <tss@iki.fi>
parents:
1782
diff
changeset
|
102 return t_get_bytes_available(); |
6396b4c0a721
Added p_get_max_easy_alloc_size().
Timo Sirainen <tss@iki.fi>
parents:
1782
diff
changeset
|
103 } |