blob: 8d4df14748459ab9cf5a56f2750a6f1fcc1f7290 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
.Dd 27 April 2024
.Dt BUFALLOC 3
.Os
.Sh NAME
.Nm bufalloc ,
.Nm bufalloc_noterm
.Nd allocate dynamic memory
.Sh LIBRARY
.Lb mlib
.Sh SYNOPSIS
.In alloc.h
.Ft "void *"
.Fn bufalloc "void *p" "size_t n" "size_t m"
.Ft "void *"
.Fn bufalloc_noterm "void *p" "size_t n" "size_t m"
.Sh DESCRIPTION
The
.Fn bufalloc
function resizes the dynamically-allocated buffer pointed to by
.Fa p
to be large enough for an array of
.Fa n
elements, each of which is
.Fa m
bytes in size.
If the buffer to be resized is
.Dv nullptr
then a freshly-allocated buffer is returned instead.
.Pp
On error
.Pq including buffer-size overflow
the
.Fn bufalloc
function prints a diagnostic to the standard output and terminates
execution.
.Pp
The
.Fn bufalloc_noterm
function is identical to
.Fn bufalloc
except on error it returns
.Dv nullptr
instead of printing a diagnostic and terminating.
.Sh RETURN VALUES
The
.Fn bufalloc
function returns a pointer to the newly allocated- or resized buffer.
.Pp
The
.Fn bufalloc
function returns a pointer to the newly allocated- or resized buffer if
successful;
otherwise the value
.Dv nullptr
is returned and the global variable
.Va errno
is set to indicate the error.
.Sh EXAMPLES
The following calls to
.Fn bufalloc
first allocate a buffer to hold 5 values of type
.Vt int
and then resize it to hold 10 values of the same type.
.Bd -literal -offset indent
int *p = bufalloc(nullptr, 5, sizeof(int));
p[0] = 69;
p[1] = 420;
/* … */
p = bufalloc(p, 10, sizeof(int));
p[9] = 1337;
.Ed
.Sh ERRORS
.Bl -tag -width Er
.It Bq Er EOVERFLOW
Overflow occured when computing buffer size.
.El
.Pp
The
.Fn bufalloc_noterm
function may fail and set the external variable
.Va errno
for any of the errors specified for the library function
.Xr realloc 3 .
.Sh SEE ALSO
.Xr realloc 3 ,
.Xr reallocarray 3
.Sh AUTHORS
.An Thomas Voss Aq Mt mail@thomasvoss.com
|