xref: /illumos-gate/usr/src/cmd/fm/fmd/common/fmd_buf.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * CDDL HEADER START
3  *
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.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <fmd_alloc.h>
30 #include <fmd_string.h>
31 #include <fmd_subr.h>
32 #include <fmd_buf.h>
33 #include <fmd.h>
34 
35 static fmd_buf_t *
36 fmd_buf_alloc(const char *name, size_t size)
37 {
38 	fmd_buf_t *bp = fmd_alloc(sizeof (fmd_buf_t), FMD_SLEEP);
39 
40 	bp->buf_name = fmd_strdup(name, FMD_SLEEP);
41 	bp->buf_next = NULL;
42 	bp->buf_data = fmd_zalloc(size, FMD_SLEEP);
43 	bp->buf_size = size;
44 	bp->buf_flags = FMD_BUF_DIRTY;
45 
46 	return (bp);
47 }
48 
49 static void
50 fmd_buf_free(fmd_buf_t *bp)
51 {
52 	fmd_strfree(bp->buf_name);
53 	fmd_free(bp->buf_data, bp->buf_size);
54 	fmd_free(bp, sizeof (fmd_buf_t));
55 }
56 
57 void
58 fmd_buf_hash_create(fmd_buf_hash_t *bhp)
59 {
60 	bhp->bh_hashlen = fmd.d_str_buckets;
61 	bhp->bh_hash = fmd_zalloc(sizeof (void *) * bhp->bh_hashlen, FMD_SLEEP);
62 	bhp->bh_count = 0;
63 }
64 
65 void
66 fmd_buf_hash_destroy(fmd_buf_hash_t *bhp)
67 {
68 	fmd_buf_t *bp, *np;
69 	uint_t i;
70 
71 	for (i = 0; i < bhp->bh_hashlen; i++) {
72 		for (bp = bhp->bh_hash[i]; bp != NULL; bp = np) {
73 			np = bp->buf_next;
74 			fmd_buf_free(bp);
75 		}
76 	}
77 
78 	fmd_free(bhp->bh_hash, sizeof (void *) * bhp->bh_hashlen);
79 	bzero(bhp, sizeof (fmd_buf_hash_t));
80 }
81 
82 void
83 fmd_buf_hash_apply(fmd_buf_hash_t *bhp, fmd_buf_f *func, void *arg)
84 {
85 	fmd_buf_t *bp;
86 	uint_t i;
87 
88 	for (i = 0; i < bhp->bh_hashlen; i++) {
89 		for (bp = bhp->bh_hash[i]; bp != NULL; bp = bp->buf_next)
90 			func(bp, arg);
91 	}
92 }
93 
94 void
95 fmd_buf_hash_commit(fmd_buf_hash_t *bhp)
96 {
97 	fmd_buf_t *bp;
98 	uint_t i;
99 
100 	for (i = 0; i < bhp->bh_hashlen; i++) {
101 		for (bp = bhp->bh_hash[i]; bp != NULL; bp = bp->buf_next)
102 			bp->buf_flags &= ~FMD_BUF_DIRTY;
103 	}
104 }
105 
106 uint_t
107 fmd_buf_hash_count(fmd_buf_hash_t *bhp)
108 {
109 	return (bhp->bh_count);
110 }
111 
112 fmd_buf_t *
113 fmd_buf_insert(fmd_buf_hash_t *bhp, const char *name, size_t size)
114 {
115 	uint_t h = fmd_strhash(name) % bhp->bh_hashlen;
116 	fmd_buf_t *bp = fmd_buf_alloc(name, size);
117 
118 	bp->buf_next = bhp->bh_hash[h];
119 	bhp->bh_hash[h] = bp;
120 	bhp->bh_count++;
121 
122 	return (bp);
123 }
124 
125 fmd_buf_t *
126 fmd_buf_lookup(fmd_buf_hash_t *bhp, const char *name)
127 {
128 	uint_t h = fmd_strhash(name) % bhp->bh_hashlen;
129 	fmd_buf_t *bp;
130 
131 	for (bp = bhp->bh_hash[h]; bp != NULL; bp = bp->buf_next) {
132 		if (strcmp(name, bp->buf_name) == 0)
133 			return (bp);
134 	}
135 
136 	return (NULL);
137 }
138 
139 void
140 fmd_buf_delete(fmd_buf_hash_t *bhp, const char *name)
141 {
142 	uint_t h = fmd_strhash(name) % bhp->bh_hashlen;
143 	fmd_buf_t *bp, **pp = &bhp->bh_hash[h];
144 
145 	for (bp = *pp; bp != NULL; bp = bp->buf_next) {
146 		if (strcmp(bp->buf_name, name) != 0)
147 			pp = &bp->buf_next;
148 		else
149 			break;
150 	}
151 
152 	if (bp != NULL) {
153 		*pp = bp->buf_next;
154 		fmd_buf_free(bp);
155 		ASSERT(bhp->bh_count != 0);
156 		bhp->bh_count--;
157 	}
158 }
159