1
/* $Cambridge: exim/exim-src/src/store.c,v 1.2 2005/01/04 10:00:42 ph10 Exp $ */
1
3
/*************************************************
2
4
* Exim - an Internet mail transport agent *
3
5
*************************************************/
5
/* Copyright (c) University of Cambridge 1995 - 2004 */
7
/* Copyright (c) University of Cambridge 1995 - 2005 */
6
8
/* See the file NOTICE for conditions of use and distribution. */
8
10
/* Exim gets and frees all its store through these functions. In the original
31
33
after accepting a message when multiple messages are received by a single
32
34
process. Resetting happens at some other times as well, usually fairly
33
35
locally after some specific processing that needs working store.
37
. There is a separate pool (POOL_SEARCH) that is used only for lookup storage.
38
This means it can be freed when search_tidyup() is called to close down all
54
60
#define STORE_BLOCK_SIZE 8192
62
/* store_reset() will not free the following block if the last used block has
63
less than this much left in it. */
65
#define STOREPOOL_MIN_SIZE 256
56
67
/* Structure describing the beginning of each big block. */
58
69
typedef struct storeblock {
75
86
int store_pool = POOL_PERM;
77
static storeblock *chainbase[2] = { NULL, NULL };
78
static storeblock *current_block[2] = { NULL, NULL };
79
static void *next_yield[2] = { NULL, NULL };
80
static int yield_length[2] = { -1, -1 };
88
static storeblock *chainbase[3] = { NULL, NULL, NULL };
89
static storeblock *current_block[3] = { NULL, NULL, NULL };
90
static void *next_yield[3] = { NULL, NULL, NULL };
91
static int yield_length[3] = { -1, -1, -1 };
82
93
/* pool_malloc holds the amount of memory used by the store pools; this goes up
83
94
and down as store is reset or released. nonpool_malloc is the total got by
91
102
NULL. This enables string_cat() to optimize its store handling for very long
92
103
strings. That's why the variable is global. */
94
void *store_last_get[2] = { NULL, NULL};
105
void *store_last_get[3] = { NULL, NULL, NULL };
132
143
int length = (size <= STORE_BLOCK_SIZE)? STORE_BLOCK_SIZE : size;
133
144
int mlength = length + ALIGNED_SIZEOF_STOREBLOCK;
134
storeblock *newblock;
136
pool_malloc += mlength; /* Used in pools */
137
nonpool_malloc -= mlength; /* Exclude from overall total */
139
newblock = store_malloc(mlength);
140
newblock->next = NULL;
141
newblock->length = length;
143
if (chainbase[store_pool] == NULL) chainbase[store_pool] = newblock;
144
else current_block[store_pool]->next = newblock;
145
storeblock *newblock = NULL;
147
/* Sometimes store_reset() may leave a block for us; check if we can use it */
149
if (current_block[store_pool] != NULL &&
150
current_block[store_pool]->next != NULL)
152
newblock = current_block[store_pool]->next;
153
if (newblock->length < length)
155
/* Give up on this block, because it's too small */
156
store_free(newblock);
161
/* If there was no free block, get a new one */
163
if (newblock == NULL)
165
pool_malloc += mlength; /* Used in pools */
166
nonpool_malloc -= mlength; /* Exclude from overall total */
167
newblock = store_malloc(mlength);
168
newblock->next = NULL;
169
newblock->length = length;
170
if (chainbase[store_pool] == NULL) chainbase[store_pool] = newblock;
171
else current_block[store_pool]->next = newblock;
146
174
current_block[store_pool] = newblock;
147
yield_length[store_pool] = length;
175
yield_length[store_pool] = newblock->length;
148
176
next_yield[store_pool] =
149
177
(void *)((char *)current_block[store_pool] + ALIGNED_SIZEOF_STOREBLOCK);
327
355
next_yield[store_pool] = (char *)ptr + (newlength % alignment);
328
356
current_block[store_pool] = b;
330
/* Free any subsequent blocks, if any */
358
/* Free any subsequent block. Do NOT free the first successor, if our
359
current block has less than 256 bytes left. This should prevent us from
360
flapping memory. However, keep this block only when it has the default size. */
362
if (yield_length[store_pool] < STOREPOOL_MIN_SIZE &&
364
b->next->length == STORE_BLOCK_SIZE)