2 .\" Copyright (c) 2006, 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 9F "Feb 15, 2016"
8 nvlist_alloc, nvlist_free, nvlist_size, nvlist_pack, nvlist_unpack, nvlist_dup,
9 nv_alloc_init, nv_alloc_fini, nvlist_xalloc, nvlist_xpack, nvlist_xunpack,
10 nvlist_xdup, nvlist_merge \- Manage a name-value pair list
14 #include <sys/nvpair.h>
18 \fBint\fR \fBnvlist_alloc\fR(\fBnvlist_t **\fR\fInvlp\fR, \fBuint_t\fR \fInvflag\fR,
19 \fBint\fR \fIkmflag\fR);
24 \fBint\fR \fBnvlist_xalloc\fR(\fBnvlist_t **\fR\fInvlp\fR, \fBuint_t\fR \fInvflag\fR, \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, \fBint\fR \fIencoding\fR,
40 \fBint\fR \fIflag\fR);
45 \fBint\fR \fBnvlist_xpack\fR(\fBnvlist_t *\fR\fInvl\fR, \fBchar **\fR\fIbufp\fR, \fBsize_t *\fR\fIbuflen\fR, \fBint\fR \fIencoding\fR,
46 \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, \fBint\fR \fIflag\fR);
56 \fBint\fR \fBnvlist_xunpack\fR(\fBchar *\fR\fIbuf\fR, \fBsize_t\fR \fIbuflen\fR, \fBnvlist_t **\fR\fInvlp\fR,
57 \fBnv_alloc_t *\fR\fInva\fR);
62 \fBint\fR \fBnvlist_dup\fR(\fBnvlist_t *\fR\fInvl\fR, \fBnvlist_t **\fR\fInvlp\fR, \fBint\fR \fIflag\fR);
67 \fBint\fR \fBnvlist_xdup\fR(\fBnvlist_t *\fR\fInvl\fR, \fBnvlist_t **\fR\fInvlp\fR, \fBnv_alloc_t *\fR\fInva\fR);
72 \fBint\fR \fBnvlist_merge\fR(\fBnvlist_t *\fR\fIdst\fR, \fBnvlist_t *\fR\fInvl\fR, \fBint\fR \fIflag\fR);
77 Pluggable Allocator Configuration:
79 \fBnv_alloc_t *\fR\fBnvlist_lookup_nv_alloc\fR(\fBnvlist_t *);\fR
84 \fBint\fR \fBnv_alloc_init\fR(\fBnv_alloc_t *\fR\fInva\fR,
85 \fBconst nv_alloc_ops_t *\fR \fInvo,\fR/* args */ ...);
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);
100 Pluggable Allocation Initialization with Fixed Allocator:
102 \fBint\fR \fBnv_alloc_init\fR(\fBnv_alloc_t *\fR\fInva\fR,
103 \fBnv_fixed_ops\fR, \fBvoid *\fR \fIbufptr\fR, \fBsize_t\fR sz);
108 Solaris DDI specific (Solaris DDI)
115 Address of a pointer to list of name-value pairs (\fBnvlist_t\fR).
124 Specify bit fields defining \fBnvlist_t\fR properties:
128 \fB\fBNV_UNIQUE_NAME\fR\fR
131 \fBnvpair\fR names are unique.
137 \fB\fBNV_UNIQUE_NAME_TYPE\fR\fR
140 Name-data type combination is unique
151 Kernel memory allocation policy, either \fBKM_SLEEP\fR or \fBKM_NOSLEEP\fR.
160 \fBnvlist_t\fR to be processed.
169 Destination \fBnvlist_t\fR.
178 Pointer to buffer to contain the encoded size.
187 Address of buffer to pack \fBnvlist\fR into. Must be 8-byte aligned. If NULL,
188 library will allocate memory.
197 Buffer containing packed \fBnvlist_t\fR.
206 Size of buffer \fIbufp\fR or \fIbuf\fR points to.
215 Encoding method for packing.
224 Pluggable allocator operations pointer (nv_alloc_ops_t).
233 Points to a nv_alloc_t structure to be used for the specified \fBnvlist_t\fR.
241 The \fBnvlist_alloc()\fR function allocates a new name-value pair list and
242 updates \fInvlp\fR to point to the handle. The argument \fInvflag\fR specifies
243 \fBnvlist_t\fR properties to remain persistent across packing, unpacking, and
247 If \fBNV_UNIQUE_NAME\fR is specified for nvflag, existing nvpairs with matching
248 names are removed before the new nvpair is added. If \fBNV_UNIQUE_NAME_TYPE\fR
249 is specified for nvflag, existing nvpairs with matching names and data types
250 are removed before the new nvpair is added. See \fBnvlist_add_byte\fR(9F) for
254 The \fBnvlist_xalloc()\fR function differs from \fBnvlist_alloc()\fR in that
255 \fBnvlist_xalloc()\fR can use a different allocator, as described in the
256 Pluggable Allocators section.
259 The \fBnvlist_free()\fR function frees a name-value pair list. If \fInvl\fR
260 is a null pointer, no action occurs.
263 The \fBnvlist_size()\fR function returns the minimum size of a contiguous
264 buffer large enough to pack \fInvl\fR. The \fIencoding\fR parameter specifies
265 the method of encoding when packing \fInvl\fR. Supported encoding methods are:
269 \fB\fBNV_ENCODE_NATIVE\fR\fR
272 Straight \fBbcopy()\fR as described in \fBbcopy\fR(9F).
278 \fB\fBNV_ENCODE_XDR\fR\fR
281 Use XDR encoding, suitable for sending to another host.
286 The \fBnvlist_pack()\fR function packs \fInvl\fR into contiguous memory
287 starting at *\fIbufp\fR. The \fIencoding\fR parameter specifies the method of
288 encoding (see above).
293 If *\fIbufp\fR is not NULL, *\fIbufp\fR is expected to be a caller-allocated
294 buffer of size *\fIbuflen\fR. The \fIkmflag\fR argument is ignored.
300 If *\fIbufp\fR is NULL, the library allocates memory and updates *\fIbufp\fR to
301 point to the memory and updates *\fIbuflen\fR to contain the size of the
302 allocated memory. The value of \fIkmflag\fR indicates the memory allocation
307 The \fBnvlist_xpack()\fR function differs from \fBnvlist_pack()\fR in that
308 \fBnvlist_xpack()\fR can use a different allocator.
311 The \fBnvlist_unpack()\fR function takes a buffer with a packed \fBnvlist_t\fR
312 and unpacks it into a searchable \fBnvlist_t\fR. The library allocates memory
313 for \fBnvlist_t\fR. The caller is responsible for freeing the memory by calling
317 The \fBnvlist_xunpack()\fR function differs from \fBnvlist_unpack()\fR in that
318 \fBnvlist_xunpack()\fR can use a different allocator.
321 The \fBnvlist_dup()\fR function makes a copy of \fInvl\fR and updates
322 \fInvlp\fR to point to the copy.
325 The \fBnvlist_xdup()\fR function differs from \fBnvlist_dup()\fR in that
326 \fBnvlist_xdup()\fR can use a different allocator.
329 The \fBnvlist_merge()\fR function adds copies of all name-value pairs from
330 \fBnvlist_t\fR \fInvl\fR to \fBnvlist_t dst\fR. Name-value pairs in dst are
331 replaced with name-value pairs from \fBnvl\fR which have identical names (if
332 dst has the type \fBNV_UNIQUE_NAME\fR), or identical names and types (if dst
333 has the type \fBNV_UNIQUE_NAME_TYPE\fR).
336 The \fBnvlist_lookup_nv_alloc()\fR function retrieves the pointer to the
337 allocator used when manipulating a name-value pair list.
338 .SS "PLUGGABLE ALLOCATORS"
340 Using Pluggable Allocators:
343 The \fBnv_alloc_init()\fR, \fBnv_alloc_reset()\fR and \fBnv_alloc_fini()\fR
344 functions provide an interface that specifies the allocator to be used when
345 manipulating a name-value pair list.
348 The \fBnv_alloc_init()\fR determines allocator properties and puts them into
349 the \fInva\fR argument. You need to specify the \fInv_arg\fR argument, the
350 \fInvo\fR argument and an optional variable argument list. The optional
351 arguments are passed to the (*\fBnv_ao_init()\fR) function.
354 The \fInva\fR argument must be passed to \fBnvlist_xalloc()\fR,
355 \fBnvlist_xpack()\fR, \fBnvlist_xunpack()\fR and \fBnvlist_xdup()\fR.
358 The \fBnv_alloc_reset()\fR function resets the allocator properties to the data
359 specified by \fBnv_alloc_init()\fR. When no (*\fBnv_ao_reset()\fR) function is
360 specified, \fBnv_alloc_reset()\fR is without effect.
363 The \fBnv_alloc_fini()\fR destroys the allocator properties determined by
364 \fBnv_alloc_init()\fR. When a (*\fBnv_ao_fini()\fR) routine is specified, it is
365 called from \fBnv_alloc_fini()\fR.
368 The disposition of the allocated objects and the memory used to store them is
369 left to the allocator implementation.
372 The `nv_alloc_sleep' and `nv_alloc_nosleep' nv_alloc_t pointers may be used
373 with nvlist_xalloc to mimic the behavior of nvlist_alloc with KM_SLEEP and
374 KM_NOSLEEP, respectively.
385 The nvpair framework provides a fixed-buffer allocator, accessible via
396 Given a buffer size and address, the fixed-buffer allocator allows for the
397 creation of nvlists in contexts where malloc or kmem_alloc services may not be
398 available. The fixed-buffer allocator is designed primarily to support the
402 Memory freed using \fBnvlist_free()\fR, pair-removal, or similar routines is
406 When used to initialize the fixed-buffer allocator, nv_alloc_init should be
411 int nv_alloc_init(nv_alloc_t *nva, const nv_alloc_ops_t *nvo,
412 void *bufptr, size_t sz);
418 When invoked on a fixed-buffer, the \fBnv_alloc_reset()\fR function resets the
419 fixed buffer and prepares it for re-use. The framework consumer is responsible
420 for freeing the buffer passed to \fBnv_alloc_init()\fR.
421 .SS "CREATING PLUGGABLE ALLOCATORS"
423 Any producer of name-value pairs may possibily specify his own allocator
424 routines. You must provide the following pluggable allocator operations in the
425 allocator implementation.
429 int (*nv_ao_init)(nv_alloc_t *nva, va_list nv_valist);
430 void (*nv_ao_fini)(nv_alloc_t *nva);
431 void *(*nv_ao_alloc)(nv_alloc_t *nva, size_t sz);
432 void (*nv_ao_reset)(nv_alloc_t *nva);
433 void (*nv_ao_free)(nv_alloc_t *nva, void *buf, size_t sz);
439 The \fInva\fR argument of the allocator implementation is always the first
443 The optional (*\fBnv_ao_init()\fR ) function is responsible for filling the
444 data specified by \fBnv_alloc_init()\fR into the \fBnva_arg()\fR argument. The
445 (*\fBnv_ao_init()\fR) function is called only when \fBnv_alloc_init()\fR is
449 The optional (*\fBnv_ao_fini()\fR) function is responsible for the cleanup of
450 the allocator implementation. It is called by \fBnv_alloc_fini()\fR.
453 The required (*\fBnv_ao_alloc()\fR) function is used in the nvpair allocation
454 framework for memory allocation. The sz argument specifies the size of the
458 The optional (*\fBnv_ao_reset()\fR) function is responsible for resetting the
459 nva_arg argument to the data specified by \fBnv_alloc_init()\fR.
462 The required (*\fBnv_ao_free()\fR) function is used in the nvpair allocator
463 framework for memory de-allocation. The argument buf is a pointer to a block
464 previously allocated by (*\fBnv_ao_alloc()\fR) function. The size argument sz
465 must exactly match the original allocation.
468 The disposition of the allocated objects and the memory used to store them is
469 left to the allocator implementation.
472 For \fBnvlist_alloc()\fR, \fBnvlist_dup()\fR, \fBnvlist_xalloc()\fR, and
503 For \fBnvlist_pack()\fR, \fBnvlist_unpack()\fR, \fBnvlist_xpack()\fR, and
504 \fBnvlist_xunpack()\fR:
547 encode/decode method not supported
552 For \fBnvlist_size()\fR:
573 For \fBnvlist_lookup_nv_alloc()\fR:
576 pointer to the allocator
579 The fixed-buffer allocator is very simple allocator. It uses a pre-allocated
580 buffer for memory allocations and it can be used in interrupt context. You are
581 responsible for allocation and de-allocation for the pre-allocated buffer.
586 * using the fixed-buffer allocator.
588 #include <sys/nvpair.h>
590 /* initialize the nvpair allocator framework */
592 init(char *buf, size_t size)
596 if ((nvap = kmem_alloc(sizeof(nv_alloc_t), KM_SLEEP)) == NULL)
599 if (nv_alloc_init(nvap, nv_fixed_ops, buf, size) == 0)
606 fini(nv_alloc_t *nvap)
609 kmem_free(nvap, sizeof(nv_alloc_t));
612 interrupt_context(nv_alloc_t *nva)
617 if ((error = nvlist_xalloc(&nvl, NV_UNIQUE_NAME, nva)) != 0)
620 if ((error = nvlist_add_int32(nvl, "name", 1234)) == 0)
621 error = send_nvl(nvl);
631 The \fBnvlist_alloc()\fR, \fBnvlist_pack()\fR, \fBnvlist_unpack()\fR, and
632 \fBnvlist_dup()\fR functions can be called from interrupt context only if the
633 \fBKM_NOSLEEP\fR flag is set. They can be called from user context with any
637 The \fBnvlist_xalloc()\fR, \fBnvlist_xpack()\fR, \fBnvlist_xunpack()\fR, and
638 \fBnvlist_xdup()\fR functions can be called from interrupt context only if (1)
639 the default allocator is used and the \fBKM_NOSLEEP\fR flag is set or (2) the
640 specified allocator did not sleep for free memory (for example, it uses a
641 pre-allocated buffer for memory allocations).
644 These functions can be called from user or kernel context with any valid flag.