My Project
omtTest.c
Go to the documentation of this file.
1 #include "omtTest.h"
2 
3 #if CHECK_LEVEL > 0
4 #define OM_CHECK CHECK_LEVEL
5 #endif
6 
7 #include "omalloc.h"
8 
9 #ifdef HAVE_OMALLOC
10 
11 omMemCell_t cells[MAX_CELLS];
12 int errors = 0;
13 int missed_errors = 0;
14 int used_regions = 0;
15 int seed;
16 
17 #if defined (__hpux) || defined (__alpha) || defined (__svr4__) || defined (__SVR4)
18 /* SF1 cosimo.medicis.polytechnique.fr V4.0 1229 alpha works */
19 #if defined (__hpux) || defined (__svr4__) || defined (__SVR4)
20 /* HPUX lacks random(). DEC OSF/1 1.2 random() returns a double. */
21 long mrand48 ();
22 void srand48();
23 static long
24 random ()
25 {
26  return mrand48 ();
27 }
28 static void srandom(long seed)
29 {
30  srand48(seed);
31 }
32 #endif
33 #endif
34 
35 #if CHECK_LEVEL > 0
37 {
38  size_t size = GET_SIZE(cell->spec);
39  size_t is_size;
40 
41  if (om_ErrorStatus != omError_NoError) return;
42  if (cell->bin != NULL)
43  {
44  if (IS_ALIGNED(cell->spec))
45  omDebugAddrAlignedBin(cell->addr, cell->bin);
46  else
47  omDebugAddrBin(cell->addr, cell->bin);
48  }
49  else
50  {
51  if (IS_ALIGNED(cell->spec))
52  omDebugAddrAlignedSize(cell->addr, size);
53  else
54  omDebugAddrSize(cell->addr, size);
55  }
56  if (om_ErrorStatus != omError_NoError) return;
57 
58  if (!OM_IS_ALIGNED(cell->addr))
59  {
61  "addr:%p is unaligned", cell->addr);
62  return;
63  }
64 
65  if (IS_ALIGNED(cell->spec) && !OM_IS_STRICT_ALIGNED(cell->addr))
66  {
68  "addr:%p is not strict unaligned", cell->addr);
69  return;
70  }
71 
72  is_size = omSizeOfAddr(cell->addr);
73  if (is_size < size)
74  {
76  "is_size==%u < size==%u", is_size, size);
77  return;
78  }
79 
80  if (is_size >> LOG_SIZEOF_LONG != omSizeWOfAddr(cell->addr))
81  {
83  "is_sizeW==%u < sizeW==%u", is_size >> LOG_SIZEOF_LONG, omSizeWOfAddr(cell->addr));
84  return;
85  }
86 
87  TestAddrContent(cell->addr, (IS_ZERO(cell->spec) ? 0 : cell->spec), is_size);
88 }
89 
90 void TestAddrContentEqual(void* s1, void* s2, size_t size)
91 {
92  int i;
93  size_t sizeW = OM_ALIGN_SIZE(size) >> LOG_SIZEOF_LONG;
94 
95  for (i=0; i<sizeW; i++)
96  {
97  if (((unsigned long*)s1)[i] != ((unsigned long*)s2)[i])
98  {
100  "s1[%u]==%d != s2[%u]==%d", i, ((unsigned long*)s1)[i], i, ((unsigned long*)s2)[i]);
101  return;
102  }
103  }
104 }
105 
106 void TestAddrContent(void* addr, unsigned long value, size_t size)
107 {
108  size_t sizeW = OM_ALIGN_SIZE(size) >> LOG_SIZEOF_LONG;
109  int i;
110 
111  if (!OM_IS_ALIGNED(addr))
112  {
114  "addr %p unaligned", addr);
115  return;
116  }
117 
118  for (i=0; i<sizeW; i++)
119  {
120  if (((unsigned long*)addr)[i] != value)
121  {
123  "word %d modified: is %u should be %u", i, ((unsigned long*)addr)[i], value);
124  return;
125  }
126  }
127 }
128 #endif
129 
131 {
132  size_t sizeW = omSizeWOfAddr(cell->addr);
133  omMemsetW(cell->addr, (IS_ZERO(cell->spec) ? 0 : cell->spec), sizeW);
134 }
135 
136 void omCheckCells(int n, int level, omMemCell_t* cells)
137 {
138 #if END_CHECK_LEVEL > 0
139  int l = om_Opts.MinCheck;
140  int i;
141 
143  om_Opts.MinCheck = 1;
144  for (i=0; i<n; i++)
145  {
146  omtTestDebug(&cells[i]);
148  {
149  errors++;
151  }
152  if ((i % 10000) == 0)
153  {
154  printf(".");
155  fflush(stdout);
156  }
157  }
158  om_Opts.MinCheck = l;
159 #endif
160 }
161 
162 
165 
167 {
168  unsigned long spec = random() + 1;
169  if (! size_range_number)
170  {
171  size_range = size_range << 1;
174  }
175  SET_SIZE(spec, GET_SIZE(spec) & (size_range -1));
177  if (GET_SIZE(spec) == 0) spec++;
178  return spec;
179 }
180 
181 
182 void TestAlloc(omMemCell cell, unsigned long spec)
183 {
184  if (DO_CHECK(spec))
185  {
186  if (DO_TRACK(spec))
187  om_Opts.MinTrack = GET_TRACK(spec);
188  else
189  om_Opts.MinTrack = 0;
190 
191  if (DO_KEEP(spec))
192  omtTestAllocKeep(cell, spec);
193  else
194  omtTestAllocDebug(cell, spec);
195  }
196  else
197  omtTestAlloc(cell, spec);
199  {
200  errors++;
202  }
203 }
204 
205 void TestRealloc(omMemCell cell, unsigned long spec)
206 {
207  if (DO_CHECK(spec))
208  {
209  if (DO_TRACK(spec))
210  om_Opts.MinTrack = GET_TRACK(spec);
211  else
212  om_Opts.MinTrack = 0;
213 
214  if (DO_KEEP(spec))
215  omtTestReallocKeep(cell, spec);
216  else
217  omtTestReallocDebug(cell, spec);
218  }
219  else
220  omtTestRealloc(cell, spec);
222  {
223  errors++;
225  }
226 }
227 
228 void TestDup(omMemCell cell, unsigned long spec)
229 {
230  if (DO_CHECK(spec))
231  {
232  if (DO_TRACK(spec))
233  om_Opts.MinTrack = GET_TRACK(spec);
234  else
235  om_Opts.MinTrack = 0;
236 
237  if (DO_KEEP(spec))
238  omtTestDupKeep(cell, spec);
239  else
240  omtTestDupDebug(cell, spec);
241  }
242  else
243  omtTestDup(cell, spec);
244 
246  {
247  errors++;
249  }
250 }
251 
252 void TestFree(omMemCell cell)
253 {
254  if (cell->addr != NULL)
255  {
256  if (DO_FREE_CHECK(cell->spec))
257  {
258  if (DO_KEEP(cell->spec))
259  omtTestFreeKeep(cell);
260  else
261  omtTestFreeDebug(cell);
262  }
263  else
264  {
265  omtTestFree(cell);
266  }
268  {
269  errors++;
271  }
272  }
273 }
274 
276 {
277  omBin sticky_bin = omFindInGList(om_StickyBins, next, max_blocks, bin->max_blocks);
278  if (sticky_bin == NULL)
279  sticky_bin = omGetStickyBinOfBin(bin);
280  return sticky_bin;
281 }
282 
283 void omtMergeStickyBins(omMemCell cell, int n)
284 {
285  int i;
286  omBin bin;
287 
288  for (i=0; i<n; i++)
289  {
290  if (cell[i].orig_bin != NULL)
291  {
292  if (omIsOnGList(om_StickyBins, next, cell[i].bin))
293  omMergeStickyBinIntoBin(cell[i].bin, cell[i].orig_bin);
294 
295  cell[i].bin = cell[i].orig_bin;
296  cell[i].orig_bin = NULL;
297  }
298  }
299 
300  bin = om_StickyBins;
301  while (bin != NULL)
302  {
303  if (bin->current_page == om_ZeroPage)
304  {
305  omBin next_bin = bin->next;
307  __omFreeBinAddr(bin);
308  bin = next_bin;
309  }
310  else
311  {
312  bin = bin->next;
313  }
314  }
315 }
316 
317 
318 void my_exit()
319 {
320  printf("\nomtTest Summary: ");
322  {
323  printf("***FAILED***errors:%d, missed_errors:%d, used_regions:%d, seed=%d\n", errors, missed_errors, used_regions, seed);
324  if (errors) exit(errors);
325  if (missed_errors) exit(missed_errors);
326  if (used_regions) exit(used_regions);
327  }
328  else
329  {
330  printf("OK\n");
331  exit(0);
332  }
333 }
334 
335 
336 int main(int argc, char* argv[])
337 {
338  int i=0, error_test = 1;
339  unsigned long spec, j;
340  int n = 1;
341  int n_cells = MAX_CELLS;
342  int decr = 2;
343  int last_kept_freed = 0;
344  om_Opts.MinCheck = CHECK_LEVEL;
345  om_Opts.Keep = KEEP_ADDR;
346  om_Opts.HowToReportErrors = 3;
347 
348  seed = time(NULL);
349 
350  omInitRet_2_Info(argv[0]);
352  omInitInfo();
353  om_Opts.PagesPerRegion = PAGES_PER_REGION;
354 
355  if (argc > 1) sscanf(argv[1], "%d", &error_test);
356  if (argc > 2) sscanf(argv[2], "%d", &seed);
357  srandom(seed);
358 
359  if (argc > 3) sscanf(argv[3], "%d", &n);
360  if (argc > 4) sscanf(argv[4], "%d", &decr);
361 
362  if (decr < 2) decr = 2;
363  printf("seed == %d\n", seed);
364  fflush(stdout);
365  while (1)
366  {
367  if (i == n_cells)
368  {
369  i = 0;
370  printf("\nCells: %d KeptAddr:%d AlwaysKeptAddr:%d\n", n_cells,
371 #ifndef OM_NDEBUG
373 #else
374  0, 0
375 #endif
376  );
377 
378  printf("Checking Memory and all cells ");
379  fflush(stdout);
381  printf("\n");
382  omPrintStats(stdout);
383  omPrintInfo(stdout);
384  if (om_Info.CurrentRegionsAlloc > 0) omPrintBinStats(stdout);
385  fflush(stdout);
386 #if CHECK_LEVEL > 0 && TRACK_LEVEL > 0
387  if (error_test && errors == 0)
388  {
390  if (missed_errors < 0)
391  {
392  my_exit();
393  }
394  }
395 #endif
396  omtMergeStickyBins(cells, n_cells);
397  while (i< n_cells)
398  {
399  TestFree(&cells[i]);
400  i++;
401  }
402  omFreeKeptAddr();
404  omPrintStats(stdout);
405  omPrintInfo(stdout);
406  if (om_Info.CurrentRegionsAlloc > 0)
407  {
408  omPrintBinStats(stdout);
409  used_regions += om_Info.CurrentRegionsAlloc;
410  }
411  omPrintUsedAddrs(stdout, 5);
412  i=0;
413  n--;
414  if (n <= 0 || n_cells <= 100)
415  {
416  my_exit();
417  }
418  else
419  {
420  n_cells = n_cells / decr;
421  }
422  }
423  spec = MyRandSpec();
424  myprintf("%d:%lu:%ld:%ld", i, spec, GET_SIZE(spec), GET_TRACK(spec));
425  myfflush(stdout);
426  if (DO_FREE(spec))
427  {
428  if (i != 0)
429  {
430  myprintf(" FREE");
431  j = spec % i;
432  myprintf(" %ld ", j);
433  myfflush(stdout);
434  TestFree(&cells[j]);
435  TestAlloc(&cells[j], spec);
436  }
437  }
438  else if (DO_REALLOC(spec))
439  {
440  if (i != 0)
441  {
442  myprintf(" REALLOC");
443  j = spec % i;
444  myprintf(" %ld ", j);
445  myfflush(stdout);
446  TestRealloc(&cells[j], spec);
447  }
448  }
449  else if (DO_DUP(spec))
450  {
451  if (i != 0)
452  {
453  myprintf(" DUP");
454  j = spec % i;
455  myprintf(" %ld ", j);
456  myfflush(stdout);
457  TestDup(&cells[j], spec);
458  }
459  }
460  else
461  {
462  myprintf(" ALLOC");
463  myfflush(stdout);
464  TestAlloc(&cells[i], spec);
465  i++;
466  if (i % 1000 == 0)
467  {
468  printf("%d:", i / 1000);
469  fflush(stdout);
470  }
471  }
472  myprintf("\n");
473  myfflush(stdout);
474  // free kept addresses from time to time
475  if ((i % 10000) == 0 && i != n_cells && i!=last_kept_freed)
476  {
477  printf("F:");
478  omFreeKeptAddr();
479  last_kept_freed = i;
480  }
481 #if 0
482  if (CHECK_LEVEL > 2)
483  {
484  for (j=0; j<i; j++)
485  {
486  omtTestDebug(&cells[j]);
487  }
488  }
489 #endif
490  }
491  return 0;
492 }
493 #else
494 
495 int main(int argc, char* argv[])
496 {
497  return 0;
498 }
499 #endif
int size(const CanonicalForm &f, const Variable &v)
int size ( const CanonicalForm & f, const Variable & v )
Definition: cf_ops.cc:600
int level(const CanonicalForm &f)
int l
Definition: cfEzgcd.cc:100
int i
Definition: cfEzgcd.cc:132
int j
Definition: facHensel.cc:110
ListNode * next
Definition: janet.h:31
#define omDebugAddrAlignedBin
Definition: omAllocDecl.h:291
omError_t omTestMemory(int check_level)
Definition: omDebug.c:94
#define omDebugAddrAlignedSize
Definition: omAllocDecl.h:292
#define omDebugAddrSize(addr, size)
Definition: omAllocDecl.h:313
#define omDebugAddrBin(addr, bin)
Definition: omAllocDecl.h:311
#define __omFreeBinAddr(addr)
omBin om_StickyBins
Definition: omBin.c:374
size_t omSizeOfAddr(const void *addr)
void * om_AlwaysKeptAddrs
Definition: omDebug.c:31
void omFreeKeptAddr()
Definition: omDebug.c:609
void * om_KeptAddr
Definition: omDebug.c:28
omError_t om_ErrorStatus
Definition: omError.c:13
omError_t omReportError(omError_t error, omError_t report_error, OM_FLR_DECL, const char *fmt,...)
Definition: omError.c:80
@ omError_NoError
Definition: omError.h:18
@ omError_Unknown
Definition: omError.h:19
#define omInitGetBackTrace()
#define NULL
Definition: omList.c:12
#define omFindInGList(ptr, next, what, value)
Definition: omList.h:104
#define omRemoveFromGList(ptr, next, addr)
Definition: omList.h:102
#define omListLength(ptr)
Definition: omList.h:62
#define omIsOnGList(ptr, next, addr)
Definition: omList.h:100
#define omMemsetW(P1, W, L)
Definition: omMemOps.h:161
omOpts_t om_Opts
Definition: omOpts.c:13
omInfo_t om_Info
Definition: omStats.c:16
omBin_t * omBin
Definition: omStructs.h:12
omBinPage_t om_ZeroPage[]
Definition: om_Alloc.c:19
void TestAlloc(omMemCell cell, unsigned long spec)
Definition: omtTest.c:182
int main(int argc, char *argv[])
Definition: omtTest.c:336
int seed
Definition: omtTest.c:15
int size_range_number
Definition: omtTest.c:164
void TestAddrContent(void *addr, unsigned long value, size_t size)
Definition: omtTest.c:106
omBin omtGetStickyBin(omBin bin)
Definition: omtTest.c:275
omMemCell_t cells[MAX_CELLS]
Definition: omtTest.c:11
int size_range
Definition: omtTest.c:163
int used_regions
Definition: omtTest.c:14
int errors
Definition: omtTest.c:12
void TestFree(omMemCell cell)
Definition: omtTest.c:252
void omtMergeStickyBins(omMemCell cell, int n)
Definition: omtTest.c:283
void my_exit()
Definition: omtTest.c:318
int missed_errors
Definition: omtTest.c:13
void TestRealloc(omMemCell cell, unsigned long spec)
Definition: omtTest.c:205
void omtTestDebug(omMemCell cell)
Definition: omtTest.c:36
void InitCellAddrContent(omMemCell cell)
Definition: omtTest.c:130
void omCheckCells(int n, int level, omMemCell_t *cells)
Definition: omtTest.c:136
int MyRandSpec()
Definition: omtTest.c:166
void TestDup(omMemCell cell, unsigned long spec)
Definition: omtTest.c:228
void TestAddrContentEqual(void *s1, void *s2, size_t size)
Definition: omtTest.c:90
#define KEEP_ADDR
Definition: omtTest.h:18
#define DO_KEEP(spec)
Definition: omtTest.h:79
void omtTestAlloc(omMemCell cell, unsigned long spec)
Definition: omtTestAlloc.c:33
#define IS_ALIGNED(spec)
Definition: omtTest.h:53
#define RANGE_MIN
Definition: omtTest.h:94
#define GET_TRACK(spec)
Definition: omtTest.h:71
#define myprintf(format, args...)
Definition: omtTest.h:45
void omtTestReallocDebug(omMemCell cell, unsigned long spec)
void omtTestAllocKeep(omMemCell cell, unsigned long spec)
#define RANGE_MAX
Definition: omtTest.h:95
#define DO_REALLOC(spec)
Definition: omtTest.h:59
#define DO_FREE(spec)
Definition: omtTest.h:58
#define IS_ZERO(spec)
Definition: omtTest.h:54
void omtTestDup(omMemCell cell, unsigned long spec)
Definition: omtTestAlloc.c:348
void omtTestReallocKeep(omMemCell cell, unsigned long spec)
#define DO_DUP(spec)
Definition: omtTest.h:60
#define DO_FREE_CHECK(spec)
Definition: omtTest.h:64
omMemCell_t * omMemCell
Definition: omtTest.h:33
void omtTestAllocDebug(omMemCell cell, unsigned long spec)
#define PAGES_PER_REGION
Definition: omtTest.h:97
void omtTestFreeDebug(omMemCell cell)
void omtTestRealloc(omMemCell cell, unsigned long spec)
Definition: omtTestAlloc.c:180
void omtTestFree(omMemCell cell)
Definition: omtTestAlloc.c:135
#define SET_SIZE(spec, size)
Definition: omtTest.h:52
void omtTestDupDebug(omMemCell cell, unsigned long spec)
int omtTestErrors()
Definition: omtTestError.c:37
#define DO_CHECK(spec)
Definition: omtTest.h:63
#define GET_SIZE(spec)
Definition: omtTest.h:51
void omtTestFreeKeep(omMemCell cell)
#define myfflush(what)
Definition: omtTest.h:46
#define CHECK_LEVEL
Definition: omtTest.h:10
#define END_CHECK_LEVEL
Definition: omtTest.h:20
void omtTestDupKeep(omMemCell cell, unsigned long spec)
#define DO_TRACK(spec)
Definition: omtTest.h:70
#define MAX_CELLS
Definition: omtTest.h:17
#define omGetStickyBinOfBin(B)
Definition: xalloc.h:300
#define omMergeStickyBinIntoBin(A, B)
Definition: xalloc.h:329
#define omPrintUsedAddrs(F, max)
Definition: xalloc.h:322
#define OM_NDEBUG
Definition: xalloc.h:10
#define omPrintStats(F)
Definition: xalloc.h:282
#define omInitInfo()
Definition: xalloc.h:279
#define omPrintInfo(F)
Definition: xalloc.h:283
#define omPrintBinStats(F)
Definition: xalloc.h:284
#define omSizeWOfAddr(P)
Definition: xalloc.h:269
#define omInitRet_2_Info(argv0)
Definition: xalloc.h:328