1 '\" te 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" 7 .SH NAME 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 11 .SH SYNOPSIS 12 .LP 13 .nf 14 #include <sys/nvpair.h> 15 16 List Manipulation: 17 18 \fBint\fR \fBnvlist_alloc\fR(\fBnvlist_t **\fR\fInvlp\fR, \fBuint_t\fR \fInvflag\fR, 19 \fBint\fR \fIkmflag\fR); 20 .fi 21 22 .LP 23 .nf 24 \fBint\fR \fBnvlist_xalloc\fR(\fBnvlist_t **\fR\fInvlp\fR, \fBuint_t\fR \fInvflag\fR, \fBnv_alloc_t *\fR\fInva\fR); 25 .fi 26 27 .LP 28 .nf 29 \fBvoid\fR \fBnvlist_free\fR(\fBnvlist_t *\fR\fInvl\fR); 30 .fi 31 32 .LP 33 .nf 34 \fBint\fR \fBnvlist_size\fR(\fBnvlist_t *\fR\fInvl\fR, \fBsize_t *\fR\fIsize\fR, \fBint\fR \fIencoding\fR); 35 .fi 36 37 .LP 38 .nf 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); 41 .fi 42 43 .LP 44 .nf 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); 47 .fi 48 49 .LP 50 .nf 51 \fBint\fR \fBnvlist_unpack\fR(\fBchar *\fR\fIbuf\fR, \fBsize_t\fR \fIbuflen\fR, \fBnvlist_t **\fR\fInvlp\fR, \fBint\fR \fIflag\fR); 52 .fi 53 54 .LP 55 .nf 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); 58 .fi 59 60 .LP 61 .nf 62 \fBint\fR \fBnvlist_dup\fR(\fBnvlist_t *\fR\fInvl\fR, \fBnvlist_t **\fR\fInvlp\fR, \fBint\fR \fIflag\fR); 63 .fi 64 65 .LP 66 .nf 67 \fBint\fR \fBnvlist_xdup\fR(\fBnvlist_t *\fR\fInvl\fR, \fBnvlist_t **\fR\fInvlp\fR, \fBnv_alloc_t *\fR\fInva\fR); 68 .fi 69 70 .LP 71 .nf 72 \fBint\fR \fBnvlist_merge\fR(\fBnvlist_t *\fR\fIdst\fR, \fBnvlist_t *\fR\fInvl\fR, \fBint\fR \fIflag\fR); 73 .fi 74 75 .LP 76 .nf 77 Pluggable Allocator Configuration: 78 79 \fBnv_alloc_t *\fR\fBnvlist_lookup_nv_alloc\fR(\fBnvlist_t *);\fR 80 .fi 81 82 .LP 83 .nf 84 \fBint\fR \fBnv_alloc_init\fR(\fBnv_alloc_t *\fR\fInva\fR, 85 \fBconst nv_alloc_ops_t *\fR \fInvo,\fR/* args */ ...); 86 .fi 87 88 .LP 89 .nf 90 \fBvoid\fR \fBnv_alloc_reset\fR(\fBnv_alloc_t *\fR\fInva\fR); 91 .fi 92 93 .LP 94 .nf 95 \fBvoid\fR \fBnv_alloc_fini\fR(\fBnv_alloc_t *\fR\fInva\fR); 96 .fi 97 98 .LP 99 .nf 100 Pluggable Allocation Initialization with Fixed Allocator: 101 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); 104 .fi 105 106 .SH INTERFACE LEVEL 107 .sp 108 .LP 109 Solaris DDI specific (Solaris DDI) 110 .SH PARAMETERS 111 .sp 112 .ne 2 113 .na 114 \fB\fInvlp\fR\fR 115 .ad 116 .RS 12n 117 Address of a pointer to list of name-value pairs (\fBnvlist_t\fR). 118 .RE 119 120 .sp 121 .ne 2 122 .na 123 \fB\fInvflag\fR\fR 124 .ad 125 .RS 12n 126 Specify bit fields defining \fBnvlist_t\fR properties: 127 .sp 128 .ne 2 129 .na 130 \fB\fBNV_UNIQUE_NAME\fR\fR 131 .ad 132 .RS 23n 133 \fBnvpair\fR names are unique. 134 .RE 135 136 .sp 137 .ne 2 138 .na 139 \fB\fBNV_UNIQUE_NAME_TYPE\fR\fR 140 .ad 141 .RS 23n 142 Name-data type combination is unique 143 .RE 144 145 .RE 146 147 .sp 148 .ne 2 149 .na 150 \fB\fIkmflag\fR\fR 151 .ad 152 .RS 12n 153 Kernel memory allocation policy, either \fBKM_SLEEP\fR or \fBKM_NOSLEEP\fR. 154 .RE 155 156 .sp 157 .ne 2 158 .na 159 \fB\fInvl\fR\fR 160 .ad 161 .RS 12n 162 \fBnvlist_t\fR to be processed. 163 .RE 164 165 .sp 166 .ne 2 167 .na 168 \fB\fIdst\fR\fR 169 .ad 170 .RS 12n 171 Destination \fBnvlist_t\fR. 172 .RE 173 174 .sp 175 .ne 2 176 .na 177 \fB\fIsize\fR\fR 178 .ad 179 .RS 12n 180 Pointer to buffer to contain the encoded size. 181 .RE 182 183 .sp 184 .ne 2 185 .na 186 \fB\fIbufp\fR\fR 187 .ad 188 .RS 12n 189 Address of buffer to pack \fBnvlist\fR into. Must be 8-byte aligned. If NULL, 190 library will allocate memory. 191 .RE 192 193 .sp 194 .ne 2 195 .na 196 \fB\fIbuf\fR\fR 197 .ad 198 .RS 12n 199 Buffer containing packed \fBnvlist_t\fR. 200 .RE 201 202 .sp 203 .ne 2 204 .na 205 \fB\fIbuflen\fR\fR 206 .ad 207 .RS 12n 208 Size of buffer \fIbufp\fR or \fIbuf\fR points to. 209 .RE 210 211 .sp 212 .ne 2 213 .na 214 \fB\fIencoding\fR\fR 215 .ad 216 .RS 12n 217 Encoding method for packing. 218 .RE 219 220 .sp 221 .ne 2 222 .na 223 \fB\fInvo\fR\fR 224 .ad 225 .RS 12n 226 Pluggable allocator operations pointer (nv_alloc_ops_t). 227 .RE 228 229 .sp 230 .ne 2 231 .na 232 \fB\fInva\fR\fR 233 .ad 234 .RS 12n 235 Points to a nv_alloc_t structure to be used for the specified \fBnvlist_t\fR. 236 .RE 237 238 .SH DESCRIPTION 239 .sp 240 .LP 241 List Manipulation: 242 .sp 243 .LP 244 The \fBnvlist_alloc()\fR function allocates a new name-value pair list and 245 updates \fInvlp\fR to point to the handle. The argument \fInvflag\fR specifies 246 \fBnvlist_t\fR properties to remain persistent across packing, unpacking, and 247 duplication. 248 .sp 249 .LP 250 If \fBNV_UNIQUE_NAME\fR is specified for nvflag, existing nvpairs with matching 251 names are removed before the new nvpair is added. If \fBNV_UNIQUE_NAME_TYPE\fR 252 is specified for nvflag, existing nvpairs with matching names and data types 253 are removed before the new nvpair is added. See \fBnvlist_add_byte\fR(9F) for 254 more details. 255 .sp 256 .LP 257 The \fBnvlist_xalloc()\fR function differs from \fBnvlist_alloc()\fR in that 258 \fBnvlist_xalloc()\fR can use a different allocator, as described in the 259 Pluggable Allocators section. 260 .sp 261 .LP 262 The \fBnvlist_free()\fR function frees a name-value pair list. If \fInvl\fR 263 is a null pointer, no action occurs. 264 .sp 265 .LP 266 The \fBnvlist_size()\fR function returns the minimum size of a contiguous 267 buffer large enough to pack \fInvl\fR. The \fIencoding\fR parameter specifies 268 the method of encoding when packing \fInvl\fR. Supported encoding methods are: 269 .sp 270 .ne 2 271 .na 272 \fB\fBNV_ENCODE_NATIVE\fR\fR 273 .ad 274 .RS 20n 275 Straight \fBbcopy()\fR as described in \fBbcopy\fR(9F). 276 .RE 277 278 .sp 279 .ne 2 280 .na 281 \fB\fBNV_ENCODE_XDR\fR\fR 282 .ad 283 .RS 20n 284 Use XDR encoding, suitable for sending to another host. 285 .RE 286 287 .sp 288 .LP 289 The \fBnvlist_pack()\fR function packs \fInvl\fR into contiguous memory 290 starting at *\fIbufp\fR. The \fIencoding\fR parameter specifies the method of 291 encoding (see above). 292 .RS +4 293 .TP 294 .ie t \(bu 295 .el o 296 If *\fIbufp\fR is not NULL, *\fIbufp\fR is expected to be a caller-allocated 297 buffer of size *\fIbuflen\fR. The \fIkmflag\fR argument is ignored. 298 .RE 299 .RS +4 300 .TP 301 .ie t \(bu 302 .el o 303 If *\fIbufp\fR is NULL, the library allocates memory and updates *\fIbufp\fR to 304 point to the memory and updates *\fIbuflen\fR to contain the size of the 305 allocated memory. The value of \fIkmflag\fR indicates the memory allocation 306 policy 307 .RE 308 .sp 309 .LP 310 The \fBnvlist_xpack()\fR function differs from \fBnvlist_pack()\fR in that 311 \fBnvlist_xpack()\fR can use a different allocator. 312 .sp 313 .LP 314 The \fBnvlist_unpack()\fR function takes a buffer with a packed \fBnvlist_t\fR 315 and unpacks it into a searchable \fBnvlist_t\fR. The library allocates memory 316 for \fBnvlist_t\fR. The caller is responsible for freeing the memory by calling 317 \fBnvlist_free()\fR. 318 .sp 319 .LP 320 The \fBnvlist_xunpack()\fR function differs from \fBnvlist_unpack()\fR in that 321 \fBnvlist_xunpack()\fR can use a different allocator. 322 .sp 323 .LP 324 The \fBnvlist_dup()\fR function makes a copy of \fInvl\fR and updates 325 \fInvlp\fR to point to the copy. 326 .sp 327 .LP 328 The \fBnvlist_xdup()\fR function differs from \fBnvlist_dup()\fR in that 329 \fBnvlist_xdup()\fR can use a different allocator. 330 .sp 331 .LP 332 The \fBnvlist_merge()\fR function adds copies of all name-value pairs from 333 \fBnvlist_t\fR \fInvl\fR to \fBnvlist_t dst\fR. Name-value pairs in dst are 334 replaced with name-value pairs from \fBnvl\fR which have identical names (if 335 dst has the type \fBNV_UNIQUE_NAME\fR), or identical names and types (if dst 336 has the type \fBNV_UNIQUE_NAME_TYPE\fR). 337 .sp 338 .LP 339 The \fBnvlist_lookup_nv_alloc()\fR function retrieves the pointer to the 340 allocator used when manipulating a name-value pair list. 341 .SS "PLUGGABLE ALLOCATORS" 342 .sp 343 .LP 344 Using Pluggable Allocators: 345 .sp 346 .LP 347 The \fBnv_alloc_init()\fR, \fBnv_alloc_reset()\fR and \fBnv_alloc_fini()\fR 348 functions provide an interface that specifies the allocator to be used when 349 manipulating a name-value pair list. 350 .sp 351 .LP 352 The \fBnv_alloc_init()\fR determines allocator properties and puts them into 353 the \fInva\fR argument. You need to specify the \fInv_arg\fR argument, the 354 \fInvo\fR argument and an optional variable argument list. The optional 355 arguments are passed to the (*\fBnv_ao_init()\fR) function. 356 .sp 357 .LP 358 The \fInva\fR argument must be passed to \fBnvlist_xalloc()\fR, 359 \fBnvlist_xpack()\fR, \fBnvlist_xunpack()\fR and \fBnvlist_xdup()\fR. 360 .sp 361 .LP 362 The \fBnv_alloc_reset()\fR function resets the allocator properties to the data 363 specified by \fBnv_alloc_init()\fR. When no (*\fBnv_ao_reset()\fR) function is 364 specified, \fBnv_alloc_reset()\fR is without effect. 365 .sp 366 .LP 367 The \fBnv_alloc_fini()\fR destroys the allocator properties determined by 368 \fBnv_alloc_init()\fR. When a (*\fBnv_ao_fini()\fR) routine is specified, it is 369 called from \fBnv_alloc_fini()\fR. 370 .sp 371 .LP 372 The disposition of the allocated objects and the memory used to store them is 373 left to the allocator implementation. 374 .sp 375 .LP 376 The `nv_alloc_sleep' and `nv_alloc_nosleep' nv_alloc_t pointers may be used 377 with nvlist_xalloc to mimic the behavior of nvlist_alloc with KM_SLEEP and 378 KM_NOSLEEP, respectively. 379 .sp 380 .in +2 381 .nf 382 o nv_alloc_nosleep 383 o nv_alloc_sleep 384 .fi 385 .in -2 386 387 .sp 388 .LP 389 The nvpair framework provides a fixed-buffer allocator, accessible via 390 nv_fixed_ops. 391 .sp 392 .in +2 393 .nf 394 o nv_fixed_ops 395 .fi 396 .in -2 397 398 .sp 399 .LP 400 Given a buffer size and address, the fixed-buffer allocator allows for the 401 creation of nvlists in contexts where malloc or kmem_alloc services may not be 402 available. The fixed-buffer allocator is designed primarily to support the 403 creation of nvlists. 404 .sp 405 .LP 406 Memory freed using \fBnvlist_free()\fR, pair-removal, or similar routines is 407 not reclaimed. 408 .sp 409 .LP 410 When used to initialize the fixed-buffer allocator, nv_alloc_init should be 411 called as follows: 412 .sp 413 .in +2 414 .nf 415 int nv_alloc_init(nv_alloc_t *nva, const nv_alloc_ops_t *nvo, 416 void *bufptr, size_t sz); 417 .fi 418 .in -2 419 420 .sp 421 .LP 422 When invoked on a fixed-buffer, the \fBnv_alloc_reset()\fR function resets the 423 fixed buffer and prepares it for re-use. The framework consumer is responsible 424 for freeing the buffer passed to \fBnv_alloc_init()\fR. 425 .SS "CREATING PLUGGABLE ALLOCATORS" 426 .sp 427 .LP 428 Any producer of name-value pairs may possibily specify his own allocator 429 routines. You must provide the following pluggable allocator operations in the 430 allocator implementation. 431 .sp 432 .in +2 433 .nf 434 int (*nv_ao_init)(nv_alloc_t *nva, va_list nv_valist); 435 void (*nv_ao_fini)(nv_alloc_t *nva); 436 void *(*nv_ao_alloc)(nv_alloc_t *nva, size_t sz); 437 void (*nv_ao_reset)(nv_alloc_t *nva); 438 void (*nv_ao_free)(nv_alloc_t *nva, void *buf, size_t sz); 439 .fi 440 .in -2 441 442 .sp 443 .LP 444 The \fInva\fR argument of the allocator implementation is always the first 445 argument. 446 .sp 447 .LP 448 The optional (*\fBnv_ao_init()\fR ) function is responsible for filling the 449 data specified by \fBnv_alloc_init()\fR into the \fBnva_arg()\fR argument. The 450 (*\fBnv_ao_init()\fR) function is called only when \fBnv_alloc_init()\fR is 451 executed. 452 .sp 453 .LP 454 The optional (*\fBnv_ao_fini()\fR) function is responsible for the cleanup of 455 the allocator implementation. It is called by \fBnv_alloc_fini()\fR. 456 .sp 457 .LP 458 The required (*\fBnv_ao_alloc()\fR) function is used in the nvpair allocation 459 framework for memory allocation. The sz argument specifies the size of the 460 requested buffer. 461 .sp 462 .LP 463 The optional (*\fBnv_ao_reset()\fR) function is responsible for resetting the 464 nva_arg argument to the data specified by \fBnv_alloc_init()\fR. 465 .sp 466 .LP 467 The required (*\fBnv_ao_free()\fR) function is used in the nvpair allocator 468 framework for memory de-allocation. The argument buf is a pointer to a block 469 previously allocated by (*\fBnv_ao_alloc()\fR) function. The size argument sz 470 must exactly match the original allocation. 471 .sp 472 .LP 473 The disposition of the allocated objects and the memory used to store them is 474 left to the allocator implementation. 475 .SH RETURN VALUES 476 .sp 477 .LP 478 For \fBnvlist_alloc()\fR, \fBnvlist_dup()\fR, \fBnvlist_xalloc()\fR, and 479 \fBnvlist_xdup()\fR: 480 .sp 481 .ne 2 482 .na 483 \fB\fB0\fR\fR 484 .ad 485 .RS 10n 486 success 487 .RE 488 489 .sp 490 .ne 2 491 .na 492 \fB\fBEINVAL\fR\fR 493 .ad 494 .RS 10n 495 invalid argument 496 .RE 497 498 .sp 499 .ne 2 500 .na 501 \fB\fBENOMEM\fR\fR 502 .ad 503 .RS 10n 504 insufficient memory 505 .RE 506 507 .sp 508 .LP 509 For \fBnvlist_pack()\fR, \fBnvlist_unpack()\fR, \fBnvlist_xpack()\fR, and 510 \fBnvlist_xunpack()\fR: 511 .sp 512 .ne 2 513 .na 514 \fB\fB0\fR\fR 515 .ad 516 .RS 11n 517 success 518 .RE 519 520 .sp 521 .ne 2 522 .na 523 \fB\fBEINVAL\fR\fR 524 .ad 525 .RS 11n 526 invalid argument 527 .RE 528 529 .sp 530 .ne 2 531 .na 532 \fB\fBENOMEM\fR\fR 533 .ad 534 .RS 11n 535 insufficient memory 536 .RE 537 538 .sp 539 .ne 2 540 .na 541 \fB\fBEFAULT\fR\fR 542 .ad 543 .RS 11n 544 encode/decode error 545 .RE 546 547 .sp 548 .ne 2 549 .na 550 \fB\fBENOTSUP\fR\fR 551 .ad 552 .RS 11n 553 encode/decode method not supported 554 .RE 555 556 .sp 557 .LP 558 For \fBnvlist_size()\fR: 559 .sp 560 .ne 2 561 .na 562 \fB\fB0\fR\fR 563 .ad 564 .RS 10n 565 success 566 .RE 567 568 .sp 569 .ne 2 570 .na 571 \fB\fBEINVAL\fR\fR 572 .ad 573 .RS 10n 574 invalid argument 575 .RE 576 577 .sp 578 .LP 579 For \fBnvlist_lookup_nv_alloc()\fR: 580 .sp 581 .LP 582 pointer to the allocator 583 .SH USAGE 584 .sp 585 .LP 586 The fixed-buffer allocator is very simple allocator. It uses a pre-allocated 587 buffer for memory allocations and it can be used in interrupt context. You are 588 responsible for allocation and de-allocation for the pre-allocated buffer. 589 .SH EXAMPLES 590 .sp 591 .in +2 592 .nf 593 /* 594 * using the fixed-buffer allocator. 595 */ 596 #include <sys/nvpair.h> 597 598 /* initialize the nvpair allocator framework */ 599 static nv_alloc_t * 600 init(char *buf, size_t size) 601 { 602 nv_alloc_t *nvap; 603 604 if ((nvap = kmem_alloc(sizeof(nv_alloc_t), KM_SLEEP)) == NULL) 605 return (NULL); 606 607 if (nv_alloc_init(nvap, nv_fixed_ops, buf, size) == 0) 608 return (nvap); 609 610 return (NULL); 611 } 612 613 static void 614 fini(nv_alloc_t *nvap) 615 { 616 nv_alloc_fini(nvap); 617 kmem_free(nvap, sizeof(nv_alloc_t)); 618 } 619 static int 620 interrupt_context(nv_alloc_t *nva) 621 { 622 nvlist_t *nvl; 623 int error; 624 625 if ((error = nvlist_xalloc(&nvl, NV_UNIQUE_NAME, nva)) != 0) 626 return (-1); 627 628 if ((error = nvlist_add_int32(nvl, "name", 1234)) == 0) 629 error = send_nvl(nvl); 630 631 nvlist_free(nvl); 632 return (error); 633 } 634 .fi 635 .in -2 636 637 .SH CONTEXT 638 .sp 639 .LP 640 The \fBnvlist_alloc()\fR, \fBnvlist_pack()\fR, \fBnvlist_unpack()\fR, and 641 \fBnvlist_dup()\fR functions can be called from interrupt context only if the 642 \fBKM_NOSLEEP\fR flag is set. They can be called from user context with any 643 valid flag. 644 .sp 645 .LP 646 The \fBnvlist_xalloc()\fR, \fBnvlist_xpack()\fR, \fBnvlist_xunpack()\fR, and 647 \fBnvlist_xdup()\fR functions can be called from interrupt context only if (1) 648 the default allocator is used and the \fBKM_NOSLEEP\fR flag is set or (2) the 649 specified allocator did not sleep for free memory (for example, it uses a 650 pre-allocated buffer for memory allocations). 651 .sp 652 .LP 653 These functions can be called from user or kernel context with any valid flag.