From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/4755 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: Proposed approach for malloc to deal with failing brk Date: Mon, 31 Mar 2014 00:32:48 -0400 Message-ID: <20140331043247.GC26358@brightrain.aerifal.cx> References: <20140331004104.GA15223@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="qjNfmADvan18RZcF" X-Trace: ger.gmane.org 1396240389 9230 80.91.229.3 (31 Mar 2014 04:33:09 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 31 Mar 2014 04:33:09 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-4759-gllmg-musl=m.gmane.org@lists.openwall.com Mon Mar 31 06:33:05 2014 Return-path: Envelope-to: gllmg-musl@plane.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1WUTu9-0004MN-Ro for gllmg-musl@plane.gmane.org; Mon, 31 Mar 2014 06:33:01 +0200 Original-Received: (qmail 13824 invoked by uid 550); 31 Mar 2014 04:33:00 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 13816 invoked from network); 31 Mar 2014 04:32:59 -0000 Content-Disposition: inline In-Reply-To: <20140331004104.GA15223@brightrain.aerifal.cx> User-Agent: Mutt/1.5.21 (2010-09-15) Xref: news.gmane.org gmane.linux.lib.musl.general:4755 Archived-At: --qjNfmADvan18RZcF Content-Type: text/plain; charset=us-ascii Content-Disposition: inline > 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 --qjNfmADvan18RZcF Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="brk_fallback.diff" 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; --qjNfmADvan18RZcF--