|
@ -30,7 +30,7 @@ |
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|
|
.\" SUCH DAMAGE. |
|
|
.\" SUCH DAMAGE. |
|
|
.\" |
|
|
.\" |
|
|
.\" $OpenBSD: malloc.3,v 1.42 2006/01/18 06:36:05 jakemsr Exp $ |
|
|
|
|
|
|
|
|
.\" $OpenBSD: malloc.3,v 1.43 2006/03/26 19:56:08 ray Exp $ |
|
|
.\" |
|
|
.\" |
|
|
.Dd August 27, 1996 |
|
|
.Dd August 27, 1996 |
|
|
.Dt MALLOC 3 |
|
|
.Dt MALLOC 3 |
|
@ -83,6 +83,29 @@ The minimum size of the protection on each object is suitably aligned and |
|
|
sized as previously stated, but the protection may extend further depending |
|
|
sized as previously stated, but the protection may extend further depending |
|
|
on where in a protected zone the object lands. |
|
|
on where in a protected zone the object lands. |
|
|
.Pp |
|
|
.Pp |
|
|
|
|
|
When using |
|
|
|
|
|
.Fn malloc |
|
|
|
|
|
be careful to avoid the following idiom: |
|
|
|
|
|
.Bd -literal -offset indent |
|
|
|
|
|
if ((p = malloc(num * size)) == NULL) |
|
|
|
|
|
err(1, "malloc"); |
|
|
|
|
|
.Ed |
|
|
|
|
|
.Pp |
|
|
|
|
|
The multiplication may lead to an integer overflow. |
|
|
|
|
|
To avoid this, |
|
|
|
|
|
.Fn calloc |
|
|
|
|
|
is recommended. |
|
|
|
|
|
.Pp |
|
|
|
|
|
If |
|
|
|
|
|
.Fn malloc |
|
|
|
|
|
must be used, be sure to test for overflow: |
|
|
|
|
|
.Bd -literal -offset indent |
|
|
|
|
|
if (num && size && SIZE_T_MAX / num < size) { |
|
|
|
|
|
errno = ENOMEM; |
|
|
|
|
|
err(1, "overflow"); |
|
|
|
|
|
} |
|
|
|
|
|
.Ed |
|
|
|
|
|
.Pp |
|
|
The |
|
|
The |
|
|
.Fn calloc |
|
|
.Fn calloc |
|
|
function allocates space for an array of |
|
|
function allocates space for an array of |
|
@ -90,6 +113,10 @@ function allocates space for an array of |
|
|
objects, each of whose size is |
|
|
objects, each of whose size is |
|
|
.Fa size . |
|
|
.Fa size . |
|
|
The space is initialized to all bits zero. |
|
|
The space is initialized to all bits zero. |
|
|
|
|
|
The use of |
|
|
|
|
|
.Fn calloc |
|
|
|
|
|
is strongly encouraged when allocating multiple sized objects |
|
|
|
|
|
in order to avoid possible integer overflows. |
|
|
.Pp |
|
|
.Pp |
|
|
The |
|
|
The |
|
|
.Fn free |
|
|
.Fn free |
|
@ -140,7 +167,7 @@ object is returned. |
|
|
.Pp |
|
|
.Pp |
|
|
When using |
|
|
When using |
|
|
.Fn realloc |
|
|
.Fn realloc |
|
|
one must be careful to avoid the following idiom: |
|
|
|
|
|
|
|
|
be careful to avoid the following idiom: |
|
|
.Bd -literal -offset indent |
|
|
.Bd -literal -offset indent |
|
|
size += 50; |
|
|
size += 50; |
|
|
if ((p = realloc(p, size)) == NULL) |
|
|
if ((p = realloc(p, size)) == NULL) |
|
@ -148,7 +175,7 @@ if ((p = realloc(p, size)) == NULL) |
|
|
.Ed |
|
|
.Ed |
|
|
.Pp |
|
|
.Pp |
|
|
Do not adjust the variable describing how much memory has been allocated |
|
|
Do not adjust the variable describing how much memory has been allocated |
|
|
until one knows the allocation has been successful. |
|
|
|
|
|
|
|
|
until the allocation has been successful. |
|
|
This can cause aberrant program behavior if the incorrect size value is used. |
|
|
This can cause aberrant program behavior if the incorrect size value is used. |
|
|
In most cases, the above sample will also result in a leak of memory. |
|
|
In most cases, the above sample will also result in a leak of memory. |
|
|
As stated earlier, a return value of |
|
|
As stated earlier, a return value of |
|
@ -167,6 +194,15 @@ p = newp; |
|
|
size = newsize; |
|
|
size = newsize; |
|
|
.Ed |
|
|
.Ed |
|
|
.Pp |
|
|
.Pp |
|
|
|
|
|
As with |
|
|
|
|
|
.Fn malloc |
|
|
|
|
|
it is important to ensure the new size value will not overflow; |
|
|
|
|
|
i.e. avoid allocations like the following: |
|
|
|
|
|
.Bd -literal -offset indent |
|
|
|
|
|
if ((newp = realloc(p, num * size)) == NULL) { |
|
|
|
|
|
... |
|
|
|
|
|
.Ed |
|
|
|
|
|
.Pp |
|
|
Malloc will first look for a symbolic link called |
|
|
Malloc will first look for a symbolic link called |
|
|
.Pa /etc/malloc.conf |
|
|
.Pa /etc/malloc.conf |
|
|
and next check the environment for a variable called |
|
|
and next check the environment for a variable called |
|
@ -255,8 +291,7 @@ Reduce the size of the cache by a factor of two. |
|
|
Double the size of the cache by a factor of two. |
|
|
Double the size of the cache by a factor of two. |
|
|
.El |
|
|
.El |
|
|
.Pp |
|
|
.Pp |
|
|
So to set a systemwide reduction of cache size and coredumps on problems |
|
|
|
|
|
one would: |
|
|
|
|
|
|
|
|
So to set a systemwide reduction of cache size and coredumps on problems: |
|
|
.Li ln -s 'A<' /etc/malloc.conf |
|
|
.Li ln -s 'A<' /etc/malloc.conf |
|
|
.Pp |
|
|
.Pp |
|
|
The |
|
|
The |
|
|