mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@aerifal.cx>
To: musl@lists.openwall.com
Subject: malloc and linux memory layout
Date: Wed, 10 Aug 2011 15:56:58 -0400	[thread overview]
Message-ID: <20110810195658.GC132@brightrain.aerifal.cx> (raw)

Since Luka has had a number of questions about when/why malloc could
fail, heap/brk/mmap allocation, etc. I thought I'd post a quick
description of the virtual address space is managed on Linux:

Each process has its own 32- or 64-bit virtual address space.
Initially, from bottom to top, it looks something like:

[low unmappable pages]
[main program text (code) segment]
[main program data segment]
[brk segment (heap)]
[....lots of open virtual address space...]
[mmap zone]
[main thread stack]
[reserved for kernelspace use]

(Note that there will be small randomized amounts of empty/unused
address space between these regions if ALSR is enabled.)x

The brk segment starts just above the program's static data and grows
upward into the big open space. The mmap zone (where mmaps are put by
default) starts just below the stack limit and continues downward as
more mappings are made. Previously-mapped ranges here are available
for reuse after they're unmapped; that's managed by the kernel. The
brk is not allowed to run into any other mapping when it grows upward,
so if there's a mapping right above the brk segment, attempts to grow
it will always fail.

Shared libraries are mapped in the mmap zone just like another other
mmap.

Typically malloc implementations will use a mix of brk and mmap to
satisfy malloc requests, using individual mmaps for large allocations
(letting the kernel take care of it and making it easy to free the
whole thing back to the system) and carving up the brk segment (and
possibly additional mmapped regions) to handle small allocations (this
avoids the overhead of syscalls and page alignment for small
allocations). musl only uses brk for small allocations, but glibc's
ptmalloc and others are happy to use mmap to make a new "heap region"
if the brk can't grow. The difference will generally only be seen if
virtual address space has already been exhausted.

Hope this helps..

Rich


             reply	other threads:[~2011-08-10 19:56 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-10 19:56 Rich Felker [this message]
2011-08-11  7:43 ` Vasiliy Kulikov
2011-08-11 14:54   ` 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=20110810195658.GC132@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).