xref: /titanic_44/usr/src/lib/libfakekernel/common/kmem.c (revision db8b037b5616a366b7dfdc01ef9552f02f9adfdd)
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  */
15 
16 #include <sys/kmem.h>
17 
18 #include <sys/debug.h>
19 #include <sys/ksynch.h>
20 #include <sys/systm.h>
21 
22 #include <umem.h>
23 
24 void	abort(void) __NORETURN;
25 
26 static int
27 kmem_failed_cb(void)
28 {
29 	abort();
30 	return (UMEM_CALLBACK_RETRY);
31 }
32 
33 #pragma init(_kmem_init)
34 static int
35 _kmem_init(void)
36 {
37 	umem_nofail_callback(kmem_failed_cb);
38 	return (0);
39 }
40 
41 static int
42 kmem2umem_flags(int kmflags)
43 {
44 	int umflags = UMEM_NOFAIL;
45 	if (kmflags & KM_NOSLEEP)
46 		umflags = UMEM_DEFAULT;
47 	return (umflags);
48 }
49 
50 int
51 kmem_debugging(void)
52 {
53 	return (0);
54 }
55 
56 void *
57 kmem_alloc(size_t size, int kmflags)
58 {
59 	return (umem_alloc(size, kmem2umem_flags(kmflags)));
60 }
61 
62 void *
63 kmem_zalloc(size_t size, int kmflags)
64 {
65 	return (umem_zalloc(size, kmem2umem_flags(kmflags)));
66 }
67 
68 
69 void
70 kmem_free(void *buf, size_t size)
71 {
72 	umem_free(buf, size);
73 }
74 
75 /* void *kmem_alloc_tryhard(size_t size, size_t *alloc_size, int kmflags); */
76 
77 kmem_cache_t *
78 kmem_cache_create(
79 	char *name,		/* descriptive name for this cache */
80 	size_t bufsize,		/* size of the objects it manages */
81 	size_t align,		/* required object alignment */
82 	int (*constructor)(void *, void *, int), /* object constructor */
83 	void (*destructor)(void *, void *),	/* object destructor */
84 	void (*reclaim)(void *), /* memory reclaim callback */
85 	void *private,		/* pass-thru arg for constr/destr/reclaim */
86 	vmem_t *vmp,		/* vmem source for slab allocation */
87 	int kcflags)		/* cache creation flags */
88 {
89 	umem_cache_t *uc;
90 	int ucflags = 0;
91 
92 	/* Ignore KMC_NOTOUCH - not needed for userland caches */
93 	if (kcflags & KMC_NODEBUG)
94 		ucflags |= UMC_NODEBUG;
95 	if (kcflags & KMC_NOMAGAZINE)
96 		ucflags |= UMC_NOMAGAZINE;
97 	if (kcflags & KMC_NOHASH)
98 		ucflags |= UMC_NOHASH;
99 
100 	uc = umem_cache_create(name, bufsize, align,
101 	    constructor, destructor, reclaim,
102 	    private, vmp, ucflags);
103 	return ((kmem_cache_t *)uc);
104 }
105 
106 void
107 kmem_cache_destroy(kmem_cache_t *kc)
108 {
109 	umem_cache_destroy((umem_cache_t *)kc);
110 }
111 
112 void *
113 kmem_cache_alloc(kmem_cache_t *kc, int kmflags)
114 {
115 	return (umem_cache_alloc((umem_cache_t *)kc,
116 	    kmem2umem_flags(kmflags)));
117 }
118 
119 void
120 kmem_cache_free(kmem_cache_t *kc, void *p)
121 {
122 	umem_cache_free((umem_cache_t *)kc, p);
123 }
124 
125 /* ARGSUSED */
126 void
127 kmem_cache_set_move(kmem_cache_t *kc,
128 	kmem_cbrc_t (*fun)(void *, void *, size_t, void *))
129 {
130 }
131 
132 /* ARGSUSED */
133 void
134 kmem_cache_reap_now(kmem_cache_t *kc)
135 {
136 }
137 
138 /* uint64_t kmem_cache_stat(kmem_cache_t *, char *); */
139 
140 /* ARGSUSED */
141 void
142 vmem_qcache_reap(struct  vmem *vmp)
143 {
144 }
145 
146 void
147 strfree(char *str)
148 {
149 	ASSERT(str != NULL);
150 	kmem_free(str, strlen(str) + 1);
151 }
152