Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/lib/mempool-system.c @ 941:4d6b69558add HEAD
Added old_size parameter to p_realloc() - we rarely need it and this way
each allocation takes 8 bytes less memory.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Fri, 10 Jan 2003 22:58:28 +0200 |
parents | fd8888f6f037 |
children | 9a011e9e7070 |
rev | line source |
---|---|
0 | 1 /* |
2 mempool-system.c : Memory pool wrapper for malloc() + realloc() + free() | |
3 | |
4 Copyright (c) 2002 Timo Sirainen | |
5 | |
6 Permission is hereby granted, free of charge, to any person obtaining | |
7 a copy of this software and associated documentation files (the | |
8 "Software"), to deal in the Software without restriction, including | |
9 without limitation the rights to use, copy, modify, merge, publish, | |
10 distribute, sublicense, and/or sell copies of the Software, and to | |
11 permit persons to whom the Software is furnished to do so, subject to | |
12 the following conditions: | |
13 | |
14 The above copyright notice and this permission notice shall be | |
15 included in all copies or substantial portions of the Software. | |
16 | |
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
18 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
20 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
21 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
22 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
23 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
24 */ | |
25 | |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
330
diff
changeset
|
26 /* @UNSAFE: whole file */ |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
330
diff
changeset
|
27 |
0 | 28 #include "lib.h" |
29 #include "mempool.h" | |
30 | |
31 #include <stdlib.h> | |
32 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
33 static void pool_system_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
|
34 static void pool_system_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
|
35 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
|
36 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
|
37 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
|
38 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
|
39 static void pool_system_clear(pool_t pool); |
330 | 40 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
41 static struct pool static_system_pool = { |
330 | 42 pool_system_ref, |
43 pool_system_unref, | |
44 | |
45 pool_system_malloc, | |
46 pool_system_free, | |
47 | |
48 pool_system_realloc, | |
49 | |
50 pool_system_clear | |
51 }; | |
0 | 52 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
53 pool_t system_pool = &static_system_pool; |
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 static void pool_system_ref(pool_t pool __attr_unused__) |
0 | 56 { |
57 } | |
58 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
59 static void pool_system_unref(pool_t pool __attr_unused__) |
0 | 60 { |
61 } | |
62 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
63 static void *pool_system_malloc(pool_t pool __attr_unused__, size_t size) |
0 | 64 { |
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
|
65 void *mem; |
0 | 66 |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
67 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:
805
diff
changeset
|
68 i_panic("Trying to allocate %"PRIuSIZE_T" bytes", size); |
0 | 69 |
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
|
70 mem = calloc(size, 1); |
4d6b69558add
Added old_size parameter to p_realloc() - we rarely need it and this way
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
71 if (mem == NULL) |
0 | 72 i_panic("pool_system_malloc(): Out of memory"); |
73 | |
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
|
74 return mem; |
0 | 75 } |
76 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
77 static void pool_system_free(pool_t pool __attr_unused__, void *mem) |
0 | 78 { |
79 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
|
80 free(mem); |
0 | 81 } |
82 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
83 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
|
84 size_t old_size, size_t new_size) |
0 | 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 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
|
87 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
|
88 |
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
|
89 mem = realloc(mem, 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
|
90 if (mem == NULL) |
4d6b69558add
Added old_size parameter to p_realloc() - we rarely need it and this way
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
91 i_panic("pool_system_realloc(): Out of memory"); |
4d6b69558add
Added old_size parameter to p_realloc() - we rarely need it and this way
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
92 |
4d6b69558add
Added old_size parameter to p_realloc() - we rarely need it and this way
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
93 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
|
94 /* clear new data */ |
4d6b69558add
Added old_size parameter to p_realloc() - we rarely need it and this way
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
95 memset(mem + old_size, 0, new_size - old_size); |
0 | 96 } |
97 | |
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 return mem; |
0 | 99 } |
100 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
101 static void pool_system_clear(pool_t pool __attr_unused__) |
0 | 102 { |
103 i_panic("pool_system_clear() must not be called"); | |
104 } |