aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2012-01-07 18:01:09 -0800
committerAlon Zakai <alonzakai@gmail.com>2012-01-07 18:01:09 -0800
commit5c02f40d3422617d0be4f0542b8b5281cae7883b (patch)
tree0a486e9da09016e3c47981d88844f3ccbc65acab
parent16230c8cf1e947e03714da3fd3307a9eff41405a (diff)
automatic malloc need detection for new
-rwxr-xr-xemcc2
-rw-r--r--tests/new.cpp38
-rw-r--r--tests/runner.py12
3 files changed, 50 insertions, 2 deletions
diff --git a/emcc b/emcc
index 36c2464a..c4575310 100755
--- a/emcc
+++ b/emcc
@@ -439,7 +439,7 @@ try:
has_dlmalloc = False
for input_file in input_files:
symbols = shared.Building.llvm_nm(in_temp(unsuffixed_basename(input_file) + '.o'))
- for malloc_def in ['malloc', 'free', 'calloc', 'memalign', 'realloc', 'valloc', 'pvalloc', 'mallinfo', 'mallopt', 'malloc_trim', 'malloc_stats', 'malloc_usable_size', 'malloc_footprint', 'malloc_max_footprint', 'independent_calloc', 'independent_comalloc']:
+ for malloc_def in ['malloc', 'free', 'calloc', 'memalign', 'realloc', 'valloc', 'pvalloc', 'mallinfo', 'mallopt', 'malloc_trim', 'malloc_stats', 'malloc_usable_size', 'malloc_footprint', 'malloc_max_footprint', 'independent_calloc', 'independent_comalloc', '_Znwj', '_Znaj', '_Znam', '_Znwm']:
if malloc_def in symbols.undefs:
need_dlmalloc = True
if malloc_def in symbols.defs:
diff --git a/tests/new.cpp b/tests/new.cpp
new file mode 100644
index 00000000..3e09fbc1
--- /dev/null
+++ b/tests/new.cpp
@@ -0,0 +1,38 @@
+// Emscripten tests
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+
+struct Structy { char data[100]; int x; };
+
+int main() {
+ int NUM = 100;
+ char* allocations[NUM];
+ for (int i = 0; i < NUM/2; i++) {
+ allocations[i] = (char*){{{ NEW }}};
+ assert(allocations[i]);
+ if (i > 10 && i%4 == 1 && allocations[i-10]) {
+ {{{ DELETE }}}(allocations[i-10]);
+ allocations[i-10] = NULL;
+ }
+ }
+ for (int i = NUM/2; i < NUM; i++) {
+ allocations[i] = (char*){{{ NEW }}};
+ assert(allocations[i]);
+ if (i > 10 && i%4 != 1 && allocations[i-10]) {
+ {{{ DELETE }}}(allocations[i-10]);
+ allocations[i-10] = NULL;
+ }
+ }
+ char* first = allocations[0];
+ for (int i = 0; i < NUM; i++) {
+ if (allocations[i]) {
+ {{{ DELETE }}}(allocations[i]);
+ }
+ }
+ char *last = (char*){{{ NEW }}}; /* should be identical, as we free'd it all */
+ char *newer = (char*){{{ NEW }}}; /* should be different */
+ printf("*%d,%d*\n", first == last, first == newer);
+}
+
diff --git a/tests/runner.py b/tests/runner.py
index 86d37e8e..34071a91 100644
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -3844,11 +3844,21 @@ def process(filename):
try_delete(os.path.join(self.get_dir(), 'src.cpp.o.js'))
output = Popen([EMCC, path_from_root('tests', 'dlmalloc_test.c'),
'-o', os.path.join(self.get_dir(), 'src.cpp.o.js')], stdout=PIPE, stderr=self.stderr_redirect).communicate()
- #print output
self.do_run('x', '*1,0*', ['200', '1'], no_build=True)
self.do_run('x', '*400,0*', ['400', '400'], no_build=True)
+ # The same for new and all its variants
+ src = open(path_from_root('tests', 'new.cpp')).read()
+ for new, delete in [
+ ('malloc(100)', 'free'),
+ ('new char[100]', 'delete[]'),
+ ('new Structy', 'delete'),
+ ('new int', 'delete'),
+ ('new Structy[10]', 'delete[]'),
+ ]:
+ self.do_run(src.replace('{{{ NEW }}}', new).replace('{{{ DELETE }}}', delete), '*1,0*')
+
def test_libcxx(self):
self.do_run(path_from_root('tests', 'libcxx'),
'june -> 30\nPrevious (in alphabetical order) is july\nNext (in alphabetical order) is march',