Path: blob/main/sys/contrib/openzfs/lib/libspl/include/umem.h
48406 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, Version 1.0 only6* (the "License"). You may not use this file except in compliance7* with the License.8*9* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE10* or https://opensource.org/licenses/CDDL-1.0.11* See the License for the specific language governing permissions12* and limitations under the License.13*14* When distributing Covered Code, include this CDDL HEADER in each15* file and include the License file at usr/src/OPENSOLARIS.LICENSE.16* If applicable, add the following below this CDDL HEADER, with the17* fields enclosed by brackets "[]" replaced with your own identifying18* information: Portions Copyright [yyyy] [name of copyright owner]19*20* CDDL HEADER END21*/22/*23* Copyright 2008 Sun Microsystems, Inc. All rights reserved.24* Use is subject to license terms.25*/2627#ifndef _LIBSPL_UMEM_H28#define _LIBSPL_UMEM_H2930/*31* XXX: We should use the real portable umem library if it is detected32* at configure time. However, if the library is not available, we can33* use a trivial malloc based implementation. This obviously impacts34* performance, but unless you are using a full userspace build of zpool for35* something other than ztest, you are likely not going to notice or care.36*37* https://labs.omniti.com/trac/portableumem38*/39#include <sys/debug.h>4041#include <stdlib.h>42#include <stdio.h>43#include <string.h>4445#ifdef __cplusplus46extern "C" {47#endif4849typedef void vmem_t;5051/*52* Flags for umem_alloc/umem_free53*/54#define UMEM_DEFAULT 0x0000 /* normal -- may fail */55#define UMEM_NOFAIL 0x0100 /* Never fails */5657/*58* Flags for umem_cache_create()59*/60#define UMC_NODEBUG 0x000200006162#define UMEM_CACHE_NAMELEN 316364typedef int umem_nofail_callback_t(void);65typedef int umem_constructor_t(void *, void *, int);66typedef void umem_destructor_t(void *, void *);67typedef void umem_reclaim_t(void *);6869typedef struct umem_cache {70char cache_name[UMEM_CACHE_NAMELEN + 1];71size_t cache_bufsize;72size_t cache_align;73umem_constructor_t *cache_constructor;74umem_destructor_t *cache_destructor;75umem_reclaim_t *cache_reclaim;76void *cache_private;77void *cache_arena;78int cache_cflags;79} umem_cache_t;8081/* Prototypes for functions to provide defaults for umem envvars */82const char *_umem_debug_init(void);83const char *_umem_options_init(void);84const char *_umem_logging_init(void);8586__attribute__((malloc, alloc_size(1)))87static inline void *88umem_alloc(size_t size, int flags)89{90void *ptr = NULL;9192do {93ptr = malloc(size);94} while (ptr == NULL && (flags & UMEM_NOFAIL));9596return (ptr);97}9899__attribute__((malloc, alloc_size(1)))100static inline void *101umem_alloc_aligned(size_t size, size_t align, int flags)102{103void *ptr = NULL;104int rc;105106do {107rc = posix_memalign(&ptr, align, size);108} while (rc == ENOMEM && (flags & UMEM_NOFAIL));109110if (rc == EINVAL) {111fprintf(stderr, "%s: invalid memory alignment (%zd)\n",112__func__, align);113if (flags & UMEM_NOFAIL)114abort();115return (NULL);116}117118return (ptr);119}120121__attribute__((malloc, alloc_size(1)))122static inline void *123umem_zalloc(size_t size, int flags)124{125void *ptr = NULL;126127ptr = umem_alloc(size, flags);128if (ptr)129memset(ptr, 0, size);130131return (ptr);132}133134static inline void135umem_free(const void *ptr, size_t size __maybe_unused)136{137free((void *)ptr);138}139140/*141* umem_free_aligned was added for supporting portability142* with non-POSIX platforms that require a different free143* to be used with aligned allocations.144*/145static inline void146umem_free_aligned(void *ptr, size_t size __maybe_unused)147{148#ifndef _WIN32149free((void *)ptr);150#else151_aligned_free(ptr);152#endif153}154155static inline void156umem_nofail_callback(umem_nofail_callback_t *cb __maybe_unused)157{}158159static inline umem_cache_t *160umem_cache_create(161const char *name, size_t bufsize, size_t align,162umem_constructor_t *constructor,163umem_destructor_t *destructor,164umem_reclaim_t *reclaim,165void *priv, void *vmp, int cflags)166{167umem_cache_t *cp;168169cp = (umem_cache_t *)umem_alloc(sizeof (umem_cache_t), UMEM_DEFAULT);170if (cp) {171strlcpy(cp->cache_name, name, UMEM_CACHE_NAMELEN);172cp->cache_bufsize = bufsize;173cp->cache_align = align;174cp->cache_constructor = constructor;175cp->cache_destructor = destructor;176cp->cache_reclaim = reclaim;177cp->cache_private = priv;178cp->cache_arena = vmp;179cp->cache_cflags = cflags;180}181182return (cp);183}184185static inline void186umem_cache_destroy(umem_cache_t *cp)187{188umem_free(cp, sizeof (umem_cache_t));189}190191__attribute__((malloc))192static inline void *193umem_cache_alloc(umem_cache_t *cp, int flags)194{195void *ptr = NULL;196197if (cp->cache_align != 0)198ptr = umem_alloc_aligned(199cp->cache_bufsize, cp->cache_align, flags);200else201ptr = umem_alloc(cp->cache_bufsize, flags);202203if (ptr && cp->cache_constructor)204cp->cache_constructor(ptr, cp->cache_private, UMEM_DEFAULT);205206return (ptr);207}208209static inline void210umem_cache_free(umem_cache_t *cp, void *ptr)211{212if (cp->cache_destructor)213cp->cache_destructor(ptr, cp->cache_private);214215if (cp->cache_align != 0)216umem_free_aligned(ptr, cp->cache_bufsize);217else218umem_free(ptr, cp->cache_bufsize);219}220221static inline void222umem_cache_reap_now(umem_cache_t *cp __maybe_unused)223{224}225226#ifdef __cplusplus227}228#endif229230#endif231232233