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