slab.h revision 271127
1246149Ssjg/*-
2246149Ssjg * Copyright (c) 2010 Isilon Systems, Inc.
3246149Ssjg * Copyright (c) 2010 iX Systems, Inc.
4246149Ssjg * Copyright (c) 2010 Panasas, Inc.
5246149Ssjg * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6246149Ssjg * All rights reserved.
7246149Ssjg *
8246149Ssjg * Redistribution and use in source and binary forms, with or without
9246149Ssjg * modification, are permitted provided that the following conditions
10246149Ssjg * are met:
11246149Ssjg * 1. Redistributions of source code must retain the above copyright
12246149Ssjg *    notice unmodified, this list of conditions, and the following
13246149Ssjg *    disclaimer.
14246149Ssjg * 2. Redistributions in binary form must reproduce the above copyright
15246149Ssjg *    notice, this list of conditions and the following disclaimer in the
16246149Ssjg *    documentation and/or other materials provided with the distribution.
17246149Ssjg *
18246149Ssjg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19246149Ssjg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20246149Ssjg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21246149Ssjg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22246149Ssjg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23246149Ssjg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24246149Ssjg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25246149Ssjg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26246149Ssjg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27246149Ssjg * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28246149Ssjg */
29246149Ssjg#ifndef	_LINUX_SLAB_H_
30246149Ssjg#define	_LINUX_SLAB_H_
31246149Ssjg
32246149Ssjg#include <sys/param.h>
33246149Ssjg#include <sys/systm.h>
34246149Ssjg#include <sys/malloc.h>
35246149Ssjg#include <vm/uma.h>
36246149Ssjg
37246149Ssjg#include <linux/types.h>
38246149Ssjg#include <linux/gfp.h>
39246149Ssjg
40246149SsjgMALLOC_DECLARE(M_KMALLOC);
41246149Ssjg
42246149Ssjg#define	kmalloc(size, flags)		malloc((size), M_KMALLOC, (flags))
43246149Ssjg#define	kzalloc(size, flags)		kmalloc((size), (flags) | M_ZERO)
44246149Ssjg#define	kzalloc_node(size, flags, node)	kzalloc(size, flags)
45246149Ssjg#define	kfree(ptr)			free(__DECONST(void *, (ptr)), M_KMALLOC)
46246149Ssjg#define	krealloc(ptr, size, flags)	realloc((ptr), (size), M_KMALLOC, (flags))
47246149Ssjg#define	kcalloc(n, size, flags)	        kmalloc((n) * (size), flags | M_ZERO)
48246149Ssjg#define	vzalloc(size)			kzalloc(size, GFP_KERNEL | __GFP_NOWARN)
49246149Ssjg#define	vfree(arg)			kfree(arg)
50246149Ssjg#define	vmalloc(size)                   kmalloc(size, GFP_KERNEL)
51246149Ssjg#define	vmalloc_node(size, node)        kmalloc(size, GFP_KERNEL)
52246149Ssjg
53246149Ssjgstruct kmem_cache {
54246149Ssjg	uma_zone_t	cache_zone;
55246149Ssjg	void		(*cache_ctor)(void *);
56246149Ssjg};
57246149Ssjg
58246149Ssjg#define	SLAB_HWCACHE_ALIGN	0x0001
59246149Ssjg
60246149Ssjgstatic inline int
61246149Ssjgkmem_ctor(void *mem, int size, void *arg, int flags)
62246149Ssjg{
63246149Ssjg	void (*ctor)(void *);
64246149Ssjg
65246149Ssjg	ctor = arg;
66246149Ssjg	ctor(mem);
67246149Ssjg
68246149Ssjg	return (0);
69246149Ssjg}
70246149Ssjg
71246149Ssjgstatic inline struct kmem_cache *
72246149Ssjgkmem_cache_create(char *name, size_t size, size_t align, u_long flags,
73246149Ssjg    void (*ctor)(void *))
74246149Ssjg{
75246149Ssjg	struct kmem_cache *c;
76246149Ssjg
77246149Ssjg	c = malloc(sizeof(*c), M_KMALLOC, M_WAITOK);
78246149Ssjg	if (align)
79246149Ssjg		align--;
80246149Ssjg	if (flags & SLAB_HWCACHE_ALIGN)
81246149Ssjg		align = UMA_ALIGN_CACHE;
82246149Ssjg	c->cache_zone = uma_zcreate(name, size, ctor ? kmem_ctor : NULL,
83246149Ssjg	    NULL, NULL, NULL, align, 0);
84246149Ssjg	c->cache_ctor = ctor;
85246149Ssjg
86246149Ssjg	return c;
87246149Ssjg}
88246149Ssjg
89246149Ssjgstatic inline void *
90246149Ssjgkmem_cache_alloc(struct kmem_cache *c, int flags)
91246149Ssjg{
92246149Ssjg	return uma_zalloc_arg(c->cache_zone, c->cache_ctor, flags);
93246149Ssjg}
94246149Ssjg
95246149Ssjgstatic inline void
96246149Ssjgkmem_cache_free(struct kmem_cache *c, void *m)
97246149Ssjg{
98246149Ssjg	uma_zfree(c->cache_zone, m);
99246149Ssjg}
100246149Ssjg
101246149Ssjgstatic inline void
102246149Ssjgkmem_cache_destroy(struct kmem_cache *c)
103246149Ssjg{
104246149Ssjg	uma_zdestroy(c->cache_zone);
105246149Ssjg	free(c, M_KMALLOC);
106246149Ssjg}
107246149Ssjg
108246149Ssjg#endif	/* _LINUX_SLAB_H_ */
109246149Ssjg