xref: /netbsd-src/external/bsd/mdocml/dist/mandoc_malloc.3 (revision 9ff1f2ac944731963b1d62cde605dabb5cb29a3a)
1*9ff1f2acSchristos.\"	Id: mandoc_malloc.3,v 1.2 2016/07/07 19:19:01 schwarze Exp
2fec65c98Schristos.\"
3fec65c98Schristos.\" Copyright (c) 2014 Ingo Schwarze <schwarze@openbsd.org>
4fec65c98Schristos.\"
5fec65c98Schristos.\" Permission to use, copy, modify, and distribute this software for any
6fec65c98Schristos.\" purpose with or without fee is hereby granted, provided that the above
7fec65c98Schristos.\" copyright notice and this permission notice appear in all copies.
8fec65c98Schristos.\"
9fec65c98Schristos.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10fec65c98Schristos.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11fec65c98Schristos.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12fec65c98Schristos.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13fec65c98Schristos.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14fec65c98Schristos.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15fec65c98Schristos.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16fec65c98Schristos.\"
17*9ff1f2acSchristos.Dd July 7, 2016
18fec65c98Schristos.Dt MANDOC_MALLOC 3
19fec65c98Schristos.Os
20fec65c98Schristos.Sh NAME
21fec65c98Schristos.Nm mandoc_malloc ,
22fec65c98Schristos.Nm mandoc_realloc ,
23fec65c98Schristos.Nm mandoc_reallocarray ,
24fec65c98Schristos.Nm mandoc_calloc ,
25fec65c98Schristos.Nm mandoc_strdup ,
26fec65c98Schristos.Nm mandoc_strndup ,
27fec65c98Schristos.Nm mandoc_asprintf
28fec65c98Schristos.Nd memory allocation function wrappers used in the mandoc library
29fec65c98Schristos.Sh SYNOPSIS
30fec65c98Schristos.In sys/types.h
31fec65c98Schristos.In mandoc_aux.h
32fec65c98Schristos.Ft "void *"
33fec65c98Schristos.Fo mandoc_malloc
34fec65c98Schristos.Fa "size_t size"
35fec65c98Schristos.Fc
36fec65c98Schristos.Ft "void *"
37fec65c98Schristos.Fo mandoc_realloc
38fec65c98Schristos.Fa "void *ptr"
39fec65c98Schristos.Fa "size_t size"
40fec65c98Schristos.Fc
41fec65c98Schristos.Ft "void *"
42fec65c98Schristos.Fo mandoc_reallocarray
43fec65c98Schristos.Fa "void *ptr"
44fec65c98Schristos.Fa "size_t nmemb"
45fec65c98Schristos.Fa "size_t size"
46fec65c98Schristos.Fc
47fec65c98Schristos.Ft "void *"
48fec65c98Schristos.Fo mandoc_calloc
49fec65c98Schristos.Fa "size_t nmemb"
50fec65c98Schristos.Fa "size_t size"
51fec65c98Schristos.Fc
52fec65c98Schristos.Ft "char *"
53fec65c98Schristos.Fo mandoc_strdup
54fec65c98Schristos.Fa "const char *s"
55fec65c98Schristos.Fc
56fec65c98Schristos.Ft "char *"
57fec65c98Schristos.Fo mandoc_strndup
58fec65c98Schristos.Fa "const char *s"
59fec65c98Schristos.Fa "size_t maxlen"
60fec65c98Schristos.Fc
61fec65c98Schristos.Ft int
62fec65c98Schristos.Fo mandoc_asprintf
63fec65c98Schristos.Fa "char **ret"
64fec65c98Schristos.Fa "const char *format"
65fec65c98Schristos.Fa "..."
66fec65c98Schristos.Fc
67fec65c98Schristos.Sh DESCRIPTION
68*9ff1f2acSchristosThese functions call the libc functions of the same names, passing
69*9ff1f2acSchristosthrough their return values when successful.
70fec65c98SchristosIn case of failure, they do not return, but instead call
71*9ff1f2acSchristos.Xr err 3 .
72*9ff1f2acSchristosThey can be used both internally by any code in the mandoc libraries
73fec65c98Schristosand externally by programs using that library, for example
74fec65c98Schristos.Xr mandoc 1 ,
75*9ff1f2acSchristos.Xr man 1 ,
76fec65c98Schristos.Xr apropos 1 ,
77*9ff1f2acSchristos.Xr makewhatis 8 ,
78fec65c98Schristosand
79*9ff1f2acSchristos.Xr man.cgi 8 .
80fec65c98Schristos.Pp
81fec65c98SchristosThe function
82fec65c98Schristos.Fn mandoc_malloc
83fec65c98Schristosallocates one new object, leaving the memory uninitialized.
84fec65c98SchristosThe functions
85fec65c98Schristos.Fn mandoc_realloc
86fec65c98Schristosand
87fec65c98Schristos.Fn mandoc_reallocarray
88fec65c98Schristoschange the size of an existing object or array, possibly moving it.
89fec65c98SchristosWhen shrinking the size, existing data is truncated; when growing,
90fec65c98Schristosthe additional memory is not initialized.
91fec65c98SchristosThe function
92fec65c98Schristos.Fn mandoc_calloc
93fec65c98Schristosallocates a new array, initializing it to zero.
94fec65c98Schristos.Pp
95fec65c98SchristosThe argument
96fec65c98Schristos.Fa size
97fec65c98Schristosis the size of each object.
98fec65c98SchristosThe argument
99fec65c98Schristos.Fa nmemb
100fec65c98Schristosis the new number of objects in the array.
101fec65c98SchristosThe argument
102fec65c98Schristos.Fa ptr
103fec65c98Schristosis a pointer to the existing object or array to be resized; if it is
104fec65c98Schristos.Dv NULL ,
105fec65c98Schristosa new object or array is allocated.
106fec65c98Schristos.Pp
107fec65c98SchristosThe functions
108fec65c98Schristos.Fn mandoc_strdup
109fec65c98Schristosand
110fec65c98Schristos.Fn mandoc_strndup
111fec65c98Schristoscopy a string into newly allocated memory.
112fec65c98SchristosFor
113fec65c98Schristos.Fn mandoc_strdup ,
114fec65c98Schristosthe string pointed to by
115fec65c98Schristos.Fa s
116fec65c98Schristosneeds to be NUL-terminated.
117fec65c98SchristosFor
118fec65c98Schristos.Fn mandoc_strndup ,
119fec65c98Schristosat most
120fec65c98Schristos.Fa maxlen
121fec65c98Schristosbytes are copied.
122fec65c98SchristosThe function
123fec65c98Schristos.Fn mandoc_asprintf
124fec65c98Schristoswrites output formatted according to
125fec65c98Schristos.Fa format
126fec65c98Schristosinto newly allocated memory and returns a pointer to the result in
127fec65c98Schristos.Fa ret .
128fec65c98SchristosFor all three string functions, the result is always NUL-terminated.
129fec65c98Schristos.Pp
130fec65c98SchristosWhen the objects and strings are no longer needed,
131fec65c98Schristosthe pointers returned by these functions can be passed to
132fec65c98Schristos.Xr free 3 .
133fec65c98Schristos.Sh RETURN VALUES
134fec65c98SchristosThe function
135fec65c98Schristos.Fn mandoc_asprintf
136fec65c98Schristosalways returns the number of characters written, excluding the
137fec65c98Schristosfinal NUL byte.
138fec65c98SchristosIt never returns -1.
139fec65c98Schristos.Pp
140fec65c98SchristosThe other functions always return a valid pointer; they never return
141fec65c98Schristos.Dv NULL .
142fec65c98Schristos.Sh FILES
143fec65c98SchristosThese functions are implemented in
144fec65c98Schristos.Pa mandoc_aux.c .
145fec65c98Schristos.Sh SEE ALSO
146fec65c98Schristos.Xr asprintf 3 ,
147*9ff1f2acSchristos.Xr err 3 ,
148fec65c98Schristos.Xr malloc 3 ,
149fec65c98Schristos.Xr strdup 3
150fec65c98Schristos.Sh STANDARDS
151fec65c98SchristosThe functions
152fec65c98Schristos.Fn malloc ,
153fec65c98Schristos.Fn realloc ,
154fec65c98Schristosand
155fec65c98Schristos.Fn calloc
156fec65c98Schristosare required by
157fec65c98Schristos.St -ansiC .
158fec65c98SchristosThe functions
159fec65c98Schristos.Fn strdup
160fec65c98Schristosand
161fec65c98Schristos.Fn strndup
162fec65c98Schristosare required by
163fec65c98Schristos.St -p1003.1-2008 .
164fec65c98SchristosThe function
165fec65c98Schristos.Fn asprintf
166fec65c98Schristosis a widespread extension that first appeared in the GNU C library.
167fec65c98Schristos.Pp
168fec65c98SchristosThe function
169fec65c98Schristos.Fn reallocarray
170fec65c98Schristosis an extension that first appeared in
171fec65c98Schristos.Ox 5.6 .
172fec65c98SchristosIf it is not provided by the operating system, the mandoc build system
173fec65c98Schristosuses a bundled portable implementation.
174fec65c98Schristos.Sh HISTORY
175fec65c98SchristosThe functions
176fec65c98Schristos.Fn mandoc_malloc ,
177fec65c98Schristos.Fn mandoc_realloc ,
178fec65c98Schristos.Fn mandoc_calloc ,
179fec65c98Schristosand
180fec65c98Schristos.Fn mandoc_strdup
181fec65c98Schristoshave been available since mandoc 1.9.12,
182fec65c98Schristos.Fn mandoc_strndup
183fec65c98Schristossince 1.11.5,
184fec65c98Schristosand
185fec65c98Schristos.Fn mandoc_asprintf
186fec65c98Schristosand
187fec65c98Schristos.Fn mandoc_reallocarray
188fec65c98Schristossince 1.12.4 and 1.13.0.
189fec65c98Schristos.Sh AUTHORS
190fec65c98Schristos.An Kristaps Dzonsons Aq Mt kristaps@bsd.lv
191fec65c98Schristos.An Ingo Schwarze Aq Mt schwarze@openbsd.org
192