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