source: git/omalloc/omtTest.c @ f5d2647

spielwiese
Last change on this file since f5d2647 was f769719, checked in by Mohamed Barakat <mohamed.barakat@…>, 13 years ago
- we all got rid of makeheader (sorry Hans ;( ) . renamed: om_Alloc.h -> omalloc.h - updated Makefile.am to install the public header files - fixed the VERSION string issue - got rid of the obsolete path stuff (Oleksandr is happier now)
  • Property mode set to 100644
File size: 10.4 KB
Line 
1#include <omalloc/omtTest.h>
2
3#if CHECK_LEVEL > 0
4#define OM_CHECK CHECK_LEVEL
5#endif
6
7#include <omalloc/omalloc.h>
8
9omMemCell_t cells[MAX_CELLS];
10int errors = 0;
11int missed_errors = 0;
12int used_regions = 0;
13int 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.  */
19long mrand48 ();
20void srand48();
21static long
22random ()
23{
24  return mrand48 ();
25}
26static void srandom(long seed)
27{
28  srand48(seed);
29}
30#endif
31#endif
32
33#if CHECK_LEVEL > 0
34void omtTestDebug(omMemCell cell)
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  {
58    omReportError(omError_Unknown, omError_NoError, OM_FLR,
59                  "addr:%p is unaligned", cell->addr);
60    return;
61  }
62
63  if (IS_ALIGNED(cell->spec) && !OM_IS_STRICT_ALIGNED(cell->addr))
64  {
65    omReportError(omError_Unknown, omError_NoError, OM_FLR,
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  {
73    omReportError(omError_Unknown, omError_NoError, OM_FLR,
74                  "is_size == %u is unaligned", is_size);
75    return;
76  }
77  if (is_size < size)
78  {
79    omReportError(omError_Unknown, omError_NoError, OM_FLR,
80                  "is_size==%u < size==%u", is_size, size);
81    return;
82  }
83
84  if (is_size >> LOG_SIZEOF_LONG != omSizeWOfAddr(cell->addr))
85  {
86    omReportError(omError_Unknown, omError_NoError, OM_FLR,
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
94void 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    {
103       omReportError(omError_Unknown, omError_NoError, OM_FLR,
104                     "s1[%u]==%d  !=  s2[%u]==%d", i, ((unsigned long*)s1)[i], i, ((unsigned long*)s2)[i]);
105       return;
106    }
107  }
108}
109
110void 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  {
117    omReportError(omError_Unknown, omError_NoError, OM_FLR,
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    {
126      omReportError(omError_Unknown, omError_NoError, OM_FLR,
127                    "word %d modified: is %u should be %u", i, ((unsigned long*)addr)[i], value);
128      return;
129    }
130  }
131}
132#endif
133
134void InitCellAddrContent(omMemCell cell)
135{
136  size_t sizeW = omSizeWOfAddr(cell->addr);
137  omMemsetW(cell->addr, (IS_ZERO(cell->spec) ? 0 : cell->spec), sizeW);
138}
139
140void 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]);
151    if (om_ErrorStatus != omError_NoError)
152    {
153      errors++;
154      om_ErrorStatus = omError_NoError;
155    }
156    if ((i % 10000) == 0)
157    {
158      printf(".");
159      fflush(stdout);
160    }
161  }
162  om_Opts.MinCheck = l;
163#endif
164}
165
166
167int size_range = RANGE_MIN;
168int size_range_number = RANGE_MAX / RANGE_MIN;
169
170int MyRandSpec()
171{
172  unsigned long spec = random() + 1;
173  if (! size_range_number)
174  {
175    size_range = size_range << 1;
176    if (size_range > RANGE_MAX) size_range = RANGE_MIN;
177    size_range_number = RANGE_MAX / size_range;
178  }
179  SET_SIZE(spec, GET_SIZE(spec) & (size_range -1));
180  size_range_number--;
181  if (GET_SIZE(spec) == 0) spec++;
182  return spec;
183}
184
185
186void 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);
202  if (om_ErrorStatus != omError_NoError)
203  {
204    errors++;
205    om_ErrorStatus = omError_NoError;
206  }
207}
208
209void 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);
225  if (om_ErrorStatus != omError_NoError)
226  {
227    errors++;
228    om_ErrorStatus = omError_NoError;
229  }
230}
231
232void 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
249  if (om_ErrorStatus != omError_NoError)
250  {
251    errors++;
252    om_ErrorStatus = omError_NoError;
253  }
254}
255
256void 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    }
271    if (om_ErrorStatus != omError_NoError)
272    {
273      errors++;
274      om_ErrorStatus = omError_NoError;
275    }
276  }
277}
278
279omBin omtGetStickyBin(omBin bin)
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
287void 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;
310      om_StickyBins = omRemoveFromGList(om_StickyBins, next, bin);
311      __omFreeBinAddr(bin);
312      bin = next_bin;
313    }
314    else
315    {
316      bin = bin->next;
317    }
318  }
319}
320
321
322void my_exit()
323{
324  printf("\nomtTest Summary: ");
325  if (errors || missed_errors || used_regions)
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
340int 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]);
354  omInitGetBackTrace();
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
375             omListLength(om_KeptAddr), omListLength(om_AlwaysKeptAddrs)
376#else
377             0, 0
378#endif
379             );
380
381      printf("Checking Memory and all cells ");
382      fflush(stdout);
383      omCheckCells(n_cells, END_CHECK_LEVEL, cells);
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      {
392        missed_errors = omtTestErrors();
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();
406      omtMergeStickyBins(cells, -1);
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}
Note: See TracBrowser for help on using the repository browser.