source: git/omalloc/omtTest.c @ e16431

fieker-DuValspielwiese
Last change on this file since e16431 was e16431, checked in by Hans Schoenemann <hannes@…>, 3 years ago
omalloc: fix alignment stuff for "make check"
  • Property mode set to 100644
File size: 10.3 KB
Line 
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
11omMemCell_t cells[MAX_CELLS];
12int errors = 0;
13int missed_errors = 0;
14int used_regions = 0;
15int 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.  */
21long mrand48 ();
22void srand48();
23static long
24random ()
25{
26  return mrand48 ();
27}
28static void srandom(long seed)
29{
30  srand48(seed);
31}
32#endif
33#endif
34
35#if CHECK_LEVEL > 0
36void omtTestDebug(omMemCell cell)
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  {
60    omReportError(omError_Unknown, omError_NoError, OM_FLR,
61                  "addr:%p is unaligned", cell->addr);
62    return;
63  }
64
65  if (IS_ALIGNED(cell->spec) && !OM_IS_STRICT_ALIGNED(cell->addr))
66  {
67    omReportError(omError_Unknown, omError_NoError, OM_FLR,
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  {
75    omReportError(omError_Unknown, omError_NoError, OM_FLR,
76                  "is_size==%u < size==%u", is_size, size);
77    return;
78  }
79
80  if (is_size >> LOG_SIZEOF_LONG != omSizeWOfAddr(cell->addr))
81  {
82    omReportError(omError_Unknown, omError_NoError, OM_FLR,
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
90void 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    {
99       omReportError(omError_Unknown, omError_NoError, OM_FLR,
100                     "s1[%u]==%d  !=  s2[%u]==%d", i, ((unsigned long*)s1)[i], i, ((unsigned long*)s2)[i]);
101       return;
102    }
103  }
104}
105
106void 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  {
113    omReportError(omError_Unknown, omError_NoError, OM_FLR,
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    {
122      omReportError(omError_Unknown, omError_NoError, OM_FLR,
123                    "word %d modified: is %u should be %u", i, ((unsigned long*)addr)[i], value);
124      return;
125    }
126  }
127}
128#endif
129
130void InitCellAddrContent(omMemCell cell)
131{
132  size_t sizeW = omSizeWOfAddr(cell->addr);
133  omMemsetW(cell->addr, (IS_ZERO(cell->spec) ? 0 : cell->spec), sizeW);
134}
135
136void 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
142  omTestMemory(level);
143  om_Opts.MinCheck = 1;
144  for (i=0; i<n; i++)
145  {
146    omtTestDebug(&cells[i]);
147    if (om_ErrorStatus != omError_NoError)
148    {
149      errors++;
150      om_ErrorStatus = omError_NoError;
151    }
152    if ((i % 10000) == 0)
153    {
154      printf(".");
155      fflush(stdout);
156    }
157  }
158  om_Opts.MinCheck = l;
159#endif
160}
161
162
163int size_range = RANGE_MIN;
164int size_range_number = RANGE_MAX / RANGE_MIN;
165
166int MyRandSpec()
167{
168  unsigned long spec = random() + 1;
169  if (! size_range_number)
170  {
171    size_range = size_range << 1;
172    if (size_range > RANGE_MAX) size_range = RANGE_MIN;
173    size_range_number = RANGE_MAX / size_range;
174  }
175  SET_SIZE(spec, GET_SIZE(spec) & (size_range -1));
176  size_range_number--;
177  if (GET_SIZE(spec) == 0) spec++;
178  return spec;
179}
180
181
182void 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);
198  if (om_ErrorStatus != omError_NoError)
199  {
200    errors++;
201    om_ErrorStatus = omError_NoError;
202  }
203}
204
205void 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);
221  if (om_ErrorStatus != omError_NoError)
222  {
223    errors++;
224    om_ErrorStatus = omError_NoError;
225  }
226}
227
228void 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
245  if (om_ErrorStatus != omError_NoError)
246  {
247    errors++;
248    om_ErrorStatus = omError_NoError;
249  }
250}
251
252void 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    }
267    if (om_ErrorStatus != omError_NoError)
268    {
269      errors++;
270      om_ErrorStatus = omError_NoError;
271    }
272  }
273}
274
275omBin omtGetStickyBin(omBin bin)
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
283void 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;
306      om_StickyBins = omRemoveFromGList(om_StickyBins, next, bin);
307      __omFreeBinAddr(bin);
308      bin = next_bin;
309    }
310    else
311    {
312      bin = bin->next;
313    }
314  }
315}
316
317
318void my_exit()
319{
320  printf("\nomtTest Summary: ");
321  if (errors || missed_errors || used_regions)
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
336int 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]);
351  omInitGetBackTrace();
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
372             omListLength(om_KeptAddr), omListLength(om_AlwaysKeptAddrs)
373#else
374             0, 0
375#endif
376             );
377
378      printf("Checking Memory and all cells ");
379      fflush(stdout);
380      omCheckCells(n_cells, END_CHECK_LEVEL, cells);
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      {
389        missed_errors = omtTestErrors();
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();
403      omtMergeStickyBins(cells, -1);
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
495int main(int argc, char* argv[])
496{
497  return 0;
498}
499#endif
Note: See TracBrowser for help on using the repository browser.