Path: blob/main/sys/contrib/openzfs/lib/libspl/include/umem.h
105338 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>44#include <errno.h>4546#ifdef __cplusplus47extern "C" {48#endif4950typedef void vmem_t;5152/*53* Flags for umem_alloc/umem_free54*/55#define UMEM_DEFAULT 0x0000 /* normal -- may fail */56#define UMEM_NOFAIL 0x0100 /* Never fails */5758/*59* Flags for umem_cache_create()60*/61#define UMC_NODEBUG 0x000200006263#define UMEM_CACHE_NAMELEN 316465typedef int umem_nofail_callback_t(void);66typedef int umem_constructor_t(void *, void *, int);67typedef void umem_destructor_t(void *, void *);68typedef void umem_reclaim_t(void *);6970typedef struct umem_cache {71char cache_name[UMEM_CACHE_NAMELEN + 1];72size_t cache_bufsize;73size_t cache_align;74umem_constructor_t *cache_constructor;75umem_destructor_t *cache_destructor;76umem_reclaim_t *cache_reclaim;77void *cache_private;78void *cache_arena;79int cache_cflags;80} umem_cache_t;8182/* Prototypes for functions to provide defaults for umem envvars */83const char *_umem_debug_init(void);84const char *_umem_options_init(void);85const char *_umem_logging_init(void);8687__attribute__((malloc, alloc_size(1)))88static inline void *89umem_alloc(size_t size, int flags)90{91void *ptr = NULL;9293do {94ptr = malloc(size);95} while (ptr == NULL && (flags & UMEM_NOFAIL));9697return (ptr);98}99100__attribute__((malloc, alloc_size(1)))101static inline void *102umem_alloc_aligned(size_t size, size_t align, int flags)103{104void *ptr = NULL;105int rc;106107do {108rc = posix_memalign(&ptr, align, size);109} while (rc == ENOMEM && (flags & UMEM_NOFAIL));110111if (rc == EINVAL) {112fprintf(stderr, "%s: invalid memory alignment (%zd)\n",113__func__, align);114if (flags & UMEM_NOFAIL)115abort();116return (NULL);117}118119return (ptr);120}121122__attribute__((malloc, alloc_size(1)))123static inline void *124umem_zalloc(size_t size, int flags)125{126void *ptr = NULL;127128ptr = umem_alloc(size, flags);129if (ptr)130memset(ptr, 0, size);131132return (ptr);133}134135static inline void136umem_free(const void *ptr, size_t size __maybe_unused)137{138free((void *)ptr);139}140141/*142* umem_free_aligned was added for supporting portability143* with non-POSIX platforms that require a different free144* to be used with aligned allocations.145*/146static inline void147umem_free_aligned(void *ptr, size_t size __maybe_unused)148{149#ifndef _WIN32150free((void *)ptr);151#else152_aligned_free(ptr);153#endif154}155156static inline void157umem_nofail_callback(umem_nofail_callback_t *cb __maybe_unused)158{}159160static inline umem_cache_t *161umem_cache_create(162const char *name, size_t bufsize, size_t align,163umem_constructor_t *constructor,164umem_destructor_t *destructor,165umem_reclaim_t *reclaim,166void *priv, void *vmp, int cflags)167{168umem_cache_t *cp;169170cp = (umem_cache_t *)umem_alloc(sizeof (umem_cache_t), UMEM_DEFAULT);171if (cp) {172strlcpy(cp->cache_name, name, UMEM_CACHE_NAMELEN);173cp->cache_bufsize = bufsize;174cp->cache_align = align;175cp->cache_constructor = constructor;176cp->cache_destructor = destructor;177cp->cache_reclaim = reclaim;178cp->cache_private = priv;179cp->cache_arena = vmp;180cp->cache_cflags = cflags;181}182183return (cp);184}185186static inline void187umem_cache_destroy(umem_cache_t *cp)188{189umem_free(cp, sizeof (umem_cache_t));190}191192__attribute__((malloc))193static inline void *194umem_cache_alloc(umem_cache_t *cp, int flags)195{196void *ptr = NULL;197198if (cp->cache_align != 0)199ptr = umem_alloc_aligned(200cp->cache_bufsize, cp->cache_align, flags);201else202ptr = umem_alloc(cp->cache_bufsize, flags);203204if (ptr && cp->cache_constructor)205cp->cache_constructor(ptr, cp->cache_private, UMEM_DEFAULT);206207return (ptr);208}209210static inline void211umem_cache_free(umem_cache_t *cp, void *ptr)212{213if (cp->cache_destructor)214cp->cache_destructor(ptr, cp->cache_private);215216if (cp->cache_align != 0)217umem_free_aligned(ptr, cp->cache_bufsize);218else219umem_free(ptr, cp->cache_bufsize);220}221222static inline void223umem_cache_reap_now(umem_cache_t *cp __maybe_unused)224{225}226227#ifdef __cplusplus228}229#endif230231#endif232233234