2 * Copyright (c) 2006-2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
35 #include <sys/debug.h>
36 #include <sys/mutex.h>
38 #include <vm/vm_page.h>
39 #include <vm/vm_object.h>
40 #include <vm/vm_kern.h>
41 #include <vm/vm_map.h>
44 #include <sys/queue.h>
45 #include <sys/stack.h>
49 static MALLOC_DEFINE(M_SOLARIS, "solaris", "Solaris");
51 #define malloc(size, type, flags) malloc(size)
52 #define free(addr, type) free(addr)
58 LIST_ENTRY(kmem_item) next;
60 static LIST_HEAD(, kmem_item) kmem_items;
61 static struct mtx kmem_items_mtx;
62 MTX_SYSINIT(kmem_items_mtx, &kmem_items_mtx, "kmem_items", MTX_DEF);
63 #endif /* KMEM_DEBUG */
66 zfs_kmem_alloc(size_t size, int kmflags)
72 size += sizeof(struct kmem_item);
74 p = malloc(size, M_SOLARIS, kmflags);
76 if (kmflags & KM_SLEEP)
82 p = (u_char *)p + sizeof(struct kmem_item);
83 stack_save(&i->stack);
84 mtx_lock(&kmem_items_mtx);
85 LIST_INSERT_HEAD(&kmem_items, i, next);
86 mtx_unlock(&kmem_items_mtx);
93 zfs_kmem_free(void *buf, size_t size __unused)
97 printf("%s: attempt to free NULL\n", __func__);
102 buf = (u_char *)buf - sizeof(struct kmem_item);
103 mtx_lock(&kmem_items_mtx);
104 LIST_FOREACH(i, &kmem_items, next) {
109 LIST_REMOVE(i, next);
110 mtx_unlock(&kmem_items_mtx);
112 free(buf, M_SOLARIS);
119 return ((uint64_t)vm_kmem_size);
126 return ((uint64_t)kmem_map->size);
130 kmem_std_constructor(void *mem, int size __unused, void *private, int flags)
132 struct kmem_cache *cache = private;
134 return (cache->kc_constructor(mem, cache->kc_private, flags));
138 kmem_std_destructor(void *mem, int size __unused, void *private)
140 struct kmem_cache *cache = private;
142 cache->kc_destructor(mem, cache->kc_private);
146 kmem_cache_create(char *name, size_t bufsize, size_t align,
147 int (*constructor)(void *, void *, int), void (*destructor)(void *, void *),
148 void (*reclaim)(void *) __unused, void *private, vmem_t *vmp, int cflags)
154 cache = kmem_alloc(sizeof(*cache), KM_SLEEP);
155 strlcpy(cache->kc_name, name, sizeof(cache->kc_name));
156 cache->kc_constructor = constructor;
157 cache->kc_destructor = destructor;
158 cache->kc_private = private;
159 #if defined(_KERNEL) && !defined(KMEM_DEBUG)
160 cache->kc_zone = uma_zcreate(cache->kc_name, bufsize,
161 constructor != NULL ? kmem_std_constructor : NULL,
162 destructor != NULL ? kmem_std_destructor : NULL,
163 NULL, NULL, align > 0 ? align - 1 : 0, cflags);
165 cache->kc_size = bufsize;
172 kmem_cache_destroy(kmem_cache_t *cache)
174 #if defined(_KERNEL) && !defined(KMEM_DEBUG)
175 uma_zdestroy(cache->kc_zone);
177 kmem_free(cache, sizeof(*cache));
181 kmem_cache_alloc(kmem_cache_t *cache, int flags)
183 #if defined(_KERNEL) && !defined(KMEM_DEBUG)
184 return (uma_zalloc_arg(cache->kc_zone, cache, flags));
188 p = kmem_alloc(cache->kc_size, flags);
189 if (p != NULL && cache->kc_constructor != NULL)
190 kmem_std_constructor(p, cache->kc_size, cache, flags);
196 kmem_cache_free(kmem_cache_t *cache, void *buf)
198 #if defined(_KERNEL) && !defined(KMEM_DEBUG)
199 uma_zfree_arg(cache->kc_zone, buf, cache);
201 if (cache->kc_destructor != NULL)
202 kmem_std_destructor(buf, cache->kc_size, cache);
203 kmem_free(buf, cache->kc_size);
209 kmem_cache_reap_now(kmem_cache_t *cache)
212 zone_drain(cache->kc_zone);
223 kmem_cache_reap_now(kmem_cache_t *cache __unused)
240 calloc(size_t n, size_t s)
242 return (kmem_zalloc(n * s, KM_NOSLEEP));
246 void kmem_show(void *);
248 kmem_show(void *dummy __unused)
252 mtx_lock(&kmem_items_mtx);
253 if (LIST_EMPTY(&kmem_items))
254 printf("KMEM_DEBUG: No leaked elements.\n");
256 printf("KMEM_DEBUG: Leaked elements:\n\n");
257 LIST_FOREACH(i, &kmem_items, next) {
258 printf("address=%p\n", i);
259 stack_print_ddb(&i->stack);
263 mtx_unlock(&kmem_items_mtx);
266 SYSUNINIT(sol_kmem, SI_SUB_CPU, SI_ORDER_FIRST, kmem_show, NULL);
267 #endif /* KMEM_DEBUG */