~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/dlmalloc_test.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Emscripten tests
 
2
 
 
3
#include <stdio.h>
 
4
#include <stdlib.h>
 
5
#include <assert.h>
 
6
 
 
7
int main(int ac, char **av)
 
8
{
 
9
  int NUM = ac > 1 ? atoi(av[1]) : 0;
 
10
  int REPS = ac > 2 ? atoi(av[2]) : 0;
 
11
  int c1 = 0, c2 = 0;
 
12
  for (int x = 0; x < REPS; x++) {
 
13
    char* allocations[NUM];
 
14
    for (int i = 0; i < NUM/2; i++) {
 
15
      allocations[i] = (char*)malloc((11*i)%1024 + x);
 
16
      //printf("zz alloc: %d\n", (int)allocations[i]);
 
17
      assert(allocations[i]);
 
18
      if (i > 10 && i%4 == 1 && allocations[i-10]) {
 
19
        //printf("zz free: %d\n", (int)allocations[i-10]);
 
20
        free(allocations[i-10]);
 
21
        allocations[i-10] = NULL;
 
22
      }
 
23
    }
 
24
    for (int i = NUM/2; i < NUM; i++) {
 
25
      allocations[i] = (char*)malloc(1024*(i+1));
 
26
      //printf("zz alloc: %d\n", (int)allocations[i]);
 
27
      assert(allocations[i]);
 
28
      if (i > 10 && i%4 != 1 && allocations[i-10]) {
 
29
        //printf("zz free: %d\n", (int)allocations[i-10]);
 
30
        free(allocations[i-10]);
 
31
        allocations[i-10] = NULL;
 
32
      }
 
33
    }
 
34
    char* first = allocations[0];
 
35
    for (int i = 0; i < NUM; i++) {
 
36
      if (allocations[i]) {
 
37
        //printf("zz free: %d\n", (int)allocations[i]);
 
38
        free(allocations[i]);
 
39
      }
 
40
    }
 
41
    char *last = (char*)malloc(512); // should be identical, as we free'd it all
 
42
    //printf("zz last: %d\n", (int)last);
 
43
    char *newer = (char*)malloc(512); // should be different
 
44
    //printf("zz newer: %d\n", (int)newer);
 
45
#ifndef __APPLE__
 
46
    c1 += first == last;
 
47
    c2 += first == newer;
 
48
#else // On OSX, it's been detected that memory is not necessarily allocated linearly, so skip this check and simulate success.
 
49
    ++c1;
 
50
#endif
 
51
  }
 
52
  printf("*%d,%d*\n", c1, c2);
 
53
}
 
54
 
 
55
/* Some debugging tools: Make JS and native code work exactly the same */
 
56
/*
 
57
time_t time ( time_t * timer )
 
58
{
 
59
  if (timer) *timer = 1;
 
60
  return 1;
 
61
}
 
62
 
 
63
long sysconf(int name)
 
64
{
 
65
  printf("sysconf: %d (30 is page size)\n", name);
 
66
  return 4096;
 
67
}
 
68
 
 
69
void *sbrk(intptr_t increment)
 
70
{
 
71
  static char spaace[1024*1024*1];
 
72
  static intptr_t where = 0;
 
73
  printf("sbrk! spaace=%d  (%d,%d)\n", (int)&spaace[0], where, increment); // copy the value printed at runtime here in native code into your js
 
74
  void *ret = &spaace[where];
 
75
  where += increment;
 
76
  return ret;
 
77
}
 
78
*/
 
79