source: git/Singular/mmisc.c @ f8519e

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