OSDN Git Service

handle mremap failure in realloc of mmap-serviced allocations
authorRich Felker <dalias@aerifal.cx>
Thu, 15 Jun 2017 16:54:40 +0000 (12:54 -0400)
committerRich Felker <dalias@aerifal.cx>
Thu, 15 Jun 2017 16:54:40 +0000 (12:54 -0400)
mremap seems to always fail on nommu, and on some non-Linux
implementations of the Linux syscall API, it at least fails to
increase allocation size, and may fail to move (i.e. defragment) the
existing mapping when shrinking it too. instead of failing realloc or
leaving an over-sized allocation that may waste a large amount of
memory, fallback to malloc-memcpy-free if mremap fails.

src/malloc/malloc.c

index c38c46f..d5ee428 100644 (file)
@@ -406,7 +406,7 @@ void *realloc(void *p, size_t n)
                if (oldlen == newlen) return p;
                base = __mremap(base, oldlen, newlen, MREMAP_MAYMOVE);
                if (base == (void *)-1)
-                       return newlen < oldlen ? p : 0;
+                       goto copy_realloc;
                self = (void *)(base + extra);
                self->csize = newlen - extra;
                return CHUNK_TO_MEM(self);
@@ -439,6 +439,7 @@ void *realloc(void *p, size_t n)
                return CHUNK_TO_MEM(self);
        }
 
+copy_realloc:
        /* As a last resort, allocate a new chunk and copy to it. */
        new = malloc(n-OVERHEAD);
        if (!new) return 0;