Commit | Line | Data |
---|---|---|
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 | |
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. | |
14 | ||
15 | Obviously the long-running processes (the daemon, the queue runner, and eximon) | |
16 | must take care not to eat store. | |
17 | ||
18 | The 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 | |
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). */ | |
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 | |
56 | must be a multiple of the alignment. We assume that 8192 is going to be | |
57 | suitably aligned. */ | |
58 | ||
59 | #define STORE_BLOCK_SIZE 8192 | |
60 | ||
61 | /* store_reset() will not free the following block if the last used block has | |
62 | less than this much left in it. */ | |
63 | ||
64 | #define STOREPOOL_MIN_SIZE 256 | |
65 | ||
66 | /* Structure describing the beginning of each big block. */ | |
67 | ||
68 | typedef 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 | |
74 | length that is not a multiple of the alignment, set up a macro for the padded | |
75 | length. */ | |
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 | |
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). */ | |
84 | ||
85 | int store_pool = POOL_PERM; | |
86 | ||
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 }; | |
91 | ||
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 | |
95 | pointer. */ | |
96 | ||
97 | static int pool_malloc = 0; | |
98 | static int nonpool_malloc = 0; | |
99 | ||
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. */ | |
103 | ||
104 | void *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 | |
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 | |
115 | store_last_was_get. | |
116 | ||
117 | Arguments: | |
118 | size amount wanted | |
119 | filename source file from which called | |
120 | linenumber line number in source file. | |
121 | ||
122 | Returns: pointer to store (panic on malloc failure) | |
123 | */ | |
124 | ||
125 | void * | |
126 | store_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 | |
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). */ | |
133 | ||
134 | if (size % alignment != 0) size += alignment - (size % alignment); | |
135 | ||
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. */ | |
139 | ||
140 | if (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 | |
181 | pointer. */ | |
182 | ||
183 | store_last_get[store_pool] = next_yield[store_pool]; | |
184 | ||
185 | /* Cut out the debugging stuff for utilities, but stop picky compilers from | |
186 | giving warnings. */ | |
187 | ||
188 | #ifdef COMPILE_UTILITY | |
189 | filename = filename; | |
190 | linenumber = linenumber; | |
191 | #else | |
192 | DEBUG(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 | 202 | VALGRIND_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 | ||
205 | next_yield[store_pool] = (void *)((char *)next_yield[store_pool] + size); | |
206 | yield_length[store_pool] -= size; | |
207 | ||
208 | return 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 | |
218 | be obtained. | |
219 | ||
220 | Arguments: | |
221 | size amount wanted | |
222 | filename source file from which called | |
223 | linenumber line number in source file. | |
224 | ||
225 | Returns: pointer to store (panic on malloc failure) | |
226 | */ | |
227 | ||
228 | void * | |
229 | store_get_perm_3(int size, const char *filename, int linenumber) | |
230 | { | |
231 | void *yield; | |
232 | int old_pool = store_pool; | |
233 | store_pool = POOL_PERM; | |
234 | yield = store_get_3(size, filename, linenumber); | |
235 | store_pool = old_pool; | |
236 | return 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 | |
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. | |
250 | ||
251 | Arguments: | |
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 | ||
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 | |
260 | be extended | |
261 | */ | |
262 | ||
263 | BOOL | |
264 | store_extend_3(void *ptr, int oldsize, int newsize, const char *filename, | |
265 | int linenumber) | |
266 | { | |
267 | int inc = newsize - oldsize; | |
268 | int rounded_oldsize = oldsize; | |
269 | ||
270 | if (rounded_oldsize % alignment != 0) | |
271 | rounded_oldsize += alignment - (rounded_oldsize % alignment); | |
272 | ||
273 | if ((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 | |
278 | giving warnings. */ | |
279 | ||
280 | #ifdef COMPILE_UTILITY | |
281 | filename = filename; | |
282 | linenumber = linenumber; | |
283 | #else | |
284 | DEBUG(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 | ||
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; | |
7f36d675 | 297 | VALGRIND_MAKE_MEM_UNDEFINED(ptr + oldsize, inc); |
059ec3d9 PH |
298 | return 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 | |
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 | |
312 | follows. | |
313 | ||
314 | Arguments: | |
315 | ptr place to back up to | |
316 | filename source file from which called | |
317 | linenumber line number in source file | |
318 | ||
319 | Returns: nothing | |
320 | */ | |
321 | ||
322 | void | |
323 | store_reset_3(void *ptr, const char *filename, int linenumber) | |
324 | { | |
325 | storeblock *bb; | |
326 | storeblock *b = current_block[store_pool]; | |
327 | char *bc = (char *)b + ALIGNED_SIZEOF_STOREBLOCK; | |
328 | int newlength; | |
329 | ||
330 | /* Last store operation was not a get */ | |
331 | ||
332 | store_last_get[store_pool] = NULL; | |
333 | ||
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. */ | |
336 | ||
337 | if ((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 | |
350 | the released memory. */ | |
351 | ||
352 | newlength = bc + b->length - (char *)ptr; | |
353 | #ifndef COMPILE_UTILITY | |
354 | if (running_in_test_harness) memset(ptr, 0xF0, newlength); | |
355 | #endif | |
7f36d675 | 356 | VALGRIND_MAKE_MEM_NOACCESS(ptr, newlength); |
059ec3d9 PH |
357 | yield_length[store_pool] = newlength - (newlength % alignment); |
358 | next_yield[store_pool] = (char *)ptr + (newlength % alignment); | |
359 | current_block[store_pool] = b; | |
360 | ||
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. */ | |
364 | ||
365 | if (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 | |
373 | bb = b->next; | |
374 | b->next = NULL; | |
375 | ||
376 | while (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 | |
385 | giving warnings. */ | |
386 | ||
387 | #ifdef COMPILE_UTILITY | |
388 | filename = filename; | |
389 | linenumber = linenumber; | |
390 | #else | |
391 | DEBUG(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 | |
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, | |
417 | releases that block. | |
418 | ||
419 | Arguments: | |
420 | block block of store to consider | |
421 | filename source file from which called | |
422 | linenumber line number in source file | |
423 | ||
424 | Returns: nothing | |
425 | */ | |
426 | ||
427 | void | |
428 | store_release_3(void *block, const char *filename, int linenumber) | |
429 | { | |
430 | storeblock *b; | |
431 | ||
432 | /* It will never be the first block, so no need to check that. */ | |
433 | ||
434 | for (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 | |
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(). | |
477 | ||
478 | Arguments: | |
479 | size amount of store wanted | |
480 | filename source file from which called | |
481 | linenumber line number in source file | |
482 | ||
483 | Returns: pointer to gotten store (panic on failure) | |
484 | */ | |
485 | ||
486 | void * | |
487 | store_malloc_3(int size, const char *filename, int linenumber) | |
488 | { | |
489 | void *yield; | |
490 | ||
491 | if (size < 16) size = 16; | |
492 | yield = malloc((size_t)size); | |
493 | ||
494 | if (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 | ||
498 | nonpool_malloc += size; | |
499 | ||
500 | /* Cut out the debugging stuff for utilities, but stop picky compilers from | |
501 | giving warnings. */ | |
502 | ||
503 | #ifdef COMPILE_UTILITY | |
504 | filename = filename; | |
505 | linenumber = linenumber; | |
506 | #else | |
507 | ||
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. */ | |
510 | ||
511 | if (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 | } | |
517 | else | |
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 | ||
524 | return yield; | |
525 | } | |
526 | ||
527 | ||
528 | /************************************************ | |
529 | * Free store * | |
530 | ************************************************/ | |
531 | ||
532 | /* This function is called by the macro store_free(). | |
533 | ||
534 | Arguments: | |
535 | block block of store to free | |
536 | filename source file from which called | |
537 | linenumber line number in source file | |
538 | ||
539 | Returns: nothing | |
540 | */ | |
541 | ||
542 | void | |
543 | store_free_3(void *block, const char *filename, int linenumber) | |
544 | { | |
545 | #ifdef COMPILE_UTILITY | |
546 | filename = filename; | |
547 | linenumber = linenumber; | |
548 | #else | |
549 | DEBUG(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 */ | |
557 | free(block); | |
558 | } | |
559 | ||
560 | /* End of store.c */ |