mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@aerifal.cx>
To: musl@lists.openwall.com
Subject: Re: Proposed approach for malloc to deal with failing brk
Date: Mon, 31 Mar 2014 00:32:48 -0400	[thread overview]
Message-ID: <20140331043247.GC26358@brightrain.aerifal.cx> (raw)
In-Reply-To: <20140331004104.GA15223@brightrain.aerifal.cx>

[-- Attachment #1: Type: text/plain, Size: 168 bytes --]

> Perhaps the best part is that this solution can be implemented in just
> a few lines of code.

And here's the patch. Please test and let me know if this works.

Rich

[-- Attachment #2: brk_fallback.diff --]
[-- Type: text/plain, Size: 1098 bytes --]

diff --git a/src/malloc/malloc.c b/src/malloc/malloc.c
index d6ad904..3c1ddcd 100644
--- a/src/malloc/malloc.c
+++ b/src/malloc/malloc.c
@@ -37,6 +37,7 @@ static struct {
 	struct bin bins[64];
 	int brk_lock[2];
 	int free_lock[2];
+	unsigned mmap_areas;
 } mal;
 
 
@@ -162,7 +163,31 @@ static struct chunk *expand_heap(size_t n)
 	new = mal.brk + n + SIZE_ALIGN + PAGE_SIZE - 1 & -PAGE_SIZE;
 	n = new - mal.brk;
 
-	if (__brk(new) != new) goto fail;
+	if (__brk(new) != new) {
+		size_t min = (size_t)PAGE_SIZE << ++mal.mmap_areas/2;
+		if (!min) mal.mmap_areas--;
+		n += -n & PAGE_SIZE-1;
+		if (n < min) n = min;
+		void *area = __mmap(0, n, PROT_READ|PROT_WRITE,
+			MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+		if (area == MAP_FAILED) {
+			mal.mmap_areas = 0;
+			goto fail;
+		}
+
+		area = (char *)area + SIZE_ALIGN - OVERHEAD;
+		w = area;
+		n -= SIZE_ALIGN;
+		w->psize = 0 | C_INUSE;
+		w->csize = n | C_INUSE;
+		w = NEXT_CHUNK(w);
+		w->psize = n | C_INUSE;
+		w->csize = 0 | C_INUSE;
+
+		unlock(mal.brk_lock);
+
+		return area;
+	}
 
 	w = MEM_TO_CHUNK(new);
 	w->psize = n | C_INUSE;

  reply	other threads:[~2014-03-31  4:32 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-03-31  0:41 Rich Felker
2014-03-31  4:32 ` Rich Felker [this message]
2014-03-31  7:44   ` u-igbb
2014-03-31 11:05 ` Szabolcs Nagy
2014-04-01 16:40 ` Vasily Kulikov
2014-04-01 17:01   ` Szabolcs Nagy
2014-04-01 17:03   ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140331043247.GC26358@brightrain.aerifal.cx \
    --to=dalias@aerifal.cx \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).