1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: mmisc.c,v 1.10 1999-09-20 18:03:50 obachman Exp $ */ |
---|
5 | |
---|
6 | /* |
---|
7 | * ABSTRACT: |
---|
8 | */ |
---|
9 | |
---|
10 | #include "mod2.h" |
---|
11 | #include "mmemory.h" |
---|
12 | #include "mmprivate.h" |
---|
13 | #include "mmpage.h" |
---|
14 | #include "febase.h" |
---|
15 | #include "distrib.h" |
---|
16 | |
---|
17 | #ifdef MTRACK |
---|
18 | #include "mmbt.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | static int mm_specIndex = 0; |
---|
22 | static int mm_specSize = 0; |
---|
23 | |
---|
24 | size_t mmSpecializeBlock( size_t size ) |
---|
25 | { |
---|
26 | if (mm_specIndex != mmGetIndex( size )) |
---|
27 | { |
---|
28 | int mm_new_specIndex = mmGetIndex( size ); |
---|
29 | if (mm_new_specIndex<0) |
---|
30 | { |
---|
31 | WerrorS("too many ring variables..."); |
---|
32 | } |
---|
33 | else |
---|
34 | { |
---|
35 | mm_specIndex = mm_new_specIndex; |
---|
36 | mm_specHeap = &(mm_theList[mm_specIndex]); |
---|
37 | } |
---|
38 | mm_specSize = mmGetSize(mm_specIndex); |
---|
39 | } |
---|
40 | return mm_specSize; |
---|
41 | } |
---|
42 | |
---|
43 | size_t mmGetSpecSize() |
---|
44 | { |
---|
45 | return mm_specSize; |
---|
46 | } |
---|
47 | |
---|
48 | |
---|
49 | size_t mmSizeL( void* adr ) |
---|
50 | { |
---|
51 | if (adr!=NULL) |
---|
52 | { |
---|
53 | adr = (size_t*)adr-1; |
---|
54 | return *(size_t*)adr; |
---|
55 | } |
---|
56 | return 0; |
---|
57 | } |
---|
58 | |
---|
59 | int mmMemAlloc( void ) |
---|
60 | { |
---|
61 | return mm_bytesMalloc + mm_bytesValloc; |
---|
62 | } |
---|
63 | int mmMemUsed( void ) |
---|
64 | { |
---|
65 | int bytesfree = 0; |
---|
66 | int i; |
---|
67 | |
---|
68 | for (i=0; mmGetSize(i) < MAX_BLOCK_SIZE; i++) |
---|
69 | bytesfree += mmListLength(mm_theList[i].current)*mm_theList[i].size; |
---|
70 | return |
---|
71 | mm_bytesMalloc + mm_bytesValloc |
---|
72 | - (bytesfree + mmListLength(mm_theList[i].current)*mm_theList[i].size); |
---|
73 | } |
---|
74 | |
---|
75 | #ifdef HAVE_SBRK |
---|
76 | #include <unistd.h> |
---|
77 | int mmMemPhysical( void ) |
---|
78 | { |
---|
79 | unsigned long ns = (unsigned long) sbrk(0); |
---|
80 | return (ns >= mm_SbrkInit ? ns - mm_SbrkInit : mm_SbrkInit - ns); |
---|
81 | } |
---|
82 | #endif |
---|
83 | |
---|
84 | |
---|
85 | int mm_bytesMalloc=0; |
---|
86 | static int mm_printMark=102400; |
---|
87 | |
---|
88 | #ifndef ABS |
---|
89 | #define ABS(x) ((x)<0?(-(x)):(x)) |
---|
90 | #endif |
---|
91 | |
---|
92 | void mmCheckPrint( void ) |
---|
93 | { |
---|
94 | int mm_bytesAlloc = mm_bytesValloc + mm_bytesMalloc; |
---|
95 | |
---|
96 | if ( ABS(mm_bytesAlloc - mm_printMark)>(100*1024) ) |
---|
97 | { |
---|
98 | int i=(mm_bytesAlloc+1023)/1024; |
---|
99 | fprintf( stdout, "[%dk]", i); |
---|
100 | fflush( stdout ); |
---|
101 | mm_printMark=mm_bytesAlloc; |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | void mmPrintStat() |
---|
106 | { |
---|
107 | #ifndef MAKE_DISTRIBUTION |
---|
108 | int i; |
---|
109 | long l,a; |
---|
110 | |
---|
111 | #ifdef HAVE_SBRK |
---|
112 | printf("Physical:%dk ", (mmMemPhysical()+ 1023)/1024); |
---|
113 | #endif |
---|
114 | printf("Alloc:%dk ", (mmMemAlloc() + 1023)/1024); |
---|
115 | printf("Used:%dk ", (mmMemUsed()+ 1023)/1024); |
---|
116 | printf("Malloc:%dk ", (mm_bytesMalloc+ 1023)/1024); |
---|
117 | printf("Valloc:%dk ", (mm_bytesValloc+ 1023)/1024); |
---|
118 | printf("Palloc:%d ", mmGetNumberOfAllocatedPages()); |
---|
119 | printf("Pused:%d ", mmGetNumberOfUsedPages()); |
---|
120 | printf("\n"); |
---|
121 | |
---|
122 | i=-1; |
---|
123 | printf("Heap\tSize\tPages\tUsage\tFree\tUsed\n"); |
---|
124 | do |
---|
125 | { |
---|
126 | i++; |
---|
127 | l = mmListLength(mm_theList[i].pages); |
---|
128 | a = mmListLength(mm_theList[i].current); |
---|
129 | printf("%d\t%ld\t%ld\t%d\t%ld\t%ld\n", |
---|
130 | i, mmGetHeapBlockSize(&mm_theList[i]), l, |
---|
131 | (l != 0 ? ((int) ((1.0 - |
---|
132 | ((double) a) |
---|
133 | / |
---|
134 | ((double) l*(SIZE_OF_PAGE/mmGetHeapBlockSize(&mm_theList[i]))) |
---|
135 | )*100.0)) |
---|
136 | : 0), |
---|
137 | a, |
---|
138 | l*(SIZE_OF_PAGE/mmGetHeapBlockSize(&mm_theList[i])) - a); |
---|
139 | } |
---|
140 | while (mmGetSize(i) < MAX_BLOCK_SIZE); |
---|
141 | #endif |
---|
142 | } |
---|
143 | |
---|
144 | #ifdef MLIST |
---|
145 | void mmTestList (int all) |
---|
146 | { |
---|
147 | DBMCB * what=mm_theDBused.next; |
---|
148 | fprintf(stderr,"list of used blocks:\n"); |
---|
149 | while (what!=NULL) |
---|
150 | { |
---|
151 | if ((all & MM_PRINT_ALL_ADDR) || what->init == 0) |
---|
152 | { |
---|
153 | fprintf( stderr, "%d bytes at %p in: %s:%d", |
---|
154 | (int)what->size, what, what->fname, what->lineno); |
---|
155 | #ifdef MTRACK |
---|
156 | mmDBPrintStack(what, all); |
---|
157 | #else |
---|
158 | fprintf( stderr, "\n"); |
---|
159 | #endif |
---|
160 | } |
---|
161 | what=what->next; |
---|
162 | } |
---|
163 | } |
---|
164 | #endif |
---|
165 | |
---|
166 | |
---|
167 | /********************************************************************** |
---|
168 | * |
---|
169 | * Some operations on linked lists of memory |
---|
170 | * |
---|
171 | **********************************************************************/ |
---|
172 | |
---|
173 | void* mmRemoveFromList(void* list, void* element) |
---|
174 | { |
---|
175 | void* nlist; |
---|
176 | void* olist; |
---|
177 | |
---|
178 | if (list == NULL) return NULL; |
---|
179 | |
---|
180 | nlist = *((void**) list); |
---|
181 | olist = list; |
---|
182 | |
---|
183 | if (list == element) return nlist; |
---|
184 | |
---|
185 | while (nlist != NULL && nlist != element) |
---|
186 | { |
---|
187 | list = nlist; |
---|
188 | nlist = *((void**) list); |
---|
189 | } |
---|
190 | |
---|
191 | if (nlist != NULL) *((void**) list) = *((void**) nlist); |
---|
192 | |
---|
193 | return olist; |
---|
194 | } |
---|
195 | |
---|
196 | int mmListLength(void* list) |
---|
197 | { |
---|
198 | int l = 0; |
---|
199 | while (list != NULL) |
---|
200 | { |
---|
201 | l++; |
---|
202 | list = *((void**) list); |
---|
203 | } |
---|
204 | return l; |
---|
205 | } |
---|
206 | |
---|
207 | void* mmListLast(void* memList) |
---|
208 | { |
---|
209 | if (memList == NULL) return NULL; |
---|
210 | |
---|
211 | while (*((void**) memList) != NULL) |
---|
212 | memList = *((void**) memList); |
---|
213 | |
---|
214 | return memList; |
---|
215 | } |
---|
216 | |
---|
217 | int mmIsAddrOnList(void* addr, void* list) |
---|
218 | { |
---|
219 | if (addr == NULL) |
---|
220 | return (list == NULL); |
---|
221 | |
---|
222 | while (list != NULL) |
---|
223 | { |
---|
224 | if (addr == list) return 1; |
---|
225 | list = *((void**) list); |
---|
226 | } |
---|
227 | return 0; |
---|
228 | } |
---|
229 | |
---|
230 | void* mmListHasCycle(void* list) |
---|
231 | { |
---|
232 | void* l1 = list; |
---|
233 | void* l2; |
---|
234 | |
---|
235 | int l = 0, i; |
---|
236 | |
---|
237 | while (l1 != NULL) |
---|
238 | { |
---|
239 | i = 0; |
---|
240 | l2 = list; |
---|
241 | while (l1 != l2) |
---|
242 | { |
---|
243 | i++; |
---|
244 | l2 = *((void**) l2); |
---|
245 | } |
---|
246 | if (i != l) return l1; |
---|
247 | l1 = *((void**) l1); |
---|
248 | l++; |
---|
249 | } |
---|
250 | return NULL; |
---|
251 | } |
---|
252 | |
---|
253 | |
---|
254 | int mmGListLength(void* list, int next) |
---|
255 | { |
---|
256 | int l = 0; |
---|
257 | while (list != NULL) |
---|
258 | { |
---|
259 | l++; |
---|
260 | list = *((void**) list + next); |
---|
261 | } |
---|
262 | return l; |
---|
263 | } |
---|
264 | |
---|
265 | void* mmGListLast(void* memList, int next) |
---|
266 | { |
---|
267 | if (memList == NULL) return NULL; |
---|
268 | |
---|
269 | while (*((void**) memList) != NULL) |
---|
270 | memList = *((void**) memList + next); |
---|
271 | |
---|
272 | return memList; |
---|
273 | } |
---|
274 | |
---|
275 | int mmIsAddrOnGList(void* addr, void* list, int next) |
---|
276 | { |
---|
277 | if (addr == NULL) |
---|
278 | return (list == NULL); |
---|
279 | |
---|
280 | while (list != NULL) |
---|
281 | { |
---|
282 | if (addr == list) return 1; |
---|
283 | list = *((void**) list + next); |
---|
284 | } |
---|
285 | return 0; |
---|
286 | } |
---|
287 | |
---|
288 | void* mmGListHasCycle(void* list, int next) |
---|
289 | { |
---|
290 | void* l1 = list; |
---|
291 | void* l2; |
---|
292 | |
---|
293 | int l = 0, i; |
---|
294 | |
---|
295 | while (l1 != NULL) |
---|
296 | { |
---|
297 | i = 0; |
---|
298 | l2 = list; |
---|
299 | while (l1 != l2) |
---|
300 | { |
---|
301 | i++; |
---|
302 | l2 = *((void**) (l2 + next)); |
---|
303 | } |
---|
304 | if (i != l) return l1; |
---|
305 | l1 = *((void**) (l1 + next)); |
---|
306 | l++; |
---|
307 | } |
---|
308 | return NULL; |
---|
309 | } |
---|
310 | |
---|
311 | /********************************************************************** |
---|
312 | * |
---|
313 | * Operations for deep profiles |
---|
314 | * |
---|
315 | **********************************************************************/ |
---|
316 | |
---|
317 | #ifdef DO_DEEP_PROFILE |
---|
318 | void _memcpyW(long* p1, long* p2, long l) |
---|
319 | { |
---|
320 | assume(l >= 0); |
---|
321 | |
---|
322 | while(l) |
---|
323 | { |
---|
324 | *p1++ = *p2++; |
---|
325 | l--; |
---|
326 | } |
---|
327 | } |
---|
328 | |
---|
329 | void _memaddW(long* p1, long* p2, long* p3, long l) |
---|
330 | { |
---|
331 | assume(l >= 0); |
---|
332 | |
---|
333 | while (l) |
---|
334 | { |
---|
335 | *p1++ = *p2++ + *p3++; |
---|
336 | l--; |
---|
337 | } |
---|
338 | } |
---|
339 | |
---|
340 | void _memsetW(long* p1, long w, long l) |
---|
341 | { |
---|
342 | assume(l >= 0); |
---|
343 | |
---|
344 | while (l) |
---|
345 | { |
---|
346 | *p1++ = w; |
---|
347 | l--; |
---|
348 | } |
---|
349 | } |
---|
350 | |
---|
351 | #endif /* DO_DEEP_PROFILE */ |
---|
352 | |
---|
353 | #ifdef sun |
---|
354 | #ifdef __svr4__ |
---|
355 | void bzero(char *s,int n) |
---|
356 | { |
---|
357 | memset(s,0,n); |
---|
358 | } |
---|
359 | #endif |
---|
360 | #endif |
---|