dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / fm / fmd / common / fmd_buf.c
blob42f50c2c6f2bbeb9f4efbdd45faf388b4b5e3543
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
24 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 #pragma ident "%Z%%M% %I% %E% SMI"
30 #include <fmd_alloc.h>
31 #include <fmd_string.h>
32 #include <fmd_subr.h>
33 #include <fmd_buf.h>
34 #include <fmd.h>
36 static fmd_buf_t *
37 fmd_buf_alloc(const char *name, size_t size)
39 fmd_buf_t *bp = fmd_alloc(sizeof (fmd_buf_t), FMD_SLEEP);
41 bp->buf_name = fmd_strdup(name, FMD_SLEEP);
42 bp->buf_next = NULL;
43 bp->buf_data = fmd_zalloc(size, FMD_SLEEP);
44 bp->buf_size = size;
45 bp->buf_flags = FMD_BUF_DIRTY;
47 return (bp);
50 static void
51 fmd_buf_free(fmd_buf_t *bp)
53 fmd_strfree(bp->buf_name);
54 fmd_free(bp->buf_data, bp->buf_size);
55 fmd_free(bp, sizeof (fmd_buf_t));
58 void
59 fmd_buf_hash_create(fmd_buf_hash_t *bhp)
61 bhp->bh_hashlen = fmd.d_str_buckets;
62 bhp->bh_hash = fmd_zalloc(sizeof (void *) * bhp->bh_hashlen, FMD_SLEEP);
63 bhp->bh_count = 0;
66 size_t
67 fmd_buf_hash_destroy(fmd_buf_hash_t *bhp)
69 size_t total = 0;
70 fmd_buf_t *bp, *np;
71 uint_t i;
73 for (i = 0; i < bhp->bh_hashlen; i++) {
74 for (bp = bhp->bh_hash[i]; bp != NULL; bp = np) {
75 np = bp->buf_next;
76 total += bp->buf_size;
77 fmd_buf_free(bp);
81 fmd_free(bhp->bh_hash, sizeof (void *) * bhp->bh_hashlen);
82 bzero(bhp, sizeof (fmd_buf_hash_t));
83 return (total);
86 void
87 fmd_buf_hash_apply(fmd_buf_hash_t *bhp, fmd_buf_f *func, void *arg)
89 fmd_buf_t *bp;
90 uint_t i;
92 for (i = 0; i < bhp->bh_hashlen; i++) {
93 for (bp = bhp->bh_hash[i]; bp != NULL; bp = bp->buf_next)
94 func(bp, arg);
98 void
99 fmd_buf_hash_commit(fmd_buf_hash_t *bhp)
101 fmd_buf_t *bp;
102 uint_t i;
104 for (i = 0; i < bhp->bh_hashlen; i++) {
105 for (bp = bhp->bh_hash[i]; bp != NULL; bp = bp->buf_next)
106 bp->buf_flags &= ~FMD_BUF_DIRTY;
110 uint_t
111 fmd_buf_hash_count(fmd_buf_hash_t *bhp)
113 return (bhp->bh_count);
116 fmd_buf_t *
117 fmd_buf_insert(fmd_buf_hash_t *bhp, const char *name, size_t size)
119 uint_t h = fmd_strhash(name) % bhp->bh_hashlen;
120 fmd_buf_t *bp = fmd_buf_alloc(name, size);
122 bp->buf_next = bhp->bh_hash[h];
123 bhp->bh_hash[h] = bp;
124 bhp->bh_count++;
126 return (bp);
129 fmd_buf_t *
130 fmd_buf_lookup(fmd_buf_hash_t *bhp, const char *name)
132 uint_t h = fmd_strhash(name) % bhp->bh_hashlen;
133 fmd_buf_t *bp;
135 for (bp = bhp->bh_hash[h]; bp != NULL; bp = bp->buf_next) {
136 if (strcmp(name, bp->buf_name) == 0)
137 return (bp);
140 return (NULL);
143 void
144 fmd_buf_delete(fmd_buf_hash_t *bhp, const char *name)
146 uint_t h = fmd_strhash(name) % bhp->bh_hashlen;
147 fmd_buf_t *bp, **pp = &bhp->bh_hash[h];
149 for (bp = *pp; bp != NULL; bp = bp->buf_next) {
150 if (strcmp(bp->buf_name, name) != 0)
151 pp = &bp->buf_next;
152 else
153 break;
156 if (bp != NULL) {
157 *pp = bp->buf_next;
158 fmd_buf_free(bp);
159 ASSERT(bhp->bh_count != 0);
160 bhp->bh_count--;