diff options
author | Alon Zakai <alonzakai@gmail.com> | 2012-01-10 15:16:27 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2012-01-10 15:16:27 -0800 |
commit | 0a71c5438c46d535cc714b927ae328563d76eba6 (patch) | |
tree | 76694c3ba9e965db29320fc964bfab0e4ee968f4 | |
parent | c5c0d1af29a152eb9504757e60ed3a2c48ea28cc (diff) | |
parent | ef3c715e8338b1f076d89239fe20aaa69956d743 (diff) |
Merge pull request #169 from jterrace/mmap_malloc_dlmalloc_fixes
malloc and mmap fixes
-rw-r--r-- | src/dlmalloc.c | 5 | ||||
-rw-r--r-- | src/library.js | 20 |
2 files changed, 18 insertions, 7 deletions
diff --git a/src/dlmalloc.c b/src/dlmalloc.c index aa37dc0d..6185b1c7 100644 --- a/src/dlmalloc.c +++ b/src/dlmalloc.c @@ -588,7 +588,10 @@ MAX_RELEASE_CHECK_RATE default: 4095 unless not HAVE_MMAP #define INSECURE 0 #endif /* INSECURE */ #ifndef HAVE_MMAP -#define HAVE_MMAP 1 +/* XXX Emscripten + * mmap uses malloc, so malloc can't use mmap + */ +#define HAVE_MMAP 0 #endif /* HAVE_MMAP */ #ifndef MMAP_CLEARS #define MMAP_CLEARS 1 diff --git a/src/library.js b/src/library.js index 5a77315c..a0160279 100644 --- a/src/library.js +++ b/src/library.js @@ -3177,10 +3177,13 @@ LibraryManager.library = { mmap__deps: ['$FS'], mmap: function(start, num, prot, flags, stream, offset) { - // FIXME: Leaky and non-share - //if (stream == -1) { // XXX We should handle -1 here, but this code leads to an infinite loop - // return allocate(num, 'i8', ALLOC_NORMAL); - //} + /* FIXME: Since mmap is normally implemented at the kernel level, + * this implementation simply uses malloc underneath the call to + * mmap. + */ + if (stream == -1) { + return allocate(num, 'i8', ALLOC_NORMAL); + } var info = FS.streams[stream]; if (!info) return -1; return allocate(info.object.contents.slice(offset, offset+num), @@ -3189,7 +3192,6 @@ LibraryManager.library = { __01mmap64_: 'mmap', munmap: function(start, num) { - // FIXME: Not really correct at all. _free(start); }, @@ -3200,7 +3202,13 @@ LibraryManager.library = { // ========================================================================== malloc: function(bytes) { - return Runtime.staticAlloc(bytes || 1); // accept 0 as an input because libc implementations tend to + /* Over-allocate to make sure it is byte-aligned by 8. + * This will leak memory, but this is only the dummy + * implementation (replaced by dlmalloc normally) so + * not an issue. + */ + ptr = Runtime.staticAlloc(bytes + 8); + return (ptr+8) & 0xFFFFFFF8; }, _Znwj: 'malloc', _Znaj: 'malloc', |