xref: /titanic_44/usr/src/cmd/fm/fmd/common/fmd_buf.c (revision 0b9e3e769e160ab52d990398d55e6318f737a940)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*0b9e3e76Smws 
237c478bd9Sstevel@tonic-gate /*
24*0b9e3e76Smws  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <fmd_alloc.h>
317c478bd9Sstevel@tonic-gate #include <fmd_string.h>
327c478bd9Sstevel@tonic-gate #include <fmd_subr.h>
337c478bd9Sstevel@tonic-gate #include <fmd_buf.h>
347c478bd9Sstevel@tonic-gate #include <fmd.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate static fmd_buf_t *
fmd_buf_alloc(const char * name,size_t size)377c478bd9Sstevel@tonic-gate fmd_buf_alloc(const char *name, size_t size)
387c478bd9Sstevel@tonic-gate {
397c478bd9Sstevel@tonic-gate 	fmd_buf_t *bp = fmd_alloc(sizeof (fmd_buf_t), FMD_SLEEP);
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate 	bp->buf_name = fmd_strdup(name, FMD_SLEEP);
427c478bd9Sstevel@tonic-gate 	bp->buf_next = NULL;
437c478bd9Sstevel@tonic-gate 	bp->buf_data = fmd_zalloc(size, FMD_SLEEP);
447c478bd9Sstevel@tonic-gate 	bp->buf_size = size;
457c478bd9Sstevel@tonic-gate 	bp->buf_flags = FMD_BUF_DIRTY;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 	return (bp);
487c478bd9Sstevel@tonic-gate }
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate static void
fmd_buf_free(fmd_buf_t * bp)517c478bd9Sstevel@tonic-gate fmd_buf_free(fmd_buf_t *bp)
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate 	fmd_strfree(bp->buf_name);
547c478bd9Sstevel@tonic-gate 	fmd_free(bp->buf_data, bp->buf_size);
557c478bd9Sstevel@tonic-gate 	fmd_free(bp, sizeof (fmd_buf_t));
567c478bd9Sstevel@tonic-gate }
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate void
fmd_buf_hash_create(fmd_buf_hash_t * bhp)597c478bd9Sstevel@tonic-gate fmd_buf_hash_create(fmd_buf_hash_t *bhp)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	bhp->bh_hashlen = fmd.d_str_buckets;
627c478bd9Sstevel@tonic-gate 	bhp->bh_hash = fmd_zalloc(sizeof (void *) * bhp->bh_hashlen, FMD_SLEEP);
637c478bd9Sstevel@tonic-gate 	bhp->bh_count = 0;
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate 
66*0b9e3e76Smws size_t
fmd_buf_hash_destroy(fmd_buf_hash_t * bhp)677c478bd9Sstevel@tonic-gate fmd_buf_hash_destroy(fmd_buf_hash_t *bhp)
687c478bd9Sstevel@tonic-gate {
69*0b9e3e76Smws 	size_t total = 0;
707c478bd9Sstevel@tonic-gate 	fmd_buf_t *bp, *np;
717c478bd9Sstevel@tonic-gate 	uint_t i;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	for (i = 0; i < bhp->bh_hashlen; i++) {
747c478bd9Sstevel@tonic-gate 		for (bp = bhp->bh_hash[i]; bp != NULL; bp = np) {
757c478bd9Sstevel@tonic-gate 			np = bp->buf_next;
76*0b9e3e76Smws 			total += bp->buf_size;
777c478bd9Sstevel@tonic-gate 			fmd_buf_free(bp);
787c478bd9Sstevel@tonic-gate 		}
797c478bd9Sstevel@tonic-gate 	}
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	fmd_free(bhp->bh_hash, sizeof (void *) * bhp->bh_hashlen);
827c478bd9Sstevel@tonic-gate 	bzero(bhp, sizeof (fmd_buf_hash_t));
83*0b9e3e76Smws 	return (total);
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate void
fmd_buf_hash_apply(fmd_buf_hash_t * bhp,fmd_buf_f * func,void * arg)877c478bd9Sstevel@tonic-gate fmd_buf_hash_apply(fmd_buf_hash_t *bhp, fmd_buf_f *func, void *arg)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	fmd_buf_t *bp;
907c478bd9Sstevel@tonic-gate 	uint_t i;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	for (i = 0; i < bhp->bh_hashlen; i++) {
937c478bd9Sstevel@tonic-gate 		for (bp = bhp->bh_hash[i]; bp != NULL; bp = bp->buf_next)
947c478bd9Sstevel@tonic-gate 			func(bp, arg);
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate void
fmd_buf_hash_commit(fmd_buf_hash_t * bhp)997c478bd9Sstevel@tonic-gate fmd_buf_hash_commit(fmd_buf_hash_t *bhp)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	fmd_buf_t *bp;
1027c478bd9Sstevel@tonic-gate 	uint_t i;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	for (i = 0; i < bhp->bh_hashlen; i++) {
1057c478bd9Sstevel@tonic-gate 		for (bp = bhp->bh_hash[i]; bp != NULL; bp = bp->buf_next)
1067c478bd9Sstevel@tonic-gate 			bp->buf_flags &= ~FMD_BUF_DIRTY;
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate uint_t
fmd_buf_hash_count(fmd_buf_hash_t * bhp)1117c478bd9Sstevel@tonic-gate fmd_buf_hash_count(fmd_buf_hash_t *bhp)
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate 	return (bhp->bh_count);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate fmd_buf_t *
fmd_buf_insert(fmd_buf_hash_t * bhp,const char * name,size_t size)1177c478bd9Sstevel@tonic-gate fmd_buf_insert(fmd_buf_hash_t *bhp, const char *name, size_t size)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	uint_t h = fmd_strhash(name) % bhp->bh_hashlen;
1207c478bd9Sstevel@tonic-gate 	fmd_buf_t *bp = fmd_buf_alloc(name, size);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	bp->buf_next = bhp->bh_hash[h];
1237c478bd9Sstevel@tonic-gate 	bhp->bh_hash[h] = bp;
1247c478bd9Sstevel@tonic-gate 	bhp->bh_count++;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	return (bp);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate fmd_buf_t *
fmd_buf_lookup(fmd_buf_hash_t * bhp,const char * name)1307c478bd9Sstevel@tonic-gate fmd_buf_lookup(fmd_buf_hash_t *bhp, const char *name)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	uint_t h = fmd_strhash(name) % bhp->bh_hashlen;
1337c478bd9Sstevel@tonic-gate 	fmd_buf_t *bp;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	for (bp = bhp->bh_hash[h]; bp != NULL; bp = bp->buf_next) {
1367c478bd9Sstevel@tonic-gate 		if (strcmp(name, bp->buf_name) == 0)
1377c478bd9Sstevel@tonic-gate 			return (bp);
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	return (NULL);
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate void
fmd_buf_delete(fmd_buf_hash_t * bhp,const char * name)1447c478bd9Sstevel@tonic-gate fmd_buf_delete(fmd_buf_hash_t *bhp, const char *name)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate 	uint_t h = fmd_strhash(name) % bhp->bh_hashlen;
1477c478bd9Sstevel@tonic-gate 	fmd_buf_t *bp, **pp = &bhp->bh_hash[h];
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	for (bp = *pp; bp != NULL; bp = bp->buf_next) {
1507c478bd9Sstevel@tonic-gate 		if (strcmp(bp->buf_name, name) != 0)
1517c478bd9Sstevel@tonic-gate 			pp = &bp->buf_next;
1527c478bd9Sstevel@tonic-gate 		else
1537c478bd9Sstevel@tonic-gate 			break;
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	if (bp != NULL) {
1577c478bd9Sstevel@tonic-gate 		*pp = bp->buf_next;
1587c478bd9Sstevel@tonic-gate 		fmd_buf_free(bp);
1597c478bd9Sstevel@tonic-gate 		ASSERT(bhp->bh_count != 0);
1607c478bd9Sstevel@tonic-gate 		bhp->bh_count--;
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate }
163