aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2011-10-08 13:28:01 -0700
committerAlon Zakai <alonzakai@gmail.com>2011-10-08 13:28:01 -0700
commit82004ce7a9d4d1be661d4adb9478a674930f7235 (patch)
tree146dfe94625e146384fba835db884d28bc7abae7
parent5ab3f6603f47cd17c276d0f8002efd3008753b97 (diff)
fix dlmalloc with typed arrays
-rw-r--r--src/dlmalloc.c1
-rw-r--r--tests/dlmalloc.c70
-rw-r--r--tests/dlmalloc_test.c36
-rw-r--r--tests/runner.py25
4 files changed, 51 insertions, 81 deletions
diff --git a/src/dlmalloc.c b/src/dlmalloc.c
index eb9c6487..7dc24006 100644
--- a/src/dlmalloc.c
+++ b/src/dlmalloc.c
@@ -1,4 +1,3 @@
-#include "emscripten.h"
#define __THROW
#define __attribute_malloc__
#define __wur
diff --git a/tests/dlmalloc.c b/tests/dlmalloc.c
deleted file mode 100644
index 37ddc6e0..00000000
--- a/tests/dlmalloc.c
+++ /dev/null
@@ -1,70 +0,0 @@
-
-int main(int ac, char **av)
-{
- int NUM = ac > 1 ? atoi(av[1]) : 0;
- int REPS = ac > 2 ? atoi(av[2]) : 0;
- int c1 = 0, c2 = 0;
- for (int x = 0; x < REPS; x++) {
- char* allocations[NUM];
- for (int i = 0; i < NUM/2; i++) {
- allocations[i] = (char*)malloc((11*i)%1024 + x);
- //printf("zz alloc: %d\n", (int)allocations[i]);
- assert(allocations[i]);
- if (i > 10 && i%4 == 1 && allocations[i-10]) {
- //printf("zz free: %d\n", (int)allocations[i-10]);
- free(allocations[i-10]);
- allocations[i-10] = NULL;
- }
- }
- for (int i = NUM/2; i < NUM; i++) {
- allocations[i] = (char*)malloc(1024*(i+1));
- //printf("zz alloc: %d\n", (int)allocations[i]);
- assert(allocations[i]);
- if (i > 10 && i%4 != 1 && allocations[i-10]) {
- //printf("zz free: %d\n", (int)allocations[i-10]);
- free(allocations[i-10]);
- allocations[i-10] = NULL;
- }
- }
- char* first = allocations[0];
- for (int i = 0; i < NUM; i++) {
- if (allocations[i]) {
- //printf("zz free: %d\n", (int)allocations[i]);
- free(allocations[i]);
- }
- }
- char *last = (char*)malloc(512); // should be identical, as we free'd it all
- //printf("zz last: %d\n", (int)last);
- char *newer = (char*)malloc(512); // should be different
- //printf("zz newer: %d\n", (int)newer);
- c1 += first == last;
- c2 += first == newer;
- }
- printf("*%d,%d*\n", c1, c2);
-}
-
-/* Some debugging tools: Make JS and native code work exactly the same */
-/*
-time_t time ( time_t * timer )
-{
- if (timer) *timer = 1;
- return 1;
-}
-
-long sysconf(int name)
-{
- printf("sysconf: %d (30 is page size)\n", name);
- return 4096;
-}
-
-void *sbrk(intptr_t increment)
-{
- static char spaace[1024*1024*1];
- static intptr_t where = 0;
- 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
- void *ret = &spaace[where];
- where += increment;
- return ret;
-}
-*/
-
diff --git a/tests/dlmalloc_test.c b/tests/dlmalloc_test.c
index 6fbc45ab..817778bd 100644
--- a/tests/dlmalloc_test.c
+++ b/tests/dlmalloc_test.c
@@ -4,7 +4,8 @@
#include <stdlib.h>
#include <assert.h>
-int main(int ac, char **av) {
+int main(int ac, char **av)
+{
int NUM = ac > 1 ? atoi(av[1]) : 0;
int REPS = ac > 2 ? atoi(av[2]) : 0;
int c1 = 0, c2 = 0;
@@ -12,16 +13,20 @@ int main(int ac, char **av) {
char* allocations[NUM];
for (int i = 0; i < NUM/2; i++) {
allocations[i] = (char*)malloc((11*i)%1024 + x);
+ //printf("zz alloc: %d\n", (int)allocations[i]);
assert(allocations[i]);
if (i > 10 && i%4 == 1 && allocations[i-10]) {
+ //printf("zz free: %d\n", (int)allocations[i-10]);
free(allocations[i-10]);
allocations[i-10] = NULL;
}
}
for (int i = NUM/2; i < NUM; i++) {
allocations[i] = (char*)malloc(1024*(i+1));
+ //printf("zz alloc: %d\n", (int)allocations[i]);
assert(allocations[i]);
if (i > 10 && i%4 != 1 && allocations[i-10]) {
+ //printf("zz free: %d\n", (int)allocations[i-10]);
free(allocations[i-10]);
allocations[i-10] = NULL;
}
@@ -29,13 +34,42 @@ int main(int ac, char **av) {
char* first = allocations[0];
for (int i = 0; i < NUM; i++) {
if (allocations[i]) {
+ //printf("zz free: %d\n", (int)allocations[i]);
free(allocations[i]);
}
}
char *last = (char*)malloc(512); // should be identical, as we free'd it all
+ //printf("zz last: %d\n", (int)last);
char *newer = (char*)malloc(512); // should be different
+ //printf("zz newer: %d\n", (int)newer);
c1 += first == last;
c2 += first == newer;
}
printf("*%d,%d*\n", c1, c2);
}
+
+/* Some debugging tools: Make JS and native code work exactly the same */
+/*
+time_t time ( time_t * timer )
+{
+ if (timer) *timer = 1;
+ return 1;
+}
+
+long sysconf(int name)
+{
+ printf("sysconf: %d (30 is page size)\n", name);
+ return 4096;
+}
+
+void *sbrk(intptr_t increment)
+{
+ static char spaace[1024*1024*1];
+ static intptr_t where = 0;
+ 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
+ void *ret = &spaace[where];
+ where += increment;
+ return ret;
+}
+*/
+
diff --git a/tests/runner.py b/tests/runner.py
index 1aca1e03..4f2cc43f 100644
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -181,10 +181,11 @@ class RunnerCore(unittest.TestCase):
# Run Emscripten
exported_settings = {}
- for setting in ['QUANTUM_SIZE', 'RELOOP', 'OPTIMIZE', 'ASSERTIONS', 'USE_TYPED_ARRAYS', 'SAFE_HEAP', 'CHECK_OVERFLOWS', 'CORRECT_OVERFLOWS', 'CORRECT_SIGNS', 'CHECK_SIGNS', 'CORRECT_OVERFLOWS_LINES', 'CORRECT_SIGNS_LINES', 'CORRECT_ROUNDINGS', 'CORRECT_ROUNDINGS_LINES', 'INVOKE_RUN', 'SAFE_HEAP_LINES', 'INIT_STACK', 'AUTO_OPTIMIZE', 'EXPORTED_FUNCTIONS', 'EXPORTED_GLOBALS', 'BUILD_AS_SHARED_LIB', 'INCLUDE_FULL_LIBRARY', 'RUNTIME_TYPE_INFO', 'DISABLE_EXCEPTION_CATCHING', 'FAST_MEMORY', 'EXCEPTION_DEBUG', 'PROFILE']:
+ for setting in ['QUANTUM_SIZE', 'RELOOP', 'OPTIMIZE', 'ASSERTIONS', 'USE_TYPED_ARRAYS', 'SAFE_HEAP', 'CHECK_OVERFLOWS', 'CORRECT_OVERFLOWS', 'CORRECT_SIGNS', 'CHECK_SIGNS', 'CORRECT_OVERFLOWS_LINES', 'CORRECT_SIGNS_LINES', 'CORRECT_ROUNDINGS', 'CORRECT_ROUNDINGS_LINES', 'INVOKE_RUN', 'SAFE_HEAP_LINES', 'INIT_STACK', 'AUTO_OPTIMIZE', 'EXPORTED_FUNCTIONS', 'EXPORTED_GLOBALS', 'BUILD_AS_SHARED_LIB', 'INCLUDE_FULL_LIBRARY', 'RUNTIME_TYPE_INFO', 'DISABLE_EXCEPTION_CATCHING', 'TOTAL_MEMORY', 'FAST_MEMORY', 'EXCEPTION_DEBUG', 'PROFILE']:
try:
value = eval(setting)
- exported_settings[setting] = value
+ if value is not None:
+ exported_settings[setting] = value
except:
pass
settings = ['-s %s=%s' % (k, json.dumps(v)) for k, v in exported_settings.items()]
@@ -2975,11 +2976,17 @@ if 'benchmark' not in str(sys.argv):
def test_dlmalloc(self):
global CORRECT_SIGNS; CORRECT_SIGNS = 2
global CORRECT_SIGNS_LINES; CORRECT_SIGNS_LINES = ['src.cpp:' + str(i) for i in [4816, 4191, 4246, 4199, 4205, 4235, 4227]]
+ global TOTAL_MEMORY; TOTAL_MEMORY = 100*1024*1024 # needed with typed arrays
- src = open(path_from_root('src', 'dlmalloc.c'), 'r').read() + '\n\n\n' + open(path_from_root('tests', 'dlmalloc.c'), 'r').read()
+ src = open(path_from_root('src', 'dlmalloc.c'), 'r').read() + '\n\n\n' + open(path_from_root('tests', 'dlmalloc_test.c'), 'r').read()
self.do_test(src, '*1,0*', ['200', '1'])
self.do_test(src, '*400,0*', ['400', '400'], no_build=True)
+ # Linked version
+ src = open(path_from_root('tests', 'dlmalloc_test.c'), 'r').read()
+ self.do_test(src, '*1,0*', ['200', '1'], extra_emscripten_args=['-m'])
+ self.do_test(src, '*400,0*', ['400', '400'], extra_emscripten_args=['-m'], no_build=True)
+
def zzztest_gl(self):
# Switch to gcc from g++ - we don't compile properly otherwise (why?)
global COMPILER
@@ -3968,10 +3975,6 @@ Child2:9
shutil.copy(filename + '.o.js', os.path.join(self.get_dir(), 'new.cpp.o.js'))
self.do_test(None, 'test\n', basename='new.cpp', no_build=True)
- def test_dlmalloc_linked(self):
- src = open(path_from_root('tests', 'dlmalloc_test.c'), 'r').read()
- self.do_test(src, '*1,0*', ['200', '1'], extra_emscripten_args=['-m'])
-
def test_linespecific(self):
global CHECK_SIGNS; CHECK_SIGNS = 0
global CHECK_OVERFLOWS; CHECK_OVERFLOWS = 0
@@ -4150,7 +4153,7 @@ Child2:9
exec('''
class %s(T):
def setUp(self):
- global COMPILER, QUANTUM_SIZE, RELOOP, OPTIMIZE, ASSERTIONS, USE_TYPED_ARRAYS, LLVM_OPTS, SAFE_HEAP, CHECK_OVERFLOWS, CORRECT_OVERFLOWS, CORRECT_OVERFLOWS_LINES, CORRECT_SIGNS, CORRECT_SIGNS_LINES, CHECK_SIGNS, COMPILER_TEST_OPTS, CORRECT_ROUNDINGS, CORRECT_ROUNDINGS_LINES, INVOKE_RUN, SAFE_HEAP_LINES, INIT_STACK, AUTO_OPTIMIZE, RUNTIME_TYPE_INFO, DISABLE_EXCEPTION_CATCHING, PROFILE
+ global COMPILER, QUANTUM_SIZE, RELOOP, OPTIMIZE, ASSERTIONS, USE_TYPED_ARRAYS, LLVM_OPTS, SAFE_HEAP, CHECK_OVERFLOWS, CORRECT_OVERFLOWS, CORRECT_OVERFLOWS_LINES, CORRECT_SIGNS, CORRECT_SIGNS_LINES, CHECK_SIGNS, COMPILER_TEST_OPTS, CORRECT_ROUNDINGS, CORRECT_ROUNDINGS_LINES, INVOKE_RUN, SAFE_HEAP_LINES, INIT_STACK, AUTO_OPTIMIZE, RUNTIME_TYPE_INFO, DISABLE_EXCEPTION_CATCHING, PROFILE, TOTAL_MEMORY, FAST_MEMORY
COMPILER = %r
llvm_opts = %d
@@ -4175,6 +4178,8 @@ class %s(T):
RUNTIME_TYPE_INFO = 0
DISABLE_EXCEPTION_CATCHING = 0
PROFILE = 0
+ TOTAL_MEMORY = FAST_MEMORY = None
+
if LLVM_OPTS:
self.pick_llvm_opts(3, True)
COMPILER_TEST_OPTS = ['-g']
@@ -4243,6 +4248,8 @@ else:
CORRECT_OVERFLOWS_LINES = CORRECT_SIGNS_LINES = CORRECT_ROUNDINGS_LINES = SAFE_HEAP_LINES = []
LLVM_OPTS = 1
DISABLE_EXCEPTION_CATCHING = 1
+ if USE_TYPED_ARRAYS:
+ TOTAL_MEMORY = 100*1024*1024 # XXX Needed for dlmalloc. TODO: Test other values
FAST_MEMORY = 10*1024*1024
TEST_REPS = 4
@@ -4405,7 +4412,7 @@ else:
global CORRECT_SIGNS; CORRECT_SIGNS = 2
global CORRECT_SIGNS_LINES; CORRECT_SIGNS_LINES = ['src.cpp:' + str(i) for i in [4816, 4191, 4246, 4199, 4205, 4235, 4227]]
- src = open(path_from_root('tests', 'dlmalloc.c'), 'r').read()
+ src = open(path_from_root('src', 'dlmalloc.c'), 'r').read() + '\n\n\n' + open(path_from_root('tests', 'dlmalloc_test.c'), 'r').read()
self.do_benchmark(src, ['400', '400'], '*400,0*')
if __name__ == '__main__':