Path: blob/main/sys/contrib/openzfs/module/nvpair/nvpair_alloc_fixed.c
48383 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* 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.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/2122/*23* Copyright 2006 Sun Microsystems, Inc. All rights reserved.24* Use is subject to license terms.25*/2627#include <sys/isa_defs.h>28#include <sys/nvpair.h>29#include <sys/sysmacros.h>3031/*32* This allocator is very simple.33* - it uses a pre-allocated buffer for memory allocations.34* - it does _not_ free memory in the pre-allocated buffer.35*36* The reason for the selected implementation is simplicity.37* This allocator is designed for the usage in interrupt context when38* the caller may not wait for free memory.39*/4041/* pre-allocated buffer for memory allocations */42typedef struct nvbuf {43uintptr_t nvb_buf; /* address of pre-allocated buffer */44uintptr_t nvb_lim; /* limit address in the buffer */45uintptr_t nvb_cur; /* current address in the buffer */46} nvbuf_t;4748/*49* Initialize the pre-allocated buffer allocator. The caller needs to supply50*51* buf address of pre-allocated buffer52* bufsz size of pre-allocated buffer53*54* nv_fixed_init() calculates the remaining members of nvbuf_t.55*/56static int57nv_fixed_init(nv_alloc_t *nva, va_list valist)58{59uintptr_t base = va_arg(valist, uintptr_t);60uintptr_t lim = base + va_arg(valist, size_t);61nvbuf_t *nvb = (nvbuf_t *)P2ROUNDUP(base, sizeof (uintptr_t));6263if (base == 0 || (uintptr_t)&nvb[1] > lim)64return (EINVAL);6566nvb->nvb_buf = (uintptr_t)&nvb[0];67nvb->nvb_cur = (uintptr_t)&nvb[1];68nvb->nvb_lim = lim;69nva->nva_arg = nvb;7071return (0);72}7374static void *75nv_fixed_alloc(nv_alloc_t *nva, size_t size)76{77nvbuf_t *nvb = nva->nva_arg;78uintptr_t new = nvb->nvb_cur;7980if (size == 0 || new + size > nvb->nvb_lim)81return (NULL);8283nvb->nvb_cur = P2ROUNDUP(new + size, sizeof (uintptr_t));8485return ((void *)new);86}8788static void89nv_fixed_free(nv_alloc_t *nva, void *buf, size_t size)90{91/* don't free memory in the pre-allocated buffer */92(void) nva, (void) buf, (void) size;93}9495static void96nv_fixed_reset(nv_alloc_t *nva)97{98nvbuf_t *nvb = nva->nva_arg;99100nvb->nvb_cur = (uintptr_t)&nvb[1];101}102103static const nv_alloc_ops_t nv_fixed_ops_def = {104.nv_ao_init = nv_fixed_init,105.nv_ao_fini = NULL,106.nv_ao_alloc = nv_fixed_alloc,107.nv_ao_free = nv_fixed_free,108.nv_ao_reset = nv_fixed_reset109};110111const nv_alloc_ops_t *const nv_fixed_ops = &nv_fixed_ops_def;112113#if defined(_KERNEL)114EXPORT_SYMBOL(nv_fixed_ops);115#endif116117118