@cindex storage allocation
This chapter describes how processes manage and use memory in a system
-that uses the GNU C library.
+that uses the GNU C library.
The GNU C Library has several functions for dynamically allocating
virtual memory in various ways. They vary in generality and in
Allocation usually brings to mind meting out scarce resources, but in
the case of virtual memory, that's not a major goal, because there is
generally much more of it than anyone needs. Memory allocation within a
-process is mainly just a matter of making sure that the same byte of
+process is mainly just a matter of making sure that the same byte of
memory isn't used to store two different things.
Processes allocate memory in two major ways: by exec and
@itemize @bullet
-@item
+@item
The @dfn{text segment} contains a program's instructions and literals and
static constants. It is allocated by exec and stays the same size for
-the life of the virtual address space.
+the life of the virtual address space.
@item
The @dfn{data segment} is working storage for the program. It can be
it by calling functions as described in @xref{Resizing the Data
Segment}. Its lower end is fixed.
-@item
+@item
The @dfn{stack segment} contains a program stack. It grows as the stack
grows, but doesn't shrink when the stack shrinks.
@node Memory Allocation
-@section Allocating Storage For a Program's Data
+@section Allocating Storage For Program Data
This section covers how ordinary programs manage storage for their data,
including the famous @code{malloc} function and some fancier facilities
block and clear it.
* Efficiency and Malloc:: Efficiency considerations in use of
these functions.
-* Aligned Memory Blocks:: Allocating specially aligned memory:
- @code{memalign} and @code{valloc}.
+* Aligned Memory Blocks:: Allocating specially aligned memory.
* Malloc Tunable Parameters:: Use @code{mallopt} to adjust allocation
parameters.
* Heap Consistency Checking:: Automatic checking for errors.
that it can hold any type of data. In the GNU system, the address is
always a multiple of eight on most systems, and a multiple of 16 on
64-bit systems. Only rarely is any higher boundary (such as a page
-boundary) necessary; for those cases, use @code{memalign} or
-@code{valloc} (@pxref{Aligned Memory Blocks}).
+boundary) necessary; for those cases, use @code{memalign},
+@code{posix_memalign} or @code{valloc} (@pxref{Aligned Memory Blocks}).
Note that the memory located after the end of the block is likely to be
in use for something else; perhaps a block already allocated by another
The address of a block returned by @code{malloc} or @code{realloc} in
the GNU system is always a multiple of eight (or sixteen on 64-bit
systems). If you need a block whose address is a multiple of a higher
-power of two than that, use @code{memalign} or @code{valloc}. These
-functions are declared in @file{stdlib.h}.
+power of two than that, use @code{memalign}, @code{posix_memalign}, or
+@code{valloc}. These functions are declared in @file{stdlib.h}.
With the GNU library, you can use @code{free} to free the blocks that
-@code{memalign} and @code{valloc} return. That does not work in BSD,
-however---BSD does not provide any way to free such blocks.
+@code{memalign}, @code{posix_memalign}, and @code{valloc} return. That
+does not work in BSD, however---BSD does not provide any way to free
+such blocks.
@comment malloc.h stdlib.h
@comment BSD
that is on the specified boundary.
@end deftypefun
+@comment stdlib.h
+@comment POSIX
+@deftypefun int posix_memalign (void **@var{memptr}, size_t @var{alignment}, size_t @var{size})
+The @code{posix_memalign} function is similar to the @code{memalign}
+function in that it returns a buffer of @var{size} bytes aligned to a
+multiple of @var{alignment}. But it adds one requirement to the
+parameter @var{alignment}: the value must be a power of two multiple of
+@code{sizeof (void *)}.
+
+If the function succeeds in allocation memory a pointer to the allocated
+memory is returned in @code{*@var{memptr}} and the return value is zero.
+Otherwise the function returns an error value indicating the problem.
+
+This function was introduced in POSIX 1003.1d.
+@end deftypefun
+
@comment malloc.h stdlib.h
@comment BSD
@deftypefun {void *} valloc (size_t @var{size})
much later, and the true cause for the problem is then very hard to
track down.
+There is one problem with @code{MALLOC_CHECK_}: in SUID or SGID binaries
+it could possibly be exploited since diverging from the normal programs
+behaviour it now writes something to the standard error desriptor.
+Therefore the use of @code{MALLOC_CHECK_} is disabled by default for
+SUID and SGID binaries. It can be enabled again by the system
+administrator by adding a file @file{/etc/suid-debug} (the content is
+not important it could be empty).
+
So, what's the difference between using @code{MALLOC_CHECK_} and linking
with @samp{-lmcheck}? @code{MALLOC_CHECK_} is orthogonal with respect to
@samp{-lmcheck}. @samp{-lmcheck} has been added for backward
@item int keepcost
This is the size of the top-most releasable chunk that normally
-borders the end of the heap (i.e. the high end of the virtual address
+borders the end of the heap (i.e. the high end of the virtual address
space's data segment).
@end table
the data segment. (This is considered success, by the way).
The function fails if it would cause the data segment to overlap another
-segment or exceed the process' data storage limit (@pxref{Limits on
+segment or exceed the process' data storage limit (@pxref{Limits on
Resources}).
The function is named for a common historical case where data storage
called the @dfn{break}.
The return value is zero on success. On failure, the return value is
-@code{-1} and @code{errno} is set accordingly. The following @code{errno}
+@code{-1} and @code{errno} is set accordingly. The following @code{errno}
values are specific to this function:
@table @code
@subsection Why Lock Pages
Because page faults cause paged out pages to be paged in transparently,
-a process rarely needs to be concerned about locking pages. However,
+a process rarely needs to be concerned about locking pages. However,
there are two reasons people sometimes are:
@itemize @bullet
Two virtual pages that are not shared memory can nonetheless be backed
by the same real frame. The kernel does this in the name of efficiency
when it knows both virtual pages contain identical data, and does it
-even if one or both of the virtual pages are locked.
+even if one or both of the virtual pages are locked.
But when a process modifies one of those pages, the kernel must get it a
separate frame and fill it with the page's data. This is known as a
address space and turn off @code{MCL_FUTURE} future locking mode.
The return value is zero if the function succeeds. Otherwise, it is
-@code{-1} and @code{errno} is set accordingly. The only way this
+@code{-1} and @code{errno} is set accordingly. The only way this
function can fail is for generic reasons that all functions and system
calls can fail, so there are no specific @code{errno} values.
@enumerate
@item
-hostname
+hostname
@cindex hostname
@item
domain name
not the same thing, and that people often incorrectly refer to entire
host names as ``domain names.''
-In DNS, the full host name is properly called the FQDN (Fully Qualified
+In DNS, the full host name is properly called the FQDN (Fully Qualified
Domain Name) and consists of the hostname, then a period, then the
domain name. The domain name itself usually has multiple components
separated by periods. So for example, a system's hostname may be
-@samp{chicken} and its domain name might be @samp{ai.mit.edu}, so
+@samp{chicken} and its domain name might be @samp{ai.mit.edu}, so
its FQDN (which is its host name) is @samp{chicken.ai.mit.edu}.
@cindex FQDN
Adding to the confusion, though, is that DNS is not the only name space
-in which a computer needs to be known. Another name space is the
+in which a computer needs to be known. Another name space is the
NIS (aka YP) name space. For NIS purposes, there is another domain
name, which is called the NIS domain name or the YP domain name. It
need not have anything to do with the DNS domain name.
Confusing things even more is the fact that in DNS, it is possible for
multiple FQDNs to refer to the same system. However, there is always
exactly one of them that is the true host name, and it is called the
-canonical FQDN.
+canonical FQDN.
In some contexts, the host name is called a ``node name.''
@pindex hostname
@pindex hostid
@pindex unistd.h
-Prototypes for these functions appear in @file{unistd.h}.
+Prototypes for these functions appear in @file{unistd.h}.
The programs @code{hostname}, @code{hostid}, and @code{domainname} work
by calling these functions.
it happens just once, at system boot time.
The proper way to establish the primary IP address of a system
-is to configure the IP address resolver to associate that IP address with
+is to configure the IP address resolver to associate that IP address with
the system's host name as returned by @code{gethostname}. For example,
put a record for the system in @file{/etc/hosts}.
@file{sys/utsname.h}.
@pindex sys/utsname.h
-As a bonus, @code{uname} also gives some information identifying the
+As a bonus, @code{uname} also gives some information identifying the
particular system your program is running on. This is the same information
-which you can get with functions targetted to this purpose described in
+which you can get with functions targetted to this purpose described in
@ref{Host Identification}.
@item char domainname[]
This is the NIS or YP domain name. It is the same value returned by
-@code{getdomainname}; see @ref{Host Identification}. This element
+@code{getdomainname}; see @ref{Host Identification}. This element
is a relatively recent invention and use of it is not as portable as
use of the rest of the structure.
The file @var{dev} is not a block device special file.
@item EBUSY
-@itemize
+@itemize @bullet
@item
The device is already mounted.
@end itemize
@item EINVAL
-@itemize
+@itemize @bullet
@item
A remount was attempted, but there is no filesystem mounted over the
@end itemize
@item EACCESS
-@itemize
+@itemize @bullet
@item
The filesystem is inherently read-only (possibly due to a switch on the
If you don't want the parameter value returned, specify a null pointer
for @var{oldval}.
-To set the parameter, specify the address and length of the new value
+To set the parameter, specify the address and length of the new value
as @var{newval} and @var{newlen}. If you don't want to set the parameter,
specify a null pointer as @var{newval}.
@table @code
@item EPERM
-The process is not permitted to access one of the components of the
+The process is not permitted to access one of the components of the
path of the system parameter or is not permitted to access the system parameter
itself in the way (read or write) that it requested.
-@c There is some indication in the Linux 2.2 code that the code is trying to
+@c There is some indication in the Linux 2.2 code that the code is trying to
@c return EACCESS here, but the EACCESS value never actually makes it to the
@c user.
@item ENOTDIR
@end deftypefun
If you have a Linux kernel with the @code{proc} filesystem, you can get
-and set most of the same parameters by reading and writing to files in
+and set most of the same parameters by reading and writing to files in
the @code{sys} directory of the @code{proc} filesystem. In the @code{sys}
directory, the directory structure represents the hierarchical structure
of the parameters. E.g. you can display the free page thresholds with
@item
@code{bdflush}
@end itemize
-