1b819ceaGordon Ross/*
2b819ceaGordon Ross * This file and its contents are supplied under the terms of the
3b819ceaGordon Ross * Common Development and Distribution License ("CDDL"), version 1.0.
4b819ceaGordon Ross * You may only use this file in accordance with the terms of version
5b819ceaGordon Ross * 1.0 of the CDDL.
6b819ceaGordon Ross *
7b819ceaGordon Ross * A full copy of the text of the CDDL should have accompanied this
8b819ceaGordon Ross * source.  A copy of the CDDL is also available via the Internet at
9b819ceaGordon Ross * http://www.illumos.org/license/CDDL.
10b819ceaGordon Ross */
11b819ceaGordon Ross
12b819ceaGordon Ross/*
13b819ceaGordon Ross * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
1436a64e6Tim Kordas * Copyright 2018, Joyent, Inc.
15b819ceaGordon Ross */
16b819ceaGordon Ross
17b819ceaGordon Ross#include <sys/kmem.h>
18b819ceaGordon Ross
19b819ceaGordon Ross#include <sys/debug.h>
20b819ceaGordon Ross#include <sys/ksynch.h>
21b819ceaGordon Ross#include <sys/systm.h>
22b819ceaGordon Ross
23b819ceaGordon Ross#include <umem.h>
24b819ceaGordon Ross
25b819ceaGordon Rossvoid	abort(void) __NORETURN;
26b819ceaGordon Ross
27b819ceaGordon Rossstatic int
28b819ceaGordon Rosskmem_failed_cb(void)
29b819ceaGordon Ross{
30b819ceaGordon Ross	abort();
31b819ceaGordon Ross	return (UMEM_CALLBACK_RETRY);
32b819ceaGordon Ross}
33b819ceaGordon Ross
34b819ceaGordon Ross#pragma init(_kmem_init)
35b819ceaGordon Rossstatic int
36b819ceaGordon Ross_kmem_init(void)
37b819ceaGordon Ross{
38b819ceaGordon Ross	umem_nofail_callback(kmem_failed_cb);
39b819ceaGordon Ross	return (0);
40b819ceaGordon Ross}
41b819ceaGordon Ross
42b819ceaGordon Rossstatic int
43b819ceaGordon Rosskmem2umem_flags(int kmflags)
44b819ceaGordon Ross{
45b819ceaGordon Ross	int umflags = UMEM_NOFAIL;
46b819ceaGordon Ross	if (kmflags & KM_NOSLEEP)
47b819ceaGordon Ross		umflags = UMEM_DEFAULT;
48b819ceaGordon Ross	return (umflags);
49b819ceaGordon Ross}
50b819ceaGordon Ross
51b819ceaGordon Rossint
52b819ceaGordon Rosskmem_debugging(void)
53b819ceaGordon Ross{
54b819ceaGordon Ross	return (0);
55b819ceaGordon Ross}
56b819ceaGordon Ross
57b819ceaGordon Rossvoid *
58b819ceaGordon Rosskmem_alloc(size_t size, int kmflags)
59b819ceaGordon Ross{
60b819ceaGordon Ross	return (umem_alloc(size, kmem2umem_flags(kmflags)));
61b819ceaGordon Ross}
62b819ceaGordon Ross
63b819ceaGordon Rossvoid *
64b819ceaGordon Rosskmem_zalloc(size_t size, int kmflags)
65b819ceaGordon Ross{
66b819ceaGordon Ross	return (umem_zalloc(size, kmem2umem_flags(kmflags)));
67b819ceaGordon Ross}
68b819ceaGordon Ross
69b819ceaGordon Ross
70b819ceaGordon Rossvoid
71b819ceaGordon Rosskmem_free(void *buf, size_t size)
72b819ceaGordon Ross{
73b819ceaGordon Ross	umem_free(buf, size);
74b819ceaGordon Ross}
75b819ceaGordon Ross
76b819ceaGordon Ross/* void *kmem_alloc_tryhard(size_t size, size_t *alloc_size, int kmflags); */
77b819ceaGordon Ross
78b819ceaGordon Rosskmem_cache_t *
79b819ceaGordon Rosskmem_cache_create(
80b819ceaGordon Ross	char *name,		/* descriptive name for this cache */
81b819ceaGordon Ross	size_t bufsize,		/* size of the objects it manages */
82b819ceaGordon Ross	size_t align,		/* required object alignment */
83b819ceaGordon Ross	int (*constructor)(void *, void *, int), /* object constructor */
84b819ceaGordon Ross	void (*destructor)(void *, void *),	/* object destructor */
85b819ceaGordon Ross	void (*reclaim)(void *), /* memory reclaim callback */
86b819ceaGordon Ross	void *private,		/* pass-thru arg for constr/destr/reclaim */
87b819ceaGordon Ross	vmem_t *vmp,		/* vmem source for slab allocation */
88b819ceaGordon Ross	int kcflags)		/* cache creation flags */
89b819ceaGordon Ross{
90b819ceaGordon Ross	umem_cache_t *uc;
91b819ceaGordon Ross	int ucflags = 0;
92b819ceaGordon Ross
93b819ceaGordon Ross	/* Ignore KMC_NOTOUCH - not needed for userland caches */
94b819ceaGordon Ross	if (kcflags & KMC_NODEBUG)
95b819ceaGordon Ross		ucflags |= UMC_NODEBUG;
96b819ceaGordon Ross	if (kcflags & KMC_NOMAGAZINE)
97b819ceaGordon Ross		ucflags |= UMC_NOMAGAZINE;
98b819ceaGordon Ross	if (kcflags & KMC_NOHASH)
99b819ceaGordon Ross		ucflags |= UMC_NOHASH;
100b819ceaGordon Ross
101b819ceaGordon Ross	uc = umem_cache_create(name, bufsize, align,
102b819ceaGordon Ross	    constructor, destructor, reclaim,
103b819ceaGordon Ross	    private, vmp, ucflags);
104b819ceaGordon Ross	return ((kmem_cache_t *)uc);
105b819ceaGordon Ross}
106b819ceaGordon Ross
107b819ceaGordon Rossvoid
108b819ceaGordon Rosskmem_cache_destroy(kmem_cache_t *kc)
109b819ceaGordon Ross{
110b819ceaGordon Ross	umem_cache_destroy((umem_cache_t *)kc);
111b819ceaGordon Ross}
112b819ceaGordon Ross
113b819ceaGordon Rossvoid *
114b819ceaGordon Rosskmem_cache_alloc(kmem_cache_t *kc, int kmflags)
115b819ceaGordon Ross{
116b819ceaGordon Ross	return (umem_cache_alloc((umem_cache_t *)kc,
117b819ceaGordon Ross	    kmem2umem_flags(kmflags)));
118b819ceaGordon Ross}
119b819ceaGordon Ross
120b819ceaGordon Rossvoid
121b819ceaGordon Rosskmem_cache_free(kmem_cache_t *kc, void *p)
122b819ceaGordon Ross{
123b819ceaGordon Ross	umem_cache_free((umem_cache_t *)kc, p);
124b819ceaGordon Ross}
125b819ceaGordon Ross
126b819ceaGordon Ross/* ARGSUSED */
127b819ceaGordon Rossvoid
128b819ceaGordon Rosskmem_cache_set_move(kmem_cache_t *kc,
12936a64e6Tim Kordas    kmem_cbrc_t (*fun)(void *, void *, size_t, void *))
130b819ceaGordon Ross{
131b819ceaGordon Ross}
132b819ceaGordon Ross
13336a64e6Tim Kordasboolean_t
13436a64e6Tim Kordaskmem_cache_reap_active(void)
13536a64e6Tim Kordas{
13636a64e6Tim Kordas	return (B_FALSE);
13736a64e6Tim Kordas}
13836a64e6Tim Kordas
139b819ceaGordon Ross/* ARGSUSED */
140b819ceaGordon Rossvoid
14136a64e6Tim Kordaskmem_cache_reap_soon(kmem_cache_t *kc)
142b819ceaGordon Ross{
143b819ceaGordon Ross}
144b819ceaGordon Ross
145b819ceaGordon Ross/* uint64_t kmem_cache_stat(kmem_cache_t *, char *); */
146b819ceaGordon Ross
147b819ceaGordon Ross/* ARGSUSED */
148b819ceaGordon Rossvoid
149b819ceaGordon Rossvmem_qcache_reap(struct  vmem *vmp)
150b819ceaGordon Ross{
151b819ceaGordon Ross}
152b819ceaGordon Ross
153b819ceaGordon Rossvoid
154b819ceaGordon Rossstrfree(char *str)
155b819ceaGordon Ross{
156b819ceaGordon Ross	ASSERT(str != NULL);
157b819ceaGordon Ross	kmem_free(str, strlen(str) + 1);
158b819ceaGordon Ross}