1 | /* emacs edit mode for this file is -*- C -*- */ |
---|
2 | /* $Id$ */ |
---|
3 | |
---|
4 | #define _POSIX_SOURCE 1 |
---|
5 | |
---|
6 | #include <config.h> |
---|
7 | |
---|
8 | #include <stdio.h> |
---|
9 | #include <stdlib.h> |
---|
10 | #include <string.h> |
---|
11 | |
---|
12 | #include "memman.h" |
---|
13 | #include "mmprivate.h" |
---|
14 | |
---|
15 | #ifdef __alpha__ |
---|
16 | const size_t mm_mcbSizes [MAXLIST] = { 8, 16, 24, 32, |
---|
17 | 40, 48, 56, 64, |
---|
18 | 80, 96, 112, 128, |
---|
19 | 160, 192, 224, 256, |
---|
20 | 512, 1024 }; |
---|
21 | #else |
---|
22 | /*const*/ size_t mm_mcbSizes [MAXLIST] = { 8, 12, 16, 20, 24, 28, 32, |
---|
23 | 40, 48, 56, 64, |
---|
24 | 80, 96, 112, 128, |
---|
25 | 160, 192, 224, 256, |
---|
26 | 512, 1024 }; |
---|
27 | #endif |
---|
28 | |
---|
29 | int |
---|
30 | mmGetIndex( size_t size ) |
---|
31 | { |
---|
32 | int i; |
---|
33 | |
---|
34 | if ( size > mm_mcbSizes[MAXLIST-1] ) |
---|
35 | return -1; |
---|
36 | else { |
---|
37 | for ( i = 0; i < MAXLIST; i++ ) |
---|
38 | if ( size <= mm_mcbSizes[i] ) |
---|
39 | return i; |
---|
40 | } |
---|
41 | return MAXLIST-1; |
---|
42 | } |
---|
43 | |
---|
44 | mcb |
---|
45 | mmNewMCBList( int i ) |
---|
46 | { |
---|
47 | char* blk = (char*)mmGetBlock(); |
---|
48 | char* top; |
---|
49 | mcb dummy, p; |
---|
50 | size_t offset = mm_mcbSizes[i]; |
---|
51 | |
---|
52 | dummy = (mcb)blk; |
---|
53 | p = dummy; |
---|
54 | top = blk + MAXDATA - offset; |
---|
55 | while ( (char*)p < top ) { |
---|
56 | *p = (void*)((char*)p + offset); |
---|
57 | p = (mcb)*p; |
---|
58 | } |
---|
59 | *p = NULL; |
---|
60 | |
---|
61 | return dummy; |
---|
62 | } |
---|
63 | |
---|
64 | #ifdef MDEBUG |
---|
65 | |
---|
66 | void |
---|
67 | mmMoveDBMCB ( pDBMCB from, pDBMCB to, DBMCB * what ) |
---|
68 | { |
---|
69 | what->prev->next = what->next; |
---|
70 | if ( what->next != NULL ) |
---|
71 | what->next->prev = what->prev; |
---|
72 | what->prev = to; |
---|
73 | what->next = to->next; |
---|
74 | if (to->next!=NULL) |
---|
75 | to->next->prev=what; |
---|
76 | to->next = what; |
---|
77 | } |
---|
78 | |
---|
79 | void |
---|
80 | mmMoveDBMCBInto ( pDBMCB to, pDBMCB what ) |
---|
81 | { |
---|
82 | if (to->next !=NULL) { |
---|
83 | what->next = to->next; |
---|
84 | what->next->prev = what; |
---|
85 | } |
---|
86 | to->next = what; |
---|
87 | what->prev = to; |
---|
88 | } |
---|
89 | |
---|
90 | void |
---|
91 | mmTakeOutDBMCB ( pDBMCB from, pDBMCB what ) |
---|
92 | { |
---|
93 | what->prev->next = what->next; |
---|
94 | if ( what->next != NULL ) |
---|
95 | what->next->prev = what->prev; |
---|
96 | } |
---|
97 | |
---|
98 | int |
---|
99 | mmPrintDBMCB ( DBMCB * what, char* msg , int given_size) |
---|
100 | { |
---|
101 | DBMCB * before=NULL; |
---|
102 | (void)fprintf( stderr, "warning: %s\n", msg ); |
---|
103 | (void)fprintf( stderr, "block %x allocated in: %s, line no %d\n", |
---|
104 | (int)what, what->fname, what->lineno ); |
---|
105 | if (strcmp(msg,"size")==0) |
---|
106 | (void)fprintf( stderr, "size is: %d, but free call said %d \n", |
---|
107 | (int)what->size, given_size ); |
---|
108 | else |
---|
109 | (void)fprintf( stderr, "size is: %d\n", (int)what->size ); |
---|
110 | #ifdef MM_BEFORE |
---|
111 | { |
---|
112 | char * queue="used"; |
---|
113 | DBMCB *x=&mm_theDBused; |
---|
114 | while (x!=NULL) { |
---|
115 | if (((unsigned)x<(unsigned)what) && ((unsigned)before<(unsigned)x)) |
---|
116 | before=x; |
---|
117 | x=x->next; |
---|
118 | } |
---|
119 | x=&mm_theDBfree; |
---|
120 | while (x!=NULL) { |
---|
121 | if (((unsigned)x<(unsigned)what) && ((unsigned)before<(unsigned)x)) { |
---|
122 | before=x; |
---|
123 | queue="free"; |
---|
124 | } |
---|
125 | x=x->next; |
---|
126 | } |
---|
127 | x=before; |
---|
128 | fprintf( stderr, "previous (%s queue) at %x to %x, alloc: %s, l: %d, size:%d\n", |
---|
129 | queue,x,(unsigned)x+RealSizeFromSize(x->size),x->fname,x->lineno,x->size); |
---|
130 | } |
---|
131 | #endif |
---|
132 | return 0; |
---|
133 | } |
---|
134 | |
---|
135 | void |
---|
136 | mmTestList ( ) |
---|
137 | { |
---|
138 | DBMCB * what=mm_theDBused.next; |
---|
139 | fprintf(stderr,"list of used blocks:\n"); |
---|
140 | while (what!=NULL) { |
---|
141 | (void)fprintf( stderr, "%d bytes at %p in: %s, line no %d\n", |
---|
142 | (int)what->size, what, what->fname, what->lineno); |
---|
143 | what=what->next; |
---|
144 | } |
---|
145 | /* |
---|
146 | //what=mm_theDBfree.next; |
---|
147 | //fprintf(stderr,"list of free blocks:\n"); |
---|
148 | //while (what!=NULL) |
---|
149 | //{ |
---|
150 | // (void)fprintf( stderr, "%d bytes at %p in: %s, line no %d\n", |
---|
151 | // what->size, what, what->fname, what->lineno ); |
---|
152 | // what=what->next; |
---|
153 | // } |
---|
154 | */ |
---|
155 | } |
---|
156 | |
---|
157 | void |
---|
158 | mmPrintFL( const char* fname, const int lineno ) |
---|
159 | { |
---|
160 | (void)fprintf( stderr, "occured in %s line %d\n", fname, lineno ); |
---|
161 | } |
---|
162 | |
---|
163 | int |
---|
164 | mmCheckDBMCB ( DBMCB * what, int flags, int size ) |
---|
165 | { |
---|
166 | char * patptr; |
---|
167 | int i; |
---|
168 | |
---|
169 | if ( ((int)what % 4 ) != 0 ) { |
---|
170 | (void)fprintf( stderr, "warning: odd address" ); |
---|
171 | return 0; |
---|
172 | } |
---|
173 | if ( (void*)what > mm_maxAddr ) { |
---|
174 | (void)fprintf( stderr, "warning: address too high" ); |
---|
175 | return 0; |
---|
176 | } |
---|
177 | if ( (void*)what < mm_minAddr ) { |
---|
178 | (void)fprintf( stderr, "warning: address too low" ); |
---|
179 | return 0; |
---|
180 | } |
---|
181 | if ( ( what->flags & flags ) != flags ) |
---|
182 | return mmPrintDBMCB( what, "flags", 0 ); |
---|
183 | if ( what->size != size ) |
---|
184 | return mmPrintDBMCB( what, "size", size ); |
---|
185 | if ( what->pattern != PATTERN ) |
---|
186 | return mmPrintDBMCB( what, "front pattern", 0 ); |
---|
187 | patptr = ((char*)&what->data) + size; |
---|
188 | for ( i = 4; i > 0; i-- ) |
---|
189 | if ( *(patptr++) != PATTERN2 ) return mmPrintDBMCB( what, "back pattern", 0 ); |
---|
190 | if ( ( what->next != NULL ) && ( what->next->prev != what ) ) |
---|
191 | return mmPrintDBMCB( what, "chain:n", 0 ); |
---|
192 | if ( ( what->prev != NULL ) && ( what->prev->next != what ) ) { |
---|
193 | mmPrintDBMCB( what->prev, "prev", 0 ); |
---|
194 | return mmPrintDBMCB( what, "chain:p", 0 ); |
---|
195 | } |
---|
196 | return 1; |
---|
197 | } |
---|
198 | |
---|
199 | int |
---|
200 | mmDBTestBlock( const void* adr, const size_t size, const char * fname, const int lineno ) |
---|
201 | { |
---|
202 | DBMCB * what; |
---|
203 | |
---|
204 | if ( ( adr == NULL ) || ( size == 0 ) ) |
---|
205 | return 1; /*TRUE*/ |
---|
206 | |
---|
207 | what = (DBMCB*)((char*)(adr) - DebugOffsetFront); |
---|
208 | if ( mm_status == MM_TMP ) { |
---|
209 | if ( ! mmCheckDBMCB( what, MM_TMPFLAG | MM_USEDFLAG, size ) ) { |
---|
210 | mmPrintFL( fname, lineno ); |
---|
211 | return 0; /*FALSE*/ |
---|
212 | } |
---|
213 | } |
---|
214 | else { |
---|
215 | if ( ! mmCheckDBMCB( what, MM_NORMFLAG | MM_USEDFLAG, size ) ) { |
---|
216 | mmPrintFL( fname, lineno ); |
---|
217 | return 0; /*FALSE*/ |
---|
218 | } |
---|
219 | } |
---|
220 | return 1; /*TRUE*/ |
---|
221 | } |
---|
222 | |
---|
223 | int |
---|
224 | mmDBTest( const void* adr, const char * fname, const int lineno ) |
---|
225 | { |
---|
226 | if (adr!=NULL) { |
---|
227 | size_t l; |
---|
228 | adr = (size_t*)adr-1; |
---|
229 | l= (adr<=mm_maxAddr) ? (*(size_t*)((int)adr&(~3))) :0; |
---|
230 | return mmDBTestBlock( adr,l, fname, lineno ); |
---|
231 | } |
---|
232 | return 1; /*TRUE*/ |
---|
233 | } |
---|
234 | |
---|
235 | #endif /* MDEBUG */ |
---|