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