4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/types.h>
30 #include <sys/syscall.h>
37 #include <libnvpair.h>
42 #include "libcpc_impl.h"
45 * Pack a request set into a buffer using libnvpair. Size of buffer is returned
49 __cpc_pack_set(cpc_set_t
*set
, uint_t flags
, size_t *buflen
)
52 nvlist_t
*setlist
, **reqlist
;
58 if (nvlist_alloc(&setlist
, 0, 0) == ENOMEM
) {
63 if ((reqlist
= (nvlist_t
**)malloc(set
->cs_nreqs
* sizeof (*reqlist
)))
70 bzero((void *)reqlist
, set
->cs_nreqs
* sizeof (*reqlist
));
73 for (req
= set
->cs_request
; req
!= NULL
; req
= req
->cr_next
) {
74 if (nvlist_alloc(&reqlist
[i
], 0, 0) == ENOMEM
)
77 if (nvlist_add_string(reqlist
[i
], "cr_event",
80 if (nvlist_add_uint64(reqlist
[i
], "cr_preset",
83 if (nvlist_add_uint32(reqlist
[i
], "cr_flags",
86 if (nvlist_add_uint32(reqlist
[i
], "cr_index",
90 if (req
->cr_nattrs
!= 0) {
93 if (nvlist_alloc(&attrs
, NV_UNIQUE_NAME
, 0) == ENOMEM
)
96 for (j
= 0; j
< req
->cr_nattrs
; j
++) {
97 if (nvlist_add_uint64(attrs
,
98 req
->cr_attr
[j
].ka_name
,
99 req
->cr_attr
[j
].ka_val
) != 0) {
105 if (nvlist_add_nvlist(reqlist
[i
], "cr_attr",
116 if (nvlist_add_nvlist_array(setlist
, "reqs", reqlist
,
120 if (nvlist_add_uint32(setlist
, "flags", flags
) != 0)
123 if (nvlist_pack(setlist
, &buf
, &packsize
, NV_ENCODE_NATIVE
,
127 for (i
= 0; i
< set
->cs_nreqs
; i
++)
128 nvlist_free(reqlist
[i
]);
130 nvlist_free(setlist
);
137 for (i
= 0; i
< set
->cs_nreqs
; i
++) {
139 nvlist_free(reqlist
[i
]);
141 nvlist_free(setlist
);
148 __cpc_strhash_alloc(void)
152 if ((p
= malloc(sizeof (cpc_strhash_t
))) == NULL
)
163 __cpc_strhash_free(cpc_strhash_t
*hash
)
165 cpc_strhash_t
*p
= hash
, *f
;
175 * Insert a new key into the hash table.
177 * Returns 0 if key was unique and insert successful.
179 * Returns 1 if key was already in table and no insert took place.
181 * Returns -1 if out of memory.
184 __cpc_strhash_add(cpc_strhash_t
*hash
, char *key
)
186 cpc_strhash_t
*p
, *tmp
;
188 for (p
= hash
; p
!= NULL
; p
= p
->next
) {
189 if (strcmp(p
->str
, key
) == 0)
193 if ((p
= malloc(sizeof (*p
))) == NULL
)
201 * The head node's current pointer must stay pointed at the first
202 * real node. We just inserted at the head.
210 __cpc_strhash_next(cpc_strhash_t
*hash
)
214 if (hash
->cur
!= NULL
) {
216 hash
->cur
= hash
->cur
->next
;