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