blob: 01493c757d94b91ab6198b5d2990230f8b12accd [file] [log] [blame]
rsccfa37a72004-04-10 18:53:55 +00001.TH MALLOC 3
2.SH NAME
rsc058b0112005-01-03 06:40:20 +00003malloc, mallocz, free, realloc, calloc, setmalloctag, setrealloctag, getmalloctag, getrealloctag \- memory allocator
rsccfa37a72004-04-10 18:53:55 +00004.SH SYNOPSIS
5.B #include <u.h>
6.br
7.B #include <libc.h>
8.PP
9.ta \w'\fLvoid* 'u
10.B
11void* malloc(ulong size)
12.PP
13.B
14void* mallocz(ulong size, int clr)
15.PP
16.B
17void free(void *ptr)
18.PP
19.B
20void* realloc(void *ptr, ulong size)
21.PP
22.B
23void* calloc(ulong nelem, ulong elsize)
24.PP
25.B
rsccfa37a72004-04-10 18:53:55 +000026void setmalloctag(void *ptr, ulong tag)
27.PP
28.B
rsc058b0112005-01-03 06:40:20 +000029ulong getmalloctag(void *ptr)
rsccfa37a72004-04-10 18:53:55 +000030.PP
31.B
32void setrealloctag(void *ptr, ulong tag)
33.PP
34.B
rsc058b0112005-01-03 06:40:20 +000035ulong getrealloctag(void *ptr)
rsccfa37a72004-04-10 18:53:55 +000036.SH DESCRIPTION
37.I Malloc
38and
39.I free
40provide a simple memory allocation package.
41.I Malloc
42returns a pointer to a new block of at least
43.I size
44bytes.
45The block is suitably aligned for storage of any type of object.
46No two active pointers from
47.I malloc
48will have the same value.
49The call
50.B malloc(0)
51returns a valid pointer rather than null.
52.PP
53The argument to
54.I free
55is a pointer to a block previously allocated by
56.IR malloc ;
57this space is made available for further allocation.
58It is legal to free a null pointer; the effect is a no-op.
59The contents of the space returned by
60.I malloc
61are undefined.
62.I Mallocz
63behaves as
64.IR malloc ,
65except that if
66.I clr
67is non-zero, the memory returned will be zeroed.
68.PP
69.I Realloc
70changes the size of the block pointed to by
71.I ptr
72to
73.I size
74bytes and returns a pointer to the (possibly moved)
75block.
76The contents will be unchanged up to the
77lesser of the new and old sizes.
78.I Realloc
79takes on special meanings when one or both arguments are zero:
80.TP
81.B "realloc(0,\ size)
82means
83.LR malloc(size) ;
84returns a pointer to the newly-allocated memory
85.TP
86.B "realloc(ptr,\ 0)
87means
88.LR free(ptr) ;
89returns null
90.TP
91.B "realloc(0,\ 0)
92no-op; returns null
93.PD
94.PP
95.I Calloc
96allocates space for
97an array of
98.I nelem
99elements of size
100.IR elsize .
101The space is initialized to zeros.
102.I Free
103frees such a block.
104.PP
rsc058b0112005-01-03 06:40:20 +0000105The memory allocator on Plan 9 maintains two word-sized fields
rsccfa37a72004-04-10 18:53:55 +0000106associated with each block, the ``malloc tag'' and the ``realloc tag''.
107By convention, the malloc tag is the PC that allocated the block,
108and the realloc tag the PC that last reallocated the block.
109These may be set or examined with
110.IR setmalloctag ,
111.IR getmalloctag ,
112.IR setrealloctag ,
113and
114.IR getrealloctag .
115When allocating blocks directly with
116.I malloc
117and
118.IR realloc ,
119these tags will be set properly.
120If a custom allocator wrapper is used,
121the allocator wrapper can set the tags
122itself (usually by passing the result of
rscbf8a59f2004-04-11 03:42:27 +0000123.IR getcallerpc (3)
rsccfa37a72004-04-10 18:53:55 +0000124to
125.IR setmalloctag )
126to provide more useful information about
127the source of allocation.
rsccfa37a72004-04-10 18:53:55 +0000128.SH SOURCE
rsc058b0112005-01-03 06:40:20 +0000129.B /usr/local/plan9/src/lib9/malloc.c
130.br
131.B /usr/local/plan9/src/lib9/malloctag.c
rsccfa37a72004-04-10 18:53:55 +0000132.SH SEE ALSO
rsccfa37a72004-04-10 18:53:55 +0000133.I trump
134(in
135.IR acid (1)),
rsc058b0112005-01-03 06:40:20 +0000136.IR getcallerpc (3)
rsccfa37a72004-04-10 18:53:55 +0000137.SH DIAGNOSTICS
138.I Malloc, realloc
139and
140.I calloc
141return 0 if there is no available memory.
142.I Errstr
143is likely to be set.
144If the allocated blocks have no malloc or realloc tags,
145.I getmalloctag
146and
147.I getrealloctag
148return
149.BR ~0 .
150.PP
rsccfa37a72004-04-10 18:53:55 +0000151The
152.I trump
153library for
154.I acid
155can be used to obtain traces of malloc execution; see
156.IR acid (1).
157.SH BUGS
158The different specification of
159.I calloc
160is bizarre.
161.PP
162User errors can corrupt the storage arena.
163The most common gaffes are (1) freeing an already freed block,
rsc058b0112005-01-03 06:40:20 +0000164(2) storing beyond the bounds of an allocated block, and (3)
rsccfa37a72004-04-10 18:53:55 +0000165freeing data that was not obtained from the allocator.
166When
167.I malloc
168and
169.I free
170detect such corruption, they abort.