]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - sys/ofed/include/linux/slab.h
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / sys / ofed / include / linux / slab.h
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #ifndef _LINUX_SLAB_H_
30 #define _LINUX_SLAB_H_
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/malloc.h>
35 #include <vm/uma.h>
36
37 #include <linux/types.h>
38 #include <linux/gfp.h>
39
40 MALLOC_DECLARE(M_KMALLOC);
41
42 #define kmalloc(size, flags)            malloc((size), M_KMALLOC, (flags))
43 #define kvmalloc(size)                  kmalloc((size), 0)
44 #define kzalloc(size, flags)            kmalloc((size), (flags) | M_ZERO)
45 #define kzalloc_node(size, flags, node) kzalloc(size, flags)
46 #define kfree(ptr)                      free(__DECONST(void *, (ptr)), M_KMALLOC)
47 #define krealloc(ptr, size, flags)      realloc((ptr), (size), M_KMALLOC, (flags))
48 #define kcalloc(n, size, flags)         kmalloc((n) * (size), flags | M_ZERO)
49 #define vzalloc(size)                   kzalloc(size, GFP_KERNEL | __GFP_NOWARN)
50 #define vfree(arg)                      kfree(arg)
51 #define kvfree(arg)                     kfree(arg)
52 #define vmalloc(size)                   kmalloc(size, GFP_KERNEL)
53 #define vmalloc_node(size, node)        kmalloc(size, GFP_KERNEL)
54
55 struct kmem_cache {
56         uma_zone_t      cache_zone;
57         void            (*cache_ctor)(void *);
58 };
59
60 #define SLAB_HWCACHE_ALIGN      0x0001
61
62 static inline int
63 kmem_ctor(void *mem, int size, void *arg, int flags)
64 {
65         void (*ctor)(void *);
66
67         ctor = arg;
68         ctor(mem);
69
70         return (0);
71 }
72
73 static inline struct kmem_cache *
74 kmem_cache_create(char *name, size_t size, size_t align, u_long flags,
75     void (*ctor)(void *))
76 {
77         struct kmem_cache *c;
78
79         c = malloc(sizeof(*c), M_KMALLOC, M_WAITOK);
80         if (align)
81                 align--;
82         if (flags & SLAB_HWCACHE_ALIGN)
83                 align = UMA_ALIGN_CACHE;
84         c->cache_zone = uma_zcreate(name, size, ctor ? kmem_ctor : NULL,
85             NULL, NULL, NULL, align, 0);
86         c->cache_ctor = ctor;
87
88         return c;
89 }
90
91 static inline void *
92 kmem_cache_alloc(struct kmem_cache *c, int flags)
93 {
94         return uma_zalloc_arg(c->cache_zone, c->cache_ctor, flags);
95 }
96
97 static inline void
98 kmem_cache_free(struct kmem_cache *c, void *m)
99 {
100         uma_zfree(c->cache_zone, m);
101 }
102
103 static inline void
104 kmem_cache_destroy(struct kmem_cache *c)
105 {
106         uma_zdestroy(c->cache_zone);
107         free(c, M_KMALLOC);
108 }
109
110 #endif  /* _LINUX_SLAB_H_ */