Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/lib/data-stack.c @ 1329:ae229b7acb4c HEAD
Mailbox names are now sent through imap-quoter instead of just escaping it.
This means that mailbox names that would require escapes are instead sent
as literals now.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Wed, 02 Apr 2003 05:05:38 +0300 |
parents | e9e08440df77 |
children | 9df02b1533b3 |
rev | line source |
---|---|
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1 /* |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
2 data-stack.c : Data stack implementation |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
3 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
4 Copyright (c) 2001-2002 Timo Sirainen |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
5 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
6 Permission is hereby granted, free of charge, to any person obtaining |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
7 a copy of this software and associated documentation files (the |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
8 "Software"), to deal in the Software without restriction, including |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
9 without limitation the rights to use, copy, modify, merge, publish, |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
10 distribute, sublicense, and/or sell copies of the Software, and to |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
11 permit persons to whom the Software is furnished to do so, subject to |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
12 the following conditions: |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
13 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
14 The above copyright notice and this permission notice shall be |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
15 included in all copies or substantial portions of the Software. |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
16 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
18 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
20 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
21 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
22 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
23 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
24 */ |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
25 |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
798
diff
changeset
|
26 /* @UNSAFE: whole file */ |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
798
diff
changeset
|
27 |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
28 #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
|
29 #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
|
30 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
31 #include <stdlib.h> |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
32 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
33 /* Use malloc() and free() for all memory allocations. Useful for debugging |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
34 memory corruption. */ |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
35 /* #define DISABLE_DATA_STACK */ |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
36 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
37 #ifndef DISABLE_DATA_STACK |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
38 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
39 /* 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
|
40 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
|
41 #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
|
42 # 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
|
43 #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
|
44 # 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
|
45 #endif |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
46 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
47 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
|
48 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
|
49 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
50 size_t size, left; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
51 /* 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
|
52 }; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
53 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
54 #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
|
55 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
56 #define STACK_BLOCK_DATA(block) \ |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
57 ((char *) (block) + SIZEOF_MEMBLOCK) |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
58 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
59 /* 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
|
60 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
|
61 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
|
62 #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
|
63 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
64 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
|
65 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
|
66 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
67 struct stack_block *block[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
|
68 size_t block_space_used[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
|
69 size_t last_alloc_size[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
|
70 }; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
71 |
1032
ea309e90c01a
Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
72 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
|
73 |
1032
ea309e90c01a
Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
74 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
|
75 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
|
76 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
|
77 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
78 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
|
79 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
|
80 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
81 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
|
82 static size_t 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
|
83 |
536
37893ed97492
changed t_push() and t_pop() to return unsigned int. added global
Timo Sirainen <tss@iki.fi>
parents:
410
diff
changeset
|
84 unsigned int t_push(void) |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
85 { |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
86 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
|
87 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
88 frame_pos++; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
89 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
|
90 /* frame block full */ |
1032
ea309e90c01a
Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
91 if (data_stack_frame == 0) { |
ea309e90c01a
Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
92 /* kludgy, but allow this before initialization */ |
ea309e90c01a
Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
93 frame_pos = 0; |
ea309e90c01a
Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
94 data_stack_init(); |
ea309e90c01a
Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
95 return t_push(); |
ea309e90c01a
Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
96 } |
ea309e90c01a
Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
97 |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
98 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
|
99 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
|
100 /* allocate new 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
|
101 frame_block = calloc(sizeof(*frame_block), 1); |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
102 if (frame_block == NULL) |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
103 i_panic("t_push(): Out of memory"); |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
104 } else { |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
105 /* 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
|
106 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
|
107 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
|
108 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
109 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
110 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
|
111 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
|
112 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
113 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
114 /* 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
|
115 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
|
116 current_frame_block->block_space_used[frame_pos] = current_block->left; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
117 current_frame_block->last_alloc_size[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
|
118 |
536
37893ed97492
changed t_push() and t_pop() to return unsigned int. added global
Timo Sirainen <tss@iki.fi>
parents:
410
diff
changeset
|
119 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
|
120 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
121 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
122 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
|
123 { |
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
|
124 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
|
125 |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
126 /* 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
|
127 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
|
128 while (block != NULL) { |
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
|
129 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
|
130 |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
131 if (unused_block == NULL || block->size > unused_block->size) { |
751
04e2c5774c3f
DEBUG: Don't send shrink messages, they could cause infinite loop.
Timo Sirainen <tss@iki.fi>
parents:
734
diff
changeset
|
132 free(unused_block); |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
133 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
|
134 } else { |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
135 free(block); |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
136 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
137 |
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
|
138 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
|
139 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
140 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
141 |
536
37893ed97492
changed t_push() and t_pop() to return unsigned int. added global
Timo Sirainen <tss@iki.fi>
parents:
410
diff
changeset
|
142 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
|
143 { |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
144 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
|
145 int popped_frame_pos; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
146 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
147 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
|
148 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
|
149 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
150 /* 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
|
151 current_block = current_frame_block->block[frame_pos]; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
152 current_block->left = current_frame_block->block_space_used[frame_pos]; |
548
797c5eb84267
DEBUG: when t_pop()ing, fill the free'd memory area with 0xde bytes.
Timo Sirainen <tss@iki.fi>
parents:
536
diff
changeset
|
153 #ifdef DEBUG |
797c5eb84267
DEBUG: when t_pop()ing, fill the free'd memory area with 0xde bytes.
Timo Sirainen <tss@iki.fi>
parents:
536
diff
changeset
|
154 memset(STACK_BLOCK_DATA(current_block) + |
797c5eb84267
DEBUG: when t_pop()ing, fill the free'd memory area with 0xde bytes.
Timo Sirainen <tss@iki.fi>
parents:
536
diff
changeset
|
155 (current_block->size - current_block->left), 0xde, |
797c5eb84267
DEBUG: when t_pop()ing, fill the free'd memory area with 0xde bytes.
Timo Sirainen <tss@iki.fi>
parents:
536
diff
changeset
|
156 current_block->left); |
797c5eb84267
DEBUG: when t_pop()ing, fill the free'd memory area with 0xde bytes.
Timo Sirainen <tss@iki.fi>
parents:
536
diff
changeset
|
157 #endif |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
158 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
|
159 /* 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
|
160 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
|
161 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
|
162 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
163 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
164 popped_frame_pos = frame_pos; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
165 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
|
166 frame_pos--; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
167 else { |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
168 /* 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
|
169 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
|
170 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
171 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
|
172 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
|
173 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
174 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
|
175 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
|
176 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
177 |
536
37893ed97492
changed t_push() and t_pop() to return unsigned int. added global
Timo Sirainen <tss@iki.fi>
parents:
410
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 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
181 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
|
182 { |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
183 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
|
184 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
|
185 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
186 prev_size = current_block == NULL ? 0 : current_block->size; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
187 alloc_size = nearest_power(prev_size + min_size); |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
188 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
189 block = malloc(SIZEOF_MEMBLOCK + alloc_size); |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
190 if (block == NULL) { |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
191 i_panic("mem_block_alloc(): " |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
192 "Out of memory when allocating %"PRIuSIZE_T" bytes", |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
193 alloc_size + SIZEOF_MEMBLOCK); |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
194 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
195 block->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
|
196 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
|
197 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
198 return block; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
199 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
200 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
201 static void *t_malloc_real(size_t size, int permanent) |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
202 { |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
203 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
|
204 void *ret; |
837
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
205 #ifdef DEBUG |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
206 int warn = FALSE; |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
207 #endif |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
208 |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
209 if (size == 0 || size > SSIZE_T_MAX) |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
210 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
|
211 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
212 /* reset t_buffer_get() mark - not really needed but makes it easier |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
213 to notice if t_malloc() is called between t_buffer_get() and |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
214 t_buffer_alloc() */ |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
215 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
|
216 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
217 /* 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
|
218 always properly */ |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
219 size = MEM_ALIGN(size); |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
220 |
402 | 221 /* used for t_try_realloc() */ |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
222 current_frame_block->last_alloc_size[frame_pos] = size; |
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 if (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
|
225 /* enough space in current block, use it */ |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
226 ret = STACK_BLOCK_DATA(current_block) + |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
227 (current_block->size - current_block->left); |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
228 if (permanent) |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
229 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
|
230 return ret; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
231 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
232 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
233 /* current block is full, see if we can use the unused_block */ |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
234 if (unused_block != NULL && unused_block->size >= size) { |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
235 block = unused_block; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
236 unused_block = NULL; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
237 } else { |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
238 block = mem_block_alloc(size); |
728
883cda17d175
DEBUG: we get warnings when growing/shrinking data stack.
Timo Sirainen <tss@iki.fi>
parents:
548
diff
changeset
|
239 #ifdef DEBUG |
837
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
240 warn = TRUE; |
728
883cda17d175
DEBUG: we get warnings when growing/shrinking data stack.
Timo Sirainen <tss@iki.fi>
parents:
548
diff
changeset
|
241 #endif |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
242 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
243 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
244 block->left = block->size; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
245 if (permanent) |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
246 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
|
247 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
|
248 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
249 current_block->next = block; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
250 current_block = block; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
251 |
837
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
252 ret = STACK_BLOCK_DATA(current_block); |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
253 #ifdef DEBUG |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
254 if (warn) { |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
255 /* warn later, so that if i_warning() wants to allocate more |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
256 memory we don't go to infinite loop */ |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
257 i_warning("Growing data stack with: %"PRIuSIZE_T, block->size); |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
258 } |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
259 #endif |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
260 |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
261 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
|
262 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
263 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
264 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
|
265 { |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
266 return 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
|
267 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
268 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
269 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
|
270 { |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
271 void *mem; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
272 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
273 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
|
274 memset(mem, 0, size); |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
275 return mem; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
276 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
277 |
402 | 278 int 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
|
279 { |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
280 size_t last_alloc_size; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
281 |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
282 if (size == 0 || size > SSIZE_T_MAX) |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
283 i_panic("Trying to allocate %"PRIuSIZE_T" bytes", size); |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
284 |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
285 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
|
286 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
287 /* see if we're trying to grow the memory we allocated last */ |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
288 if (STACK_BLOCK_DATA(current_block) + |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
289 (current_block->size - current_block->left - |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
290 last_alloc_size) == mem) { |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
291 /* yeah, see if we have space to grow */ |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
292 size = MEM_ALIGN(size); |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
293 if (current_block->left >= size - last_alloc_size) { |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
294 /* just shrink the available size */ |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
295 current_block->left -= size - last_alloc_size; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
296 current_frame_block->last_alloc_size[frame_pos] = size; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
297 return TRUE; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
298 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
299 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
300 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
301 return FALSE; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
302 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
303 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
304 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
|
305 { |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
306 void *ret; |
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 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
|
309 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
310 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
|
311 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
|
312 return ret; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
313 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
314 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
315 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
|
316 { |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
317 size_t old_size; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
318 void *new_buffer; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
319 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
320 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
|
321 if (size <= old_size) |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
322 return buffer; |
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 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
|
325 if (new_buffer != buffer) |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
326 memcpy(new_buffer, buffer, old_size); |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
327 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
328 return new_buffer; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
329 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
330 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
331 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
|
332 { |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
333 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
|
334 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
|
335 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
|
336 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
337 /* we've already reserved the space, now we just mark it used */ |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
338 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
|
339 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
340 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
341 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
|
342 { |
1032
ea309e90c01a
Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
343 if (data_stack_frame == 0) { |
ea309e90c01a
Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
344 data_stack_frame = 1; |
ea309e90c01a
Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
345 |
ea309e90c01a
Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
346 current_block = mem_block_alloc(INITIAL_STACK_SIZE); |
ea309e90c01a
Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
347 current_block->left = current_block->size; |
ea309e90c01a
Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
348 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
|
349 |
1032
ea309e90c01a
Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
350 current_frame_block = NULL; |
ea309e90c01a
Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
351 unused_frame_blocks = NULL; |
ea309e90c01a
Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
352 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
|
353 |
1032
ea309e90c01a
Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
354 last_buffer_block = NULL; |
ea309e90c01a
Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
355 last_buffer_size = 0; |
ea309e90c01a
Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
356 } |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
357 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
358 t_push(); |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
359 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
360 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
361 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
|
362 { |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
363 t_pop(); |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
364 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
365 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
|
366 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
|
367 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
368 while (unused_frame_blocks != NULL) { |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
369 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
|
370 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
|
371 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
372 free(frame_block); |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
373 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
374 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
375 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
|
376 free(unused_block); |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
377 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
378 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
379 #else |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
380 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
381 struct stack_frame { |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
382 struct stack_frame *next; |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
383 struct frame_alloc *allocs; |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
384 }; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
385 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
386 struct frame_alloc { |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
387 struct frame_alloc *next; |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
388 void *mem; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
389 }; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
390 |
841
e3a979b1b55d
t_try_realloc() wasn't working right with DISABLE_DATA_STACK.
Timo Sirainen <tss@iki.fi>
parents:
839
diff
changeset
|
391 unsigned int data_stack_frame; |
e3a979b1b55d
t_try_realloc() wasn't working right with DISABLE_DATA_STACK.
Timo Sirainen <tss@iki.fi>
parents:
839
diff
changeset
|
392 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
393 static struct stack_frame *current_frame; |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
394 static void *buffer_mem; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
395 |
536
37893ed97492
changed t_push() and t_pop() to return unsigned int. added global
Timo Sirainen <tss@iki.fi>
parents:
410
diff
changeset
|
396 unsigned int t_push(void) |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
397 { |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
398 struct stack_frame *frame; |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
399 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
400 frame = malloc(sizeof(struct stack_frame)); |
410
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
402
diff
changeset
|
401 if (frame == NULL) |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
402
diff
changeset
|
402 i_panic("t_push(): Out of memory"); |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
403 frame->allocs = NULL; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
404 |
402 | 405 frame->next = current_frame; |
406 current_frame = frame; | |
536
37893ed97492
changed t_push() and t_pop() to return unsigned int. added global
Timo Sirainen <tss@iki.fi>
parents:
410
diff
changeset
|
407 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
|
408 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
409 |
536
37893ed97492
changed t_push() and t_pop() to return unsigned int. added global
Timo Sirainen <tss@iki.fi>
parents:
410
diff
changeset
|
410 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
|
411 { |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
412 struct stack_frame *frame; |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
413 struct frame_alloc *alloc; |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
414 |
402 | 415 frame = current_frame; |
416 current_frame = frame->next; | |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
417 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
418 while (frame->allocs != NULL) { |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
419 alloc = frame->allocs; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
420 frame->allocs = alloc->next; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
421 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
422 free(alloc->mem); |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
423 free(alloc); |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
424 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
425 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
426 free(frame); |
536
37893ed97492
changed t_push() and t_pop() to return unsigned int. added global
Timo Sirainen <tss@iki.fi>
parents:
410
diff
changeset
|
427 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
|
428 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
429 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
430 static void add_alloc(void *mem) |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
431 { |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
432 struct frame_alloc *alloc; |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
433 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
857
diff
changeset
|
434 alloc = malloc(sizeof(struct frame_alloc)); |
410
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
402
diff
changeset
|
435 if (alloc == NULL) |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
402
diff
changeset
|
436 i_panic("add_alloc(): Out of memory"); |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
437 alloc->mem = mem; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
438 alloc->next = current_frame->allocs; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
439 current_frame->allocs = alloc; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
440 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
441 if (buffer_mem != NULL) { |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
442 free(buffer_mem); |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
443 buffer_mem = NULL; |
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 } |
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 *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
|
448 { |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
449 void *mem; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
450 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
451 mem = malloc(size); |
410
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
402
diff
changeset
|
452 if (mem == NULL) |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
402
diff
changeset
|
453 i_panic("t_malloc(): Out of memory"); |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
454 add_alloc(mem); |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
455 return mem; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
456 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
457 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
458 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
|
459 { |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
460 void *mem; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
461 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
462 mem = calloc(size, 1); |
410
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
402
diff
changeset
|
463 if (mem == NULL) |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
402
diff
changeset
|
464 i_panic("t_malloc0(): Out of memory"); |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
465 add_alloc(mem); |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
466 return mem; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
467 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
468 |
841
e3a979b1b55d
t_try_realloc() wasn't working right with DISABLE_DATA_STACK.
Timo Sirainen <tss@iki.fi>
parents:
839
diff
changeset
|
469 int t_try_realloc(void *mem __attr_unused__, size_t size __attr_unused__) |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
470 { |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
471 return FALSE; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
472 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
473 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
474 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
|
475 { |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
476 buffer_mem = realloc(buffer_mem, size); |
410
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
402
diff
changeset
|
477 if (buffer_mem == NULL) |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
402
diff
changeset
|
478 i_panic("t_buffer_get(): Out of memory"); |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
479 return buffer_mem; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
480 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
481 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
482 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
|
483 { |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
484 i_assert(buffer == buffer_mem); |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
485 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
486 buffer_mem = realloc(buffer_mem, size); |
410
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
402
diff
changeset
|
487 if (buffer_mem == NULL) |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
402
diff
changeset
|
488 i_panic("t_buffer_reget(): Out of memory"); |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
489 return buffer_mem; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
490 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
491 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
492 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
|
493 { |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
494 void *mem; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
495 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
496 i_assert(buffer_mem != NULL); |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
497 |
402 | 498 mem = realloc(buffer_mem, size); |
410
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
402
diff
changeset
|
499 if (mem == NULL) |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
402
diff
changeset
|
500 i_panic("t_buffer_alloc(): Out of memory"); |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
501 buffer_mem = NULL; |
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 add_alloc(mem); |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
504 } |
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 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
|
507 { |
1032
ea309e90c01a
Allow calling t_push() before data_stack_init().
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
508 data_stack_frame = 0; |
402 | 509 current_frame = NULL; |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
510 buffer_mem = NULL; |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
511 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
512 t_push(); |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
513 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
514 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
515 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
|
516 { |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
517 t_pop(); |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
518 |
536
37893ed97492
changed t_push() and t_pop() to return unsigned int. added global
Timo Sirainen <tss@iki.fi>
parents:
410
diff
changeset
|
519 if (data_stack_frame != 0) |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
520 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
|
521 } |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
522 |
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
523 #endif |