2 .\" Copyright (c) 2004, Sun Microsystems, Inc. All Rights Reserved.
3 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
4 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
5 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
6 .TH NVLIST_ALLOC 3NVPAIR "Feb 15, 2016"
8 nvlist_alloc, nvlist_free, nvlist_size, nvlist_pack, nvlist_unpack, nvlist_dup,
9 nvlist_merge, nvlist_xalloc, nvlist_xpack, nvlist_xunpack, nvlist_xdup,
10 nvlist_lookup_nv_alloc, nv_alloc_init, nv_alloc_reset, nv_alloc_fini \- manage
11 a name-value pair list
15 \fBcc\fR [ \fIflag\fR... ] \fIfile\fR... \fB-lnvpair\fR [ \fIlibrary\fR... ]
16 #include <libnvpair.h>
18 \fBint\fR \fBnvlist_alloc\fR(\fBnvlist_t **\fR\fInvlp\fR, \fBuint_t\fR \fInvflag\fR, \fBint\fR \fIflag\fR);
23 \fBint\fR \fBnvlist_xalloc\fR(\fBnvlist_t **\fR\fInvlp\fR, \fBuint_t\fR \fInvflag\fR,
24 \fBnv_alloc_t *\fR \fInva\fR);
29 \fBvoid\fR \fBnvlist_free\fR(\fBnvlist_t *\fR\fInvl\fR);
34 \fBint\fR \fBnvlist_size\fR(\fBnvlist_t *\fR\fInvl\fR, \fBsize_t *\fR\fIsize\fR, \fBint\fR \fIencoding\fR);
39 \fBint\fR \fBnvlist_pack\fR(\fBnvlist_t *\fR\fInvl\fR, \fBchar **\fR\fIbufp\fR, \fBsize_t *\fR\fIbuflen\fR,
40 \fBint\fR \fIencoding\fR, \fBint\fR \fIflag\fR);
45 \fBint\fR \fBnvlist_xpack\fR(\fBnvlist_t *\fR\fInvl\fR, \fBchar **\fR\fIbufp\fR, \fBsize_t *\fR\fIbuflen\fR,
46 \fBint\fR \fIencoding\fR, \fBnv_alloc_t *\fR \fInva\fR);
51 \fBint\fR \fBnvlist_unpack\fR(\fBchar *\fR\fIbuf\fR, \fBsize_t\fR \fIbuflen\fR, \fBnvlist_t **\fR\fInvlp\fR,
52 \fBint\fR \fIflag\fR);
57 \fBint\fR \fBnvlist_xunpack\fR(\fBchar *\fR\fIbuf\fR, \fBsize_t\fR \fIbuflen\fR, \fBnvlist_t **\fR\fInvlp\fR,
58 \fBnv_alloc_t *\fR \fInva\fR);
63 \fBint\fR \fBnvlist_dup\fR(\fBnvlist_t *\fR\fInvl\fR, \fBnvlist_t **\fR\fInvlp\fR, \fBint\fR \fIflag\fR);
68 \fBint\fR \fBnvlist_xdup\fR(\fBnvlist_t *\fR\fInvl\fR, \fBnvlist_t **\fR\fInvlp\fR,
69 \fBnv_alloc_t *\fR \fInva\fR);
74 \fBint\fR \fBnvlist_merge\fR(\fBnvlist_t *\fR\fIdst\fR, \fBnvlist_t *\fR\fInvl\fR, \fBint\fR \fIflag\fR);
79 \fBnv_alloc_t *\fR \fBnvlist_lookup_nv_alloc\fR(\fBnvlist_t *\fR\fInvl\fR);
84 \fBint\fR \fBnv_alloc_init\fR(\fBnv_alloc_t *\fR\fInva\fR, \fBconst nv_alloc_ops_t *\fR\fInvo\fR,
85 \fB/*\fR \fIargs\fR */ ...);
90 \fBvoid\fR \fBnv_alloc_reset\fR(\fBnv_alloc_t *\fR\fInva\fR);
95 \fBvoid\fR \fBnv_alloc_fini\fR(\fBnv_alloc_t *\fR\fInva\fR);
104 Address of a pointer to \fBnvlist_t\fR.
113 Specify bit fields defining \fBnvlist\fR properties:
117 \fB\fBNV_UNIQUE_NAME\fR\fR
120 The \fBnvpair\fR names are unique.
126 \fB\fBNV_UNIQUE_NAME_TYPE\fR\fR
129 Name-data type combination is unique.
140 Specify 0. Reserved for future use.
149 The \fBnvlist_t\fR to be processed.
158 The destination \fBnvlist_t\fR.
167 Pointer to buffer to contain the encoded size.
176 Address of buffer to pack \fBnvlist\fR into. Must be 8-byte aligned. If
177 \fINULL\fR, library will allocate memory.
186 Buffer containing packed \fBnvlist\fR.
195 Size of buffer \fIbufp\fR or \fIbuf\fR points to.
204 Encoding method for packing.
213 Pluggable allocator operations pointer (\fBnv_alloc_ops_t\fR).
222 A pointer to an \fBnv_alloc_t\fR structure to be used for the specified
227 .SS "List Manipulation"
229 The \fBnvlist_alloc()\fR function allocates a new name-value pair list and
230 updates \fInvlp\fR to point to the handle. The \fInvflag\fR argument specifies
231 \fBnvlist\fR properties to remain persistent across packing, unpacking, and
232 duplication. If \fBNV_UNIQUE_NAME\fR was specified for \fInvflag\fR, existing
233 nvpairs with matching names are removed before the new nvpair is added. If
234 \fBNV_UNIQUE_NAME_TYPE\fR was specified for \fInvflag\fR, existing nvpairs with
235 matching names and data types are removed before the new nvpair is added. See
236 \fBnvlist_add_byte\fR(3NVPAIR) for more information.
239 The \fBnvlist_xalloc()\fR function is identical to \fBnvlist_alloc()\fR except
240 that \fBnvlist_xalloc()\fR can use a different allocator, as described in the
241 Pluggable Allocators section.
244 The \fBnvlist_free()\fR function frees a name-value pair list. If \fInvl\fR
245 is a null pointer, no action occurs.
248 The \fBnvlist_size()\fR function returns the minimum size of a contiguous
249 buffer large enough to pack \fInvl\fR. The \fIencoding\fR parameter specifies
250 the method of encoding when packing \fInvl\fR. Supported encoding methods are:
254 \fB\fBNV_ENCODE_NATIVE\fR\fR
257 Straight \fBbcopy()\fR as described in \fBbcopy\fR(3C).
263 \fB\fBNV_ENCODE_XDR\fR\fR
266 Use XDR encoding, suitable for sending to another host.
271 The \fBnvlist_pack()\fR function packs \fInvl\fR into contiguous memory
272 starting at *\fIbufp\fR. The \fIencoding\fR parameter specifies the method of
273 encoding (see above).
278 If *\fIbufp\fR is not \fINULL\fR, *\fIbufp\fR is expected to be a
279 caller-allocated buffer of size *\fIbuflen\fR.
285 If *\fIbufp\fR is \fINULL\fR, the library will allocate memory and update
286 *\fIbufp\fR to point to the memory and update *\fIbuflen\fR to contain the size
287 of the allocated memory.
291 The \fBnvlist_xpack()\fR function is identical to \fBnvlist_pack()\fR except
292 that \fBnvlist_xpack()\fR can use a different allocator.
295 The \fBnvlist_unpack()\fR function takes a buffer with a packed \fBnvlist_t\fR
296 and unpacks it into a searchable \fBnvlist_t\fR. The library allocates memory
297 for \fBnvlist_t\fR. The caller is responsible for freeing the memory by calling
301 The \fBnvlist_xunpack()\fR function is identical to \fBnvlist_unpack()\fR
302 except that \fBnvlist_xunpack()\fR can use a different allocator.
305 The \fBnvlist_dup()\fR function makes a copy of \fInvl\fR and updates
306 \fInvlp\fR to point to the copy.
309 The \fBnvlist_xdup()\fR function is identical to \fBnvlist_dup()\fR except that
310 \fBnvlist_xdup()\fR can use a different allocator.
313 The \fBnvlist_merge()\fR function adds copies of all name-value pairs from
314 \fInvl\fR to \fIdst\fR. Name-value pairs in \fIdst\fR are replaced with
315 name-value pairs from \fInvl\fR that have identical names (if \fIdst\fR has the
316 type \fBNV_UNIQUE_NAME\fR) or identical names and types (if \fIdst\fR has the
317 type \fBNV_UNIQUE_NAME_TYPE\fR).
320 The \fBnvlist_lookup_nv_alloc()\fR function retrieves the pointer to the
321 allocator that was used when manipulating a name-value pair list.
322 .SS "Pluggable Allocators"
323 .SS "Using Pluggable Allocators"
325 The \fBnv_alloc_init()\fR, \fBnv_alloc_reset()\fR and \fBnv_alloc_fini()\fR
326 functions provide an interface to specify the allocator to be used when
327 manipulating a name-value pair list.
330 The \fBnv_alloc_init()\fR function determines the allocator properties and puts
331 them into the \fInva\fR argument. The application must specify the \fInv_arg\fR
332 and \fInvo\fR arguments and an optional variable argument list. The optional
333 arguments are passed to the (*\fBnv_ao_init()\fR) function.
336 The \fInva\fR argument must be passed to \fBnvlist_xalloc()\fR,
337 \fBnvlist_xpack()\fR, \fBnvlist_xunpack()\fR and \fBnvlist_xdup()\fR.
340 The \fBnv_alloc_reset()\fR function is responsible for resetting the allocator
341 properties to the data specified by \fBnv_alloc_init()\fR. When no
342 (*\fBnv_ao_reset()\fR) function is specified, \fBnv_alloc_reset()\fR has no
346 The \fBnv_alloc_fini()\fR function destroys the allocator properties determined
347 by \fBnv_alloc_init()\fR. When a (*\fBnv_ao_fini()\fR) function is specified,
348 it is called from \fBnv_alloc_fini()\fR.
351 The disposition of the allocated objects and the memory used to store them is
352 left to the allocator implementation.
355 The \fBnv_alloc_nosleep\fR \fBnv_alloc_t\fR can be used with
356 \fBnvlist_xalloc()\fR to mimic the behavior of \fBnvlist_alloc()\fR.
359 The nvpair allocator framework provides a pointer to the operation structure of
360 a fixed buffer allocator. This allocator, \fBnv_fixed_ops\fR, uses a
361 pre-allocated buffer for memory allocations. It is intended primarily for
362 kernel use and is described on \fBnvlist_alloc\fR(9F).
365 An example program that uses the pluggable allocator functionality is provided
366 on \fBnvlist_alloc\fR(9F).
367 .SS "Creating Pluggable Allocators"
369 Any producer of name-value pairs can specify its own allocator functions. The
370 application must provide the following pluggable allocator operations:
374 int (*nv_ao_init)(nv_alloc_t *nva, va_list nv_valist);
375 void (*nv_ao_fini)(nv_alloc_t *nva);
376 void *(*nv_ao_alloc)(nv_alloc_t *nva, size_t sz);
377 void (*nv_ao_reset)(nv_alloc_t *nva);
378 void (*nv_ao_free)(nv_alloc_t *nva, void *buf, size_t sz);
384 The \fInva\fR argument of the allocator implementation is always the first
388 The optional (*\fBnv_ao_init()\fR) function is responsible for filling the data
389 specified by \fBnv_alloc_init()\fR into the \fInva_arg\fR argument. The
390 (*\fBnv_ao_init()\fR) function is only called when \fBnv_alloc_init()\fR is
394 The optional (*\fBnv_ao_fini()\fR) function is responsible for the cleanup of
395 the allocator implementation. It is called by \fBnv_alloc_fini()\fR.
398 The required (*\fBnv_ao_alloc()\fR) function is used in the nvpair allocation
399 framework for memory allocation. The \fIsz\fR argument specifies the size of
400 the requested buffer.
403 The optional (*\fBnv_ao_reset()\fR) function is responsible for resetting the
404 \fInva_arg\fR argument to the data specified by \fBnv_alloc_init()\fR.
407 The required (*\fBnv_ao_free()\fR) function is used in the nvpair allocator
408 framework for memory deallocation. The \fIbuf\fR argument is a pointer to a
409 block previously allocated by the (*\fBnv_ao_alloc()\fR) function. The size
410 argument \fIsz\fR must exactly match the original allocation.
413 The disposition of the allocated objects and the memory used to store them is
414 left to the allocator implementation.
417 These functions return 0 on success and an error value on failure.
420 The \fBnvlist_lookup_nv_alloc()\fR function returns a pointer to an allocator.
423 These functions will fail if:
430 There is an invalid argument.
435 The \fBnvlist_alloc()\fR, \fBnvlist_dup()\fR, \fBnvlist_pack()\fR,
436 \fBnvlist_unpack()\fR, \fBnvlist_merge()\fR, \fBnvlist_xalloc()\fR,
437 \fBnvlist_xdup()\fR, \fBnvlist_xpack()\fR, and \fBnvlist_xunpack()\fR functions
445 There is insufficient memory.
450 The \fBnvlist_pack()\fR, \fBnvlist_unpack()\fR, \fBnvlist_xpack()\fR, and
451 \fBnvlist_xunpack()\fR functions will fail if:
458 An encode/decode error occurs.
467 An encode/decode method is not supported.
474 * Program to create an nvlist.
477 #include <sys/types.h>
479 #include <libnvpair.h>
481 /* generate a packed nvlist */
483 create_packed_nvlist(char **buf, uint_t *buflen, int encode)
485 uchar_t bytes[] = {0xaa, 0xbb, 0xcc, 0xdd};
486 int32_t int32[] = {3, 4, 5};
487 char *strs[] = {"child0", "child1", "child2"};
491 err = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0); /* allocate list */
493 (void) printf("nvlist_alloc() failed\en");
497 /* add a value of some types */
498 if ((nvlist_add_byte(nvl, "byte", bytes[0]) != 0) ||
499 (nvlist_add_int32(nvl, "int32", int32[0]) != 0) ||
500 (nvlist_add_int32_array(nvl, "int32_array", int32, 3) != 0) ||
501 (nvlist_add_string_array(nvl, "string_array", strs, 3) != 0)) {
506 err = nvlist_size(nvl, buflen, encode);
508 (void) printf("nvlist_size: %s\en", strerror(err));
513 /* pack into contig. memory */
514 err = nvlist_pack(nvl, buf, buflen, encode, 0);
516 (void) printf("nvlist_pack: %s\en", strerror(err));
518 /* free the original list */
523 /* selectively print nvpairs */
525 nvlist_lookup_and_print(nvlist_t *nvl)
531 if (nvlist_lookup_int32(nvl, "int32", &int_val) == 0)
532 (void) printf("int32 = %d\en", int_val);
533 if (nvlist_lookup_string_array(nvl, "string_array", &str_val, &nval)
535 (void) printf("string_array =");
536 for (i = 0; i < nval; i++)
537 (void) printf(" %s", str_val[i]);
538 (void) printf("\en");
544 main(int argc, char *argv[])
549 nvlist_t *nvl = NULL;
551 if (create_packed_nvlist(&buf, &buflen, NV_ENCODE_XDR) != 0) {
552 (void) printf("cannot create packed nvlist buffer\en");
556 /* unpack into an nvlist_t */
557 err = nvlist_unpack(buf, buflen, &nvl, 0);
559 (void) printf("nvlist_unpack(): %s\en", strerror(err));
563 /* selectively print out attributes */
564 nvlist_lookup_and_print(nvl);
572 See \fBattributes\fR(5) for descriptions of the following attributes:
580 ATTRIBUTE TYPE ATTRIBUTE VALUE
582 Interface Stability Evolving
589 \fBlibnvpair\fR(3LIB), \fBattributes\fR(5), \fBnvlist_alloc\fR(9F)