xref: /onnv-gate/usr/src/cmd/fm/fmd/common/fmd_buf.c (revision 1429:f6c2fc2d708c)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22*1429Smws 
230Sstevel@tonic-gate /*
24*1429Smws  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <fmd_alloc.h>
310Sstevel@tonic-gate #include <fmd_string.h>
320Sstevel@tonic-gate #include <fmd_subr.h>
330Sstevel@tonic-gate #include <fmd_buf.h>
340Sstevel@tonic-gate #include <fmd.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate static fmd_buf_t *
fmd_buf_alloc(const char * name,size_t size)370Sstevel@tonic-gate fmd_buf_alloc(const char *name, size_t size)
380Sstevel@tonic-gate {
390Sstevel@tonic-gate 	fmd_buf_t *bp = fmd_alloc(sizeof (fmd_buf_t), FMD_SLEEP);
400Sstevel@tonic-gate 
410Sstevel@tonic-gate 	bp->buf_name = fmd_strdup(name, FMD_SLEEP);
420Sstevel@tonic-gate 	bp->buf_next = NULL;
430Sstevel@tonic-gate 	bp->buf_data = fmd_zalloc(size, FMD_SLEEP);
440Sstevel@tonic-gate 	bp->buf_size = size;
450Sstevel@tonic-gate 	bp->buf_flags = FMD_BUF_DIRTY;
460Sstevel@tonic-gate 
470Sstevel@tonic-gate 	return (bp);
480Sstevel@tonic-gate }
490Sstevel@tonic-gate 
500Sstevel@tonic-gate static void
fmd_buf_free(fmd_buf_t * bp)510Sstevel@tonic-gate fmd_buf_free(fmd_buf_t *bp)
520Sstevel@tonic-gate {
530Sstevel@tonic-gate 	fmd_strfree(bp->buf_name);
540Sstevel@tonic-gate 	fmd_free(bp->buf_data, bp->buf_size);
550Sstevel@tonic-gate 	fmd_free(bp, sizeof (fmd_buf_t));
560Sstevel@tonic-gate }
570Sstevel@tonic-gate 
580Sstevel@tonic-gate void
fmd_buf_hash_create(fmd_buf_hash_t * bhp)590Sstevel@tonic-gate fmd_buf_hash_create(fmd_buf_hash_t *bhp)
600Sstevel@tonic-gate {
610Sstevel@tonic-gate 	bhp->bh_hashlen = fmd.d_str_buckets;
620Sstevel@tonic-gate 	bhp->bh_hash = fmd_zalloc(sizeof (void *) * bhp->bh_hashlen, FMD_SLEEP);
630Sstevel@tonic-gate 	bhp->bh_count = 0;
640Sstevel@tonic-gate }
650Sstevel@tonic-gate 
66*1429Smws size_t
fmd_buf_hash_destroy(fmd_buf_hash_t * bhp)670Sstevel@tonic-gate fmd_buf_hash_destroy(fmd_buf_hash_t *bhp)
680Sstevel@tonic-gate {
69*1429Smws 	size_t total = 0;
700Sstevel@tonic-gate 	fmd_buf_t *bp, *np;
710Sstevel@tonic-gate 	uint_t i;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	for (i = 0; i < bhp->bh_hashlen; i++) {
740Sstevel@tonic-gate 		for (bp = bhp->bh_hash[i]; bp != NULL; bp = np) {
750Sstevel@tonic-gate 			np = bp->buf_next;
76*1429Smws 			total += bp->buf_size;
770Sstevel@tonic-gate 			fmd_buf_free(bp);
780Sstevel@tonic-gate 		}
790Sstevel@tonic-gate 	}
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	fmd_free(bhp->bh_hash, sizeof (void *) * bhp->bh_hashlen);
820Sstevel@tonic-gate 	bzero(bhp, sizeof (fmd_buf_hash_t));
83*1429Smws 	return (total);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate 
860Sstevel@tonic-gate void
fmd_buf_hash_apply(fmd_buf_hash_t * bhp,fmd_buf_f * func,void * arg)870Sstevel@tonic-gate fmd_buf_hash_apply(fmd_buf_hash_t *bhp, fmd_buf_f *func, void *arg)
880Sstevel@tonic-gate {
890Sstevel@tonic-gate 	fmd_buf_t *bp;
900Sstevel@tonic-gate 	uint_t i;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	for (i = 0; i < bhp->bh_hashlen; i++) {
930Sstevel@tonic-gate 		for (bp = bhp->bh_hash[i]; bp != NULL; bp = bp->buf_next)
940Sstevel@tonic-gate 			func(bp, arg);
950Sstevel@tonic-gate 	}
960Sstevel@tonic-gate }
970Sstevel@tonic-gate 
980Sstevel@tonic-gate void
fmd_buf_hash_commit(fmd_buf_hash_t * bhp)990Sstevel@tonic-gate fmd_buf_hash_commit(fmd_buf_hash_t *bhp)
1000Sstevel@tonic-gate {
1010Sstevel@tonic-gate 	fmd_buf_t *bp;
1020Sstevel@tonic-gate 	uint_t i;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	for (i = 0; i < bhp->bh_hashlen; i++) {
1050Sstevel@tonic-gate 		for (bp = bhp->bh_hash[i]; bp != NULL; bp = bp->buf_next)
1060Sstevel@tonic-gate 			bp->buf_flags &= ~FMD_BUF_DIRTY;
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate uint_t
fmd_buf_hash_count(fmd_buf_hash_t * bhp)1110Sstevel@tonic-gate fmd_buf_hash_count(fmd_buf_hash_t *bhp)
1120Sstevel@tonic-gate {
1130Sstevel@tonic-gate 	return (bhp->bh_count);
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate fmd_buf_t *
fmd_buf_insert(fmd_buf_hash_t * bhp,const char * name,size_t size)1170Sstevel@tonic-gate fmd_buf_insert(fmd_buf_hash_t *bhp, const char *name, size_t size)
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate 	uint_t h = fmd_strhash(name) % bhp->bh_hashlen;
1200Sstevel@tonic-gate 	fmd_buf_t *bp = fmd_buf_alloc(name, size);
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	bp->buf_next = bhp->bh_hash[h];
1230Sstevel@tonic-gate 	bhp->bh_hash[h] = bp;
1240Sstevel@tonic-gate 	bhp->bh_count++;
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	return (bp);
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate fmd_buf_t *
fmd_buf_lookup(fmd_buf_hash_t * bhp,const char * name)1300Sstevel@tonic-gate fmd_buf_lookup(fmd_buf_hash_t *bhp, const char *name)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	uint_t h = fmd_strhash(name) % bhp->bh_hashlen;
1330Sstevel@tonic-gate 	fmd_buf_t *bp;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	for (bp = bhp->bh_hash[h]; bp != NULL; bp = bp->buf_next) {
1360Sstevel@tonic-gate 		if (strcmp(name, bp->buf_name) == 0)
1370Sstevel@tonic-gate 			return (bp);
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	return (NULL);
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate void
fmd_buf_delete(fmd_buf_hash_t * bhp,const char * name)1440Sstevel@tonic-gate fmd_buf_delete(fmd_buf_hash_t *bhp, const char *name)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate 	uint_t h = fmd_strhash(name) % bhp->bh_hashlen;
1470Sstevel@tonic-gate 	fmd_buf_t *bp, **pp = &bhp->bh_hash[h];
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	for (bp = *pp; bp != NULL; bp = bp->buf_next) {
1500Sstevel@tonic-gate 		if (strcmp(bp->buf_name, name) != 0)
1510Sstevel@tonic-gate 			pp = &bp->buf_next;
1520Sstevel@tonic-gate 		else
1530Sstevel@tonic-gate 			break;
1540Sstevel@tonic-gate 	}
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	if (bp != NULL) {
1570Sstevel@tonic-gate 		*pp = bp->buf_next;
1580Sstevel@tonic-gate 		fmd_buf_free(bp);
1590Sstevel@tonic-gate 		ASSERT(bhp->bh_count != 0);
1600Sstevel@tonic-gate 		bhp->bh_count--;
1610Sstevel@tonic-gate 	}
1620Sstevel@tonic-gate }
163