xref: /titanic_41/usr/src/common/nvpair/nvpair_alloc_fixed.c (revision 5ad820458efd0fdb914baff9c1447c22b819fa23)
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
5*5ad82045Snd150628  * Common Development and Distribution License (the "License").
6*5ad82045Snd150628  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*5ad82045Snd150628 
227c478bd9Sstevel@tonic-gate /*
23*5ad82045Snd150628  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
307c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
317c478bd9Sstevel@tonic-gate #include <sys/nvpair.h>
327c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
337c478bd9Sstevel@tonic-gate #if defined(_KERNEL) && !defined(_BOOT)
347c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
357c478bd9Sstevel@tonic-gate #else
367c478bd9Sstevel@tonic-gate #include <stdarg.h>
377c478bd9Sstevel@tonic-gate #include <strings.h>
387c478bd9Sstevel@tonic-gate #endif
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * This allocator is very simple.
427c478bd9Sstevel@tonic-gate  *  - it uses a pre-allocated buffer for memory allocations.
437c478bd9Sstevel@tonic-gate  *  - it does _not_ free memory in the pre-allocated buffer.
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  * The reason for the selected implemention is simplicity.
467c478bd9Sstevel@tonic-gate  * This allocator is designed for the usage in interrupt context when
477c478bd9Sstevel@tonic-gate  * the caller may not wait for free memory.
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /* pre-allocated buffer for memory allocations */
517c478bd9Sstevel@tonic-gate typedef struct nvbuf {
527c478bd9Sstevel@tonic-gate 	uintptr_t	nvb_buf;	/* address of pre-allocated buffer */
537c478bd9Sstevel@tonic-gate 	uintptr_t 	nvb_lim;	/* limit address in the buffer */
547c478bd9Sstevel@tonic-gate 	uintptr_t	nvb_cur;	/* current address in the buffer */
557c478bd9Sstevel@tonic-gate } nvbuf_t;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate  * Initialize the pre-allocated buffer allocator. The caller needs to supply
597c478bd9Sstevel@tonic-gate  *
607c478bd9Sstevel@tonic-gate  *   buf	address of pre-allocated buffer
617c478bd9Sstevel@tonic-gate  *   bufsz	size of pre-allocated buffer
627c478bd9Sstevel@tonic-gate  *
637c478bd9Sstevel@tonic-gate  * nv_fixed_init() calculates the remaining members of nvbuf_t.
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate static int
nv_fixed_init(nv_alloc_t * nva,va_list valist)667c478bd9Sstevel@tonic-gate nv_fixed_init(nv_alloc_t *nva, va_list valist)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	uintptr_t base = va_arg(valist, uintptr_t);
697c478bd9Sstevel@tonic-gate 	uintptr_t lim = base + va_arg(valist, size_t);
707c478bd9Sstevel@tonic-gate 	nvbuf_t *nvb = (nvbuf_t *)P2ROUNDUP(base, sizeof (uintptr_t));
717c478bd9Sstevel@tonic-gate 
72*5ad82045Snd150628 	if (base == 0 || (uintptr_t)&nvb[1] > lim)
737c478bd9Sstevel@tonic-gate 		return (EINVAL);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	nvb->nvb_buf = (uintptr_t)&nvb[0];
767c478bd9Sstevel@tonic-gate 	nvb->nvb_cur = (uintptr_t)&nvb[1];
777c478bd9Sstevel@tonic-gate 	nvb->nvb_lim = lim;
787c478bd9Sstevel@tonic-gate 	nva->nva_arg = nvb;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	return (0);
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate static void *
nv_fixed_alloc(nv_alloc_t * nva,size_t size)847c478bd9Sstevel@tonic-gate nv_fixed_alloc(nv_alloc_t *nva, size_t size)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	nvbuf_t *nvb = nva->nva_arg;
877c478bd9Sstevel@tonic-gate 	uintptr_t new = nvb->nvb_cur;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	if (size == 0 || new + size > nvb->nvb_lim)
907c478bd9Sstevel@tonic-gate 		return (NULL);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	nvb->nvb_cur = P2ROUNDUP(new + size, sizeof (uintptr_t));
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	return ((void *)new);
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate /*ARGSUSED*/
987c478bd9Sstevel@tonic-gate static void
nv_fixed_free(nv_alloc_t * nva,void * buf,size_t size)997c478bd9Sstevel@tonic-gate nv_fixed_free(nv_alloc_t *nva, void *buf, size_t size)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	/* don't free memory in the pre-allocated buffer */
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static void
nv_fixed_reset(nv_alloc_t * nva)1057c478bd9Sstevel@tonic-gate nv_fixed_reset(nv_alloc_t *nva)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate 	nvbuf_t *nvb = nva->nva_arg;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	nvb->nvb_cur = (uintptr_t)&nvb[1];
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate const nv_alloc_ops_t nv_fixed_ops_def = {
1137c478bd9Sstevel@tonic-gate 	nv_fixed_init,	/* nv_ao_init() */
1147c478bd9Sstevel@tonic-gate 	NULL,		/* nv_ao_fini() */
1157c478bd9Sstevel@tonic-gate 	nv_fixed_alloc,	/* nv_ao_alloc() */
1167c478bd9Sstevel@tonic-gate 	nv_fixed_free,	/* nv_ao_free() */
1177c478bd9Sstevel@tonic-gate 	nv_fixed_reset	/* nv_ao_reset() */
1187c478bd9Sstevel@tonic-gate };
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate const nv_alloc_ops_t *nv_fixed_ops = &nv_fixed_ops_def;
121