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]
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>
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
);
43 bp
->buf_data
= fmd_zalloc(size
, FMD_SLEEP
);
45 bp
->buf_flags
= FMD_BUF_DIRTY
;
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
));
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
);
67 fmd_buf_hash_destroy(fmd_buf_hash_t
*bhp
)
73 for (i
= 0; i
< bhp
->bh_hashlen
; i
++) {
74 for (bp
= bhp
->bh_hash
[i
]; bp
!= NULL
; bp
= np
) {
76 total
+= bp
->buf_size
;
81 fmd_free(bhp
->bh_hash
, sizeof (void *) * bhp
->bh_hashlen
);
82 bzero(bhp
, sizeof (fmd_buf_hash_t
));
87 fmd_buf_hash_apply(fmd_buf_hash_t
*bhp
, fmd_buf_f
*func
, void *arg
)
92 for (i
= 0; i
< bhp
->bh_hashlen
; i
++) {
93 for (bp
= bhp
->bh_hash
[i
]; bp
!= NULL
; bp
= bp
->buf_next
)
99 fmd_buf_hash_commit(fmd_buf_hash_t
*bhp
)
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
;
111 fmd_buf_hash_count(fmd_buf_hash_t
*bhp
)
113 return (bhp
->bh_count
);
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
;
130 fmd_buf_lookup(fmd_buf_hash_t
*bhp
, const char *name
)
132 uint_t h
= fmd_strhash(name
) % bhp
->bh_hashlen
;
135 for (bp
= bhp
->bh_hash
[h
]; bp
!= NULL
; bp
= bp
->buf_next
) {
136 if (strcmp(name
, bp
->buf_name
) == 0)
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)
159 ASSERT(bhp
->bh_count
!= 0);