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 #include <sys/mod.h>
22
23 /*
24 * This allocator is very simple.
25 * - it uses a pre-allocated buffer for memory allocations.
26 * - it does _not_ free memory in the pre-allocated buffer.
27 *
28 * The reason for the selected implementation is simplicity.
29 * This allocator is designed for the usage in interrupt context when
30 * the caller may not wait for free memory.
31 */
32
33 /* pre-allocated buffer for memory allocations */
34 typedef struct nvbuf {
35 uintptr_t nvb_buf; /* address of pre-allocated buffer */
36 uintptr_t nvb_lim; /* limit address in the buffer */
37 uintptr_t nvb_cur; /* current address in the buffer */
38 } nvbuf_t;
39
40 /*
41 * Initialize the pre-allocated buffer allocator. The caller needs to supply
42 *
43 * buf address of pre-allocated buffer
44 * bufsz size of pre-allocated buffer
45 *
46 * nv_fixed_init() calculates the remaining members of nvbuf_t.
47 */
48 static int
nv_fixed_init(nv_alloc_t * nva,va_list valist)49 nv_fixed_init(nv_alloc_t *nva, va_list valist)
50 {
51 uintptr_t base = va_arg(valist, uintptr_t);
52 uintptr_t lim = base + va_arg(valist, size_t);
53 nvbuf_t *nvb = (nvbuf_t *)P2ROUNDUP(base, sizeof (uintptr_t));
54
55 if (base == 0 || (uintptr_t)&nvb[1] > lim)
56 return (EINVAL);
57
58 nvb->nvb_buf = (uintptr_t)&nvb[0];
59 nvb->nvb_cur = (uintptr_t)&nvb[1];
60 nvb->nvb_lim = lim;
61 nva->nva_arg = nvb;
62
63 return (0);
64 }
65
66 static void *
nv_fixed_alloc(nv_alloc_t * nva,size_t size)67 nv_fixed_alloc(nv_alloc_t *nva, size_t size)
68 {
69 nvbuf_t *nvb = nva->nva_arg;
70 uintptr_t new = nvb->nvb_cur;
71
72 if (size == 0 || new + size > nvb->nvb_lim)
73 return (NULL);
74
75 nvb->nvb_cur = P2ROUNDUP(new + size, sizeof (uintptr_t));
76
77 return ((void *)new);
78 }
79
80 static void
nv_fixed_free(nv_alloc_t * nva,void * buf,size_t size)81 nv_fixed_free(nv_alloc_t *nva, void *buf, size_t size)
82 {
83 /* don't free memory in the pre-allocated buffer */
84 (void) nva, (void) buf, (void) size;
85 }
86
87 static void
nv_fixed_reset(nv_alloc_t * nva)88 nv_fixed_reset(nv_alloc_t *nva)
89 {
90 nvbuf_t *nvb = nva->nva_arg;
91
92 nvb->nvb_cur = (uintptr_t)&nvb[1];
93 }
94
95 static const nv_alloc_ops_t nv_fixed_ops_def = {
96 .nv_ao_init = nv_fixed_init,
97 .nv_ao_fini = NULL,
98 .nv_ao_alloc = nv_fixed_alloc,
99 .nv_ao_free = nv_fixed_free,
100 .nv_ao_reset = nv_fixed_reset
101 };
102
103 const nv_alloc_ops_t *const nv_fixed_ops = &nv_fixed_ops_def;
104
105 EXPORT_SYMBOL(nv_fixed_ops);
106