8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libcpc / common / subr.c
blob68a94c4c1d241b37d6a7793fc72caf908e82444f
1 /*
2 * CDDL HEADER START
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
7 * with the License.
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]
20 * CDDL HEADER END
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>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <strings.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <libintl.h>
37 #include <libnvpair.h>
38 #include <thread.h>
39 #include <synch.h>
41 #include "libcpc.h"
42 #include "libcpc_impl.h"
45 * Pack a request set into a buffer using libnvpair. Size of buffer is returned
46 * in buflen.
48 char *
49 __cpc_pack_set(cpc_set_t *set, uint_t flags, size_t *buflen)
51 cpc_request_t *req;
52 nvlist_t *setlist, **reqlist;
53 size_t packsize = 0;
54 char *buf = NULL;
55 int i;
56 int j;
58 if (nvlist_alloc(&setlist, 0, 0) == ENOMEM) {
59 errno = ENOMEM;
60 return (NULL);
63 if ((reqlist = (nvlist_t **)malloc(set->cs_nreqs * sizeof (*reqlist)))
64 == NULL) {
65 nvlist_free(setlist);
66 errno = ENOMEM;
67 return (NULL);
70 bzero((void *)reqlist, set->cs_nreqs * sizeof (*reqlist));
72 i = 0;
73 for (req = set->cs_request; req != NULL; req = req->cr_next) {
74 if (nvlist_alloc(&reqlist[i], 0, 0) == ENOMEM)
75 goto nomem;
77 if (nvlist_add_string(reqlist[i], "cr_event",
78 req->cr_event) != 0)
79 goto nomem;
80 if (nvlist_add_uint64(reqlist[i], "cr_preset",
81 req->cr_preset) != 0)
82 goto nomem;
83 if (nvlist_add_uint32(reqlist[i], "cr_flags",
84 req->cr_flags) != 0)
85 goto nomem;
86 if (nvlist_add_uint32(reqlist[i], "cr_index",
87 req->cr_index) != 0)
88 goto nomem;
90 if (req->cr_nattrs != 0) {
91 nvlist_t *attrs;
93 if (nvlist_alloc(&attrs, NV_UNIQUE_NAME, 0) == ENOMEM)
94 goto nomem;
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) {
100 nvlist_free(attrs);
101 goto nomem;
105 if (nvlist_add_nvlist(reqlist[i], "cr_attr",
106 attrs) != 0) {
107 nvlist_free(attrs);
108 goto nomem;
111 nvlist_free(attrs);
113 i++;
116 if (nvlist_add_nvlist_array(setlist, "reqs", reqlist,
117 set->cs_nreqs) != 0)
118 goto nomem;
120 if (nvlist_add_uint32(setlist, "flags", flags) != 0)
121 goto nomem;
123 if (nvlist_pack(setlist, &buf, &packsize, NV_ENCODE_NATIVE,
124 0) != 0)
125 goto nomem;
127 for (i = 0; i < set->cs_nreqs; i++)
128 nvlist_free(reqlist[i]);
130 nvlist_free(setlist);
131 free(reqlist);
133 *buflen = packsize;
134 return (buf);
136 nomem:
137 for (i = 0; i < set->cs_nreqs; i++) {
138 if (reqlist[i] != 0)
139 nvlist_free(reqlist[i]);
141 nvlist_free(setlist);
142 free(reqlist);
143 errno = ENOMEM;
144 return (NULL);
147 cpc_strhash_t *
148 __cpc_strhash_alloc(void)
150 cpc_strhash_t *p;
152 if ((p = malloc(sizeof (cpc_strhash_t))) == NULL)
153 return (NULL);
155 p->str = "";
156 p->cur = NULL;
157 p->next = NULL;
159 return (p);
162 void
163 __cpc_strhash_free(cpc_strhash_t *hash)
165 cpc_strhash_t *p = hash, *f;
167 while (p != NULL) {
168 f = p;
169 p = p->next;
170 free(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)
190 return (1);
193 if ((p = malloc(sizeof (*p))) == NULL)
194 return (-1);
196 p->str = key;
197 tmp = hash->next;
198 hash->next = p;
199 p->next = tmp;
201 * The head node's current pointer must stay pointed at the first
202 * real node. We just inserted at the head.
204 hash->cur = p;
206 return (0);
209 char *
210 __cpc_strhash_next(cpc_strhash_t *hash)
212 cpc_strhash_t *p;
214 if (hash->cur != NULL) {
215 p = hash->cur;
216 hash->cur = hash->cur->next;
217 return (p->str);
220 return (NULL);