xref: /illumos-gate/usr/src/cmd/fm/fmd/common/fmd_buf.c (revision 2a8bcb4e)
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 #include <fmd_alloc.h>
297c478bd9Sstevel@tonic-gate #include <fmd_string.h>
307c478bd9Sstevel@tonic-gate #include <fmd_subr.h>
317c478bd9Sstevel@tonic-gate #include <fmd_buf.h>
327c478bd9Sstevel@tonic-gate #include <fmd.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate static fmd_buf_t *
fmd_buf_alloc(const char * name,size_t size)357c478bd9Sstevel@tonic-gate fmd_buf_alloc(const char *name, size_t size)
367c478bd9Sstevel@tonic-gate {
377c478bd9Sstevel@tonic-gate 	fmd_buf_t *bp = fmd_alloc(sizeof (fmd_buf_t), FMD_SLEEP);
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate 	bp->buf_name = fmd_strdup(name, FMD_SLEEP);
407c478bd9Sstevel@tonic-gate 	bp->buf_next = NULL;
417c478bd9Sstevel@tonic-gate 	bp->buf_data = fmd_zalloc(size, FMD_SLEEP);
427c478bd9Sstevel@tonic-gate 	bp->buf_size = size;
437c478bd9Sstevel@tonic-gate 	bp->buf_flags = FMD_BUF_DIRTY;
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 	return (bp);
467c478bd9Sstevel@tonic-gate }
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate static void
fmd_buf_free(fmd_buf_t * bp)497c478bd9Sstevel@tonic-gate fmd_buf_free(fmd_buf_t *bp)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	fmd_strfree(bp->buf_name);
527c478bd9Sstevel@tonic-gate 	fmd_free(bp->buf_data, bp->buf_size);
537c478bd9Sstevel@tonic-gate 	fmd_free(bp, sizeof (fmd_buf_t));
547c478bd9Sstevel@tonic-gate }
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate void
fmd_buf_hash_create(fmd_buf_hash_t * bhp)577c478bd9Sstevel@tonic-gate fmd_buf_hash_create(fmd_buf_hash_t *bhp)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	bhp->bh_hashlen = fmd.d_str_buckets;
607c478bd9Sstevel@tonic-gate 	bhp->bh_hash = fmd_zalloc(sizeof (void *) * bhp->bh_hashlen, FMD_SLEEP);
617c478bd9Sstevel@tonic-gate 	bhp->bh_count = 0;
627c478bd9Sstevel@tonic-gate }
637c478bd9Sstevel@tonic-gate 
64*0b9e3e76Smws size_t
fmd_buf_hash_destroy(fmd_buf_hash_t * bhp)657c478bd9Sstevel@tonic-gate fmd_buf_hash_destroy(fmd_buf_hash_t *bhp)
667c478bd9Sstevel@tonic-gate {
67*0b9e3e76Smws 	size_t total = 0;
687c478bd9Sstevel@tonic-gate 	fmd_buf_t *bp, *np;
697c478bd9Sstevel@tonic-gate 	uint_t i;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	for (i = 0; i < bhp->bh_hashlen; i++) {
727c478bd9Sstevel@tonic-gate 		for (bp = bhp->bh_hash[i]; bp != NULL; bp = np) {
737c478bd9Sstevel@tonic-gate 			np = bp->buf_next;
74*0b9e3e76Smws 			total += bp->buf_size;
757c478bd9Sstevel@tonic-gate 			fmd_buf_free(bp);
767c478bd9Sstevel@tonic-gate 		}
777c478bd9Sstevel@tonic-gate 	}
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	fmd_free(bhp->bh_hash, sizeof (void *) * bhp->bh_hashlen);
807c478bd9Sstevel@tonic-gate 	bzero(bhp, sizeof (fmd_buf_hash_t));
81*0b9e3e76Smws 	return (total);
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate void
fmd_buf_hash_apply(fmd_buf_hash_t * bhp,fmd_buf_f * func,void * arg)857c478bd9Sstevel@tonic-gate fmd_buf_hash_apply(fmd_buf_hash_t *bhp, fmd_buf_f *func, void *arg)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate 	fmd_buf_t *bp;
887c478bd9Sstevel@tonic-gate 	uint_t i;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	for (i = 0; i < bhp->bh_hashlen; i++) {
917c478bd9Sstevel@tonic-gate 		for (bp = bhp->bh_hash[i]; bp != NULL; bp = bp->buf_next)
927c478bd9Sstevel@tonic-gate 			func(bp, arg);
937c478bd9Sstevel@tonic-gate 	}
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate void
fmd_buf_hash_commit(fmd_buf_hash_t * bhp)977c478bd9Sstevel@tonic-gate fmd_buf_hash_commit(fmd_buf_hash_t *bhp)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	fmd_buf_t *bp;
1007c478bd9Sstevel@tonic-gate 	uint_t i;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	for (i = 0; i < bhp->bh_hashlen; i++) {
1037c478bd9Sstevel@tonic-gate 		for (bp = bhp->bh_hash[i]; bp != NULL; bp = bp->buf_next)
1047c478bd9Sstevel@tonic-gate 			bp->buf_flags &= ~FMD_BUF_DIRTY;
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate uint_t
fmd_buf_hash_count(fmd_buf_hash_t * bhp)1097c478bd9Sstevel@tonic-gate fmd_buf_hash_count(fmd_buf_hash_t *bhp)
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate 	return (bhp->bh_count);
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate fmd_buf_t *
fmd_buf_insert(fmd_buf_hash_t * bhp,const char * name,size_t size)1157c478bd9Sstevel@tonic-gate fmd_buf_insert(fmd_buf_hash_t *bhp, const char *name, size_t size)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	uint_t h = fmd_strhash(name) % bhp->bh_hashlen;
1187c478bd9Sstevel@tonic-gate 	fmd_buf_t *bp = fmd_buf_alloc(name, size);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	bp->buf_next = bhp->bh_hash[h];
1217c478bd9Sstevel@tonic-gate 	bhp->bh_hash[h] = bp;
1227c478bd9Sstevel@tonic-gate 	bhp->bh_count++;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	return (bp);
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate fmd_buf_t *
fmd_buf_lookup(fmd_buf_hash_t * bhp,const char * name)1287c478bd9Sstevel@tonic-gate fmd_buf_lookup(fmd_buf_hash_t *bhp, const char *name)
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate 	uint_t h = fmd_strhash(name) % bhp->bh_hashlen;
1317c478bd9Sstevel@tonic-gate 	fmd_buf_t *bp;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	for (bp = bhp->bh_hash[h]; bp != NULL; bp = bp->buf_next) {
1347c478bd9Sstevel@tonic-gate 		if (strcmp(name, bp->buf_name) == 0)
1357c478bd9Sstevel@tonic-gate 			return (bp);
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	return (NULL);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate void
fmd_buf_delete(fmd_buf_hash_t * bhp,const char * name)1427c478bd9Sstevel@tonic-gate fmd_buf_delete(fmd_buf_hash_t *bhp, const char *name)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	uint_t h = fmd_strhash(name) % bhp->bh_hashlen;
1457c478bd9Sstevel@tonic-gate 	fmd_buf_t *bp, **pp = &bhp->bh_hash[h];
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	for (bp = *pp; bp != NULL; bp = bp->buf_next) {
1487c478bd9Sstevel@tonic-gate 		if (strcmp(bp->buf_name, name) != 0)
1497c478bd9Sstevel@tonic-gate 			pp = &bp->buf_next;
1507c478bd9Sstevel@tonic-gate 		else
1517c478bd9Sstevel@tonic-gate 			break;
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	if (bp != NULL) {
1557c478bd9Sstevel@tonic-gate 		*pp = bp->buf_next;
1567c478bd9Sstevel@tonic-gate 		fmd_buf_free(bp);
1577c478bd9Sstevel@tonic-gate 		ASSERT(bhp->bh_count != 0);
1587c478bd9Sstevel@tonic-gate 		bhp->bh_count--;
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate }
161