Remove obsolete $Cambridge$ CVS revision strings.
[exim.git] / src / src / store.c
CommitLineData
059ec3d9
PH
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
0a49a7a4 5/* Copyright (c) University of Cambridge 1995 - 2009 */
059ec3d9
PH
6/* See the file NOTICE for conditions of use and distribution. */
7
8/* Exim gets and frees all its store through these functions. In the original
9implementation there was a lot of mallocing and freeing of small bits of store.
10The philosophy has now changed to a scheme which includes the concept of
11"stacking pools" of store. For the short-lived processes, there isn't any real
12need to do any garbage collection, but the stack concept allows quick resetting
13in places where this seems sensible.
14
15Obviously the long-running processes (the daemon, the queue runner, and eximon)
16must take care not to eat store.
17
18The following different types of store are recognized:
19
20. Long-lived, large blocks: This is implemented by retaining the original
21 malloc/free functions, and it used for permanent working buffers and for
22 getting blocks to cut up for the other types.
23
24. Long-lived, small blocks: This is used for blocks that have to survive until
25 the process exits. It is implemented as a stacking pool (POOL_PERM). This is
26 functionally the same as store_malloc(), except that the store can't be
27 freed, but I expect it to be more efficient for handling small blocks.
28
29. Short-lived, short blocks: Most of the dynamic store falls into this
30 category. It is implemented as a stacking pool (POOL_MAIN) which is reset
31 after accepting a message when multiple messages are received by a single
32 process. Resetting happens at some other times as well, usually fairly
33 locally after some specific processing that needs working store.
34
35. There is a separate pool (POOL_SEARCH) that is used only for lookup storage.
36 This means it can be freed when search_tidyup() is called to close down all
37 the lookup caching.
38*/
39
40
41#include "exim.h"
7f36d675 42#include "memcheck.h"
059ec3d9
PH
43
44
45/* We need to know how to align blocks of data for general use. I'm not sure
46how to get an alignment factor in general. In the current world, a value of 8
47is probably right, and this is sizeof(double) on some systems and sizeof(void
48*) on others, so take the larger of those. Since everything in this expression
49is a constant, the compiler should optimize it to a simple constant wherever it
50appears (I checked that gcc does do this). */
51
52#define alignment \
53 ((sizeof(void *) > sizeof(double))? sizeof(void *) : sizeof(double))
54
55/* Size of block to get from malloc to carve up into smaller ones. This
56must be a multiple of the alignment. We assume that 8192 is going to be
57suitably aligned. */
58
59#define STORE_BLOCK_SIZE 8192
60
61/* store_reset() will not free the following block if the last used block has
62less than this much left in it. */
63
64#define STOREPOOL_MIN_SIZE 256
65
66/* Structure describing the beginning of each big block. */
67
68typedef struct storeblock {
69 struct storeblock *next;
70 size_t length;
71} storeblock;
72
73/* Just in case we find ourselves on a system where the structure above has a
74length that is not a multiple of the alignment, set up a macro for the padded
75length. */
76
77#define ALIGNED_SIZEOF_STOREBLOCK \
78 (((sizeof(storeblock) + alignment - 1) / alignment) * alignment)
79
80/* Variables holding data for the local pools of store. The current pool number
81is held in store_pool, which is global so that it can be changed from outside.
82Setting the initial length values to -1 forces a malloc for the first call,
83even if the length is zero (which is used for getting a point to reset to). */
84
85int store_pool = POOL_PERM;
86
87static storeblock *chainbase[3] = { NULL, NULL, NULL };
88static storeblock *current_block[3] = { NULL, NULL, NULL };
89static void *next_yield[3] = { NULL, NULL, NULL };
90static int yield_length[3] = { -1, -1, -1 };
91
92/* pool_malloc holds the amount of memory used by the store pools; this goes up
93and down as store is reset or released. nonpool_malloc is the total got by
94malloc from other calls; this doesn't go down because it is just freed by
95pointer. */
96
97static int pool_malloc = 0;
98static int nonpool_malloc = 0;
99
100/* This variable is set by store_get() to its yield, and by store_reset() to
101NULL. This enables string_cat() to optimize its store handling for very long
102strings. That's why the variable is global. */
103
104void *store_last_get[3] = { NULL, NULL, NULL };
105
106
107
108/*************************************************
109* Get a block from the current pool *
110*************************************************/
111
112/* Running out of store is a total disaster. This function is called via the
113macro store_get(). It passes back a block of store within the current big
114block, getting a new one if necessary. The address is saved in
115store_last_was_get.
116
117Arguments:
118 size amount wanted
119 filename source file from which called
120 linenumber line number in source file.
121
122Returns: pointer to store (panic on malloc failure)
123*/
124
125void *
126store_get_3(int size, const char *filename, int linenumber)
127{
128/* Round up the size to a multiple of the alignment. Although this looks a
129messy statement, because "alignment" is a constant expression, the compiler can
130do a reasonable job of optimizing, especially if the value of "alignment" is a
131power of two. I checked this with -O2, and gcc did very well, compiling it to 4
132instructions on a Sparc (alignment = 8). */
133
134if (size % alignment != 0) size += alignment - (size % alignment);
135
136/* If there isn't room in the current block, get a new one. The minimum
137size is STORE_BLOCK_SIZE, and we would expect this to be the norm, since
138these functions are mostly called for small amounts of store. */
139
140if (size > yield_length[store_pool])
141 {
142 int length = (size <= STORE_BLOCK_SIZE)? STORE_BLOCK_SIZE : size;
143 int mlength = length + ALIGNED_SIZEOF_STOREBLOCK;
144 storeblock *newblock = NULL;
145
146 /* Sometimes store_reset() may leave a block for us; check if we can use it */
147
148 if (current_block[store_pool] != NULL &&
149 current_block[store_pool]->next != NULL)
150 {
151 newblock = current_block[store_pool]->next;
152 if (newblock->length < length)
153 {
154 /* Give up on this block, because it's too small */
155 store_free(newblock);
156 newblock = NULL;
157 }
158 }
159
160 /* If there was no free block, get a new one */
161
162 if (newblock == NULL)
163 {
164 pool_malloc += mlength; /* Used in pools */
165 nonpool_malloc -= mlength; /* Exclude from overall total */
166 newblock = store_malloc(mlength);
167 newblock->next = NULL;
168 newblock->length = length;
169 if (chainbase[store_pool] == NULL) chainbase[store_pool] = newblock;
170 else current_block[store_pool]->next = newblock;
171 }
172
173 current_block[store_pool] = newblock;
174 yield_length[store_pool] = newblock->length;
175 next_yield[store_pool] =
176 (void *)((char *)current_block[store_pool] + ALIGNED_SIZEOF_STOREBLOCK);
7f36d675 177 VALGRIND_MAKE_MEM_NOACCESS(next_yield[store_pool], yield_length[store_pool]);
059ec3d9
PH
178 }
179
180/* There's (now) enough room in the current block; the yield is the next
181pointer. */
182
183store_last_get[store_pool] = next_yield[store_pool];
184
185/* Cut out the debugging stuff for utilities, but stop picky compilers from
186giving warnings. */
187
188#ifdef COMPILE_UTILITY
189filename = filename;
190linenumber = linenumber;
191#else
192DEBUG(D_memory)
193 {
194 if (running_in_test_harness)
195 debug_printf("---%d Get %5d\n", store_pool, size);
196 else
197 debug_printf("---%d Get %6p %5d %-14s %4d\n", store_pool,
198 store_last_get[store_pool], size, filename, linenumber);
199 }
200#endif /* COMPILE_UTILITY */
201
7f36d675 202VALGRIND_MAKE_MEM_UNDEFINED(store_last_get[store_pool], size);
059ec3d9
PH
203/* Update next pointer and number of bytes left in the current block. */
204
205next_yield[store_pool] = (void *)((char *)next_yield[store_pool] + size);
206yield_length[store_pool] -= size;
207
208return store_last_get[store_pool];
209}
210
211
212
213/*************************************************
214* Get a block from the PERM pool *
215*************************************************/
216
217/* This is just a convenience function, useful when just a single block is to
218be obtained.
219
220Arguments:
221 size amount wanted
222 filename source file from which called
223 linenumber line number in source file.
224
225Returns: pointer to store (panic on malloc failure)
226*/
227
228void *
229store_get_perm_3(int size, const char *filename, int linenumber)
230{
231void *yield;
232int old_pool = store_pool;
233store_pool = POOL_PERM;
234yield = store_get_3(size, filename, linenumber);
235store_pool = old_pool;
236return yield;
237}
238
239
240
241/*************************************************
242* Extend a block if it is at the top *
243*************************************************/
244
245/* While reading strings of unknown length, it is often the case that the
246string is being read into the block at the top of the stack. If it needs to be
247extended, it is more efficient just to extend the top block rather than
248allocate a new block and then have to copy the data. This function is provided
249for the use of string_cat(), but of course can be used elsewhere too.
250
251Arguments:
252 ptr pointer to store block
253 oldsize current size of the block, as requested by user
254 newsize new size required
255 filename source file from which called
256 linenumber line number in source file
257
258Returns: TRUE if the block is at the top of the stack and has been
259 extended; FALSE if it isn't at the top of the stack, or cannot
260 be extended
261*/
262
263BOOL
264store_extend_3(void *ptr, int oldsize, int newsize, const char *filename,
265 int linenumber)
266{
267int inc = newsize - oldsize;
268int rounded_oldsize = oldsize;
269
270if (rounded_oldsize % alignment != 0)
271 rounded_oldsize += alignment - (rounded_oldsize % alignment);
272
273if ((char *)ptr + rounded_oldsize != (char *)(next_yield[store_pool]) ||
274 inc > yield_length[store_pool] + rounded_oldsize - oldsize)
275 return FALSE;
276
277/* Cut out the debugging stuff for utilities, but stop picky compilers from
278giving warnings. */
279
280#ifdef COMPILE_UTILITY
281filename = filename;
282linenumber = linenumber;
283#else
284DEBUG(D_memory)
285 {
286 if (running_in_test_harness)
287 debug_printf("---%d Ext %5d\n", store_pool, newsize);
288 else
289 debug_printf("---%d Ext %6p %5d %-14s %4d\n", store_pool, ptr, newsize,
290 filename, linenumber);
291 }
292#endif /* COMPILE_UTILITY */
293
294if (newsize % alignment != 0) newsize += alignment - (newsize % alignment);
295next_yield[store_pool] = (char *)ptr + newsize;
296yield_length[store_pool] -= newsize - rounded_oldsize;
7f36d675 297VALGRIND_MAKE_MEM_UNDEFINED(ptr + oldsize, inc);
059ec3d9
PH
298return TRUE;
299}
300
301
302
303
304/*************************************************
305* Back up to a previous point on the stack *
306*************************************************/
307
308/* This function resets the next pointer, freeing any subsequent whole blocks
309that are now unused. Normally it is given a pointer that was the yield of a
310call to store_get, and is therefore aligned, but it may be given an offset
311after such a pointer in order to release the end of a block and anything that
312follows.
313
314Arguments:
315 ptr place to back up to
316 filename source file from which called
317 linenumber line number in source file
318
319Returns: nothing
320*/
321
322void
323store_reset_3(void *ptr, const char *filename, int linenumber)
324{
325storeblock *bb;
326storeblock *b = current_block[store_pool];
327char *bc = (char *)b + ALIGNED_SIZEOF_STOREBLOCK;
328int newlength;
329
330/* Last store operation was not a get */
331
332store_last_get[store_pool] = NULL;
333
334/* See if the place is in the current block - as it often will be. Otherwise,
335search for the block in which it lies. */
336
337if ((char *)ptr < bc || (char *)ptr > bc + b->length)
338 {
339 for (b = chainbase[store_pool]; b != NULL; b = b->next)
340 {
341 bc = (char *)b + ALIGNED_SIZEOF_STOREBLOCK;
342 if ((char *)ptr >= bc && (char *)ptr <= bc + b->length) break;
343 }
344 if (b == NULL)
345 log_write(0, LOG_MAIN|LOG_PANIC_DIE, "internal error: store_reset(%d) "
346 "failed: pool=%d %-14s %4d", ptr, store_pool, filename, linenumber);
347 }
348
349/* Back up, rounding to the alignment if necessary. When testing, flatten
350the released memory. */
351
352newlength = bc + b->length - (char *)ptr;
353#ifndef COMPILE_UTILITY
354if (running_in_test_harness) memset(ptr, 0xF0, newlength);
355#endif
7f36d675 356VALGRIND_MAKE_MEM_NOACCESS(ptr, newlength);
059ec3d9
PH
357yield_length[store_pool] = newlength - (newlength % alignment);
358next_yield[store_pool] = (char *)ptr + (newlength % alignment);
359current_block[store_pool] = b;
360
361/* Free any subsequent block. Do NOT free the first successor, if our
362current block has less than 256 bytes left. This should prevent us from
363flapping memory. However, keep this block only when it has the default size. */
364
365if (yield_length[store_pool] < STOREPOOL_MIN_SIZE &&
366 b->next != NULL &&
367 b->next->length == STORE_BLOCK_SIZE)
7f36d675 368 {
059ec3d9 369 b = b->next;
7f36d675
DW
370 VALGRIND_MAKE_MEM_NOACCESS((char *)b + ALIGNED_SIZEOF_STOREBLOCK, b->length - ALIGNED_SIZEOF_STOREBLOCK);
371 }
059ec3d9
PH
372
373bb = b->next;
374b->next = NULL;
375
376while (bb != NULL)
377 {
378 b = bb;
379 bb = bb->next;
380 pool_malloc -= b->length + ALIGNED_SIZEOF_STOREBLOCK;
381 store_free_3(b, filename, linenumber);
382 }
383
384/* Cut out the debugging stuff for utilities, but stop picky compilers from
385giving warnings. */
386
387#ifdef COMPILE_UTILITY
388filename = filename;
389linenumber = linenumber;
390#else
391DEBUG(D_memory)
392 {
393 if (running_in_test_harness)
394 debug_printf("---%d Rst ** %d\n", store_pool, pool_malloc);
395 else
396 debug_printf("---%d Rst %6p ** %-14s %4d %d\n", store_pool, ptr,
397 filename, linenumber, pool_malloc);
398 }
399#endif /* COMPILE_UTILITY */
400}
401
402
403
404
405
406/************************************************
407* Release store *
408************************************************/
409
410/* This function is specifically provided for use when reading very
411long strings, e.g. header lines. When the string gets longer than a
412complete block, it gets copied to a new block. It is helpful to free
413the old block iff the previous copy of the string is at its start,
414and therefore the only thing in it. Otherwise, for very long strings,
415dead store can pile up somewhat disastrously. This function checks that
416the pointer it is given is the first thing in a block, and if so,
417releases that block.
418
419Arguments:
420 block block of store to consider
421 filename source file from which called
422 linenumber line number in source file
423
424Returns: nothing
425*/
426
427void
428store_release_3(void *block, const char *filename, int linenumber)
429{
430storeblock *b;
431
432/* It will never be the first block, so no need to check that. */
433
434for (b = chainbase[store_pool]; b != NULL; b = b->next)
435 {
436 storeblock *bb = b->next;
437 if (bb != NULL && (char *)block == (char *)bb + ALIGNED_SIZEOF_STOREBLOCK)
438 {
439 b->next = bb->next;
440 pool_malloc -= bb->length + ALIGNED_SIZEOF_STOREBLOCK;
441
442 /* Cut out the debugging stuff for utilities, but stop picky compilers
443 from giving warnings. */
444
445 #ifdef COMPILE_UTILITY
446 filename = filename;
447 linenumber = linenumber;
448 #else
449 DEBUG(D_memory)
450 {
451 if (running_in_test_harness)
452 debug_printf("-Release %d\n", pool_malloc);
453 else
454 debug_printf("-Release %6p %-20s %4d %d\n", (void *)bb, filename,
455 linenumber, pool_malloc);
456 }
457 if (running_in_test_harness)
458 memset(bb, 0xF0, bb->length+ALIGNED_SIZEOF_STOREBLOCK);
459 #endif /* COMPILE_UTILITY */
460
461 free(bb);
462 return;
463 }
464 }
465}
466
467
468
469
470/*************************************************
471* Malloc store *
472*************************************************/
473
474/* Running out of store is a total disaster for exim. Some malloc functions
475do not run happily on very small sizes, nor do they document this fact. This
476function is called via the macro store_malloc().
477
478Arguments:
479 size amount of store wanted
480 filename source file from which called
481 linenumber line number in source file
482
483Returns: pointer to gotten store (panic on failure)
484*/
485
486void *
487store_malloc_3(int size, const char *filename, int linenumber)
488{
489void *yield;
490
491if (size < 16) size = 16;
492yield = malloc((size_t)size);
493
494if (yield == NULL)
495 log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to malloc %d bytes of memory: "
496 "called from line %d of %s", size, linenumber, filename);
497
498nonpool_malloc += size;
499
500/* Cut out the debugging stuff for utilities, but stop picky compilers from
501giving warnings. */
502
503#ifdef COMPILE_UTILITY
504filename = filename;
505linenumber = linenumber;
506#else
507
508/* If running in test harness, spend time making sure all the new store
509is not filled with zeros so as to catch problems. */
510
511if (running_in_test_harness)
512 {
513 memset(yield, 0xF0, (size_t)size);
514 DEBUG(D_memory) debug_printf("--Malloc %5d %d %d\n", size, pool_malloc,
515 nonpool_malloc);
516 }
517else
518 {
519 DEBUG(D_memory) debug_printf("--Malloc %6p %5d %-14s %4d %d %d\n", yield,
520 size, filename, linenumber, pool_malloc, nonpool_malloc);
521 }
522#endif /* COMPILE_UTILITY */
523
524return yield;
525}
526
527
528/************************************************
529* Free store *
530************************************************/
531
532/* This function is called by the macro store_free().
533
534Arguments:
535 block block of store to free
536 filename source file from which called
537 linenumber line number in source file
538
539Returns: nothing
540*/
541
542void
543store_free_3(void *block, const char *filename, int linenumber)
544{
545#ifdef COMPILE_UTILITY
546filename = filename;
547linenumber = linenumber;
548#else
549DEBUG(D_memory)
550 {
551 if (running_in_test_harness)
552 debug_printf("----Free\n");
553 else
554 debug_printf("----Free %6p %-20s %4d\n", block, filename, linenumber);
555 }
556#endif /* COMPILE_UTILITY */
557free(block);
558}
559
560/* End of store.c */