From 282ad434a8bca760372f98ceec8a15725bf30bd1 Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Thu, 23 Jan 2014 01:08:37 +0100 Subject: [PATCH] Memory allocation: do not use mmap for memory allocation on 64 bit. On Windows we can only do mmap memory allocation up to 4 GB, which causes a crash when doing very large renders on 64 bit systems with a lot of memory. As far as I can tell the reason to use mmap is to get around address space limitation on some 32 bit operating systems, and I can't see a reason to use it on 64 bit. For the original explanation see here: http://orange.blender.org/blog/stupid-memory-problems Fixes T37841. --- intern/guardedalloc/intern/mallocn_guarded_impl.c | 6 ++++++ intern/guardedalloc/intern/mallocn_lockfree_impl.c | 6 ++++++ intern/guardedalloc/intern/mmap_win.c | 1 + 3 files changed, 13 insertions(+) diff --git a/intern/guardedalloc/intern/mallocn_guarded_impl.c b/intern/guardedalloc/intern/mallocn_guarded_impl.c index 92392ce4dd3..9aa140d824c 100644 --- a/intern/guardedalloc/intern/mallocn_guarded_impl.c +++ b/intern/guardedalloc/intern/mallocn_guarded_impl.c @@ -541,6 +541,12 @@ void *MEM_guarded_mapallocN(size_t len, const char *str) { MemHead *memh; + /* on 64 bit, simply use calloc instead, as mmap does not support + * allocating > 4 GB on Windows. the only reason mapalloc exists + * is to get around address space limitations in 32 bit OSes. */ + if(sizeof(void*) >= 8) + return MEM_lockfree_callocN(len, str); + len = SIZET_ALIGN_4(len); #if defined(WIN32) diff --git a/intern/guardedalloc/intern/mallocn_lockfree_impl.c b/intern/guardedalloc/intern/mallocn_lockfree_impl.c index 44f51a34134..2c7c087966a 100644 --- a/intern/guardedalloc/intern/mallocn_lockfree_impl.c +++ b/intern/guardedalloc/intern/mallocn_lockfree_impl.c @@ -265,6 +265,12 @@ void *MEM_lockfree_mapallocN(size_t len, const char *str) { MemHead *memh; + /* on 64 bit, simply use calloc instead, as mmap does not support + * allocating > 4 GB on Windows. the only reason mapalloc exists + * is to get around address space limitations in 32 bit OSes. */ + if(sizeof(void*) >= 8) + return MEM_lockfree_callocN(len, str); + len = SIZET_ALIGN_4(len); #if defined(WIN32) diff --git a/intern/guardedalloc/intern/mmap_win.c b/intern/guardedalloc/intern/mmap_win.c index 3096c589101..4f65c0a742f 100644 --- a/intern/guardedalloc/intern/mmap_win.c +++ b/intern/guardedalloc/intern/mmap_win.c @@ -129,6 +129,7 @@ void *mmap(void *UNUSED(start), size_t len, int prot, int flags, int fd, off_t o } } + /* note len is passed to a 32 bit DWORD, so can't be > 4 GB */ maphandle = CreateFileMapping(fhandle, NULL, prot_flags, 0, len, NULL); if (maphandle == 0) { errno = EBADF;