1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2009 */
6 /* See the file NOTICE for conditions of use and distribution. */
8 /* Exim gets and frees all its store through these functions. In the original
9 implementation there was a lot of mallocing and freeing of small bits of store.
10 The 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
12 need to do any garbage collection, but the stack concept allows quick resetting
13 in places where this seems sensible.
15 Obviously the long-running processes (the daemon, the queue runner, and eximon)
16 must take care not to eat store.
18 The following different types of store are recognized:
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.
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.
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.
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
45 /* We need to know how to align blocks of data for general use. I'm not sure
46 how to get an alignment factor in general. In the current world, a value of 8
47 is 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
49 is a constant, the compiler should optimize it to a simple constant wherever it
50 appears (I checked that gcc does do this). */
53 ((sizeof(void *) > sizeof(double))? sizeof(void *) : sizeof(double))
55 /* Size of block to get from malloc to carve up into smaller ones. This
56 must be a multiple of the alignment. We assume that 8192 is going to be
59 #define STORE_BLOCK_SIZE 8192
61 /* store_reset() will not free the following block if the last used block has
62 less than this much left in it. */
64 #define STOREPOOL_MIN_SIZE 256
66 /* Structure describing the beginning of each big block. */
68 typedef struct storeblock {
69 struct storeblock *next;
73 /* Just in case we find ourselves on a system where the structure above has a
74 length that is not a multiple of the alignment, set up a macro for the padded
77 #define ALIGNED_SIZEOF_STOREBLOCK \
78 (((sizeof(storeblock) + alignment - 1) / alignment) * alignment)
80 /* Variables holding data for the local pools of store. The current pool number
81 is held in store_pool, which is global so that it can be changed from outside.
82 Setting the initial length values to -1 forces a malloc for the first call,
83 even if the length is zero (which is used for getting a point to reset to). */
85 int store_pool = POOL_PERM;
87 static storeblock *chainbase[3] = { NULL, NULL, NULL };
88 static storeblock *current_block[3] = { NULL, NULL, NULL };
89 static void *next_yield[3] = { NULL, NULL, NULL };
90 static int yield_length[3] = { -1, -1, -1 };
92 /* pool_malloc holds the amount of memory used by the store pools; this goes up
93 and down as store is reset or released. nonpool_malloc is the total got by
94 malloc from other calls; this doesn't go down because it is just freed by
97 static int pool_malloc = 0;
98 static int nonpool_malloc = 0;
100 /* This variable is set by store_get() to its yield, and by store_reset() to
101 NULL. This enables string_cat() to optimize its store handling for very long
102 strings. That's why the variable is global. */
104 void *store_last_get[3] = { NULL, NULL, NULL };
108 /*************************************************
109 * Get a block from the current pool *
110 *************************************************/
112 /* Running out of store is a total disaster. This function is called via the
113 macro store_get(). It passes back a block of store within the current big
114 block, getting a new one if necessary. The address is saved in
119 filename source file from which called
120 linenumber line number in source file.
122 Returns: pointer to store (panic on malloc failure)
126 store_get_3(int size, const char *filename, int linenumber)
128 /* Round up the size to a multiple of the alignment. Although this looks a
129 messy statement, because "alignment" is a constant expression, the compiler can
130 do a reasonable job of optimizing, especially if the value of "alignment" is a
131 power of two. I checked this with -O2, and gcc did very well, compiling it to 4
132 instructions on a Sparc (alignment = 8). */
134 if (size % alignment != 0) size += alignment - (size % alignment);
136 /* If there isn't room in the current block, get a new one. The minimum
137 size is STORE_BLOCK_SIZE, and we would expect this to be the norm, since
138 these functions are mostly called for small amounts of store. */
140 if (size > yield_length[store_pool])
142 int length = (size <= STORE_BLOCK_SIZE)? STORE_BLOCK_SIZE : size;
143 int mlength = length + ALIGNED_SIZEOF_STOREBLOCK;
144 storeblock *newblock = NULL;
146 /* Sometimes store_reset() may leave a block for us; check if we can use it */
148 if (current_block[store_pool] != NULL &&
149 current_block[store_pool]->next != NULL)
151 newblock = current_block[store_pool]->next;
152 if (newblock->length < length)
154 /* Give up on this block, because it's too small */
155 store_free(newblock);
160 /* If there was no free block, get a new one */
162 if (newblock == NULL)
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;
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);
177 VALGRIND_MAKE_MEM_NOACCESS(next_yield[store_pool], yield_length[store_pool]);
180 /* There's (now) enough room in the current block; the yield is the next
183 store_last_get[store_pool] = next_yield[store_pool];
185 /* Cut out the debugging stuff for utilities, but stop picky compilers from
188 #ifdef COMPILE_UTILITY
190 linenumber = linenumber;
194 if (running_in_test_harness)
195 debug_printf("---%d Get %5d\n", store_pool, size);
197 debug_printf("---%d Get %6p %5d %-14s %4d\n", store_pool,
198 store_last_get[store_pool], size, filename, linenumber);
200 #endif /* COMPILE_UTILITY */
202 VALGRIND_MAKE_MEM_UNDEFINED(store_last_get[store_pool], size);
203 /* Update next pointer and number of bytes left in the current block. */
205 next_yield[store_pool] = (void *)((char *)next_yield[store_pool] + size);
206 yield_length[store_pool] -= size;
208 return store_last_get[store_pool];
213 /*************************************************
214 * Get a block from the PERM pool *
215 *************************************************/
217 /* This is just a convenience function, useful when just a single block is to
222 filename source file from which called
223 linenumber line number in source file.
225 Returns: pointer to store (panic on malloc failure)
229 store_get_perm_3(int size, const char *filename, int linenumber)
232 int old_pool = store_pool;
233 store_pool = POOL_PERM;
234 yield = store_get_3(size, filename, linenumber);
235 store_pool = old_pool;
241 /*************************************************
242 * Extend a block if it is at the top *
243 *************************************************/
245 /* While reading strings of unknown length, it is often the case that the
246 string is being read into the block at the top of the stack. If it needs to be
247 extended, it is more efficient just to extend the top block rather than
248 allocate a new block and then have to copy the data. This function is provided
249 for the use of string_cat(), but of course can be used elsewhere too.
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
258 Returns: 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
264 store_extend_3(void *ptr, int oldsize, int newsize, const char *filename,
267 int inc = newsize - oldsize;
268 int rounded_oldsize = oldsize;
270 if (rounded_oldsize % alignment != 0)
271 rounded_oldsize += alignment - (rounded_oldsize % alignment);
273 if ((char *)ptr + rounded_oldsize != (char *)(next_yield[store_pool]) ||
274 inc > yield_length[store_pool] + rounded_oldsize - oldsize)
277 /* Cut out the debugging stuff for utilities, but stop picky compilers from
280 #ifdef COMPILE_UTILITY
282 linenumber = linenumber;
286 if (running_in_test_harness)
287 debug_printf("---%d Ext %5d\n", store_pool, newsize);
289 debug_printf("---%d Ext %6p %5d %-14s %4d\n", store_pool, ptr, newsize,
290 filename, linenumber);
292 #endif /* COMPILE_UTILITY */
294 if (newsize % alignment != 0) newsize += alignment - (newsize % alignment);
295 next_yield[store_pool] = (char *)ptr + newsize;
296 yield_length[store_pool] -= newsize - rounded_oldsize;
297 VALGRIND_MAKE_MEM_UNDEFINED(ptr + oldsize, inc);
304 /*************************************************
305 * Back up to a previous point on the stack *
306 *************************************************/
308 /* This function resets the next pointer, freeing any subsequent whole blocks
309 that are now unused. Normally it is given a pointer that was the yield of a
310 call to store_get, and is therefore aligned, but it may be given an offset
311 after such a pointer in order to release the end of a block and anything that
315 ptr place to back up to
316 filename source file from which called
317 linenumber line number in source file
323 store_reset_3(void *ptr, const char *filename, int linenumber)
326 storeblock *b = current_block[store_pool];
327 char *bc = (char *)b + ALIGNED_SIZEOF_STOREBLOCK;
330 /* Last store operation was not a get */
332 store_last_get[store_pool] = NULL;
334 /* See if the place is in the current block - as it often will be. Otherwise,
335 search for the block in which it lies. */
337 if ((char *)ptr < bc || (char *)ptr > bc + b->length)
339 for (b = chainbase[store_pool]; b != NULL; b = b->next)
341 bc = (char *)b + ALIGNED_SIZEOF_STOREBLOCK;
342 if ((char *)ptr >= bc && (char *)ptr <= bc + b->length) break;
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);
349 /* Back up, rounding to the alignment if necessary. When testing, flatten
350 the released memory. */
352 newlength = bc + b->length - (char *)ptr;
353 #ifndef COMPILE_UTILITY
354 if (running_in_test_harness) memset(ptr, 0xF0, newlength);
356 VALGRIND_MAKE_MEM_NOACCESS(ptr, newlength);
357 yield_length[store_pool] = newlength - (newlength % alignment);
358 next_yield[store_pool] = (char *)ptr + (newlength % alignment);
359 current_block[store_pool] = b;
361 /* Free any subsequent block. Do NOT free the first successor, if our
362 current block has less than 256 bytes left. This should prevent us from
363 flapping memory. However, keep this block only when it has the default size. */
365 if (yield_length[store_pool] < STOREPOOL_MIN_SIZE &&
367 b->next->length == STORE_BLOCK_SIZE)
370 VALGRIND_MAKE_MEM_NOACCESS((char *)b + ALIGNED_SIZEOF_STOREBLOCK, b->length - ALIGNED_SIZEOF_STOREBLOCK);
380 pool_malloc -= b->length + ALIGNED_SIZEOF_STOREBLOCK;
381 store_free_3(b, filename, linenumber);
384 /* Cut out the debugging stuff for utilities, but stop picky compilers from
387 #ifdef COMPILE_UTILITY
389 linenumber = linenumber;
393 if (running_in_test_harness)
394 debug_printf("---%d Rst ** %d\n", store_pool, pool_malloc);
396 debug_printf("---%d Rst %6p ** %-14s %4d %d\n", store_pool, ptr,
397 filename, linenumber, pool_malloc);
399 #endif /* COMPILE_UTILITY */
406 /************************************************
408 ************************************************/
410 /* This function is specifically provided for use when reading very
411 long strings, e.g. header lines. When the string gets longer than a
412 complete block, it gets copied to a new block. It is helpful to free
413 the old block iff the previous copy of the string is at its start,
414 and therefore the only thing in it. Otherwise, for very long strings,
415 dead store can pile up somewhat disastrously. This function checks that
416 the pointer it is given is the first thing in a block, and if so,
420 block block of store to consider
421 filename source file from which called
422 linenumber line number in source file
428 store_release_3(void *block, const char *filename, int linenumber)
432 /* It will never be the first block, so no need to check that. */
434 for (b = chainbase[store_pool]; b != NULL; b = b->next)
436 storeblock *bb = b->next;
437 if (bb != NULL && (char *)block == (char *)bb + ALIGNED_SIZEOF_STOREBLOCK)
440 pool_malloc -= bb->length + ALIGNED_SIZEOF_STOREBLOCK;
442 /* Cut out the debugging stuff for utilities, but stop picky compilers
443 from giving warnings. */
445 #ifdef COMPILE_UTILITY
447 linenumber = linenumber;
451 if (running_in_test_harness)
452 debug_printf("-Release %d\n", pool_malloc);
454 debug_printf("-Release %6p %-20s %4d %d\n", (void *)bb, filename,
455 linenumber, pool_malloc);
457 if (running_in_test_harness)
458 memset(bb, 0xF0, bb->length+ALIGNED_SIZEOF_STOREBLOCK);
459 #endif /* COMPILE_UTILITY */
470 /*************************************************
472 *************************************************/
474 /* Running out of store is a total disaster for exim. Some malloc functions
475 do not run happily on very small sizes, nor do they document this fact. This
476 function is called via the macro store_malloc().
479 size amount of store wanted
480 filename source file from which called
481 linenumber line number in source file
483 Returns: pointer to gotten store (panic on failure)
487 store_malloc_3(int size, const char *filename, int linenumber)
491 if (size < 16) size = 16;
492 yield = malloc((size_t)size);
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);
498 nonpool_malloc += size;
500 /* Cut out the debugging stuff for utilities, but stop picky compilers from
503 #ifdef COMPILE_UTILITY
505 linenumber = linenumber;
508 /* If running in test harness, spend time making sure all the new store
509 is not filled with zeros so as to catch problems. */
511 if (running_in_test_harness)
513 memset(yield, 0xF0, (size_t)size);
514 DEBUG(D_memory) debug_printf("--Malloc %5d %d %d\n", size, pool_malloc,
519 DEBUG(D_memory) debug_printf("--Malloc %6p %5d %-14s %4d %d %d\n", yield,
520 size, filename, linenumber, pool_malloc, nonpool_malloc);
522 #endif /* COMPILE_UTILITY */
528 /************************************************
530 ************************************************/
532 /* This function is called by the macro store_free().
535 block block of store to free
536 filename source file from which called
537 linenumber line number in source file
543 store_free_3(void *block, const char *filename, int linenumber)
545 #ifdef COMPILE_UTILITY
547 linenumber = linenumber;
551 if (running_in_test_harness)
552 debug_printf("----Free\n");
554 debug_printf("----Free %6p %-20s %4d\n", block, filename, linenumber);
556 #endif /* COMPILE_UTILITY */