xref: /onnv-gate/usr/src/common/nvpair/nvpair_alloc_fixed.c (revision 2856:6f4d5ee1906a)
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
5*2856Snd150628  * Common Development and Distribution License (the "License").
6*2856Snd150628  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*2856Snd150628 
220Sstevel@tonic-gate /*
23*2856Snd150628  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/stropts.h>
300Sstevel@tonic-gate #include <sys/isa_defs.h>
310Sstevel@tonic-gate #include <sys/nvpair.h>
320Sstevel@tonic-gate #include <sys/sysmacros.h>
330Sstevel@tonic-gate #if defined(_KERNEL) && !defined(_BOOT)
340Sstevel@tonic-gate #include <sys/varargs.h>
350Sstevel@tonic-gate #else
360Sstevel@tonic-gate #include <stdarg.h>
370Sstevel@tonic-gate #include <strings.h>
380Sstevel@tonic-gate #endif
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate  * This allocator is very simple.
420Sstevel@tonic-gate  *  - it uses a pre-allocated buffer for memory allocations.
430Sstevel@tonic-gate  *  - it does _not_ free memory in the pre-allocated buffer.
440Sstevel@tonic-gate  *
450Sstevel@tonic-gate  * The reason for the selected implemention is simplicity.
460Sstevel@tonic-gate  * This allocator is designed for the usage in interrupt context when
470Sstevel@tonic-gate  * the caller may not wait for free memory.
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /* pre-allocated buffer for memory allocations */
510Sstevel@tonic-gate typedef struct nvbuf {
520Sstevel@tonic-gate 	uintptr_t	nvb_buf;	/* address of pre-allocated buffer */
530Sstevel@tonic-gate 	uintptr_t 	nvb_lim;	/* limit address in the buffer */
540Sstevel@tonic-gate 	uintptr_t	nvb_cur;	/* current address in the buffer */
550Sstevel@tonic-gate } nvbuf_t;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate  * Initialize the pre-allocated buffer allocator. The caller needs to supply
590Sstevel@tonic-gate  *
600Sstevel@tonic-gate  *   buf	address of pre-allocated buffer
610Sstevel@tonic-gate  *   bufsz	size of pre-allocated buffer
620Sstevel@tonic-gate  *
630Sstevel@tonic-gate  * nv_fixed_init() calculates the remaining members of nvbuf_t.
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate static int
nv_fixed_init(nv_alloc_t * nva,va_list valist)660Sstevel@tonic-gate nv_fixed_init(nv_alloc_t *nva, va_list valist)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate 	uintptr_t base = va_arg(valist, uintptr_t);
690Sstevel@tonic-gate 	uintptr_t lim = base + va_arg(valist, size_t);
700Sstevel@tonic-gate 	nvbuf_t *nvb = (nvbuf_t *)P2ROUNDUP(base, sizeof (uintptr_t));
710Sstevel@tonic-gate 
72*2856Snd150628 	if (base == 0 || (uintptr_t)&nvb[1] > lim)
730Sstevel@tonic-gate 		return (EINVAL);
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	nvb->nvb_buf = (uintptr_t)&nvb[0];
760Sstevel@tonic-gate 	nvb->nvb_cur = (uintptr_t)&nvb[1];
770Sstevel@tonic-gate 	nvb->nvb_lim = lim;
780Sstevel@tonic-gate 	nva->nva_arg = nvb;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	return (0);
810Sstevel@tonic-gate }
820Sstevel@tonic-gate 
830Sstevel@tonic-gate static void *
nv_fixed_alloc(nv_alloc_t * nva,size_t size)840Sstevel@tonic-gate nv_fixed_alloc(nv_alloc_t *nva, size_t size)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate 	nvbuf_t *nvb = nva->nva_arg;
870Sstevel@tonic-gate 	uintptr_t new = nvb->nvb_cur;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	if (size == 0 || new + size > nvb->nvb_lim)
900Sstevel@tonic-gate 		return (NULL);
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	nvb->nvb_cur = P2ROUNDUP(new + size, sizeof (uintptr_t));
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	return ((void *)new);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate 
970Sstevel@tonic-gate /*ARGSUSED*/
980Sstevel@tonic-gate static void
nv_fixed_free(nv_alloc_t * nva,void * buf,size_t size)990Sstevel@tonic-gate nv_fixed_free(nv_alloc_t *nva, void *buf, size_t size)
1000Sstevel@tonic-gate {
1010Sstevel@tonic-gate 	/* don't free memory in the pre-allocated buffer */
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate static void
nv_fixed_reset(nv_alloc_t * nva)1050Sstevel@tonic-gate nv_fixed_reset(nv_alloc_t *nva)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate 	nvbuf_t *nvb = nva->nva_arg;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	nvb->nvb_cur = (uintptr_t)&nvb[1];
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate const nv_alloc_ops_t nv_fixed_ops_def = {
1130Sstevel@tonic-gate 	nv_fixed_init,	/* nv_ao_init() */
1140Sstevel@tonic-gate 	NULL,		/* nv_ao_fini() */
1150Sstevel@tonic-gate 	nv_fixed_alloc,	/* nv_ao_alloc() */
1160Sstevel@tonic-gate 	nv_fixed_free,	/* nv_ao_free() */
1170Sstevel@tonic-gate 	nv_fixed_reset	/* nv_ao_reset() */
1180Sstevel@tonic-gate };
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate const nv_alloc_ops_t *nv_fixed_ops = &nv_fixed_ops_def;
121