source: git/Singular/mmisc.c @ 105efec

spielwiese
Last change on this file since 105efec was 105efec, checked in by Hans Schönemann <hannes@…>, 25 years ago
* hannes: MTRACK fixes git-svn-id: file:///usr/local/Singular/svn/trunk@2962 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 6.2 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: mmisc.c,v 1.4 1999-03-19 16:00:07 Singular 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
19static int mm_specIndex = 0;
20static int mm_specSize = 0;
21
22size_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
41size_t mmGetSpecSize()
42{
43  return mm_specSize;
44}
45
46
47size_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
57int mmMemAlloc( void )
58{
59  return mm_bytesMalloc + mm_bytesValloc;
60}
61int 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>
75int 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
83int mm_bytesMalloc=0;
84static int mm_printMark=102400;
85
86#ifndef ABS
87#define ABS(x) ((x)<0?(-(x)):(x))
88#endif
89
90void 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
104void 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
142void mmTestList ( )
143{
144  DBMCB * what=mm_theDBused.next;
145  fprintf(stderr,"list of used blocks:\n");
146  while (what!=NULL)
147  {
148    fprintf( stderr, "%d bytes at %p in: %s:%d",
149      (int)what->size, what, what->fname, what->lineno);
150#ifdef MTRACK
151    mmPrintStack(what->bt_stack);
152#else
153    fprintf( stderr, "\n");
154#endif
155    what=what->next;
156  }
157}
158#endif
159
160
161/**********************************************************************
162 *
163 * Some operations on linked lists of memory
164 *
165 **********************************************************************/
166
167void* mmRemoveFromList(void* list, void* element)
168{
169  void* nlist;
170  void* olist;
171 
172  if (list == NULL) return NULL;
173
174  nlist = *((void**) list);
175  olist = list;
176 
177  if (list == element) return nlist;
178 
179  while (nlist != NULL && nlist != element)
180  {
181    list = nlist;
182    nlist = *((void**) list);
183  }
184 
185  if (nlist != NULL) *((void**) list) = *((void**) nlist);
186 
187  return olist;
188}
189
190int mmListLength(void* list)
191{
192  int l = 0;
193  while (list != NULL)
194  {
195    l++;
196    list = *((void**) list);
197  }
198  return l;
199}
200
201void* mmListLast(void* memList)
202{
203  if (memList == NULL) return NULL;
204
205  while (*((void**) memList) != NULL)
206    memList = *((void**) memList);
207
208  return memList;
209}
210
211int mmIsAddrOnList(void* addr, void* list)
212{
213  if (addr == NULL)
214    return (list == NULL);
215 
216  while (list != NULL)
217  {
218    if (addr == list) return 1;
219    list = *((void**) list);
220  }
221  return 0;
222}
223
224void* mmListHasCycle(void* list)
225{
226  void* l1 = list;
227  void* l2;
228 
229  int l = 0, i;
230
231  while (l1 != NULL)
232  {
233    i = 0;
234    l2 = list;
235    while (l1 != l2)
236    {
237      i++;
238      l2 = *((void**) l2);
239    }
240    if (i != l) return l1;
241    l1 = *((void**) l1);
242    l++;
243  }
244  return NULL;
245}
246   
247
248int mmGListLength(void* list, int next)
249{
250  int l = 0;
251  while (list != NULL)
252  {
253    l++;
254    list = *((void**) list + next);
255  }
256  return l;
257}
258
259void* mmGListLast(void* memList, int next)
260{
261  if (memList == NULL) return NULL;
262
263  while (*((void**) memList) != NULL)
264    memList = *((void**) memList + next);
265
266  return memList;
267}
268
269int mmIsAddrOnGList(void* addr, void* list, int next)
270{
271  if (addr == NULL)
272    return (list == NULL);
273 
274  while (list != NULL)
275  {
276    if (addr == list) return 1;
277    list = *((void**) list + next);
278  }
279  return 0;
280}
281
282void* mmGListHasCycle(void* list, int next)
283{
284  void* l1 = list;
285  void* l2;
286 
287  int l = 0, i;
288
289  while (l1 != NULL)
290  {
291    i = 0;
292    l2 = list;
293    while (l1 != l2)
294    {
295      i++;
296      l2 = *((void**) (l2 + next));
297    }
298    if (i != l) return l1;
299    l1 = *((void**) (l1 + next));
300    l++;
301  }
302  return NULL;
303}
304
305/**********************************************************************
306 *
307 * Operations for deep profiles
308 *
309 **********************************************************************/
310
311#ifdef DO_DEEP_PROFILE
312void _memcpyW(void* p1, void* p2, long l)
313{
314  assume(l >= 0);
315
316  while(l)
317  {
318    *p1++ = *p2++;
319    l--;
320  }
321}
322
323void _memaddW(void* p1, void* p2, void* p3, long l)
324{
325  assume(l >= 0);
326 
327  while (l)
328  {
329    *p1++ = *p2++ + *p3++;
330    l--;
331  }
332}
333
334void _memsetW(void* p1, long w, long l)
335{
336  assume(l >= 0);
337 
338  while (l)
339  {
340    *p1++ = w;
341    l--;
342  }
343}
344
345#endif /* DO_DEEP_PROFILE */
346
347#ifdef sun
348#ifdef __svr4__
349void bzero(char *s,int n)
350{
351  memset(s,0,n);
352}
353#endif
354#endif
Note: See TracBrowser for help on using the repository browser.