Permalink
Browse files

fix dlmalloc with typed arrays

  • Loading branch information...
1 parent 5ab3f66 commit 82004ce7a9d4d1be661d4adb9478a674930f7235 @kripken committed Oct 8, 2011
Showing with 51 additions and 81 deletions.
  1. +0 −1 src/dlmalloc.c
  2. +0 −70 tests/dlmalloc.c
  3. +35 −1 tests/dlmalloc_test.c
  4. +16 −9 tests/runner.py
View
@@ -1,4 +1,3 @@
-#include "emscripten.h"
#define __THROW
#define __attribute_malloc__
#define __wur
View
@@ -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;
-}
-*/
-
View
@@ -4,38 +4,72 @@
#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;
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;
+}
+*/
+
View
@@ -181,10 +181,11 @@ def do_emscripten(self, filename, output_processor=None, append_ext=True, extra_
# 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 @@ def test_fasta(self):
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 @@ def test_autoassemble(self):
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 @@ def make_test(name, compiler, llvm_opts, embetter, quantum_size, typed_arrays):
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 @@ def setUp(self):
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 @@ def test_eliminator(self):
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 @@ 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]]
- 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__':

0 comments on commit 82004ce

Please sign in to comment.