summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/malloc.3
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/stdlib/malloc.3')
-rw-r--r--src/lib/libc/stdlib/malloc.329
1 files changed, 22 insertions, 7 deletions
diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3
index 414f0a9770..2a636b88cc 100644
--- a/src/lib/libc/stdlib/malloc.3
+++ b/src/lib/libc/stdlib/malloc.3
@@ -30,14 +30,15 @@
30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31.\" SUCH DAMAGE. 31.\" SUCH DAMAGE.
32.\" 32.\"
33.\" $OpenBSD: malloc.3,v 1.73 2013/07/18 10:14:49 schwarze Exp $ 33.\" $OpenBSD: malloc.3,v 1.74 2014/04/21 13:17:32 deraadt Exp $
34.\" 34.\"
35.Dd $Mdocdate: July 18 2013 $ 35.Dd $Mdocdate: April 21 2014 $
36.Dt MALLOC 3 36.Dt MALLOC 3
37.Os 37.Os
38.Sh NAME 38.Sh NAME
39.Nm malloc , 39.Nm malloc ,
40.Nm calloc , 40.Nm calloc ,
41.Nm mallocarray ,
41.Nm realloc , 42.Nm realloc ,
42.Nm free , 43.Nm free ,
43.Nm cfree 44.Nm cfree
@@ -49,12 +50,14 @@
49.Ft void * 50.Ft void *
50.Fn calloc "size_t nmemb" "size_t size" 51.Fn calloc "size_t nmemb" "size_t size"
51.Ft void * 52.Ft void *
53.Fn mallocarray "size_t nmemb" "size_t size"
54.Ft void *
52.Fn realloc "void *ptr" "size_t size" 55.Fn realloc "void *ptr" "size_t size"
53.Ft void 56.Ft void
54.Fn free "void *ptr" 57.Fn free "void *ptr"
55.Ft void 58.Ft void
56.Fn cfree "void *ptr" 59.Fn cfree "void *ptr"
57.Ft char * 60.Ft char * Ns
58.Va malloc_options ; 61.Va malloc_options ;
59.Sh DESCRIPTION 62.Sh DESCRIPTION
60The 63The
@@ -91,10 +94,18 @@ if ((p = malloc(num * size)) == NULL)
91 err(1, "malloc"); 94 err(1, "malloc");
92.Ed 95.Ed
93.Pp 96.Pp
94The multiplication may lead to an integer overflow. 97The multiplication may lead to an integer overflow, which can
95To avoid this, 98be avoided using the extension
99.Fn mallocarray ,
100as follows:
101.Bd -literal -offset indent
102if ((p = mallocarray(num, size)) == NULL)
103 err(1, "malloc");
104.Ed
105.Pp
106Alternatively
96.Fn calloc 107.Fn calloc
97is recommended. 108is a more portable solution which comes with the cost of clearing memory.
98.Pp 109.Pp
99If 110If
100.Fn malloc 111.Fn malloc
@@ -324,7 +335,8 @@ it is buggy.
324The default number of free pages cached is 64. 335The default number of free pages cached is 64.
325.Sh RETURN VALUES 336.Sh RETURN VALUES
326The 337The
327.Fn malloc 338.Fn malloc ,
339.Fn mallocarray ,
328and 340and
329.Fn calloc 341.Fn calloc
330functions return a pointer to the allocated space if successful; otherwise, 342functions return a pointer to the allocated space if successful; otherwise,
@@ -482,3 +494,6 @@ random.
482A rewrite by Otto Moerbeek introducing a new central data structure and more 494A rewrite by Otto Moerbeek introducing a new central data structure and more
483randomization appeared in 495randomization appeared in
484.Ox 4.4 . 496.Ox 4.4 .
497.Fn mallocarray
498appeared in
499.Ox 5.6 .