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

« back to all changes in this revision

Viewing changes to tests/dlmalloc_proxy.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-09-20 22:44:35 UTC
  • mfrom: (4.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20130920224435-apuwj4fsl3fqv1a6
Tags: 1.5.6~20130920~6010666-1
* New snapshot release
* Update the list of supported architectures to the same as libv8
  (Closes: #723129)
* emlibtool has been removed from upstream.
* Fix warning syntax-error-in-dep5-copyright
* Refresh of the patches

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
#include <dlfcn.h>
 
7
 
 
8
typedef void *(*mallocer)(int n);
 
9
typedef void (*freeer)(void *p);
 
10
 
 
11
void *lib_handle;
 
12
int handles = 0;
 
13
mallocer mallocproxy = NULL;
 
14
freeer freeproxy = NULL;
 
15
 
 
16
void get_lib() {
 
17
  //printf("get lib\n");
 
18
  lib_handle = dlopen("liblib.so", RTLD_NOW);
 
19
  assert(lib_handle != NULL);
 
20
  handles++;
 
21
 
 
22
  mallocproxy = (mallocer)dlsym(lib_handle, "mallocproxy");
 
23
  assert(mallocproxy!= NULL);
 
24
  freeproxy = (freeer)dlsym(lib_handle, "freeproxy");
 
25
  assert(freeproxy!= NULL);
 
26
}
 
27
 
 
28
void unget_lib() {
 
29
  //printf("unget lib\n");
 
30
  assert(lib_handle);
 
31
  dlclose(lib_handle);
 
32
  handles--;
 
33
  if (handles == 0) lib_handle = NULL;
 
34
}
 
35
 
 
36
int main() {
 
37
  int n = 0, total = 0, l = 0;
 
38
  void *allocs[50];
 
39
  allocs[10] = malloc(10); // pull in real malloc
 
40
  for (int i = 0; i < 1000; i++) {
 
41
    //printf("%d: total ever %d MB, current MB %d, total libs %d\n", i, total, n, l);
 
42
    if (i % 5 == 0) {
 
43
      if (handles < 10) {
 
44
        get_lib();
 
45
        l++;
 
46
      }
 
47
    }
 
48
    if (i % 7 == 0) {
 
49
      if (handles > 0) unget_lib();
 
50
    }
 
51
    if (i % 3 == 0) {
 
52
      if (handles > 0) {
 
53
        if (n < 10) {
 
54
          if (i % 2 == 0) {
 
55
            //printf("alloc\n");
 
56
            allocs[n++] = mallocproxy(1024*1024);
 
57
          } else {
 
58
            //printf("real alloc\n");
 
59
            allocs[n++] = malloc(1024*1024);
 
60
          }
 
61
          total++;
 
62
        } else {
 
63
          //printf("real free\n");
 
64
          free(allocs[--n]); // real free
 
65
        }
 
66
      }
 
67
    }
 
68
    if (i % 4 == 0) {
 
69
      if (handles > 0 && n > 0) {
 
70
        //printf("free\n");
 
71
        if (i % 2 == 0) {
 
72
          //printf("free\n");
 
73
          freeproxy(allocs[--n]);
 
74
        } else {
 
75
          //printf("real free\n");
 
76
          free(allocs[--n]);
 
77
        }
 
78
      }
 
79
    }
 
80
  }
 
81
  while (n > 0) free(allocs[--n]); // real free
 
82
  while (handles > 0) unget_lib();
 
83
  printf("*%d,%d*\n", total, l);
 
84
}
 
85