aboutsummaryrefslogtreecommitdiffstats
path: root/man3/libsimple_vmemalloc.3
blob: 0be0b71f6d248b78fae76494c561414a9b2253ce (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
.TH LIBSIMPLE_VMEMALLOC 3 libsimple
.SH NAME
libsimple_vmemalloc \- allocate memory in a flexible manner

.SH SYNOPSIS
.nf
#include <libsimple.h>

enum libsimple_memalloc_option {
	/* constants omitted, see \fBDESCRIPTION\fP */
};

void *libsimple_vmemalloc(size_t \fIn\fP, va_list \fIap\fP);
void *libsimple_envmemalloc(int \fIstatus\fP, size_t \fIn\fP, va_list \fIap\fP);
inline void *libsimple_evmemalloc(size_t \fIn\fP, va_list \fIap\fP);
inline void *libsimple_memalloc(size_t \fIn\fP, ..., /* LIBSIMPLE_MEMALLOC_END */);
inline void *libsimple_enmemalloc(int \fIstatus\fP, size_t \fIn\fP, ..., /* LIBSIMPLE_MEMALLOC_END */);
inline void *libsimple_ememalloc(size_t \fIn\fP, ..., /* LIBSIMPLE_MEMALLOC_END */);
.fi
.PP
Link with
.IR \-lsimple .

.SH DESCRIPTION
The
.BR libsimple_vmemalloc ()
function is a flexible alternative to standard allocation
functions.
.PP
.I va
is a list of options that specify the behaviour, its
end is marked by
.BR LIBSIMPLE_MEMALLOC_END ,
the following options (which is the type
.BR "enum libsimple_memalloc_option" )
are recognised:
.TP
.B LIBSIMPLE_MEMALLOC_ZERO_INIT
The allocated memory shall be initialisd with NUL bytes.
.TP
.B LIBSIMPLE_MEMALLOC_CONDITIONAL_ZERO_INIT
The allocated memory shall be initialisd with NUL bytes
with the argument, which should be of the type
.BR int ,
is nonzero.
.TP
.B LIBSIMPLE_MEMALLOC_UNIQUE_IF_ZERO
If attempting to allocate 0 bytes,
rather than failing with
.I errno
set to
.BR EINVAL ,
a unique pointer (that can be deallocated)
should be returned.
.TP
.B LIBSIMPLE_MEMALLOC_NULL_IF_ZERO
If attempting to allocate 0 bytes,
rather than failing with
.I errno
set to
.BR EINVAL ,
a
.B NULL
should be returned.
.TP
.B LIBSIMPLE_MEMALLOC_ALIGNMENT
The value of the next argument, which should be of the type
.BR size_t ,
should be used as the alignment of the returned pointer.
By default the alignment is
.IR "alignof(max_align_t)" .
.TP
.B LIBSIMPLE_MEMALLOC_PAGE_ALIGNMENT
The alignment of the returned pointer should be the page size.
By default the alignment is
.IR "alignof(max_align_t)" .
.TP
.B LIBSIMPLE_MEMALLOC_ALIGNMENT_TO_CACHE_LINE
Align the pointer to the cache line (usuallly 64 bytes),
in addition other specific or default alignment.
.TP
.B LIBSIMPLE_MEMALLOC_ALLOW_CACHE_LINE_SPLITTING
Allow the allocated memory to misaligned to the
cache line such that the memory is stored over
one more cache line than necessary. For example,
if the cache line is 64 bytes and the requested
alignment is 8 bytes, pointer can be aligned to
the cache line or off by 8, 16, 24, 32, 40, 48,
or 56 bytes. If 32 is allocated, an misalignment
to the cache line by 0, 8, 16, 24, or 32 is
normally used, by if
.B LIBSIMPLE_MEMALLOC_ALLOW_CACHE_LINE_SPLITTING
is used, the alignment can be of with 0, 8, 16,
24, 32, 40, 48, or 56 bytes.
.TP
.B LIBSIMPLE_MEMALLOC_ROUND_UP_SIZE_TO_ALIGNMENT
The number of bytes to allocated should be rounded up to
the next multiple of the alignment, unless it already is
a multiple of the alignment.
.TP
.B LIBSIMPLE_MEMALLOC_ELEMENT_SIZE
The next argument, which should be of the type
.BR size_t ,
shall act as a multiplier for the number of bytes to allocate
(default multiplier is 1), effectively, making the specified
allocation size specified in elements rather than in bytes.
.TP
.B LIBSIMPLE_MEMALLOC_PRODUCT_SIZE
If
.I n
is zero, the product of the following arguments,
which should be of the type
.BR size_t ,
up to the first 0, shall be used as the allocation size.
If
.I n
is not zero, the product of the following
.I n
arguments, also of type
.BR size_t ,
shall be used as the allocation size.
By default
.I n
bytes are allocated.
.TP
.B LIBSIMPLE_MEMALLOC_VA_PRODUCT_SIZE
Like
.BR LIBSIMPLE_MEMALLOC_PRODUCT_SIZE ,
the arguments are read from the next argument,
which should be of the type
.BR va_list .
.TP
.B LIBSIMPLE_MEMALLOC_1_VA_PRODUCT_SIZE
Like
.BR LIBSIMPLE_MEMALLOC_PRODUCT_SIZE ,
except two arguments read from
.IR ap :
a
.BR size_t ,
which act as the first factor, and a
.B va_list
with the rest of the factors.
.TP
.B LIBSIMPLE_MEMALLOC_VA_LIST
Arguments from the next argument, which should be of the type
.B va_list
and should end with
.BR LIBSIMPLE_MEMALLOC_END ,
shall be parsed as options for the memory allocation.
.PP
Each
.B enum libsimple_memalloc_option
constant have a self-referencing macros defined
which can be used to test which constants are defined.
.PP
The
.BR libsimple_envmemalloc ()
and
.BR libsimple_evmemalloc ()
functions are versions of the
.BR libsimple_vmemalloc ()
that terminate the process by calling the
.BR libsimple_enprintf (3)
(with
.I status
as the exit value) and
.BR libsimple_eprintf (3)
functions, respectively.
.PP
The
.BR libsimple_memalloc (),
.BR libsimple_enmemalloc (),
and
.BR libsimple_ememalloc ()
functions are versions of the
.BR libsimple_vmemalloc (),
.BR libsimple_envmemalloc (),
and
.BR libsimple_evmemalloc ()
functions, respectively, that use variadic arguments
instead of
.BR va_list .

.SH RETURN VALUE
The
.BR libsimple_vmemalloc (),
.BR libsimple_envmemalloc (),
.BR libsimple_ememalloc (),
.BR libsimple_memalloc (),
.BR libsimple_enmemalloc (),
and
.BR libsimple_ememalloc ()
functions return the a pointer to the allocated
memory upon successful completion; otherwise the
.BR libsimple_vmemalloc ()
and
.BR libsimple_memalloc ()
functions return
.B NULL
and set
.I errno
to indicate the error, whereas the
.BR libsimple_envmemalloc (),
.BR libsimple_ememalloc (),
.BR libsimple_enmemalloc (),
and
.BR libsimple_ememalloc ()
functions terminate the process.

.SH ERRORS
The
.BR libsimple_vmemalloc ()
and
.BR libsimple_memalloc ()
functions will fail if
.TP
.B EINVAL
An invalid argument is specified.
.TP
.B EINVAL
An option is specified twice or in
conjunction with a mutually exclusive option.
.TP
.B ENOMEM
Enough memory could not be allocated.
.PP
The
.BR libsimple_envmemalloc (),
.BR libsimple_ememalloc (),
.BR libsimple_enmemalloc (),
and
.BR libsimple_ememalloc ()
functions terminate the process on failure.

.SH ATTRIBUTES
For an explanation of the terms used in this section, see
.BR attributes (7).
.TS
allbox;
lb lb lb
l l l.
Interface	Attribute	Value
T{
.BR libsimple_vmemalloc (),
.br
.BR libsimple_envmemalloc (),
.br
.BR libsimple_ememalloc (),
.br
.BR libsimple_memalloc (),
.br
.BR libsimple_enmemalloc (),
.br
.BR libsimple_ememalloc ()
T}	Thread safety	MT-Safe
T{
.BR libsimple_vmemalloc (),
.br
.BR libsimple_envmemalloc (),
.br
.BR libsimple_ememalloc (),
.br
.BR libsimple_memalloc (),
.br
.BR libsimple_enmemalloc (),
.br
.BR libsimple_ememalloc ()
T}	Async-signal safety	AS-Safe
T{
.BR libsimple_vmemalloc (),
.br
.BR libsimple_envmemalloc (),
.br
.BR libsimple_ememalloc (),
.br
.BR libsimple_memalloc (),
.br
.BR libsimple_enmemalloc (),
.br
.BR libsimple_ememalloc ()
T}	Async-cancel safety	AC-Safe
.TE

.SH EXAMPLES
None.

.SH APPLICATION USAGE
None.

.SH RATIONALE
None.

.SH FUTURE DIRECTIONS
None.

.SH NOTES
None.

.SH BUGS
None.

.SH SEE ALSO
.BR libsimple_varrayalloc (3),
.BR libsimple_enmalloc (3),
.BR libsimple_mallocz (3),
.BR libsimple_vmallocn (3),
.BR libsimple_vmalloczn (3),
.BR libsimple_encalloc (3),
.BR libsimple_vcallocn (3),
.BR libsimple_enrealloc (3),
.BR libsimple_reallocarray (3),
.BR libsimple_vreallocn (3),
.BR libsimple_reallocf (3),
.BR libsimple_reallocarrayf (3),
.BR libsimple_vreallocfn (3),
.BR libsimple_aligned_realloc (3),
.BR libsimple_aligned_reallocarray (3),
.BR libsimple_aligned_vreallocn (3),
.BR libsimple_aligned_reallocf (3),
.BR libsimple_aligned_reallocarrayf (3),
.BR libsimple_aligned_vreallocfn (3),
.BR libsimple_memalign (3),
.BR libsimple_memalignz (3),
.BR libsimple_vmemalignn (3),
.BR libsimple_vmemalignzn (3),
.BR libsimple_enposix_memalign (3),
.BR libsimple_posix_memalignz (3),
.BR libsimple_vposix_memalignn (3),
.BR libsimple_vposix_memalignzn (3),
.BR libsimple_enaligned_alloc (3),
.BR libsimple_aligned_allocz (3),
.BR libsimple_valigned_allocn (3),
.BR libsimple_valigned_alloczn (3),
.BR libsimple_pvalloc (3),
.BR libsimple_pvallocz (3),
.BR libsimple_vpvallocn (3),
.BR libsimple_vpvalloczn (3),
.BR libsimple_valloc (3),
.BR libsimple_vallocz (3),
.BR libsimple_vvallocn (3),
.BR libsimple_vvalloczn (3)