xref: /freebsd/sys/contrib/openzfs/module/nvpair/nvpair_alloc_fixed.c (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * https://opensource.org/license/CDDL-1.0.
11  */
12 
13 /*
14  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
15  * Use is subject to license terms.
16  */
17 
18 #include <sys/isa_defs.h>
19 #include <sys/nvpair.h>
20 #include <sys/sysmacros.h>
21 
22 /*
23  * This allocator is very simple.
24  *  - it uses a pre-allocated buffer for memory allocations.
25  *  - it does _not_ free memory in the pre-allocated buffer.
26  *
27  * The reason for the selected implementation is simplicity.
28  * This allocator is designed for the usage in interrupt context when
29  * the caller may not wait for free memory.
30  */
31 
32 /* pre-allocated buffer for memory allocations */
33 typedef struct nvbuf {
34 	uintptr_t	nvb_buf;	/* address of pre-allocated buffer */
35 	uintptr_t 	nvb_lim;	/* limit address in the buffer */
36 	uintptr_t	nvb_cur;	/* current address in the buffer */
37 } nvbuf_t;
38 
39 /*
40  * Initialize the pre-allocated buffer allocator. The caller needs to supply
41  *
42  *   buf	address of pre-allocated buffer
43  *   bufsz	size of pre-allocated buffer
44  *
45  * nv_fixed_init() calculates the remaining members of nvbuf_t.
46  */
47 static int
nv_fixed_init(nv_alloc_t * nva,va_list valist)48 nv_fixed_init(nv_alloc_t *nva, va_list valist)
49 {
50 	uintptr_t base = va_arg(valist, uintptr_t);
51 	uintptr_t lim = base + va_arg(valist, size_t);
52 	nvbuf_t *nvb = (nvbuf_t *)P2ROUNDUP(base, sizeof (uintptr_t));
53 
54 	if (base == 0 || (uintptr_t)&nvb[1] > lim)
55 		return (EINVAL);
56 
57 	nvb->nvb_buf = (uintptr_t)&nvb[0];
58 	nvb->nvb_cur = (uintptr_t)&nvb[1];
59 	nvb->nvb_lim = lim;
60 	nva->nva_arg = nvb;
61 
62 	return (0);
63 }
64 
65 static void *
nv_fixed_alloc(nv_alloc_t * nva,size_t size)66 nv_fixed_alloc(nv_alloc_t *nva, size_t size)
67 {
68 	nvbuf_t *nvb = nva->nva_arg;
69 	uintptr_t new = nvb->nvb_cur;
70 
71 	if (size == 0 || new + size > nvb->nvb_lim)
72 		return (NULL);
73 
74 	nvb->nvb_cur = P2ROUNDUP(new + size, sizeof (uintptr_t));
75 
76 	return ((void *)new);
77 }
78 
79 static void
nv_fixed_free(nv_alloc_t * nva,void * buf,size_t size)80 nv_fixed_free(nv_alloc_t *nva, void *buf, size_t size)
81 {
82 	/* don't free memory in the pre-allocated buffer */
83 	(void) nva, (void) buf, (void) size;
84 }
85 
86 static void
nv_fixed_reset(nv_alloc_t * nva)87 nv_fixed_reset(nv_alloc_t *nva)
88 {
89 	nvbuf_t *nvb = nva->nva_arg;
90 
91 	nvb->nvb_cur = (uintptr_t)&nvb[1];
92 }
93 
94 static const nv_alloc_ops_t nv_fixed_ops_def = {
95 	.nv_ao_init = nv_fixed_init,
96 	.nv_ao_fini = NULL,
97 	.nv_ao_alloc = nv_fixed_alloc,
98 	.nv_ao_free = nv_fixed_free,
99 	.nv_ao_reset = nv_fixed_reset
100 };
101 
102 const nv_alloc_ops_t *const nv_fixed_ops = &nv_fixed_ops_def;
103 
104 #if defined(_KERNEL)
105 EXPORT_SYMBOL(nv_fixed_ops);
106 #endif
107