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