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