annotate src/lib/data-stack.c @ 22664:fea53c2725c0

director: Fix director_max_parallel_moves/kicks type Should be uint, not time.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Thu, 09 Nov 2017 12:24:16 +0200
parents 2e2563132d5f
children cb108f786fb4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
21390
2e2563132d5f Updated copyright notices to include the year 2017.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents: 21323
diff changeset
1 /* Copyright (c) 2002-2017 Dovecot authors, see the included COPYING file */
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
2
805
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 798
diff changeset
3 /* @UNSAFE: whole file */
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 798
diff changeset
4
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
5 #include "lib.h"
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
6 #include "data-stack.h"
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
7
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
8
1786
49b6103dd2e0 Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents: 1741
diff changeset
9 #ifdef HAVE_GC_GC_H
49b6103dd2e0 Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents: 1741
diff changeset
10 # include <gc/gc.h>
49b6103dd2e0 Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents: 1741
diff changeset
11 #elif defined (HAVE_GC_H)
49b6103dd2e0 Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents: 1741
diff changeset
12 # include <gc.h>
49b6103dd2e0 Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents: 1741
diff changeset
13 #endif
49b6103dd2e0 Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents: 1741
diff changeset
14
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
15 /* Initial stack size - this should be kept in a size that doesn't exceed
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
16 in a normal use to avoid extra malloc()ing. */
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents: 751
diff changeset
17 #ifdef DEBUG
798
1c67ec58f0a1 Use a bit larger initial data stack size when DEBUG is set - current one is
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
18 # define INITIAL_STACK_SIZE (1024*10)
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents: 751
diff changeset
19 #else
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents: 751
diff changeset
20 # define INITIAL_STACK_SIZE (1024*32)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents: 751
diff changeset
21 #endif
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
22
1786
49b6103dd2e0 Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents: 1741
diff changeset
23 #ifdef DEBUG
17639
b4eec0a20bba lib: data-stack - enable tighter sanity checks on stack_block allocations
Phil Carmody <phil@dovecot.fi>
parents: 17638
diff changeset
24 # define CLEAR_CHR 0xD5 /* D5 is mnemonic for "Data 5tack" */
7026
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
25 # define SENTRY_COUNT (4*8)
17639
b4eec0a20bba lib: data-stack - enable tighter sanity checks on stack_block allocations
Phil Carmody <phil@dovecot.fi>
parents: 17638
diff changeset
26 # define BLOCK_CANARY ((void *)0xBADBADD5BADBADD5) /* contains 'D5' */
b4eec0a20bba lib: data-stack - enable tighter sanity checks on stack_block allocations
Phil Carmody <phil@dovecot.fi>
parents: 17638
diff changeset
27 # define BLOCK_CANARY_CHECK(block) i_assert((block)->canary == BLOCK_CANARY)
17643
38c7901e9ff6 lib: data-stack - helper macro for requested/allocated size
Phil Carmody <phil@dovecot.fi>
parents: 17642
diff changeset
28 # define ALLOC_SIZE(size) (MEM_ALIGN(sizeof(size_t)) + MEM_ALIGN(size + SENTRY_COUNT))
5362
9e86dbe68663 Added data_stack_set_clean_after_pop()
Timo Sirainen <tss@iki.fi>
parents: 4598
diff changeset
29 #else
1786
49b6103dd2e0 Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents: 1741
diff changeset
30 # define CLEAR_CHR 0
17639
b4eec0a20bba lib: data-stack - enable tighter sanity checks on stack_block allocations
Phil Carmody <phil@dovecot.fi>
parents: 17638
diff changeset
31 # define BLOCK_CANARY NULL
b4eec0a20bba lib: data-stack - enable tighter sanity checks on stack_block allocations
Phil Carmody <phil@dovecot.fi>
parents: 17638
diff changeset
32 # define BLOCK_CANARY_CHECK(block) do { ; } while(0)
17643
38c7901e9ff6 lib: data-stack - helper macro for requested/allocated size
Phil Carmody <phil@dovecot.fi>
parents: 17642
diff changeset
33 # define ALLOC_SIZE(size) MEM_ALIGN(size)
1786
49b6103dd2e0 Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents: 1741
diff changeset
34 #endif
49b6103dd2e0 Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents: 1741
diff changeset
35
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 857
diff changeset
36 struct stack_block {
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 857
diff changeset
37 struct stack_block *next;
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
38
7037
215e96e1f6a3 DEBUG: Fixed clearing data stack memory.
Timo Sirainen <tss@iki.fi>
parents: 7036
diff changeset
39 size_t size, left, lowwater;
17639
b4eec0a20bba lib: data-stack - enable tighter sanity checks on stack_block allocations
Phil Carmody <phil@dovecot.fi>
parents: 17638
diff changeset
40 /* NULL or a poison value, just in case something accesses
17022
2d2614769339 Data stack has now an extra NULL pointer padding before its actual data starts.
Timo Sirainen <tss@iki.fi>
parents: 15715
diff changeset
41 the memory in front of an allocated area */
17639
b4eec0a20bba lib: data-stack - enable tighter sanity checks on stack_block allocations
Phil Carmody <phil@dovecot.fi>
parents: 17638
diff changeset
42 void *canary;
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
43 /* unsigned char data[]; */
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
44 };
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
45
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 857
diff changeset
46 #define SIZEOF_MEMBLOCK MEM_ALIGN(sizeof(struct stack_block))
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
47
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
48 #define STACK_BLOCK_DATA(block) \
7026
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
49 ((unsigned char *) (block) + SIZEOF_MEMBLOCK)
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
50
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
51 /* current_frame_block contains last t_push()ed frames. After that new
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 857
diff changeset
52 stack_frame_block is created and it's ->prev is set to
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 857
diff changeset
53 current_frame_block. */
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
54 #define BLOCK_FRAME_COUNT 32
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
55
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 857
diff changeset
56 struct stack_frame_block {
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 857
diff changeset
57 struct stack_frame_block *prev;
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
58
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 857
diff changeset
59 struct stack_block *block[BLOCK_FRAME_COUNT];
17638
a5f479be46b9 lib: cosmetic - whitespace cleanup in allocator/memory-related code
Phil Carmody <phil@dovecot.fi>
parents: 17637
diff changeset
60 size_t block_space_used[BLOCK_FRAME_COUNT];
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
61 size_t last_alloc_size[BLOCK_FRAME_COUNT];
17635
6ea0584e3861 lib: add identifying markers to data-stack frames
Phil Carmody <phil@dovecot.fi>
parents: 17185
diff changeset
62 #ifdef DEBUG
6ea0584e3861 lib: add identifying markers to data-stack frames
Phil Carmody <phil@dovecot.fi>
parents: 17185
diff changeset
63 const char *marker[BLOCK_FRAME_COUNT];
17637
d3914e9ffba3 lib: add rudementary statistics gathering to data-stack debugging
Phil Carmody <phil@dovecot.fi>
parents: 17636
diff changeset
64 /* Fairly arbitrary profiling data */
d3914e9ffba3 lib: add rudementary statistics gathering to data-stack debugging
Phil Carmody <phil@dovecot.fi>
parents: 17636
diff changeset
65 unsigned long long alloc_bytes[BLOCK_FRAME_COUNT];
d3914e9ffba3 lib: add rudementary statistics gathering to data-stack debugging
Phil Carmody <phil@dovecot.fi>
parents: 17636
diff changeset
66 unsigned int alloc_count[BLOCK_FRAME_COUNT];
17635
6ea0584e3861 lib: add identifying markers to data-stack frames
Phil Carmody <phil@dovecot.fi>
parents: 17185
diff changeset
67 #endif
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
68 };
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
69
1032
ea309e90c01a Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 903
diff changeset
70 unsigned int data_stack_frame = 0;
536
37893ed97492 changed t_push() and t_pop() to return unsigned int. added global
Timo Sirainen <tss@iki.fi>
parents: 410
diff changeset
71
1032
ea309e90c01a Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 903
diff changeset
72 static int frame_pos = BLOCK_FRAME_COUNT-1; /* in current_frame_block */
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 857
diff changeset
73 static struct stack_frame_block *current_frame_block;
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 857
diff changeset
74 static struct stack_frame_block *unused_frame_blocks;
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
75
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 857
diff changeset
76 static struct stack_block *current_block; /* block now used for allocation */
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 857
diff changeset
77 static struct stack_block *unused_block; /* largest unused block is kept here */
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
78
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 857
diff changeset
79 static struct stack_block *last_buffer_block;
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
80 static size_t last_buffer_size;
14384
e79496bb09f5 data-stack: Fixed calling t_push()/t_malloc() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
81 #ifdef DEBUG
e79496bb09f5 data-stack: Fixed calling t_push()/t_malloc() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
82 static bool clean_after_pop = TRUE;
e79496bb09f5 data-stack: Fixed calling t_push()/t_malloc() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
83 #else
5362
9e86dbe68663 Added data_stack_set_clean_after_pop()
Timo Sirainen <tss@iki.fi>
parents: 4598
diff changeset
84 static bool clean_after_pop = FALSE;
14384
e79496bb09f5 data-stack: Fixed calling t_push()/t_malloc() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
85 #endif
6934
f6118fc9c23e If we fail with out of memory, make sure i_panic() can allocate enough
Timo Sirainen <tss@iki.fi>
parents: 6825
diff changeset
86 static bool outofmem = FALSE;
f6118fc9c23e If we fail with out of memory, make sure i_panic() can allocate enough
Timo Sirainen <tss@iki.fi>
parents: 6825
diff changeset
87
7510
553f26afc900 make outofmem_area static
Diego Liziero <diegoliz@gmail.com>
parents: 7509
diff changeset
88 static union {
6934
f6118fc9c23e If we fail with out of memory, make sure i_panic() can allocate enough
Timo Sirainen <tss@iki.fi>
parents: 6825
diff changeset
89 struct stack_block block;
10215
728a029f56f9 data stack: Fixes to handling out-of-memory situations.
Timo Sirainen <tss@iki.fi>
parents: 9259
diff changeset
90 unsigned char data[512];
6934
f6118fc9c23e If we fail with out of memory, make sure i_panic() can allocate enough
Timo Sirainen <tss@iki.fi>
parents: 6825
diff changeset
91 } outofmem_area;
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
92
17647
5567eedee0c2 lib: data-stack - helper for last alloc ends at block.data+(size-left)
Phil Carmody <phil@dovecot.fi>
parents: 17646
diff changeset
93 static inline
5567eedee0c2 lib: data-stack - helper for last alloc ends at block.data+(size-left)
Phil Carmody <phil@dovecot.fi>
parents: 17646
diff changeset
94 unsigned char *data_stack_after_last_alloc(struct stack_block *block)
5567eedee0c2 lib: data-stack - helper for last alloc ends at block.data+(size-left)
Phil Carmody <phil@dovecot.fi>
parents: 17646
diff changeset
95 {
5567eedee0c2 lib: data-stack - helper for last alloc ends at block.data+(size-left)
Phil Carmody <phil@dovecot.fi>
parents: 17646
diff changeset
96 return STACK_BLOCK_DATA(block) + (block->size - block->left);
5567eedee0c2 lib: data-stack - helper for last alloc ends at block.data+(size-left)
Phil Carmody <phil@dovecot.fi>
parents: 17646
diff changeset
97 }
5567eedee0c2 lib: data-stack - helper for last alloc ends at block.data+(size-left)
Phil Carmody <phil@dovecot.fi>
parents: 17646
diff changeset
98
9259
8fc3639ef601 DEBUG: data-stack buffer overflow checking code was causing false positives.
Timo Sirainen <tss@iki.fi>
parents: 8590
diff changeset
99 static void data_stack_last_buffer_reset(bool preserve_data ATTR_UNUSED)
7440
213dd8e78ad8 DEBUG: Catch if we write past t_buffer_get()ed memory.
Timo Sirainen <tss@iki.fi>
parents: 7326
diff changeset
100 {
213dd8e78ad8 DEBUG: Catch if we write past t_buffer_get()ed memory.
Timo Sirainen <tss@iki.fi>
parents: 7326
diff changeset
101 if (last_buffer_block != NULL) {
213dd8e78ad8 DEBUG: Catch if we write past t_buffer_get()ed memory.
Timo Sirainen <tss@iki.fi>
parents: 7326
diff changeset
102 #ifdef DEBUG
17653
9bf0c6d936ef lib: data-stack - start sentry checks immediately after the reserved buffer
Phil Carmody <phil@dovecot.fi>
parents: 17652
diff changeset
103 unsigned char *last_alloc_end, *p, *pend;
7440
213dd8e78ad8 DEBUG: Catch if we write past t_buffer_get()ed memory.
Timo Sirainen <tss@iki.fi>
parents: 7326
diff changeset
104
17647
5567eedee0c2 lib: data-stack - helper for last alloc ends at block.data+(size-left)
Phil Carmody <phil@dovecot.fi>
parents: 17646
diff changeset
105 last_alloc_end = data_stack_after_last_alloc(current_block);
17653
9bf0c6d936ef lib: data-stack - start sentry checks immediately after the reserved buffer
Phil Carmody <phil@dovecot.fi>
parents: 17652
diff changeset
106 p = last_alloc_end + MEM_ALIGN(sizeof(size_t)) + last_buffer_size;
9bf0c6d936ef lib: data-stack - start sentry checks immediately after the reserved buffer
Phil Carmody <phil@dovecot.fi>
parents: 17652
diff changeset
107 pend = last_alloc_end + ALLOC_SIZE(last_buffer_size);
7440
213dd8e78ad8 DEBUG: Catch if we write past t_buffer_get()ed memory.
Timo Sirainen <tss@iki.fi>
parents: 7326
diff changeset
108 #endif
213dd8e78ad8 DEBUG: Catch if we write past t_buffer_get()ed memory.
Timo Sirainen <tss@iki.fi>
parents: 7326
diff changeset
109 /* reset t_buffer_get() mark - not really needed but makes it
213dd8e78ad8 DEBUG: Catch if we write past t_buffer_get()ed memory.
Timo Sirainen <tss@iki.fi>
parents: 7326
diff changeset
110 easier to notice if t_malloc()/t_push()/t_pop() is called
213dd8e78ad8 DEBUG: Catch if we write past t_buffer_get()ed memory.
Timo Sirainen <tss@iki.fi>
parents: 7326
diff changeset
111 between t_buffer_get() and t_buffer_alloc().
213dd8e78ad8 DEBUG: Catch if we write past t_buffer_get()ed memory.
Timo Sirainen <tss@iki.fi>
parents: 7326
diff changeset
112 do this before we get to i_panic() to avoid recursive
213dd8e78ad8 DEBUG: Catch if we write past t_buffer_get()ed memory.
Timo Sirainen <tss@iki.fi>
parents: 7326
diff changeset
113 panics. */
213dd8e78ad8 DEBUG: Catch if we write past t_buffer_get()ed memory.
Timo Sirainen <tss@iki.fi>
parents: 7326
diff changeset
114 last_buffer_block = NULL;
213dd8e78ad8 DEBUG: Catch if we write past t_buffer_get()ed memory.
Timo Sirainen <tss@iki.fi>
parents: 7326
diff changeset
115
213dd8e78ad8 DEBUG: Catch if we write past t_buffer_get()ed memory.
Timo Sirainen <tss@iki.fi>
parents: 7326
diff changeset
116 #ifdef DEBUG
17653
9bf0c6d936ef lib: data-stack - start sentry checks immediately after the reserved buffer
Phil Carmody <phil@dovecot.fi>
parents: 17652
diff changeset
117 while (p < pend)
9bf0c6d936ef lib: data-stack - start sentry checks immediately after the reserved buffer
Phil Carmody <phil@dovecot.fi>
parents: 17652
diff changeset
118 if (*p++ != CLEAR_CHR)
7440
213dd8e78ad8 DEBUG: Catch if we write past t_buffer_get()ed memory.
Timo Sirainen <tss@iki.fi>
parents: 7326
diff changeset
119 i_panic("t_buffer_get(): buffer overflow");
9259
8fc3639ef601 DEBUG: data-stack buffer overflow checking code was causing false positives.
Timo Sirainen <tss@iki.fi>
parents: 8590
diff changeset
120
8fc3639ef601 DEBUG: data-stack buffer overflow checking code was causing false positives.
Timo Sirainen <tss@iki.fi>
parents: 8590
diff changeset
121 if (!preserve_data) {
17640
3e7dbf85d034 lib: data-stack - simplify expressions in data_stack_last_buffer_reset
Phil Carmody <phil@dovecot.fi>
parents: 17639
diff changeset
122 p = last_alloc_end;
9259
8fc3639ef601 DEBUG: data-stack buffer overflow checking code was causing false positives.
Timo Sirainen <tss@iki.fi>
parents: 8590
diff changeset
123 memset(p, CLEAR_CHR, SENTRY_COUNT);
8fc3639ef601 DEBUG: data-stack buffer overflow checking code was causing false positives.
Timo Sirainen <tss@iki.fi>
parents: 8590
diff changeset
124 }
7440
213dd8e78ad8 DEBUG: Catch if we write past t_buffer_get()ed memory.
Timo Sirainen <tss@iki.fi>
parents: 7326
diff changeset
125 #endif
213dd8e78ad8 DEBUG: Catch if we write past t_buffer_get()ed memory.
Timo Sirainen <tss@iki.fi>
parents: 7326
diff changeset
126 }
213dd8e78ad8 DEBUG: Catch if we write past t_buffer_get()ed memory.
Timo Sirainen <tss@iki.fi>
parents: 7326
diff changeset
127 }
213dd8e78ad8 DEBUG: Catch if we write past t_buffer_get()ed memory.
Timo Sirainen <tss@iki.fi>
parents: 7326
diff changeset
128
17635
6ea0584e3861 lib: add identifying markers to data-stack frames
Phil Carmody <phil@dovecot.fi>
parents: 17185
diff changeset
129 unsigned int t_push(const char *marker)
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
130 {
17638
a5f479be46b9 lib: cosmetic - whitespace cleanup in allocator/memory-related code
Phil Carmody <phil@dovecot.fi>
parents: 17637
diff changeset
131 struct stack_frame_block *frame_block;
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
132
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
133 frame_pos++;
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
134 if (frame_pos == BLOCK_FRAME_COUNT) {
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
135 /* frame block full */
1032
ea309e90c01a Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 903
diff changeset
136 if (data_stack_frame == 0) {
ea309e90c01a Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 903
diff changeset
137 /* kludgy, but allow this before initialization */
ea309e90c01a Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 903
diff changeset
138 frame_pos = 0;
ea309e90c01a Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 903
diff changeset
139 data_stack_init();
17635
6ea0584e3861 lib: add identifying markers to data-stack frames
Phil Carmody <phil@dovecot.fi>
parents: 17185
diff changeset
140 return t_push(marker);
1032
ea309e90c01a Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 903
diff changeset
141 }
ea309e90c01a Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 903
diff changeset
142
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
143 frame_pos = 0;
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
144 if (unused_frame_blocks == NULL) {
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
145 /* allocate new block */
1786
49b6103dd2e0 Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents: 1741
diff changeset
146 #ifndef USE_GC
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 857
diff changeset
147 frame_block = calloc(sizeof(*frame_block), 1);
1786
49b6103dd2e0 Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents: 1741
diff changeset
148 #else
49b6103dd2e0 Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents: 1741
diff changeset
149 frame_block = GC_malloc(sizeof(*frame_block));
49b6103dd2e0 Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents: 1741
diff changeset
150 #endif
3198
cb285bd5d8c9 If we run out of memory, exit with FATAL_OUTOFMEM status instead of dumping
Timo Sirainen <tss@iki.fi>
parents: 1992
diff changeset
151 if (frame_block == NULL) {
cb285bd5d8c9 If we run out of memory, exit with FATAL_OUTOFMEM status instead of dumping
Timo Sirainen <tss@iki.fi>
parents: 1992
diff changeset
152 i_fatal_status(FATAL_OUTOFMEM,
cb285bd5d8c9 If we run out of memory, exit with FATAL_OUTOFMEM status instead of dumping
Timo Sirainen <tss@iki.fi>
parents: 1992
diff changeset
153 "t_push(): Out of memory");
cb285bd5d8c9 If we run out of memory, exit with FATAL_OUTOFMEM status instead of dumping
Timo Sirainen <tss@iki.fi>
parents: 1992
diff changeset
154 }
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
155 } else {
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
156 /* use existing unused frame_block */
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
157 frame_block = unused_frame_blocks;
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
158 unused_frame_blocks = unused_frame_blocks->prev;
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
159 }
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
160
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
161 frame_block->prev = current_frame_block;
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
162 current_frame_block = frame_block;
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
163 }
9259
8fc3639ef601 DEBUG: data-stack buffer overflow checking code was causing false positives.
Timo Sirainen <tss@iki.fi>
parents: 8590
diff changeset
164 data_stack_last_buffer_reset(FALSE);
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
165
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
166 /* mark our current position */
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
167 current_frame_block->block[frame_pos] = current_block;
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
168 current_frame_block->block_space_used[frame_pos] = current_block->left;
17638
a5f479be46b9 lib: cosmetic - whitespace cleanup in allocator/memory-related code
Phil Carmody <phil@dovecot.fi>
parents: 17637
diff changeset
169 current_frame_block->last_alloc_size[frame_pos] = 0;
17635
6ea0584e3861 lib: add identifying markers to data-stack frames
Phil Carmody <phil@dovecot.fi>
parents: 17185
diff changeset
170 #ifdef DEBUG
6ea0584e3861 lib: add identifying markers to data-stack frames
Phil Carmody <phil@dovecot.fi>
parents: 17185
diff changeset
171 current_frame_block->marker[frame_pos] = marker;
17637
d3914e9ffba3 lib: add rudementary statistics gathering to data-stack debugging
Phil Carmody <phil@dovecot.fi>
parents: 17636
diff changeset
172 current_frame_block->alloc_bytes[frame_pos] = 0ULL;
d3914e9ffba3 lib: add rudementary statistics gathering to data-stack debugging
Phil Carmody <phil@dovecot.fi>
parents: 17636
diff changeset
173 current_frame_block->alloc_count[frame_pos] = 0;
17635
6ea0584e3861 lib: add identifying markers to data-stack frames
Phil Carmody <phil@dovecot.fi>
parents: 17185
diff changeset
174 #else
6ea0584e3861 lib: add identifying markers to data-stack frames
Phil Carmody <phil@dovecot.fi>
parents: 17185
diff changeset
175 (void)marker; /* only used for debugging */
6ea0584e3861 lib: add identifying markers to data-stack frames
Phil Carmody <phil@dovecot.fi>
parents: 17185
diff changeset
176 #endif
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
177
17638
a5f479be46b9 lib: cosmetic - whitespace cleanup in allocator/memory-related code
Phil Carmody <phil@dovecot.fi>
parents: 17637
diff changeset
178 return data_stack_frame++;
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
179 }
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
180
17635
6ea0584e3861 lib: add identifying markers to data-stack frames
Phil Carmody <phil@dovecot.fi>
parents: 17185
diff changeset
181 unsigned int t_push_named(const char *format, ...)
6ea0584e3861 lib: add identifying markers to data-stack frames
Phil Carmody <phil@dovecot.fi>
parents: 17185
diff changeset
182 {
6ea0584e3861 lib: add identifying markers to data-stack frames
Phil Carmody <phil@dovecot.fi>
parents: 17185
diff changeset
183 unsigned int ret = t_push(NULL);
6ea0584e3861 lib: add identifying markers to data-stack frames
Phil Carmody <phil@dovecot.fi>
parents: 17185
diff changeset
184 #ifdef DEBUG
6ea0584e3861 lib: add identifying markers to data-stack frames
Phil Carmody <phil@dovecot.fi>
parents: 17185
diff changeset
185 va_list args;
6ea0584e3861 lib: add identifying markers to data-stack frames
Phil Carmody <phil@dovecot.fi>
parents: 17185
diff changeset
186 va_start(args, format);
6ea0584e3861 lib: add identifying markers to data-stack frames
Phil Carmody <phil@dovecot.fi>
parents: 17185
diff changeset
187 current_frame_block->marker[frame_pos] = p_strdup_vprintf(unsafe_data_stack_pool, format, args);
6ea0584e3861 lib: add identifying markers to data-stack frames
Phil Carmody <phil@dovecot.fi>
parents: 17185
diff changeset
188 va_end(args);
6ea0584e3861 lib: add identifying markers to data-stack frames
Phil Carmody <phil@dovecot.fi>
parents: 17185
diff changeset
189 #else
6ea0584e3861 lib: add identifying markers to data-stack frames
Phil Carmody <phil@dovecot.fi>
parents: 17185
diff changeset
190 (void)format; /* unused in non-DEBUG builds */
6ea0584e3861 lib: add identifying markers to data-stack frames
Phil Carmody <phil@dovecot.fi>
parents: 17185
diff changeset
191 #endif
6ea0584e3861 lib: add identifying markers to data-stack frames
Phil Carmody <phil@dovecot.fi>
parents: 17185
diff changeset
192
6ea0584e3861 lib: add identifying markers to data-stack frames
Phil Carmody <phil@dovecot.fi>
parents: 17185
diff changeset
193 return ret;
6ea0584e3861 lib: add identifying markers to data-stack frames
Phil Carmody <phil@dovecot.fi>
parents: 17185
diff changeset
194 }
6ea0584e3861 lib: add identifying markers to data-stack frames
Phil Carmody <phil@dovecot.fi>
parents: 17185
diff changeset
195
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 857
diff changeset
196 static void free_blocks(struct stack_block *block)
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
197 {
1320
e9e08440df77 When data stack grew larger than two blocks, t_pop()ing crashed when trying
Timo Sirainen <tss@iki.fi>
parents: 1032
diff changeset
198 struct stack_block *next;
e9e08440df77 When data stack grew larger than two blocks, t_pop()ing crashed when trying
Timo Sirainen <tss@iki.fi>
parents: 1032
diff changeset
199
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
200 /* free all the blocks, except if any of them is bigger than
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
201 unused_block, replace it */
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
202 while (block != NULL) {
17639
b4eec0a20bba lib: data-stack - enable tighter sanity checks on stack_block allocations
Phil Carmody <phil@dovecot.fi>
parents: 17638
diff changeset
203 BLOCK_CANARY_CHECK(block);
1320
e9e08440df77 When data stack grew larger than two blocks, t_pop()ing crashed when trying
Timo Sirainen <tss@iki.fi>
parents: 1032
diff changeset
204 next = block->next;
e9e08440df77 When data stack grew larger than two blocks, t_pop()ing crashed when trying
Timo Sirainen <tss@iki.fi>
parents: 1032
diff changeset
205
7037
215e96e1f6a3 DEBUG: Fixed clearing data stack memory.
Timo Sirainen <tss@iki.fi>
parents: 7036
diff changeset
206 if (clean_after_pop)
215e96e1f6a3 DEBUG: Fixed clearing data stack memory.
Timo Sirainen <tss@iki.fi>
parents: 7036
diff changeset
207 memset(STACK_BLOCK_DATA(block), CLEAR_CHR, block->size);
215e96e1f6a3 DEBUG: Fixed clearing data stack memory.
Timo Sirainen <tss@iki.fi>
parents: 7036
diff changeset
208
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
209 if (unused_block == NULL || block->size > unused_block->size) {
5362
9e86dbe68663 Added data_stack_set_clean_after_pop()
Timo Sirainen <tss@iki.fi>
parents: 4598
diff changeset
210 #ifndef USE_GC
751
04e2c5774c3f DEBUG: Don't send shrink messages, they could cause infinite loop.
Timo Sirainen <tss@iki.fi>
parents: 734
diff changeset
211 free(unused_block);
5362
9e86dbe68663 Added data_stack_set_clean_after_pop()
Timo Sirainen <tss@iki.fi>
parents: 4598
diff changeset
212 #endif
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
213 unused_block = block;
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
214 } else {
5362
9e86dbe68663 Added data_stack_set_clean_after_pop()
Timo Sirainen <tss@iki.fi>
parents: 4598
diff changeset
215 #ifndef USE_GC
10215
728a029f56f9 data stack: Fixes to handling out-of-memory situations.
Timo Sirainen <tss@iki.fi>
parents: 9259
diff changeset
216 if (block != &outofmem_area.block)
728a029f56f9 data stack: Fixes to handling out-of-memory situations.
Timo Sirainen <tss@iki.fi>
parents: 9259
diff changeset
217 free(block);
5362
9e86dbe68663 Added data_stack_set_clean_after_pop()
Timo Sirainen <tss@iki.fi>
parents: 4598
diff changeset
218 #endif
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
219 }
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
220
1320
e9e08440df77 When data stack grew larger than two blocks, t_pop()ing crashed when trying
Timo Sirainen <tss@iki.fi>
parents: 1032
diff changeset
221 block = next;
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
222 }
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
223 }
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
224
7026
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
225 #ifdef DEBUG
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
226 static void t_pop_verify(void)
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
227 {
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
228 struct stack_block *block;
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
229 unsigned char *p;
17642
9d85b31dfb74 lib: data-stack - disambiguate sizes in t_pop_verify
Phil Carmody <phil@dovecot.fi>
parents: 17640
diff changeset
230 size_t pos, max_pos, used_size;
7026
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
231
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
232 block = current_frame_block->block[frame_pos];
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
233 pos = block->size - current_frame_block->block_space_used[frame_pos];
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
234 while (block != NULL) {
17639
b4eec0a20bba lib: data-stack - enable tighter sanity checks on stack_block allocations
Phil Carmody <phil@dovecot.fi>
parents: 17638
diff changeset
235 BLOCK_CANARY_CHECK(block);
7026
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
236 used_size = block->size - block->left;
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
237 p = STACK_BLOCK_DATA(block);
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
238 while (pos < used_size) {
17642
9d85b31dfb74 lib: data-stack - disambiguate sizes in t_pop_verify
Phil Carmody <phil@dovecot.fi>
parents: 17640
diff changeset
239 size_t requested_size = *(size_t *)(p + pos);
9d85b31dfb74 lib: data-stack - disambiguate sizes in t_pop_verify
Phil Carmody <phil@dovecot.fi>
parents: 17640
diff changeset
240 if (used_size - pos < requested_size)
17636
9251b51dda54 lib: add markers to data-stack debug prints
Phil Carmody <phil@dovecot.fi>
parents: 17635
diff changeset
241 i_panic("data stack[%s]: saved alloc size broken",
9251b51dda54 lib: add markers to data-stack debug prints
Phil Carmody <phil@dovecot.fi>
parents: 17635
diff changeset
242 current_frame_block->marker[frame_pos]);
17643
38c7901e9ff6 lib: data-stack - helper macro for requested/allocated size
Phil Carmody <phil@dovecot.fi>
parents: 17642
diff changeset
243 max_pos = pos + ALLOC_SIZE(requested_size);
38c7901e9ff6 lib: data-stack - helper macro for requested/allocated size
Phil Carmody <phil@dovecot.fi>
parents: 17642
diff changeset
244 pos += MEM_ALIGN(sizeof(size_t)) + requested_size;
7026
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
245
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
246 for (; pos < max_pos; pos++) {
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
247 if (p[pos] != CLEAR_CHR)
17636
9251b51dda54 lib: add markers to data-stack debug prints
Phil Carmody <phil@dovecot.fi>
parents: 17635
diff changeset
248 i_panic("data stack[%s]: buffer overflow",
9251b51dda54 lib: add markers to data-stack debug prints
Phil Carmody <phil@dovecot.fi>
parents: 17635
diff changeset
249 current_frame_block->marker[frame_pos]);
7026
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
250 }
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
251 }
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
252
9259
8fc3639ef601 DEBUG: data-stack buffer overflow checking code was causing false positives.
Timo Sirainen <tss@iki.fi>
parents: 8590
diff changeset
253 /* if we had used t_buffer_get(), the rest of the buffer
8fc3639ef601 DEBUG: data-stack buffer overflow checking code was causing false positives.
Timo Sirainen <tss@iki.fi>
parents: 8590
diff changeset
254 may not contain CLEAR_CHRs. but we've already checked all
8fc3639ef601 DEBUG: data-stack buffer overflow checking code was causing false positives.
Timo Sirainen <tss@iki.fi>
parents: 8590
diff changeset
255 the allocations, so there's no need to check them anyway. */
7026
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
256 block = block->next;
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
257 pos = 0;
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
258 }
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
259 }
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
260 #endif
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
261
536
37893ed97492 changed t_push() and t_pop() to return unsigned int. added global
Timo Sirainen <tss@iki.fi>
parents: 410
diff changeset
262 unsigned int t_pop(void)
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
263 {
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 857
diff changeset
264 struct stack_frame_block *frame_block;
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
265
6825
85385079b044 Use likely() and unlikely() macros to make commonly checked error handling
Timo Sirainen <tss@iki.fi>
parents: 6440
diff changeset
266 if (unlikely(frame_pos < 0))
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
267 i_panic("t_pop() called with empty stack");
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
268
9259
8fc3639ef601 DEBUG: data-stack buffer overflow checking code was causing false positives.
Timo Sirainen <tss@iki.fi>
parents: 8590
diff changeset
269 data_stack_last_buffer_reset(FALSE);
7026
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
270 #ifdef DEBUG
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
271 t_pop_verify();
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
272 #endif
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
273
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
274 /* update the current block */
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
275 current_block = current_frame_block->block[frame_pos];
17639
b4eec0a20bba lib: data-stack - enable tighter sanity checks on stack_block allocations
Phil Carmody <phil@dovecot.fi>
parents: 17638
diff changeset
276 BLOCK_CANARY_CHECK(current_block);
7026
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
277 if (clean_after_pop) {
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
278 size_t pos, used_size;
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
279
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
280 pos = current_block->size -
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
281 current_frame_block->block_space_used[frame_pos];
7037
215e96e1f6a3 DEBUG: Fixed clearing data stack memory.
Timo Sirainen <tss@iki.fi>
parents: 7036
diff changeset
282 used_size = current_block->size - current_block->lowwater;
17646
90a8729d81c8 lib: data-stack - add vital sanity-preserving assert to t_pop
Phil Carmody <phil@dovecot.fi>
parents: 17645
diff changeset
283 i_assert(used_size >= pos);
7026
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
284 memset(STACK_BLOCK_DATA(current_block) + pos, CLEAR_CHR,
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
285 used_size - pos);
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
286 }
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
287 current_block->left = current_frame_block->block_space_used[frame_pos];
7037
215e96e1f6a3 DEBUG: Fixed clearing data stack memory.
Timo Sirainen <tss@iki.fi>
parents: 7036
diff changeset
288 current_block->lowwater = current_block->left;
7026
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
289
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
290 if (current_block->next != NULL) {
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
291 /* free unused blocks */
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
292 free_blocks(current_block->next);
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
293 current_block->next = NULL;
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
294 }
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
295
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
296 if (frame_pos > 0)
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
297 frame_pos--;
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
298 else {
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
299 /* frame block is now unused, add it to unused list */
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
300 frame_pos = BLOCK_FRAME_COUNT-1;
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
301
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
302 frame_block = current_frame_block;
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
303 current_frame_block = frame_block->prev;
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
304
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
305 frame_block->prev = unused_frame_blocks;
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
306 unused_frame_blocks = frame_block;
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
307 }
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
308
17638
a5f479be46b9 lib: cosmetic - whitespace cleanup in allocator/memory-related code
Phil Carmody <phil@dovecot.fi>
parents: 17637
diff changeset
309 return --data_stack_frame;
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
310 }
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
311
6939
c7b42fea5fcc Added T_FRAME*() macros. It's too easy to accidentally break t_push/t_pop
Timo Sirainen <tss@iki.fi>
parents: 6934
diff changeset
312 void t_pop_check(unsigned int *id)
c7b42fea5fcc Added T_FRAME*() macros. It's too easy to accidentally break t_push/t_pop
Timo Sirainen <tss@iki.fi>
parents: 6934
diff changeset
313 {
c7b42fea5fcc Added T_FRAME*() macros. It's too easy to accidentally break t_push/t_pop
Timo Sirainen <tss@iki.fi>
parents: 6934
diff changeset
314 if (unlikely(t_pop() != *id))
c7b42fea5fcc Added T_FRAME*() macros. It's too easy to accidentally break t_push/t_pop
Timo Sirainen <tss@iki.fi>
parents: 6934
diff changeset
315 i_panic("Leaked t_pop() call");
c7b42fea5fcc Added T_FRAME*() macros. It's too easy to accidentally break t_push/t_pop
Timo Sirainen <tss@iki.fi>
parents: 6934
diff changeset
316 *id = 0;
c7b42fea5fcc Added T_FRAME*() macros. It's too easy to accidentally break t_push/t_pop
Timo Sirainen <tss@iki.fi>
parents: 6934
diff changeset
317 }
c7b42fea5fcc Added T_FRAME*() macros. It's too easy to accidentally break t_push/t_pop
Timo Sirainen <tss@iki.fi>
parents: 6934
diff changeset
318
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 857
diff changeset
319 static struct stack_block *mem_block_alloc(size_t min_size)
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
320 {
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 857
diff changeset
321 struct stack_block *block;
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
322 size_t prev_size, alloc_size;
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
323
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
324 prev_size = current_block == NULL ? 0 : current_block->size;
21323
d223fad9767f global: Make sure *_malloc() calculations won't cause integer overflows.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 19552
diff changeset
325 alloc_size = nearest_power(MALLOC_ADD(prev_size, min_size));
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
326
21323
d223fad9767f global: Make sure *_malloc() calculations won't cause integer overflows.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 19552
diff changeset
327 /* nearest_power() returns 2^n values, so alloc_size can't be
d223fad9767f global: Make sure *_malloc() calculations won't cause integer overflows.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 19552
diff changeset
328 anywhere close to SIZE_MAX */
1786
49b6103dd2e0 Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents: 1741
diff changeset
329 #ifndef USE_GC
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
330 block = malloc(SIZEOF_MEMBLOCK + alloc_size);
1786
49b6103dd2e0 Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents: 1741
diff changeset
331 #else
4597
fc07c947eed9 Fixes for using GC
Timo Sirainen <tss@iki.fi>
parents: 3863
diff changeset
332 block = GC_malloc(SIZEOF_MEMBLOCK + alloc_size);
1786
49b6103dd2e0 Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents: 1741
diff changeset
333 #endif
6825
85385079b044 Use likely() and unlikely() macros to make commonly checked error handling
Timo Sirainen <tss@iki.fi>
parents: 6440
diff changeset
334 if (unlikely(block == NULL)) {
6934
f6118fc9c23e If we fail with out of memory, make sure i_panic() can allocate enough
Timo Sirainen <tss@iki.fi>
parents: 6825
diff changeset
335 if (outofmem) {
f6118fc9c23e If we fail with out of memory, make sure i_panic() can allocate enough
Timo Sirainen <tss@iki.fi>
parents: 6825
diff changeset
336 if (min_size > outofmem_area.block.left)
f6118fc9c23e If we fail with out of memory, make sure i_panic() can allocate enough
Timo Sirainen <tss@iki.fi>
parents: 6825
diff changeset
337 abort();
f6118fc9c23e If we fail with out of memory, make sure i_panic() can allocate enough
Timo Sirainen <tss@iki.fi>
parents: 6825
diff changeset
338 return &outofmem_area.block;
f6118fc9c23e If we fail with out of memory, make sure i_panic() can allocate enough
Timo Sirainen <tss@iki.fi>
parents: 6825
diff changeset
339 }
f6118fc9c23e If we fail with out of memory, make sure i_panic() can allocate enough
Timo Sirainen <tss@iki.fi>
parents: 6825
diff changeset
340 outofmem = TRUE;
6440
e10e2147d994 If data stack growing fails because of out-of-memory, something's probably
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
341 i_panic("data stack: Out of memory when allocating %"
e10e2147d994 If data stack growing fails because of out-of-memory, something's probably
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
342 PRIuSIZE_T" bytes", alloc_size + SIZEOF_MEMBLOCK);
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
343 }
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
344 block->size = alloc_size;
7051
1d37d2997220 Don't access uninitialized memory.
Timo Sirainen <tss@iki.fi>
parents: 7037
diff changeset
345 block->left = 0;
1d37d2997220 Don't access uninitialized memory.
Timo Sirainen <tss@iki.fi>
parents: 7037
diff changeset
346 block->lowwater = block->size;
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
347 block->next = NULL;
17639
b4eec0a20bba lib: data-stack - enable tighter sanity checks on stack_block allocations
Phil Carmody <phil@dovecot.fi>
parents: 17638
diff changeset
348 block->canary = BLOCK_CANARY;
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
349
7026
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
350 #ifdef DEBUG
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
351 memset(STACK_BLOCK_DATA(block), CLEAR_CHR, alloc_size);
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
352 #endif
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
353 return block;
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
354 }
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
355
3863
55df57c028d4 Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents: 3232
diff changeset
356 static void *t_malloc_real(size_t size, bool permanent)
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
357 {
7026
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
358 void *ret;
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
359 size_t alloc_size;
837
ec6dd72cb8e3 Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
360 #ifdef DEBUG
3863
55df57c028d4 Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents: 3232
diff changeset
361 bool warn = FALSE;
17185
9b50caaa4467 DEBUG: Make sure errno isn't changed by vsnprintf()/malloc()/free() implementations.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
362 int old_errno = errno;
837
ec6dd72cb8e3 Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
363 #endif
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
364
6825
85385079b044 Use likely() and unlikely() macros to make commonly checked error handling
Timo Sirainen <tss@iki.fi>
parents: 6440
diff changeset
365 if (unlikely(size == 0 || size > SSIZE_T_MAX))
839
34cb1d196d2b String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents: 837
diff changeset
366 i_panic("Trying to allocate %"PRIuSIZE_T" bytes", size);
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
367
6825
85385079b044 Use likely() and unlikely() macros to make commonly checked error handling
Timo Sirainen <tss@iki.fi>
parents: 6440
diff changeset
368 if (unlikely(data_stack_frame == 0)) {
1992
63e67430b580 allow using data stack without data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 1786
diff changeset
369 /* kludgy, but allow this before initialization */
63e67430b580 allow using data stack without data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 1786
diff changeset
370 data_stack_init();
63e67430b580 allow using data stack without data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 1786
diff changeset
371 }
17639
b4eec0a20bba lib: data-stack - enable tighter sanity checks on stack_block allocations
Phil Carmody <phil@dovecot.fi>
parents: 17638
diff changeset
372 BLOCK_CANARY_CHECK(current_block);
1992
63e67430b580 allow using data stack without data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 1786
diff changeset
373
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
374 /* allocate only aligned amount of memory so alignment comes
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
375 always properly */
17643
38c7901e9ff6 lib: data-stack - helper macro for requested/allocated size
Phil Carmody <phil@dovecot.fi>
parents: 17642
diff changeset
376 alloc_size = ALLOC_SIZE(size);
38c7901e9ff6 lib: data-stack - helper macro for requested/allocated size
Phil Carmody <phil@dovecot.fi>
parents: 17642
diff changeset
377 #ifdef DEBUG
17637
d3914e9ffba3 lib: add rudementary statistics gathering to data-stack debugging
Phil Carmody <phil@dovecot.fi>
parents: 17636
diff changeset
378 if(permanent) {
d3914e9ffba3 lib: add rudementary statistics gathering to data-stack debugging
Phil Carmody <phil@dovecot.fi>
parents: 17636
diff changeset
379 current_frame_block->alloc_bytes[frame_pos] += alloc_size;
d3914e9ffba3 lib: add rudementary statistics gathering to data-stack debugging
Phil Carmody <phil@dovecot.fi>
parents: 17636
diff changeset
380 current_frame_block->alloc_count[frame_pos]++;
d3914e9ffba3 lib: add rudementary statistics gathering to data-stack debugging
Phil Carmody <phil@dovecot.fi>
parents: 17636
diff changeset
381 }
7026
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
382 #endif
9259
8fc3639ef601 DEBUG: data-stack buffer overflow checking code was causing false positives.
Timo Sirainen <tss@iki.fi>
parents: 8590
diff changeset
383 data_stack_last_buffer_reset(TRUE);
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
384
402
e90b95f6d962 s/t_try_grow/t_try_realloc/
Timo Sirainen <tss@iki.fi>
parents: 399
diff changeset
385 /* used for t_try_realloc() */
7026
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
386 current_frame_block->last_alloc_size[frame_pos] = alloc_size;
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
387
17645
87629dec23f1 lib: data-stack - pull common code out of if/else branches in t_malloc_real
Phil Carmody <phil@dovecot.fi>
parents: 17644
diff changeset
388 if (current_block->left < alloc_size) {
17644
76136a4ee1a3 lib: data-stack - reorder full current block code
Phil Carmody <phil@dovecot.fi>
parents: 17643
diff changeset
389 struct stack_block *block;
76136a4ee1a3 lib: data-stack - reorder full current block code
Phil Carmody <phil@dovecot.fi>
parents: 17643
diff changeset
390
7026
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
391 /* current block is full, see if we can use the unused_block */
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
392 if (unused_block != NULL && unused_block->size >= alloc_size) {
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
393 block = unused_block;
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
394 unused_block = NULL;
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
395 } else {
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
396 block = mem_block_alloc(alloc_size);
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
397 #ifdef DEBUG
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
398 warn = TRUE;
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
399 #endif
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
400 }
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
401
7026
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
402 block->left = block->size;
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
403 block->next = NULL;
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
404 current_block->next = block;
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
405 current_block = block;
17645
87629dec23f1 lib: data-stack - pull common code out of if/else branches in t_malloc_real
Phil Carmody <phil@dovecot.fi>
parents: 17644
diff changeset
406 }
17644
76136a4ee1a3 lib: data-stack - reorder full current block code
Phil Carmody <phil@dovecot.fi>
parents: 17643
diff changeset
407
17645
87629dec23f1 lib: data-stack - pull common code out of if/else branches in t_malloc_real
Phil Carmody <phil@dovecot.fi>
parents: 17644
diff changeset
408 /* enough space in current block, use it */
17647
5567eedee0c2 lib: data-stack - helper for last alloc ends at block.data+(size-left)
Phil Carmody <phil@dovecot.fi>
parents: 17646
diff changeset
409 ret = data_stack_after_last_alloc(current_block);
17645
87629dec23f1 lib: data-stack - pull common code out of if/else branches in t_malloc_real
Phil Carmody <phil@dovecot.fi>
parents: 17644
diff changeset
410
87629dec23f1 lib: data-stack - pull common code out of if/else branches in t_malloc_real
Phil Carmody <phil@dovecot.fi>
parents: 17644
diff changeset
411 if (current_block->left - alloc_size < current_block->lowwater)
87629dec23f1 lib: data-stack - pull common code out of if/else branches in t_malloc_real
Phil Carmody <phil@dovecot.fi>
parents: 17644
diff changeset
412 current_block->lowwater = current_block->left - alloc_size;
87629dec23f1 lib: data-stack - pull common code out of if/else branches in t_malloc_real
Phil Carmody <phil@dovecot.fi>
parents: 17644
diff changeset
413 if (permanent)
87629dec23f1 lib: data-stack - pull common code out of if/else branches in t_malloc_real
Phil Carmody <phil@dovecot.fi>
parents: 17644
diff changeset
414 current_block->left -= alloc_size;
17644
76136a4ee1a3 lib: data-stack - reorder full current block code
Phil Carmody <phil@dovecot.fi>
parents: 17643
diff changeset
415
728
883cda17d175 DEBUG: we get warnings when growing/shrinking data stack.
Timo Sirainen <tss@iki.fi>
parents: 548
diff changeset
416 #ifdef DEBUG
17645
87629dec23f1 lib: data-stack - pull common code out of if/else branches in t_malloc_real
Phil Carmody <phil@dovecot.fi>
parents: 17644
diff changeset
417 if (warn && getenv("DEBUG_SILENT") == NULL) {
87629dec23f1 lib: data-stack - pull common code out of if/else branches in t_malloc_real
Phil Carmody <phil@dovecot.fi>
parents: 17644
diff changeset
418 /* warn after allocation, so if i_warning() wants to
87629dec23f1 lib: data-stack - pull common code out of if/else branches in t_malloc_real
Phil Carmody <phil@dovecot.fi>
parents: 17644
diff changeset
419 allocate more memory we don't go to infinite loop */
87629dec23f1 lib: data-stack - pull common code out of if/else branches in t_malloc_real
Phil Carmody <phil@dovecot.fi>
parents: 17644
diff changeset
420 i_warning("Growing data stack by %"PRIuSIZE_T" as "
87629dec23f1 lib: data-stack - pull common code out of if/else branches in t_malloc_real
Phil Carmody <phil@dovecot.fi>
parents: 17644
diff changeset
421 "'%s' reaches %llu bytes from %u allocations.",
87629dec23f1 lib: data-stack - pull common code out of if/else branches in t_malloc_real
Phil Carmody <phil@dovecot.fi>
parents: 17644
diff changeset
422 current_block->size,
87629dec23f1 lib: data-stack - pull common code out of if/else branches in t_malloc_real
Phil Carmody <phil@dovecot.fi>
parents: 17644
diff changeset
423 current_frame_block->marker[frame_pos],
87629dec23f1 lib: data-stack - pull common code out of if/else branches in t_malloc_real
Phil Carmody <phil@dovecot.fi>
parents: 17644
diff changeset
424 current_frame_block->alloc_bytes[frame_pos],
87629dec23f1 lib: data-stack - pull common code out of if/else branches in t_malloc_real
Phil Carmody <phil@dovecot.fi>
parents: 17644
diff changeset
425 current_frame_block->alloc_count[frame_pos]);
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
426 }
7026
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
427 memcpy(ret, &size, sizeof(size));
8dc08ad6717c DEBUG: Make sure memory allocated from data stack doesn't overflow the
Timo Sirainen <tss@iki.fi>
parents: 7025
diff changeset
428 ret = PTR_OFFSET(ret, MEM_ALIGN(sizeof(size)));
7052
c751ec28b869 Fixed buffer overflow checking code.
Timo Sirainen <tss@iki.fi>
parents: 7051
diff changeset
429 /* make sure the sentry contains CLEAR_CHRs. it might not if we
c751ec28b869 Fixed buffer overflow checking code.
Timo Sirainen <tss@iki.fi>
parents: 7051
diff changeset
430 had used t_buffer_get(). */
7282
bbae5b6b6d2b DEBUG: Buffer overflow checking caused false errors sometimes.
Timo Sirainen <tss@iki.fi>
parents: 7086
diff changeset
431 memset(PTR_OFFSET(ret, size), CLEAR_CHR,
bbae5b6b6d2b DEBUG: Buffer overflow checking caused false errors sometimes.
Timo Sirainen <tss@iki.fi>
parents: 7086
diff changeset
432 MEM_ALIGN(size + SENTRY_COUNT) - size);
17185
9b50caaa4467 DEBUG: Make sure errno isn't changed by vsnprintf()/malloc()/free() implementations.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
433
9b50caaa4467 DEBUG: Make sure errno isn't changed by vsnprintf()/malloc()/free() implementations.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
434 /* we rely on errno not changing. it shouldn't. */
9b50caaa4467 DEBUG: Make sure errno isn't changed by vsnprintf()/malloc()/free() implementations.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
435 i_assert(errno == old_errno);
837
ec6dd72cb8e3 Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
436 #endif
17638
a5f479be46b9 lib: cosmetic - whitespace cleanup in allocator/memory-related code
Phil Carmody <phil@dovecot.fi>
parents: 17637
diff changeset
437 return ret;
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
438 }
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
439
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
440 void *t_malloc(size_t size)
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
441 {
17638
a5f479be46b9 lib: cosmetic - whitespace cleanup in allocator/memory-related code
Phil Carmody <phil@dovecot.fi>
parents: 17637
diff changeset
442 return t_malloc_real(size, TRUE);
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
443 }
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
444
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
445 void *t_malloc0(size_t size)
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
446 {
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
447 void *mem;
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
448
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
449 mem = t_malloc_real(size, TRUE);
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
450 memset(mem, 0, size);
17638
a5f479be46b9 lib: cosmetic - whitespace cleanup in allocator/memory-related code
Phil Carmody <phil@dovecot.fi>
parents: 17637
diff changeset
451 return mem;
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
452 }
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
453
3863
55df57c028d4 Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents: 3232
diff changeset
454 bool t_try_realloc(void *mem, size_t size)
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
455 {
17650
27ceef4cbd10 lib: data-stack - fix incorrect pointer comparison in t_try_realloc in DEBUG builds
Phil Carmody <phil@dovecot.fi>
parents: 17649
diff changeset
456 size_t debug_adjust = 0, last_alloc_size;
17647
5567eedee0c2 lib: data-stack - helper for last alloc ends at block.data+(size-left)
Phil Carmody <phil@dovecot.fi>
parents: 17646
diff changeset
457 unsigned char *after_last_alloc;
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
458
6825
85385079b044 Use likely() and unlikely() macros to make commonly checked error handling
Timo Sirainen <tss@iki.fi>
parents: 6440
diff changeset
459 if (unlikely(size == 0 || size > SSIZE_T_MAX))
839
34cb1d196d2b String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents: 837
diff changeset
460 i_panic("Trying to allocate %"PRIuSIZE_T" bytes", size);
17639
b4eec0a20bba lib: data-stack - enable tighter sanity checks on stack_block allocations
Phil Carmody <phil@dovecot.fi>
parents: 17638
diff changeset
461 BLOCK_CANARY_CHECK(current_block);
839
34cb1d196d2b String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents: 837
diff changeset
462
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
463 last_alloc_size = current_frame_block->last_alloc_size[frame_pos];
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
464
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
465 /* see if we're trying to grow the memory we allocated last */
17647
5567eedee0c2 lib: data-stack - helper for last alloc ends at block.data+(size-left)
Phil Carmody <phil@dovecot.fi>
parents: 17646
diff changeset
466 after_last_alloc = data_stack_after_last_alloc(current_block);
17650
27ceef4cbd10 lib: data-stack - fix incorrect pointer comparison in t_try_realloc in DEBUG builds
Phil Carmody <phil@dovecot.fi>
parents: 17649
diff changeset
467 #ifdef DEBUG
27ceef4cbd10 lib: data-stack - fix incorrect pointer comparison in t_try_realloc in DEBUG builds
Phil Carmody <phil@dovecot.fi>
parents: 17649
diff changeset
468 debug_adjust = MEM_ALIGN(sizeof(size_t));
27ceef4cbd10 lib: data-stack - fix incorrect pointer comparison in t_try_realloc in DEBUG builds
Phil Carmody <phil@dovecot.fi>
parents: 17649
diff changeset
469 #endif
27ceef4cbd10 lib: data-stack - fix incorrect pointer comparison in t_try_realloc in DEBUG builds
Phil Carmody <phil@dovecot.fi>
parents: 17649
diff changeset
470 if (after_last_alloc - last_alloc_size + debug_adjust == mem) {
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
471 /* yeah, see if we have space to grow */
17648
beb56c7124ab lib: data-stack - t_try_realloc get alloc size right in DEBUG builds
Phil Carmody <phil@dovecot.fi>
parents: 17647
diff changeset
472 size_t new_alloc_size, alloc_growth;
beb56c7124ab lib: data-stack - t_try_realloc get alloc size right in DEBUG builds
Phil Carmody <phil@dovecot.fi>
parents: 17647
diff changeset
473
beb56c7124ab lib: data-stack - t_try_realloc get alloc size right in DEBUG builds
Phil Carmody <phil@dovecot.fi>
parents: 17647
diff changeset
474 new_alloc_size = ALLOC_SIZE(size);
beb56c7124ab lib: data-stack - t_try_realloc get alloc size right in DEBUG builds
Phil Carmody <phil@dovecot.fi>
parents: 17647
diff changeset
475 alloc_growth = (new_alloc_size - last_alloc_size);
17652
e33102604242 lib: data-stack - add DEBUG size and sentry updating to t_try_realloc
Phil Carmody <phil@dovecot.fi>
parents: 17650
diff changeset
476 #ifdef DEBUG
e33102604242 lib: data-stack - add DEBUG size and sentry updating to t_try_realloc
Phil Carmody <phil@dovecot.fi>
parents: 17650
diff changeset
477 size_t old_raw_size; /* sorry, non-C99 users - add braces if you need them */
18189
3ef7f3d53d17 lib: data-stack - fix pointer arithmetic compiler warning
Phil Carmody <phil@dovecot.fi>
parents: 18137
diff changeset
478 old_raw_size = *(size_t *)PTR_OFFSET(mem, -(ptrdiff_t)MEM_ALIGN(sizeof(size_t)));
17652
e33102604242 lib: data-stack - add DEBUG size and sentry updating to t_try_realloc
Phil Carmody <phil@dovecot.fi>
parents: 17650
diff changeset
479 i_assert(ALLOC_SIZE(old_raw_size) == last_alloc_size);
e33102604242 lib: data-stack - add DEBUG size and sentry updating to t_try_realloc
Phil Carmody <phil@dovecot.fi>
parents: 17650
diff changeset
480 /* Only check one byte for over-run, that catches most
e33102604242 lib: data-stack - add DEBUG size and sentry updating to t_try_realloc
Phil Carmody <phil@dovecot.fi>
parents: 17650
diff changeset
481 offenders who are likely to use t_try_realloc() */
e33102604242 lib: data-stack - add DEBUG size and sentry updating to t_try_realloc
Phil Carmody <phil@dovecot.fi>
parents: 17650
diff changeset
482 i_assert(((unsigned char*)mem)[old_raw_size] == CLEAR_CHR);
e33102604242 lib: data-stack - add DEBUG size and sentry updating to t_try_realloc
Phil Carmody <phil@dovecot.fi>
parents: 17650
diff changeset
483 #endif
e33102604242 lib: data-stack - add DEBUG size and sentry updating to t_try_realloc
Phil Carmody <phil@dovecot.fi>
parents: 17650
diff changeset
484
17648
beb56c7124ab lib: data-stack - t_try_realloc get alloc size right in DEBUG builds
Phil Carmody <phil@dovecot.fi>
parents: 17647
diff changeset
485 if (current_block->left >= alloc_growth) {
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
486 /* just shrink the available size */
17648
beb56c7124ab lib: data-stack - t_try_realloc get alloc size right in DEBUG builds
Phil Carmody <phil@dovecot.fi>
parents: 17647
diff changeset
487 current_block->left -= alloc_growth;
17649
3856ea7037db lib: data-stack - fix realloc/lowwater bug
Phil Carmody <phil@dovecot.fi>
parents: 17648
diff changeset
488 if (current_block->left < current_block->lowwater)
3856ea7037db lib: data-stack - fix realloc/lowwater bug
Phil Carmody <phil@dovecot.fi>
parents: 17648
diff changeset
489 current_block->lowwater = current_block->left;
17648
beb56c7124ab lib: data-stack - t_try_realloc get alloc size right in DEBUG builds
Phil Carmody <phil@dovecot.fi>
parents: 17647
diff changeset
490 current_frame_block->last_alloc_size[frame_pos] =
beb56c7124ab lib: data-stack - t_try_realloc get alloc size right in DEBUG builds
Phil Carmody <phil@dovecot.fi>
parents: 17647
diff changeset
491 new_alloc_size;
17652
e33102604242 lib: data-stack - add DEBUG size and sentry updating to t_try_realloc
Phil Carmody <phil@dovecot.fi>
parents: 17650
diff changeset
492 #ifdef DEBUG
e33102604242 lib: data-stack - add DEBUG size and sentry updating to t_try_realloc
Phil Carmody <phil@dovecot.fi>
parents: 17650
diff changeset
493 /* All reallocs are permanent by definition
e33102604242 lib: data-stack - add DEBUG size and sentry updating to t_try_realloc
Phil Carmody <phil@dovecot.fi>
parents: 17650
diff changeset
494 However, they don't count as a new allocation */
e33102604242 lib: data-stack - add DEBUG size and sentry updating to t_try_realloc
Phil Carmody <phil@dovecot.fi>
parents: 17650
diff changeset
495 current_frame_block->alloc_bytes[frame_pos] += alloc_growth;
18189
3ef7f3d53d17 lib: data-stack - fix pointer arithmetic compiler warning
Phil Carmody <phil@dovecot.fi>
parents: 18137
diff changeset
496 *(size_t *)PTR_OFFSET(mem, -(ptrdiff_t)MEM_ALIGN(sizeof(size_t))) = size;
17652
e33102604242 lib: data-stack - add DEBUG size and sentry updating to t_try_realloc
Phil Carmody <phil@dovecot.fi>
parents: 17650
diff changeset
497 memset(PTR_OFFSET(mem, size), CLEAR_CHR,
e33102604242 lib: data-stack - add DEBUG size and sentry updating to t_try_realloc
Phil Carmody <phil@dovecot.fi>
parents: 17650
diff changeset
498 new_alloc_size - size - MEM_ALIGN(sizeof(size_t)));
e33102604242 lib: data-stack - add DEBUG size and sentry updating to t_try_realloc
Phil Carmody <phil@dovecot.fi>
parents: 17650
diff changeset
499 #endif
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
500 return TRUE;
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
501 }
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
502 }
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
503
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
504 return FALSE;
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
505 }
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
506
3232
8508869ab276 Added t_get_bytes_available().
Timo Sirainen <tss@iki.fi>
parents: 3198
diff changeset
507 size_t t_get_bytes_available(void)
8508869ab276 Added t_get_bytes_available().
Timo Sirainen <tss@iki.fi>
parents: 3198
diff changeset
508 {
7849
9a00bd31ca99 t_get_bytes_available(): Return a bit less than is available because some
Timo Sirainen <tss@iki.fi>
parents: 7510
diff changeset
509 #ifndef DEBUG
9a00bd31ca99 t_get_bytes_available(): Return a bit less than is available because some
Timo Sirainen <tss@iki.fi>
parents: 7510
diff changeset
510 const unsigned int extra = MEM_ALIGN_SIZE-1;
9a00bd31ca99 t_get_bytes_available(): Return a bit less than is available because some
Timo Sirainen <tss@iki.fi>
parents: 7510
diff changeset
511 #else
9a00bd31ca99 t_get_bytes_available(): Return a bit less than is available because some
Timo Sirainen <tss@iki.fi>
parents: 7510
diff changeset
512 const unsigned int extra = MEM_ALIGN_SIZE-1 + SENTRY_COUNT +
9a00bd31ca99 t_get_bytes_available(): Return a bit less than is available because some
Timo Sirainen <tss@iki.fi>
parents: 7510
diff changeset
513 MEM_ALIGN(sizeof(size_t));
9a00bd31ca99 t_get_bytes_available(): Return a bit less than is available because some
Timo Sirainen <tss@iki.fi>
parents: 7510
diff changeset
514 #endif
17639
b4eec0a20bba lib: data-stack - enable tighter sanity checks on stack_block allocations
Phil Carmody <phil@dovecot.fi>
parents: 17638
diff changeset
515 BLOCK_CANARY_CHECK(current_block);
7849
9a00bd31ca99 t_get_bytes_available(): Return a bit less than is available because some
Timo Sirainen <tss@iki.fi>
parents: 7510
diff changeset
516 return current_block->left < extra ? current_block->left :
9a00bd31ca99 t_get_bytes_available(): Return a bit less than is available because some
Timo Sirainen <tss@iki.fi>
parents: 7510
diff changeset
517 current_block->left - extra;
3232
8508869ab276 Added t_get_bytes_available().
Timo Sirainen <tss@iki.fi>
parents: 3198
diff changeset
518 }
8508869ab276 Added t_get_bytes_available().
Timo Sirainen <tss@iki.fi>
parents: 3198
diff changeset
519
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
520 void *t_buffer_get(size_t size)
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
521 {
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
522 void *ret;
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
523
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
524 ret = t_malloc_real(size, FALSE);
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
525
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
526 last_buffer_size = size;
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
527 last_buffer_block = current_block;
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
528 return ret;
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
529 }
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
530
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
531 void *t_buffer_reget(void *buffer, size_t size)
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
532 {
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
533 size_t old_size;
17638
a5f479be46b9 lib: cosmetic - whitespace cleanup in allocator/memory-related code
Phil Carmody <phil@dovecot.fi>
parents: 17637
diff changeset
534 void *new_buffer;
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
535
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
536 old_size = last_buffer_size;
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
537 if (size <= old_size)
17638
a5f479be46b9 lib: cosmetic - whitespace cleanup in allocator/memory-related code
Phil Carmody <phil@dovecot.fi>
parents: 17637
diff changeset
538 return buffer;
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
539
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
540 new_buffer = t_buffer_get(size);
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
541 if (new_buffer != buffer)
17638
a5f479be46b9 lib: cosmetic - whitespace cleanup in allocator/memory-related code
Phil Carmody <phil@dovecot.fi>
parents: 17637
diff changeset
542 memcpy(new_buffer, buffer, old_size);
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
543
17638
a5f479be46b9 lib: cosmetic - whitespace cleanup in allocator/memory-related code
Phil Carmody <phil@dovecot.fi>
parents: 17637
diff changeset
544 return new_buffer;
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
545 }
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
546
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
547 void t_buffer_alloc(size_t size)
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
548 {
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
549 i_assert(last_buffer_block != NULL);
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
550 i_assert(last_buffer_size >= size);
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
551 i_assert(current_block->left >= size);
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
552
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
553 /* we've already reserved the space, now we just mark it used */
14682
d0d7b810646b Make sure we check all the functions' return values. Minor API changes to simplify this.
Timo Sirainen <tss@iki.fi>
parents: 14446
diff changeset
554 (void)t_malloc_real(size, TRUE);
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
555 }
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
556
7326
5017c74367e3 Added t_buffer_alloc_last_full().
Timo Sirainen <tss@iki.fi>
parents: 7282
diff changeset
557 void t_buffer_alloc_last_full(void)
5017c74367e3 Added t_buffer_alloc_last_full().
Timo Sirainen <tss@iki.fi>
parents: 7282
diff changeset
558 {
5017c74367e3 Added t_buffer_alloc_last_full().
Timo Sirainen <tss@iki.fi>
parents: 7282
diff changeset
559 if (last_buffer_block != NULL)
14682
d0d7b810646b Make sure we check all the functions' return values. Minor API changes to simplify this.
Timo Sirainen <tss@iki.fi>
parents: 14446
diff changeset
560 (void)t_malloc_real(last_buffer_size, TRUE);
7326
5017c74367e3 Added t_buffer_alloc_last_full().
Timo Sirainen <tss@iki.fi>
parents: 7282
diff changeset
561 }
5017c74367e3 Added t_buffer_alloc_last_full().
Timo Sirainen <tss@iki.fi>
parents: 7282
diff changeset
562
7025
984c05510dbc DEBUG: Data stack wasn't cleared at t_pop() as it should have been. Also
Timo Sirainen <tss@iki.fi>
parents: 6939
diff changeset
563 void data_stack_set_clean_after_pop(bool enable ATTR_UNUSED)
5362
9e86dbe68663 Added data_stack_set_clean_after_pop()
Timo Sirainen <tss@iki.fi>
parents: 4598
diff changeset
564 {
7025
984c05510dbc DEBUG: Data stack wasn't cleared at t_pop() as it should have been. Also
Timo Sirainen <tss@iki.fi>
parents: 6939
diff changeset
565 #ifndef DEBUG
5362
9e86dbe68663 Added data_stack_set_clean_after_pop()
Timo Sirainen <tss@iki.fi>
parents: 4598
diff changeset
566 clean_after_pop = enable;
7025
984c05510dbc DEBUG: Data stack wasn't cleared at t_pop() as it should have been. Also
Timo Sirainen <tss@iki.fi>
parents: 6939
diff changeset
567 #endif
5362
9e86dbe68663 Added data_stack_set_clean_after_pop()
Timo Sirainen <tss@iki.fi>
parents: 4598
diff changeset
568 }
9e86dbe68663 Added data_stack_set_clean_after_pop()
Timo Sirainen <tss@iki.fi>
parents: 4598
diff changeset
569
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
570 void data_stack_init(void)
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
571 {
14384
e79496bb09f5 data-stack: Fixed calling t_push()/t_malloc() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
572 if (data_stack_frame > 0) {
e79496bb09f5 data-stack: Fixed calling t_push()/t_malloc() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
573 /* already initialized (we did auto-initialization in
e79496bb09f5 data-stack: Fixed calling t_push()/t_malloc() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
574 t_malloc/t_push) */
e79496bb09f5 data-stack: Fixed calling t_push()/t_malloc() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
575 return;
e79496bb09f5 data-stack: Fixed calling t_push()/t_malloc() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
576 }
e79496bb09f5 data-stack: Fixed calling t_push()/t_malloc() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
577 data_stack_frame = 1;
1032
ea309e90c01a Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 903
diff changeset
578
14384
e79496bb09f5 data-stack: Fixed calling t_push()/t_malloc() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
579 outofmem_area.block.size = outofmem_area.block.left =
e79496bb09f5 data-stack: Fixed calling t_push()/t_malloc() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
580 sizeof(outofmem_area) - sizeof(outofmem_area.block);
6934
f6118fc9c23e If we fail with out of memory, make sure i_panic() can allocate enough
Timo Sirainen <tss@iki.fi>
parents: 6825
diff changeset
581
14384
e79496bb09f5 data-stack: Fixed calling t_push()/t_malloc() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
582 current_block = mem_block_alloc(INITIAL_STACK_SIZE);
e79496bb09f5 data-stack: Fixed calling t_push()/t_malloc() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
583 current_block->left = current_block->size;
e79496bb09f5 data-stack: Fixed calling t_push()/t_malloc() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
584 current_block->next = NULL;
536
37893ed97492 changed t_push() and t_pop() to return unsigned int. added global
Timo Sirainen <tss@iki.fi>
parents: 410
diff changeset
585
14384
e79496bb09f5 data-stack: Fixed calling t_push()/t_malloc() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
586 current_frame_block = NULL;
e79496bb09f5 data-stack: Fixed calling t_push()/t_malloc() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
587 unused_frame_blocks = NULL;
e79496bb09f5 data-stack: Fixed calling t_push()/t_malloc() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
588 frame_pos = BLOCK_FRAME_COUNT-1;
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
589
14384
e79496bb09f5 data-stack: Fixed calling t_push()/t_malloc() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
590 last_buffer_block = NULL;
e79496bb09f5 data-stack: Fixed calling t_push()/t_malloc() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
591 last_buffer_size = 0;
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
592
17635
6ea0584e3861 lib: add identifying markers to data-stack frames
Phil Carmody <phil@dovecot.fi>
parents: 17185
diff changeset
593 (void)t_push("data_stack_init");
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
594 }
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
595
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
596 void data_stack_deinit(void)
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
597 {
14682
d0d7b810646b Make sure we check all the functions' return values. Minor API changes to simplify this.
Timo Sirainen <tss@iki.fi>
parents: 14446
diff changeset
598 (void)t_pop();
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
599
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
600 if (frame_pos != BLOCK_FRAME_COUNT-1)
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
601 i_panic("Missing t_pop() call");
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
602
1786
49b6103dd2e0 Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents: 1741
diff changeset
603 #ifndef USE_GC
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
604 while (unused_frame_blocks != NULL) {
17638
a5f479be46b9 lib: cosmetic - whitespace cleanup in allocator/memory-related code
Phil Carmody <phil@dovecot.fi>
parents: 17637
diff changeset
605 struct stack_frame_block *frame_block = unused_frame_blocks;
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
606 unused_frame_blocks = unused_frame_blocks->prev;
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
607
1786
49b6103dd2e0 Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents: 1741
diff changeset
608 free(frame_block);
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
609 }
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
610
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
611 free(current_block);
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
612 free(unused_block);
1786
49b6103dd2e0 Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents: 1741
diff changeset
613 #endif
49b6103dd2e0 Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents: 1741
diff changeset
614 unused_frame_blocks = NULL;
49b6103dd2e0 Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents: 1741
diff changeset
615 current_block = NULL;
49b6103dd2e0 Added support for Boehm GC. However it seems to be crashing for some reason
Timo Sirainen <tss@iki.fi>
parents: 1741
diff changeset
616 unused_block = NULL;
399
383503837741 s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
617 }