xref: /illumos-gate/usr/src/common/nvpair/nvpair_alloc_fixed.c (revision dd72704bd9e794056c558153663c739e2012d721)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/stropts.h>
28 #include <sys/isa_defs.h>
29 #include <sys/nvpair.h>
30 #include <sys/sysmacros.h>
31 #if defined(_KERNEL) && !defined(_BOOT)
32 #include <sys/varargs.h>
33 #else
34 #include <stdarg.h>
35 #include <strings.h>
36 #endif
37 
38 /*
39  * This allocator is very simple.
40  *  - it uses a pre-allocated buffer for memory allocations.
41  *  - it does _not_ free memory in the pre-allocated buffer.
42  *
43  * The reason for the selected implemention is simplicity.
44  * This allocator is designed for the usage in interrupt context when
45  * the caller may not wait for free memory.
46  */
47 
48 /* pre-allocated buffer for memory allocations */
49 typedef struct nvbuf {
50 	uintptr_t	nvb_buf;	/* address of pre-allocated buffer */
51 	uintptr_t 	nvb_lim;	/* limit address in the buffer */
52 	uintptr_t	nvb_cur;	/* current address in the buffer */
53 } nvbuf_t;
54 
55 /*
56  * Initialize the pre-allocated buffer allocator. The caller needs to supply
57  *
58  *   buf	address of pre-allocated buffer
59  *   bufsz	size of pre-allocated buffer
60  *
61  * nv_fixed_init() calculates the remaining members of nvbuf_t.
62  */
63 static int
64 nv_fixed_init(nv_alloc_t *nva, va_list valist)
65 {
66 	uintptr_t base = va_arg(valist, uintptr_t);
67 	uintptr_t lim = base + va_arg(valist, size_t);
68 	nvbuf_t *nvb = (nvbuf_t *)P2ROUNDUP(base, sizeof (uintptr_t));
69 
70 	if (base == 0 || (uintptr_t)&nvb[1] > lim)
71 		return (EINVAL);
72 
73 	nvb->nvb_buf = (uintptr_t)&nvb[0];
74 	nvb->nvb_cur = (uintptr_t)&nvb[1];
75 	nvb->nvb_lim = lim;
76 	nva->nva_arg = nvb;
77 
78 	return (0);
79 }
80 
81 static void *
82 nv_fixed_alloc(nv_alloc_t *nva, size_t size)
83 {
84 	nvbuf_t *nvb = nva->nva_arg;
85 	uintptr_t new = nvb->nvb_cur;
86 
87 	if (size == 0 || new + size > nvb->nvb_lim)
88 		return (NULL);
89 
90 	nvb->nvb_cur = P2ROUNDUP(new + size, sizeof (uintptr_t));
91 
92 	return ((void *)new);
93 }
94 
95 /*ARGSUSED*/
96 static void
97 nv_fixed_free(nv_alloc_t *nva, void *buf, size_t size)
98 {
99 	/* don't free memory in the pre-allocated buffer */
100 }
101 
102 static void
103 nv_fixed_reset(nv_alloc_t *nva)
104 {
105 	nvbuf_t *nvb = nva->nva_arg;
106 
107 	nvb->nvb_cur = (uintptr_t)&nvb[1];
108 }
109 
110 const nv_alloc_ops_t nv_fixed_ops_def = {
111 	nv_fixed_init,	/* nv_ao_init() */
112 	NULL,		/* nv_ao_fini() */
113 	nv_fixed_alloc,	/* nv_ao_alloc() */
114 	nv_fixed_free,	/* nv_ao_free() */
115 	nv_fixed_reset	/* nv_ao_reset() */
116 };
117 
118 const nv_alloc_ops_t *nv_fixed_ops = &nv_fixed_ops_def;
119